Repository navigation
fix: oauth multiple concurrent login - #42079
Conversation
b963118 to
601b8c4
Compare
|
Hi @zhaohuabing, thanks for tackling that! If I am not mistaken you track multiple csrf token in a single cookie ? I guess it would be better to create a dedicated cookie for each token/codeVerifier |
Yes, this solves the cases where multiple tabs invoke the authentication flow sequentially, but wold have issue with concurrently running flows.
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. |
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? |
513885d to
afe209a
Compare
56dfebf to
7cd0f0e
Compare
|
CC @envoyproxy/dependency-shepherds: Your approval is needed for changes made to |
1bdcb59 to
3f775d5
Compare
| 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 |
There was a problem hiding this comment.
It would be preferable to merge these changes in the upstream repo vs. carrying a patch here. https://github.com/envoyproxy/examples. /cc @phlax
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Signed-off-by: Huabing Zhao <[email protected]>
Signed-off-by: Huabing Zhao <[email protected]>
|
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
left a comment
There was a problem hiding this comment.
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.
|
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]>
d74f824 to
86ade6c
Compare
Signed-off-by: Huabing(Robin) Zhao <[email protected]>
Signed-off-by: Huabing Zhao <[email protected]>
|
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. |
|
/lgtm deps |
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]>
<!-- !!!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]>
<!-- !!!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]>
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:]