Skip to content

Give Max-Age precedence over Expires in CookieParser - #138339

Merged
ManickaP merged 1 commit into
dotnet:mainfrom
darrellchee:fix/issue-138251
Nov 27, 2026
Merged

ManickaP merged 1 commit into
dotnet:mainfrom
darrellchee:fix/issue-138251

Conversation

@darrellchee

@darrellchee darrellchee commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

When a Set-Cookie header has both Expires and Max-Age, CookieContainer.SetCookies used whichever attribute came first. CookieParser.Get() tracked both with one shared expiresSet flag, so the second attribute was skipped. RFC 6265 4.1.2.2 and 5.3 step 3 say Max-Age has precedence regardless of order. PHP's setcookie() emits Expires first, so a short-lived cookie could be dropped on receipt (server clock behind), and Max-Age=0 with a future Expires did not delete the cookie.

Changes:

  • CookieParser.Get() tracks Max-Age with its own maxAgeSet flag. A successfully parsed Max-Age always sets the expiry, and Expires is ignored once Max-Age has been seen.
  • Invalid values behave as before: an unparseable Max-Age still rejects the cookie unless an Expires came before it.
  • Tests for both attribute orders, with a positive Max-Age and with Max-Age=0.

Not changed, to keep this focused: the parser still uses the first occurrence of a repeated attribute (RFC 6265 5.3 says the last Max-Age wins), and an invalid Expires or Max-Age that comes first still rejects the cookie (RFC 6265 5.2 says to ignore it). Happy to follow up on either if wanted.

Testing: the new tests fail on main for the Expires-first rows and pass with the fix. I ran the System.Net.Primitives cookie unit and functional test files (230 + 103 tests) against the changed sources, plus the issue's repro and extra edge cases (both orders, Max-Age=0/-1/int.MaxValue, quoted and upper-case attributes, several cookies in one header).

Before / After

Left (before): the issue's repro keeps 0 cookies when Expires comes first, and Max-Age=0 leaves the cookie in place.
Right (after): 1 cookie in both orders, and Max-Age=0 removes the cookie.

compare.mp4

Fixes #138251

When a Set-Cookie header had both Expires and Max-Age, the parser
used whichever attribute came first, because both shared one
"already set" flag. RFC 6265 (4.1.2.2 and 5.3) says Max-Age has
precedence regardless of order.

Track Max-Age with its own flag so a successfully parsed Max-Age
always sets the expiry, and Expires is ignored once Max-Age is seen.
An invalid Max-Age keeps the previous behavior: it rejects the
cookie unless an Expires attribute came before it.

Add tests for both attribute orders with a positive Max-Age and with
Max-Age=0.

Fixes dotnet#138251

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 26, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@darrellchee

Copy link
Copy Markdown
Contributor Author

The failures here look unrelated to this change. NoCallback_RevokedCertificate_RevocationChecking_MatchesExpected is tracked in #138301, and an osx-arm64 Libraries_CheckedCoreCLR leg lost its agent. This PR only touches CookieParser.cs and its tests. Could someone rerun the failed legs when convenient?

@ManickaP ManickaP 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, thanks.

@ManickaP
ManickaP merged commit 546a94e into dotnet:main Nov 27, 2026
78 of 81 checks passed
@darrellchee

Copy link
Copy Markdown
Contributor Author

Thank you @ManickaP!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Net community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CookieContainer: when a Set-Cookie has both Expires and Max-Age, the first one wins instead of Max-Age

2 participants