Skip to content

oauth2: chunk large token cookies to avoid browsers dropping them - #47185

Closed
kanurag94 wants to merge 1 commit into
envoyproxy:mainfrom
kanurag94:oauth2-cookie-size
Closed

kanurag94 wants to merge 1 commit into
envoyproxy:mainfrom
kanurag94:oauth2-cookie-size

Conversation

@kanurag94

@kanurag94 kanurag94 commented Sep 3, 2026 •

Copy link
Copy Markdown
Member

Commit Message: oauth2: chunk large token cookies to avoid browsers dropping them

Additional Description: token cookies can be larger than 4096 bytes, and most modern browsers reject a cookie that large. The cookie is silently dropped, so the HMAC no longer validates and the user is redirected to the authorization server on every request. Thus in this PR, we split such a token into <name>_0 .. <name>_N-1 cookies plus a <name>_chunks cookie holding the count, and recombine them during read.

The recombine happens before decryption, since the cookie holds ciphertext while the HMAC covers the decrypted value. Doing it any later gives an HMAC over ciphertext that can never match.

A token is split into at most 15 cookies. One too large for that is written as a single cookie, as it is today. A fully chunked session may need max_request_headers_kb raised, and a large id token now reaches a configured end_session_endpoint as a correspondingly large id_token_hint.

The guard defaults to true. A cookie that already fits is written exactly as before, byte for byte, so the only sessions that change are ones the client was already dropping. For those, a mixed fleet during a rolling upgrade is no worse than today: an instance that cannot recombine the cookies restarts the flow, which is what happens on every request now. The case to watch is a non-browser client that accepts cookies over 4096 bytes today, since it works now and would see the split during an upgrade; setting the guard to false restores the old behavior. Recombining and deleting chunked cookies is always enabled, so the guard can be turned back off safely.

Supersedes #28799. Overlaps #43135.

Risk Level: Low
Testing: unit and integration tests
Docs Changes: added the new counters to the oauth2 statistics table
Release Notes: changelogs/current/minor_behavior_changes/oauth2__chunk-large-token-cookies.rst
Platform Specific Features: n/a
Runtime guard: envoy.reloadable_features.oauth2_chunk_large_token_cookies
Fixes #27875

@repokitteh-read-only

Copy link
Copy Markdown

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #47185 was opened by kanurag94.

see: more, trace.

@kanurag94
kanurag94 force-pushed the oauth2-cookie-size branch 5 times, most recently from d1b45b0 to 06b03db Compare September 4, 2026 02:28
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions Bot added the stale stalebot believes this issue/PR has not been touched recently label Oct 4, 2026
@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because it has not had activity in the last 37 days. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

@github-actions github-actions Bot closed this Oct 11, 2026
htuch pushed a commit that referenced this pull request Oct 13, 2026
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:

```yaml
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)**

```mermaid
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"]
```

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 #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. Thanks to @shashankram and @andy-fong for
the earlier 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.

---------

Signed-off-by: Anurag Aggarwal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale stalebot believes this issue/PR has not been touched recently

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant