Fix response cookie duplication in ASP.NET Core integration#3361
Open
MO2k4 wants to merge 3 commits intoAzure:mainfrom
Open
Fix response cookie duplication in ASP.NET Core integration#3361MO2k4 wants to merge 3 commits intoAzure:mainfrom
MO2k4 wants to merge 3 commits intoAzure:mainfrom
Conversation
The IHeaderDictionary indexer setters in AspNetCoreHttpHeadersCollection used TryAddWithoutValidation which appends values instead of replacing them. When ASP.NET Core's ResponseCookies.Append reads existing Set-Cookie values and writes them back with the new cookie, the append behavior caused exponential duplication. Fix: call Remove(key) before TryAddWithoutValidation in both indexer setters to give correct replace semantics.
Contributor
|
/azp run dotnet-worker.public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Match ASP.NET Core's HeaderDictionary contract: assigning StringValues.Empty via the indexer should remove the header rather than store an empty entry. Addresses PR review feedback on Azure#3361.
Contributor
|
/azp run dotnet-worker.public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Contributor
|
@MO2k4 can you update the release notes https://github.com/Azure/azure-functions-dotnet-worker/blob/main/extensions/Worker.Extensions.Http.AspNetCore/release_notes.md to reflect this change? Remove the existing entry there, that has already shipped. |
Contributor
Author
|
@jviau release notes are added and i removed the old entry |
Contributor
|
/azp run dotnet-worker.public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
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.
Summary
Fixes #3353 — calling
response.Cookies.Append()multiple times duplicates all previousSet-Cookieheader values exponentially.IHeaderDictionaryindexer setters inAspNetCoreHttpHeadersCollectionusedTryAddWithoutValidation()which appends values to an existing key. The contract requires replace semantics. When ASP.NET Core's internalResponseCookies.Append()reads all existingSet-Cookievalues, appends the new cookie, and sets the combined result back via the indexer, the old values are preserved AND the combined values are added — causing exponential duplication.Remove(key)beforeTryAddWithoutValidation(key, value)in both indexer setters (explicitIHeaderDictionaryand public).AspNetCoreHttpHeadersCollection.cs.Note for reviewers
The
Add(string key, StringValues value)method (line 157) in the same class also usesTryAddWithoutValidationwithout removing first. This meansIHeaderDictionary.Addhas append behavior rather than throwing on duplicate keys. This is not the cause of the cookie bug (ASP.NET Core uses the indexer, notAdd, for cookie header manipulation), but it may warrant a follow-up to align with standardIDictionary.Addsemantics.Test plan
IndexerSetter_ShouldReplaceValues_NotAppend— sets a header twice via the indexer, asserts only the second value remainsCookiesAppend_ShouldNotDuplicateSetCookieHeaders— reproduces exact issue: 1 manualSet-Cookieheader + 3Cookies.Appendcalls = 4 values (was 15 before fix)MultipleCookiesAppend_ShouldNotDuplicate— 3Cookies.Appendcalls = 3 valuesWorker.Extensions.Http.AspNetCore.TestspassDotNetWorker.Testspass