Skip to content

fix(cookies): do not serialize a finite maxAge outside the long long range - #6244

Open
waw666waw666 wants to merge 1 commit into
nodejs:mainfrom
waw666waw666:fix/cookie-maxage-out-of-range
Open

waw666waw666 wants to merge 1 commit into
nodejs:mainfrom
waw666waw666:fix/cookie-maxage-out-of-range

Conversation

@waw666waw666

Copy link
Copy Markdown

This relates to...

Relates to #5832 and #6123, which fixed the same class of problem for the expires field of the same Cookie dictionary. No open issue covers maxAge; this was found by auditing the remaining numeric field after those fixes.

Rationale

The maxAge member of the Cookie dictionary is converted with the WebIDL long long converter, which wraps finite numbers modulo 2^64. A finite maxAge outside the long long range therefore serializes as an unrelated value:

import { Headers, setCookie } from 'undici'

const headers = new Headers()
setCookie(headers, { name: 'session', value: 'value', maxAge: 2 ** 64 })
headers.get('set-cookie')
// 'session=value; Max-Age=0'  -- the cookie is deleted immediately

2 ** 64 + 2 ** 32 becomes Max-Age=4294967296, and 2 ** 63 wraps to a negative value that only throws by accident. As with expires in #6123, an out-of-range finite value must not become a different, valid-looking value through integer wraparound.

Changes

Bug Fixes

Breaking Changes and Deprecations

N/A. The only behavior change is for inputs that previously produced an arbitrary wrapped Max-Age value.

Status

Tests

New cases in test/cookie/cookies.js (in the Cookie Set test, next to the existing out-of-range expires cases): 2 ** 63, 2 ** 64, 2 ** 64 + 2 ** 32, Number.MAX_VALUE, -Number.MAX_VALUE and -(2 ** 64) serialize without a Max-Age attribute, and 2 ** 62 still serializes as-is.

  • npm run test:cookies: 93 pass / 0 fail with this change; on the base commit the new assertions fail (92 pass / 1 fail, the Cookie Set test) and everything else is unchanged.
  • npx eslint lib/web/cookies/index.js test/cookie/cookies.js: clean.

…range

Signed-off-by: waw666waw666 <184799977+waw666waw666@users.noreply.github.com>

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@codecov-commenter

codecov-commenter commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.73%. Comparing base (6b22aba) to head (c54f687).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6244      +/-   ##
==========================================
+ Coverage   91.72%   91.73%   +0.01%     
==========================================
  Files         113      113              
  Lines       44835    44861      +26     
==========================================
+ Hits        41124    41155      +31     
+ Misses       3711     3706       -5     

☔ 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.

This branch has not been deployed

No deployments
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.

3 participants