Skip to content

oauth2: add token_cookie_chunking to split large token cookies - #48086

Merged
htuch merged 3 commits into
envoyproxy:mainfrom
kanurag94:oauth2-token-cookie-chunking
Oct 13, 2026
Merged

htuch merged 3 commits into
envoyproxy:mainfrom
kanurag94:oauth2-token-cookie-chunking

Conversation

@kanurag94

@kanurag94 kanurag94 commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

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:

config:
  token_cookie_chunking:
    max_cookie_size: 4096   # default; largest cookie written, name + value + attributes (512..4096)
    max_chunks: 8           # default; most cookies per token (2..16)

Writing (only when token_cookie_chunking is set)

  1. A token that fits in one cookie is written as before, byte for byte.
  2. A larger token is split into <name>_0 .. <name>_N-1 plus a <name>_chunks=N count cookie. Every cookie stays within max_cookie_size, including its name and attributes.
  3. A token that needs more than max_chunks chunks gets a 503 before any cookie is set, so the browser never stores a session it cannot send back.

Reading (always on)

flowchart LR
  A["Cookie: X_0, X_1, X_chunks=2"] --> B["rejoin"]
  B --> C["decrypt"]
  C --> D["HMAC check"]
  D --> E["upstream sees X=token"]
Loading
  1. Chunks are rejoined first in decodeHeaders, before decryption and HMAC validation. The HMAC covers the decrypted value, so the rejoin has to happen before decryption.
  2. The chunk cookies are then replaced by a single cookie, so the rest of the filter and the upstream do not need to know about chunks.
  3. A malformed set (bad count, missing or empty chunk) is dropped and the single cookie is used if there is one.
  4. Up to 16 chunks (the API maximum) are read whatever max_chunks is 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:

  • Why opt-in config rather than a runtime guard: as @yanavlasov pointed out on oauth2: chunk large token cookies #43135, chunking by default breaks rolling upgrades because an older Envoy cannot read the chunks, and a runtime guard is removed after a few releases, taking away the safe upgrade path. With the field unset, nothing changes for existing users. Operators set it once the whole fleet has this change. Removing it later is safe because reading never depends on it.
  • Several large tokens can together exceed the default max_request_headers_kb. The API doc says so.
  • Small fix along the way: deleting a token cookie named __Secure-/__Host- now carries the Secure attribute, as sign-out already did; browsers ignore the deletion otherwise.
  • New counters: 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 Secure to __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 TokenCookieChunking message on OAuth2Config (unset = off) with optional max_cookie_size (512..4096) and max_chunks (2..16) limits.

AI assistance: this change was prepared with help from an AI coding assistant. I reviewed and understand all of it.

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]>
@repokitteh-read-only

Copy link
Copy Markdown

CC @envoyproxy/api-shepherds: Your approval is needed for changes made to (api/envoy/|docs/root/api-docs/).
envoyproxy/api-shepherds assignee is @htuch
CC @envoyproxy/api-watchers: FYI only for changes made to (api/envoy/|docs/root/api-docs/).

🐱

Caused by: #48086 was opened by kanurag94.

see: more, trace.

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 wbpcode 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.

Thanks for the contribution. Initial batch of comments. 🙇

@wbpcode

wbpcode commented Oct 12, 2026

Copy link
Copy Markdown
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]>
@kanurag94 kanurag94 changed the title oauth2: add chunk_large_token_cookies to split large token cookies oauth2: add token_cookie_chunking to split large token cookies Oct 12, 2026
@wbpcode

wbpcode commented Oct 12, 2026

Copy link
Copy Markdown
Member

/lgtm api

@htuch htuch 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!

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.

oauth2 filter: browser blocks BearerToken cookie because its size too large

3 participants