Repository navigation
oauth2: add token_cookie_chunking to split large token cookies - #48086
Merged
Merged
Conversation
Browsers drop a cookie larger than 4096 bytes, so a large access, ID or refresh token sends the user back to the authorization server on every request. With the new opt-in OAuth2Config.chunk_large_token_cookies, such a token is written as <name>_0..N-1 plus a <name>_chunks count, and a token that needs more than 8 chunks gets a 503 instead of cookies the browser would drop. Chunks are always rejoined before decryption and HMAC validation, so the option can be turned off safely, and each response deletes the cookie forms the new value does not overwrite. Fixes envoyproxy#27875 Signed-off-by: Anurag Aggarwal <[email protected]>
|
CC @envoyproxy/api-shepherds: Your approval is needed for changes made to |
Replace the chunk_large_token_cookies bool with a TokenCookieChunking message. Setting it turns chunking on, and max_cookie_size (default 4096) and max_chunks (default 8, up to 16) bound the cookies Envoy writes. Requests are read up to the API maximum of 16 chunks regardless of the configured limit, so lowering it or removing the setting keeps existing sessions. Signed-off-by: Anurag Aggarwal <[email protected]>
wbpcode
reviewed
Oct 12, 2026
wbpcode
left a comment
Member
There was a problem hiding this comment.
Thanks for the contribution. Initial batch of comments. 🙇
Member
|
/assign |
Build chunk cookie names in one reused buffer, pass TokenCookieState by value, and format the cookie domain attribute once in the filter config instead of on every token cookie deletion. Signed-off-by: Anurag Aggarwal <[email protected]>
Member
|
/lgtm api |
This was referenced Oct 19, 2026
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.
Commit Message: oauth2: add token_cookie_chunking to split large token cookies
Additional Description:
Browsers do not store a cookie larger than 4096 bytes. When an IdP issues a large access, ID or refresh token, the browser silently drops that cookie, the HMAC check fails on the next request, and the user is redirected to the IdP again and again (#27875).
This adds an opt-in
OAuth2Config.token_cookie_chunking. Setting it turns chunking on; both limits are optional:Writing (only when
token_cookie_chunkingis set)<name>_0..<name>_N-1plus a<name>_chunks=Ncount cookie. Every cookie stays withinmax_cookie_size, including its name and attributes.max_chunkschunks gets a503before any cookie is set, so the browser never stores a session it cannot send back.Reading (always on)
decodeHeaders, before decryption and HMAC validation. The HMAC covers the decrypted value, so the rejoin has to happen before decryption.max_chunksis set to, so lowering it or removing the setting never breaks existing sessions.Replacing old cookies: every response deletes whatever form of a token cookie the client sent that the new value does not overwrite: stale chunks, a single cookie that is now chunked, or chunks that are now a single cookie. Sign-out deletes chunks too. When the count is malformed, it deletes all 16 possible indexes.
Notes:
max_request_headers_kb. The API doc says so.__Secure-/__Host-now carries theSecureattribute, as sign-out already did; browsers ignore the deletion otherwise.oauth_token_cookie_chunked,oauth_token_cookie_reassembled,oauth_token_cookie_malformed_chunks,oauth_token_cookie_oversized.Supersedes #43135 and #47185 and #28799. This is a long running PR and revived again and again. Thanks to @shashankram and @andy-fong for the latest work, and to @wbpcode for the implementation this is ported from.
Risk Level: Low (opt-in; with the field unset, Envoy only additionally reads chunked cookies, which it never wrote before, and adds
Secureto__Secure-/__Host-token cookie deletions)Testing: Unit tests (chunking threshold, configured size/chunk limits, chunk limit/503 on login and refresh, rejoin+decrypt, malformed sets, sign-out deletion with domain/Secure, refresh replacing old cookie forms, round trip through the real HMAC validator). Integration test for a full login and replay with a 6000-byte token under both CBC and GCM.
Docs Changes: API docs for the new message; OAuth2 filter docs note and stats.
Release Notes: Added under
new_features.Platform Specific Features: N/A
Fixes #27875
[Optional API Considerations:] New
TokenCookieChunkingmessage onOAuth2Config(unset = off) with optionalmax_cookie_size(512..4096) andmax_chunks(2..16) limits.AI assistance: this change was prepared with help from an AI coding assistant. I reviewed and understand all of it.