Skip to content

fix: oauth multiple concurrent login - #42079

Merged
phlax merged 37 commits into
envoyproxy:mainfrom
zhaohuabing:fix-oauth2-login
Dec 30, 2025
Merged

phlax merged 37 commits into
envoyproxy:mainfrom
zhaohuabing:fix-oauth2-login

Conversation

@zhaohuabing

@zhaohuabing zhaohuabing commented Nov 18, 2025 •

Copy link
Copy Markdown
Member

Commit Message:
This PR uses a random string to identify a client oauth flow, and create a distinct crsf and code verifier cookie for each oauth flow.

This change allows the oauth2 filter correctly handle the csrf verification for multiple concurrent requests to the resources protected by the same ouath2 filter.

Additional Description:
Risk Level: low
Testing: unit and integration tests, and also manually verified using Envoy Gateway SecurityPolicy with Keycloak 26.4.6 as IDP. https://github.com/zhaohuabing/playground/tree/main/gateway/oidc-keycloak
Docs Changes: no.
Release Notes: yes.
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #41902]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional API Considerations:]

@zhaohuabing
zhaohuabing marked this pull request as draft November 18, 2025 04:52
@zhaohuabing
zhaohuabing force-pushed the fix-oauth2-login branch 3 times, most recently from b963118 to 601b8c4 Compare November 20, 2025 08:36
@denniskniep

denniskniep commented Nov 21, 2025 •

Copy link
Copy Markdown
Contributor

Hi @zhaohuabing,

thanks for tackling that! If I am not mistaken you track multiple csrf token in a single cookie ?
This fix should solve an issue that especially occurs when multiple tabs are involved. I think we will have a race condition and the last tab who update the cookie wins. Which leads to loss of tokens from other tabs currently running auth.

I guess it would be better to create a dedicated cookie for each token/codeVerifier
and the cookie name has a random postfix where the chance of collision with other tabs is very low

@zhaohuabing

zhaohuabing commented Nov 21, 2025 •

Copy link
Copy Markdown
Member Author

thanks for tackling that! If I am not mistaken you track multiple csrf token in a single cookie ?
This fix should solve an issue that especially occurs when multiple tabs are involved. I think we will have a race condition and the last tab who update the cookie wins. Which leads to loss of tokens from other tabs currently running auth.

Yes, this solves the cases where multiple tabs invoke the authentication flow sequentially, but wold have issue with concurrently running flows.

I guess it would be better to create a dedicated cookie for each token/codeVerifier
and the cookie name has a random postfix where the chance of collision with other tabs is very low

I’m still figuring out how to distinguish different client authentication flows (tabs). I do have a rough idea though—we would be able to add a random suffix for each flow and store it in the state parameter, which persists throughout all the requests of an authentication flow.

@denniskniep

Copy link
Copy Markdown
Contributor

I’m still figuring out how to distinguish different client authentication flows (tabs). I do have a rough idea though—we would be able to add a random suffix for each flow and store it in the state parameter, which persists throughout all the requests of an authentication flow.

That sounds good to me.
And the corresponding tokens then are stored in dedicated cookies identified by those suffixes, correct?

@zhaohuabing

Copy link
Copy Markdown
Member Author

I’m still figuring out how to distinguish different client authentication flows (tabs). I do have a rough idea though—we would be able to add a random suffix for each flow and store it in the state parameter, which persists throughout all the requests of an authentication flow.

That sounds good to me. And the corresponding tokens then are stored in dedicated cookies identified by those suffixes, correct?

Yes, and we should also avoid creating too many cookies, since the combined size can bloat the request headers and exceed browser or server limit.

@denniskniep

denniskniep commented Nov 21, 2025 •

Copy link
Copy Markdown
Contributor

Yes, and we should also avoid creating too many cookies, since the combined size can bloat the request headers and exceed browser or server limit.

