Skip to content

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

Closed

Description

@kh34

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

Description:

I expected the cookie for BearerToken to be set on the response when forward_bearer_token: true. Instead, the browser (Chrome Version 114.0.5735.106) returns an error message on the attempt:

The attempt to set a cookie via a Set-Cookie header was blocked because the cookie was too large. The combined size of the name and value must be less than or equal to 4096 characters. 

Then, the behavior is similar to #21819, where the browser enters a 302 redirect loop.

It would be an improvement if the oauth2 filter could do anyone of the following:

  1. Log a warning if the cookie size is too large
  2. Allow one to only set the Authorization header and not use cookies
  3. Set multiple cookies, each less than the max size, for a large BearerToken, e.g. splitting it up into parts that could be reassembled by the consumer

Repro steps:

With auth_scopes set to only openid, the id token is a reasonable size. With cloud providers such as Azure, when adding additional scopes for Microsoft applications, e.g. https://cognitiveservices.azure.com/.default, the access token returned seems to return a list of groups, which for some users can be quite large. Microsoft doesn't seem to allow customization of this token.

Config:

Using envoy:v1.26.1

                      - name: envoy.filters.http.oauth2
                        typed_config:
                          "@type": type.googleapis.com/envoy.extensions.filters.http.oauth2.v3.OAuth2
                          config:
                            token_endpoint:
                              cluster: <omitted>
                              uri: https://login.microsoftonline.com/<omitted>/oauth2/v2.0/token
                              timeout: 10s
                            authorization_endpoint: https://login.microsoftonline.com/<omitted>/oauth2/v2.0/authorize
                            redirect_uri: "https://%REQ(:authority)%/callback"
                            redirect_path_matcher:
                              path:
                                prefix: /callback
                            pass_through_matcher:
                              name: authorization
                              present_match: true
                            signout_path:
                              path:
                                exact: /signout
                            forward_bearer_token: true
                            credentials:
                              client_id: "<omitted>"
                              token_secret:
                                name: token
                                sds_config:
                                  resource_api_version: V3
                                  path_config_source:
                                    path: "/etc/envoy/token-secret.yaml"
                              hmac_secret:
                                name: hmac
                                sds_config:
                                  resource_api_version: V3
                                  path_config_source:
                                    path: "/etc/envoy/hmac-secret.yaml"
                            auth_scopes:
                              - openid
                              - <any scope for a Microsoft application>

Logs:

The 302 redirect loop will eventually end with:

[2023-06-08 13:09:06.628][51][debug][oauth2] [source/extensions/filters/http/oauth2/oauth_client.cc:88] Oauth response body: {"error":"invalid_grant","error_description":"AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token.\r\nTrace ID: <omitted>r\nCorrelation ID: <omitted>\r\nTimestamp: 2023-06-08 13:09:06Z","error_codes":[54005],"timestamp":"2023-06-08 13:09:06Z","trace_id":"<omitted>","correlation_id":"<omitted>"}

Activity

  1. added
    triage
    on Jun 8, 2023
  2. added and removed
    triage
    on Jun 8, 2023
  3. ravenblackx commented on Jun 8, 2023

    @ravenblackx
    Contributor
  4. github-actions commented on Jul 8, 2023

    @github-actions

    This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or "no stalebot" or other activity occurs. Thank you for your contributions.

  5. added
    stale
    on Jul 8, 2023
  6. github-actions commented on Jul 15, 2023

    @github-actions

    This issue has been automatically closed because it has not had activity in the last 37 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted" or "no stalebot". Thank you for your contributions.

  7. kanurag94 commented on Aug 3, 2023

    @kanurag94
    Member

    @phlax, @mattklein123, I wish to work on this, may you please help reopen the issue?

  8. reopened this on Aug 3, 2023
  9. removed
    stale
    on Aug 3, 2023
  10. derekargueta commented on Aug 22, 2023

    @derekargueta
    Member

    #28728 seems somewhat related in that the fix for that issue will result in a shorter token which may resolve this issue as well

  11. 11 remaining items

  12. shashankram commented on Jan 19, 2026

    @shashankram

    @kanurag94 are you still working on this? Please see #27875 (comment)

  13. reopened this on Jan 19, 2026
  14. removed
    stale
    on Jan 19, 2026
  15. shashankram commented on Jan 23, 2026

    @shashankram

    I opened a PR to enable chunking for large token cookies #43135

  16. kanurag94 commented on Jan 23, 2026

    @kanurag94
    Member

    @shashankram I was of the opinion with the new knobs being added that the community might want to handle this case differently. Will check the PR. Thanks!

  17. added 4 commits that reference this issue on Jan 24, 2026
    6f17072
    0d6f26d
    9f71400
    b4cd0b3
  18. github-actions commented on Feb 22, 2026

    @github-actions

    This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in the next 7 days unless it is tagged "help wanted" or "no stalebot" or other activity occurs. Thank you for your contributions.

  19. added
    stale
    on Feb 22, 2026
  20. added
    no stalebot
    and removed
    stale
    on Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/cookiesarea/oauthbugno stalebot

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions