Repository navigation
Give Max-Age precedence over Expires in CookieParser - #138339
Merged
Merged
Conversation
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>
|
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. |
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
3 tasks
Contributor
Author
|
The failures here look unrelated to this change. |
Contributor
Author
|
Thank you @ManickaP! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a
Set-Cookieheader has bothExpiresandMax-Age,CookieContainer.SetCookiesused whichever attribute came first.CookieParser.Get()tracked both with one sharedexpiresSetflag, so the second attribute was skipped. RFC 6265 4.1.2.2 and 5.3 step 3 sayMax-Agehas precedence regardless of order. PHP'ssetcookie()emitsExpiresfirst, so a short-lived cookie could be dropped on receipt (server clock behind), andMax-Age=0with a futureExpiresdid not delete the cookie.Changes:
CookieParser.Get()tracksMax-Agewith its ownmaxAgeSetflag. A successfully parsedMax-Agealways sets the expiry, andExpiresis ignored onceMax-Agehas been seen.Max-Agestill rejects the cookie unless anExpirescame before it.Max-Ageand withMax-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-Agewins), and an invalidExpiresorMax-Agethat 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
mainfor theExpires-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
Expirescomes first, andMax-Age=0leaves the cookie in place.Right (after): 1 cookie in both orders, and
Max-Age=0removes the cookie.compare.mp4
Fixes #138251