Skip to content

fix: ignore malformed Max-Age values (RFC 6265 5.2.2) - #90

Merged
nfriedly merged 1 commit into
nfriedly:masterfrom
cpruijsen:fix/issue-89-2fd980b5
Oct 16, 2026
Merged

nfriedly merged 1 commit into
nfriedly:masterfrom
cpruijsen:fix/issue-89-2fd980b5

Conversation

@cpruijsen

Copy link
Copy Markdown
Contributor

Ignore a Max-Age attribute whose value is not an integer, as RFC 6265 section 5.2.2 requires. Fixes #89.

Before this change, Max-Age=12junk, Max-Age=12.5 and Max-Age=+12 all parsed as maxAge: 12, because parseInt accepts any leading integer prefix. lib/set-cookie.js now checks the trimmed value against /^-?\d+$/ before it converts it. A malformed value is dropped, and the cookie and its other attributes stay. Valid values, including negative values such as -1, parse as before.

I used a regex instead of Number(value), because Number accepts 12.5 and turns "" into 0. I can change it to parseInt plus a leftover-character check if you prefer that style.

The new max-age attribute (rfc 6265 5.2.2) tests in test/set-cookie-parser.js fail for the malformed values without the change and pass with it.

Out of scope: Expires with an unparseable date still gives Invalid Date.

@nfriedly

nfriedly commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

Hey, thanks! Do you have any real-world examples of malformed max-age values, or is this more of a theoretical concern?

Oh, and it looks like #91 addresses the invalid date

@cpruijsen

Copy link
Copy Markdown
Contributor Author

Fair question. I have no real-world example. #89 used made-up values, so this is mainly a conformance fix. The practical point is consistency: tough-cookie and undici (behind Node's getSetCookies) both ignore 12junk, 12.5 and +12. This library returns maxAge: 12 for them. If that is not enough reason to change it, I am happy to close this. And yes, #91 covers the Expires case.

@nfriedly
nfriedly merged commit 22d4baa into nfriedly:master Oct 16, 2026
3 checks passed
@nfriedly

Copy link
Copy Markdown
Owner

This will be published shortly in v3.1.4

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.

Malformed Max-Age values are accepted as valid integer prefixes

2 participants