Skip to content

accept signed integers in cookie max-age when computing expiry#1872

Open
HrachShah wants to merge 2 commits into
httpie:masterfrom
HrachShah:fix/max-age-signed-int
Open

accept signed integers in cookie max-age when computing expiry#1872
HrachShah wants to merge 2 commits into
httpie:masterfrom
HrachShah:fix/max-age-signed-int

Conversation

@HrachShah

Copy link
Copy Markdown

Summary

get_expired_cookies / _max_age_to_expires gates the max-age value with str.isdigit(), which only matches strictly positive base-10 integers. Per RFC 6265 § 5.2.2, a negative max-age indicates the cookie expired N seconds in the past and should be treated as already-expired, so the current gate silently drops the attribute for any Max-Age=-300 (or other negative) value.

Repro

from httpie.utils import get_expired_cookies

# Per RFC 6265 § 5.2.2, this cookie expired 5 minutes ago and must be
# marked expired (so HTTPie stops sending it on the next request).
# Pre-fix: returns []   (silent drop — cookie is kept and resent).
# Post-fix: returns [{'name': 'session', 'path': '/'}]
get_expired_cookies('session=abc; Max-Age=-300; path=/; domain=.example.com')

Fix

_max_age_to_expires now uses a small ^([-+]?)(\d+)$ regex instead of str.isdigit(), and int()s the captured value. The re module is already imported at the top of utils.py, and int is preferable to float for an attribute that is always an integer in spec. Bare - or other non-numeric input is still ignored, matching the previous behavior.

Tests

Adds test_get_expired_cookies_handles_signed_max_age with five parametrized cases: negative, positive-in-future, positive-in-past (now=100 vs max-age=10), max-age=0 boundary, and a bare - that should be ignored rather than raising.

All 10 expired-cookie tests pass; the 18 pre-existing failures elsewhere in the suite are unchanged.

Zo Bot added 2 commits May 29, 2026 01:43
…ss.run raises OSError when the executable is missing or has permission issues; catching all Exception masked this specific failure
_max_age_to_expires gates the max-age value with str.isdigit() (and
previously also rejected a leading '+'), which only matches strictly
positive base-10 integers. Per RFC 6265 § 5.2.2 a negative max-age
indicates the cookie expired N seconds in the past and should be
treated as already-expired, so the current gate silently drops the
attribute for any "Max-Age=-300" or similar. The replacement regex
accepts an optional leading '-' or '+' followed by one or more
digits, then int()'s the captured value so the resulting expires
is correctly in the past for negative input and in the future for
positive input. Bare '-' or other garbage is still ignored, matching
the previous behavior. float() was also swapped for int() to avoid
fractional expiry times for a property that is always an integer in
spec, and to keep the regex+int pipeline straightforward.

Adds test_get_expired_cookies_handles_signed_max_age with five
parametrized cases: negative, positive-in-future, positive-in-past
(now=100 vs max-age=10), max-age=0 boundary, and a bare '-' that
should be ignored rather than raising.
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.

1 participant