Agree, we are setting an expiration and now we can proactively delete them once used, correct? Because no other tab is relying on the same cookie anymore. Those two controls should keep them to a reasonable count. What do you think?

@zhaohuabing zhaohuabing changed the title fix: multiple concurrent login fix: oauth multiple concurrent login Nov 24, 2025
@zhaohuabing
zhaohuabing force-pushed the fix-oauth2-login branch 4 times, most recently from 56dfebf to 7cd0f0e Compare November 24, 2025 07:37
@zhaohuabing
zhaohuabing marked this pull request as ready for review November 26, 2025 07:59
@repokitteh-read-only repokitteh-read-only Bot added the deps Approval required for changes to Envoy's external dependencies label Nov 27, 2025
@repokitteh-read-only

Copy link
Copy Markdown

CC @envoyproxy/dependency-shepherds: Your approval is needed for changes made to (bazel/.*repos.*\.bzl)|(bazel/dependency_imports\.bzl)|(api/bazel/.*\.bzl)|(.*/requirements\.txt)|(.*\.patch).
envoyproxy/dependency-shepherds assignee is @moderation

🐱

Caused by: #42079 was synchronize by zhaohuabing.

see: more, trace.

@zhaohuabing
zhaohuabing force-pushed the fix-oauth2-login branch 3 times, most recently from 1bdcb59 to 3f775d5 Compare November 27, 2025 10:08
Comment on lines +1 to +47
diff --git a/single-page-app/verify.sh b/single-page-app/verify.sh
index c05e34e..ced1ead 100755
--- a/single-page-app/verify.sh
+++ b/single-page-app/verify.sh
@@ -132,14 +132,14 @@ test_auth () {
"${curl_args[@]}"

responds_with_header \
- "set-cookie: OauthNonce=" \
+ "set-cookie: OauthNonce" \
"${proxy_scheme}://localhost:${proxy_port}/login" \
"${curl_args[@]}"

random=$(openssl rand -hex 8 | head -c -1)
hmac=$(echo -n "${random}" | openssl dgst -sha256 -hmac "${HMAC_SECRET}" -binary|base64)
csrf_token=${random}.${hmac}
- encoded_state=$(echo -n "{\"url\":\"${proxy_scheme}://localhost:${proxy_port}/login\",\"csrf_token\":\"${csrf_token}\"}" | basenc --base64url --wrap=0 | sed 's/=//g')
+ encoded_state=$(echo -n "{\"url\":\"${proxy_scheme}://localhost:${proxy_port}/login\",\"csrf_token\":\"${csrf_token}\",\"flow_id\":\"${random}\"}" | basenc --base64url --wrap=0 | sed 's/=//g')
code_verifier="Fc1bBwAAAAAVzVsHAAAAABjf6i_Hvf8T2dEuEhPhhDNMlp16az-0dxisL-TzJKaZjOMF8nov_pG377FHmpKcsA"
code_challenge="YRQaBq_UpkWzfr6JvtNnh7LMfmPVcIKVYdV98ugwmLY"
run_log "Fetch the myhub authorization page (${name})"
@@ -154,7 +154,7 @@ test_auth () {

run_log "Return to the app and receive creds (${name})"
CODE=$(_curl "${curl_args[@]}" --head "http://localhost:${PORT_MYHUB}/authorize?client_id=0123456789&code_challenge=${code_challenge}&code_challenge_method=S256&redirect_uri=${proxy_scheme}%3A%2F%2Flocalhost%3A${proxy_port}%2Fauthorize&response_type=code&scope=user%3Aemail&state=${encoded_state}" | grep Location | cut -d= -f2 | cut -d\& -f1)
- RESPONSE=$(_curl "${curl_args[@]}" --cookie "OauthNonce=${csrf_token}" --cookie "CodeVerifier=${code_verifier}" --head "${proxy_scheme}://localhost:${proxy_port}/authorize?code=$CODE&state=${encoded_state}")
+ RESPONSE=$(_curl "${curl_args[@]}" --cookie "OauthNonce.${random}=${csrf_token}" --cookie "CodeVerifier.${random}=${code_verifier}" --cookie "OauthNonce=${csrf_token}" --cookie "CodeVerifier=${code_verifier}" --head "${proxy_scheme}://localhost:${proxy_port}/authorize?code=$CODE&state=${encoded_state}")
echo "$RESPONSE" | grep "HTTP/1.1 302 Found"
echo "$RESPONSE" | grep "location: ${proxy_scheme}://localhost:${proxy_port}/login"
echo "$RESPONSE" | grep "set-cookie: OauthHMAC="
@@ -311,7 +311,7 @@ responds_with_header \
"http://localhost:${PORT_DEV_PROXY}/login"

responds_with_header \
- "set-cookie: OauthNonce=" \
+ "set-cookie: OauthNonce" \
"http://localhost:${PORT_DEV_PROXY}/login"

run_log "Test production app (Github)"
@@ -324,6 +324,6 @@ responds_with_header \
"https://localhost:${PORT_PROXY}/login" \
-k
responds_with_header \
- "set-cookie: OauthNonce=" \
+ "set-cookie: OauthNonce" \
"https://localhost:${PORT_PROXY}/login" \
-k

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be preferable to merge these changes in the upstream repo vs. carrying a patch here. https://github.com/envoyproxy/examples. /cc @phlax

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.

i think its a chicken and egg situation - probably patching here first is the easiest path - if you make the changes in examples (without a load of defensive code) then it would fail

@zhaohuabing zhaohuabing Nov 28, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch has been included in this PR to get CI passing. We can remove it once this PR looks good and envoyproxy/examples#937 has been merged.

@zhaohuabing
zhaohuabing marked this pull request as draft November 28, 2025 03:59
@zhaohuabing
zhaohuabing marked this pull request as ready for review November 28, 2025 04:15
@zhaohuabing

Copy link
Copy Markdown
Member Author

Hi @bplotnick @wbpcode while I'm adding more tests, feel free to continue reviewing the code and sharing any feedback.

Signed-off-by: Huabing(Robin) Zhao <[email protected]>
wbpcode
wbpcode previously approved these changes Dec 21, 2025

@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 so much for this great change. This code change looks good to me. And I found you add the back-forward support to the legacy cookie, it's make the code a little more complex and it's OK to me.

Will defer this to @bplotnick as the cross-companies reviewer & owner. Ping to to merge this PR once this get an approval from @bplotnick. :)

Thanks again. It's to not easy to complete this.

@zhaohuabing

zhaohuabing commented Dec 22, 2025 •

Copy link
Copy Markdown
Member Author

Hi @wbpcode, thanks for the help with the review! The backward-compatibility logic is only needed when old and new versions of Envoy coexist behind the same load balancer, which is mainly the rolling-update use case.

I plan to clean it up with a follow-up PR after the next release, so it won't confuse later contributors.

Signed-off-by: Huabing(Robin) Zhao <[email protected]>
Signed-off-by: Huabing(Robin) Zhao <[email protected]>
Signed-off-by: Huabing Zhao <[email protected]>
@ravenblackx

Copy link
Copy Markdown
Contributor

Looks like this still needs a depedency-shepherd stamp from @moderation

@zhaohuabing

Copy link
Copy Markdown
Member Author

Looks like this still needs a depedency-shepherd stamp from @moderation

Waiting for envoyproxy/examples#937 to land, then the bazel/envoy_examples.patch can be removed from this PR.

@moderation

Copy link
Copy Markdown
Contributor

/lgtm deps

@repokitteh-read-only repokitteh-read-only Bot removed the deps Approval required for changes to Envoy's external dependencies label Dec 30, 2025

@phlax phlax 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 @zhaohuabing

@phlax
phlax merged commit 34fa476 into envoyproxy:main Dec 30, 2025
grnmeira pushed a commit to grnmeira/envoy that referenced this pull request Mar 20, 2026
Signed-off-by: Huabing Zhao <[email protected]>
Signed-off-by: Huabing (Robin) Zhao <[email protected]>
Signed-off-by: Huabing(Robin) Zhao <[email protected]>
Signed-off-by: Gustavo <[email protected]>
wbpcode pushed a commit that referenced this pull request Apr 15, 2026
<!--
!!!ATTENTION!!!

If you are fixing *any* crash or *any* potential security issue, *do
not*
open a pull request in this repo. Please report the issue via emailing
[email protected] where the issue will be triaged
appropriately.
Thank you in advance for helping to keep Envoy secure.

!!!ATTENTION!!!

For an explanation of how to fill out the fields, please see the
relevant section
in
[PULL_REQUESTS.md](https://github.com/envoyproxy/envoy/blob/main/PULL_REQUESTS.md)

!!!ATTENTION!!!

Please check the [use of generative AI
policy](https://github.com/envoyproxy/envoy/blob/main/CONTRIBUTING.md?plain=1#L41).

You may use generative AI only if you fully understand the code. You
need to disclose
this usage in the PR description to ensure transparency.
-->

Commit Message: OAuth filter now removes the `OauthExpires` cookie from
the user's browser upon logout.
Additional Description: Previously the filter unset other OAuth session
related cookies such as `OauthHMAC` but did not clear this one remaining
cookie. This cookie refers to an already invalidated session state so
therefore should be safely removed.
Risk Level: Medium
Testing: Ran `bazel test -c fastbuild
//test/extensions/filters/http/oauth2:filter_test` with new tests
Docs Changes: None
Release Notes: Added
Platform Specific Features: None
[Optional Runtime guard:] N/A
[Optional Fixes #Issue] N/A
[Optional Fixes commit #PR or SHA] N/A
[Optional Deprecated:] N/A
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]
N/A

cc @zhaohuabing as this is somewhat related to your OAuth cookie work in
#42079

---------

Signed-off-by: Connor Rogers <[email protected]>
krinkinmu pushed a commit to grnmeira/envoy that referenced this pull request Apr 20, 2026
<!--
!!!ATTENTION!!!

If you are fixing *any* crash or *any* potential security issue, *do
not*
open a pull request in this repo. Please report the issue via emailing
[email protected] where the issue will be triaged
appropriately.
Thank you in advance for helping to keep Envoy secure.

!!!ATTENTION!!!

For an explanation of how to fill out the fields, please see the
relevant section
in
[PULL_REQUESTS.md](https://github.com/envoyproxy/envoy/blob/main/PULL_REQUESTS.md)

!!!ATTENTION!!!

Please check the [use of generative AI
policy](https://github.com/envoyproxy/envoy/blob/main/CONTRIBUTING.md?plain=1#L41).

You may use generative AI only if you fully understand the code. You
need to disclose
this usage in the PR description to ensure transparency.
-->

Commit Message: OAuth filter now removes the `OauthExpires` cookie from
the user's browser upon logout.
Additional Description: Previously the filter unset other OAuth session
related cookies such as `OauthHMAC` but did not clear this one remaining
cookie. This cookie refers to an already invalidated session state so
therefore should be safely removed.
Risk Level: Medium
Testing: Ran `bazel test -c fastbuild
//test/extensions/filters/http/oauth2:filter_test` with new tests
Docs Changes: None
Release Notes: Added
Platform Specific Features: None
[Optional Runtime guard:] N/A
[Optional Fixes #Issue] N/A
[Optional Fixes commit #PR or SHA] N/A
[Optional Deprecated:] N/A
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]
N/A

cc @zhaohuabing as this is somewhat related to your OAuth cookie work in
envoyproxy#42079

---------

Signed-off-by: Connor Rogers <[email protected]>
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: csrf token validation failed when multiple requests access a route with OAuth2 filter simultaneously

8 participants