Skip to content

fix: accept a negative Set-Cookie Max-Age attribute#5571

Open
arshsmith1 wants to merge 1 commit into
nodejs:mainfrom
arshsmith1:cookie-negative-max-age
Open

fix: accept a negative Set-Cookie Max-Age attribute#5571
arshsmith1 wants to merge 1 commit into
nodejs:mainfrom
arshsmith1:cookie-negative-max-age

Conversation

@arshsmith1

Copy link
Copy Markdown

This relates to...

N/A

Rationale

parseUnparsedAttributes in lib/web/cookies/parse.js validates the Max-Age attribute-value with /^\d+$/, applied to the whole value rather than to the remainder after the sign. Any negative Max-Age therefore fails the check and the attribute is dropped from the parsed cookie:

getSetCookies(new Headers({ 'set-cookie': 'id=a; Max-Age=-1' }))
// [{ name: 'id', value: 'a' }]
// should be: [{ name: 'id', value: 'a', maxAge: -1 }]

RFC 6265bis section 5.6.2 is explicit that a leading - is allowed:

If the first character of the attribute-value is neither a DIGIT, nor a "-" character followed by a DIGIT, ignore the cookie-av.

If the remainder of attribute-value contains a non-DIGIT character, ignore the cookie-av.

If delta-seconds is less than or equal to zero (0), let expiry-time be the earliest representable date and time.

The step-1 check at line 193 already accepts - as a first character, and the last step above only has meaning if negative values reach it, so the intent in the surrounding code is the spec behaviour. Max-Age=-1 is the usual way a server expires a cookie, and today that signal is silently lost by getSetCookies().

The generation side is deliberately left alone: validateCookieMaxAge still rejects negatives, which matches the max-age-av = "Max-Age=" non-zero-digit *DIGIT grammar a server must emit. Parsing is permissive, serialising stays strict.

Changes

Relax the digit check to /^-?\d+$/ so a single leading sign is accepted and a non-DIGIT remainder is still ignored. Max-Age=-, --1, -1a, +1 and the empty value all stay ignored, and positive values are unchanged.

Features

N/A

Bug Fixes

  • Set-Cookie: ...; Max-Age=-1 now parses to maxAge: -1 instead of dropping the attribute.

Breaking Changes and Deprecations

N/A

Status

Signed-off-by: arshiya tabasum <arshi@bugqore.com>
@mcollina
mcollina requested a review from KhafraDev July 20, 2026 10:26
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.48%. Comparing base (1383989) to head (317fc61).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5571      +/-   ##
==========================================
+ Coverage   93.47%   93.48%   +0.01%     
==========================================
  Files         110      110              
  Lines       37560    37560              
==========================================
+ Hits        35108    35112       +4     
+ Misses       2452     2448       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants