Skip to content

Quality: Invalid SESSION_MAX_AGE_SECS is silently ignored, causing wrong session lifetime#15367

Open
Nam0101 wants to merge 1 commit into
hail-is:mainfrom
Nam0101:contribai/improve/quality/invalid-session-max-age-secs-is-silently
Open

Quality: Invalid SESSION_MAX_AGE_SECS is silently ignored, causing wrong session lifetime#15367
Nam0101 wants to merge 1 commit into
hail-is:mainfrom
Nam0101:contribai/improve/quality/invalid-session-max-age-secs-is-silently

Conversation

@Nam0101
Copy link
Copy Markdown

@Nam0101 Nam0101 commented Mar 31, 2026

✨ Code Quality

Problem

max_age() catches ValueError when SESSION_MAX_AGE_SECS is malformed, logs it, and silently falls back to 30 days. This hides configuration mistakes and can materially change authentication behavior (sessions lasting far longer or shorter than intended) without failing fast.

Severity: medium
File: gear/gear/auth_utils.py

Solution

Fail fast on invalid configuration instead of defaulting:
try:
MAX_AGE_SECS = int(s)
except ValueError as e:
raise ValueError(f"Invalid SESSION_MAX_AGE_SECS={s!r}; expected integer seconds") from e
Optionally also validate positivity:
if MAX_AGE_SECS <= 0:
raise ValueError("SESSION_MAX_AGE_SECS must be > 0")

Changes

  • gear/gear/auth_utils.py (modified)

Change Description

Fixes #<issue_number> (delete if N/A)

Brief description and justification of what this PR is doing.

Security Assessment

Delete all except the correct answer:

  • This change potentially impacts the Hail Batch instance as deployed by Broad Institute in GCP
    • The Impact Rating, Impact Description, and Appsec Review sections are required
  • This change cannot impact the Hail Batch instance as deployed by Broad Institute in GCP
    • The Impact Rating, Impact Description, and Appsec Review sections can be deleted

Impact Rating

Delete all except the correct answer:

  • This change has a high security impact
  • This change has a medium security impact
  • This change has a low security impact
  • This change has no security impact

Impact Description

Replace this content with a description of the impact of the change:

  • For none/low impact: a quick one/two sentence justification of the rating.
    • Example: "Docs only", "Low-level refactoring of non-security code", etc.
  • For medium/high impact: provide a description of the impact and the mitigations in place.
    • Example: "New UI text field added in analogy to existing elements, with input strings escaped and validated against code injection"

Appsec Review

  • Required: The impact has been assessed and approved by appsec

Closes #15366

… wrong session lifetime

`max_age()` catches `ValueError` when `SESSION_MAX_AGE_SECS` is malformed, logs it, and silently falls back to 30 days. This hides configuration mistakes and can materially change authentication behavior (sessions lasting far longer or shorter than intended) without failing fast.


Affected files: auth_utils.py

Signed-off-by: Nguyen Van Nam <nam.nv205106@gmail.com>
@Nam0101 Nam0101 requested a review from a team as a code owner March 31, 2026 18:03
Copy link
Copy Markdown
Collaborator

@cjllanwarne cjllanwarne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, thanks for the contribution

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens session lifetime configuration parsing in gear by failing fast when SESSION_MAX_AGE_SECS is malformed or non-positive, avoiding silently falling back to a default that can change authentication behavior.

Changes:

  • Raise a ValueError when SESSION_MAX_AGE_SECS cannot be parsed as an integer.
  • Validate SESSION_MAX_AGE_SECS is > 0 instead of accepting zero/negative values.
  • Remove exception logging + fallback behavior in favor of explicit failure.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread gear/gear/auth_utils.py
Comment on lines 16 to +21
try:
MAX_AGE_SECS = int(s)
except ValueError:
logging.exception("Unable to interpret SESSION_MAX_AGE_SECS as an integer.")
except ValueError as e:
raise ValueError(f"Invalid SESSION_MAX_AGE_SECS={s!r}; expected integer seconds") from e
if MAX_AGE_SECS <= 0:
raise ValueError('SESSION_MAX_AGE_SECS must be > 0')
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAX_AGE_SECS is assigned before the <= 0 validation. If SESSION_MAX_AGE_SECS is 0/negative, this function raises, but the global remains set to an invalid value; if the exception is caught and max_age() is called again, it will skip re-initialization and return the invalid cached value. Consider validating in a local variable first (or resetting MAX_AGE_SECS back to None before raising) so the cache can’t be left in a bad state.

Copilot uses AI. Check for mistakes.
Comment thread gear/gear/auth_utils.py
Comment on lines 12 to +21
def max_age():
global MAX_AGE_SECS
if MAX_AGE_SECS is None:
if (s := os.getenv('SESSION_MAX_AGE_SECS')) is not None:
try:
MAX_AGE_SECS = int(s)
except ValueError:
logging.exception("Unable to interpret SESSION_MAX_AGE_SECS as an integer.")
except ValueError as e:
raise ValueError(f"Invalid SESSION_MAX_AGE_SECS={s!r}; expected integer seconds") from e
if MAX_AGE_SECS <= 0:
raise ValueError('SESSION_MAX_AGE_SECS must be > 0')
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated behavior (raising on malformed/non-positive SESSION_MAX_AGE_SECS) is security/behavior-sensitive and currently untested. There are existing pytest-based tests for other gear utilities (e.g. batch/test/test_time_limited_max_size_cache.py), so it would be good to add unit tests that cover: valid override, malformed value raises, non-positive value raises, and that failures don’t poison the global cache for subsequent calls.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: invalid session_max_age_secs is silently ignored, causing wrong session lifetime

3 participants