From 9591c4dc10e1a35f86ec576edc6202e0fec90d89 Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Mon, 29 Jan 2024 17:42:07 +0100 Subject: [PATCH] security: fix CVE-2024-23647 (cherry-pick #8345) (#8346) security: fix CVE-2024-23647 (#8345) * security: fix CVE-2024-23647 * add tests * add website --------- Signed-off-by: Jens Langhammer Co-authored-by: Jens L --- .../providers/oauth2/tests/test_token_pkce.py | 77 ++++++++++++++++++- authentik/providers/oauth2/views/token.py | 6 +- website/docs/security/CVE-2024-23647.md | 27 +++++++ website/sidebars.js | 1 + 4 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 website/docs/security/CVE-2024-23647.md diff --git a/authentik/providers/oauth2/tests/test_token_pkce.py b/authentik/providers/oauth2/tests/test_token_pkce.py index 23bcf359fa42..9cbad7fc05c3 100644 --- a/authentik/providers/oauth2/tests/test_token_pkce.py +++ b/authentik/providers/oauth2/tests/test_token_pkce.py @@ -22,8 +22,9 @@ def setUp(self) -> None: self.factory = RequestFactory() self.app = Application.objects.create(name=generate_id(), slug="test") - def test_pkce_missing_in_token(self): - """Test full with pkce""" + def test_pkce_missing_in_authorize(self): + """Test PKCE with code_challenge in authorize request + and missing verifier in token request""" flow = create_test_flow() provider = OAuth2Provider.objects.create( name=generate_id(), @@ -74,7 +75,77 @@ def test_pkce_missing_in_token(self): ) self.assertJSONEqual( response.content, - {"error": "invalid_request", "error_description": "The request is otherwise malformed"}, + { + "error": "invalid_grant", + "error_description": ( + "The provided authorization grant or refresh token is invalid, expired, " + "revoked, does not match the redirection URI used in the authorization " + "request, or was issued to another client" + ), + }, + ) + self.assertEqual(response.status_code, 400) + + def test_pkce_missing_in_token(self): + """Test PKCE with missing code_challenge in authorization request but verifier + set in token request""" + flow = create_test_flow() + provider = OAuth2Provider.objects.create( + name=generate_id(), + client_id="test", + authorization_flow=flow, + redirect_uris="foo://localhost", + access_code_validity="seconds=100", + ) + Application.objects.create(name="app", slug="app", provider=provider) + state = generate_id() + user = create_test_admin_user() + self.client.force_login(user) + header = b64encode(f"{provider.client_id}:{provider.client_secret}".encode()).decode() + # Step 1, initiate params and get redirect to flow + self.client.get( + reverse("authentik_providers_oauth2:authorize"), + data={ + "response_type": "code", + "client_id": "test", + "state": state, + "redirect_uri": "foo://localhost", + # "code_challenge": challenge, + # "code_challenge_method": "S256", + }, + ) + response = self.client.get( + reverse("authentik_api:flow-executor", kwargs={"flow_slug": flow.slug}), + ) + code: AuthorizationCode = AuthorizationCode.objects.filter(user=user).first() + self.assertJSONEqual( + response.content.decode(), + { + "component": "xak-flow-redirect", + "type": ChallengeTypes.REDIRECT.value, + "to": f"foo://localhost?code={code.code}&state={state}", + }, + ) + response = self.client.post( + reverse("authentik_providers_oauth2:token"), + data={ + "grant_type": GRANT_TYPE_AUTHORIZATION_CODE, + "code": code.code, + "code_verifier": generate_id(), + "redirect_uri": "foo://localhost", + }, + HTTP_AUTHORIZATION=f"Basic {header}", + ) + self.assertJSONEqual( + response.content, + { + "error": "invalid_grant", + "error_description": ( + "The provided authorization grant or refresh token is invalid, expired, " + "revoked, does not match the redirection URI used in the authorization " + "request, or was issued to another client" + ), + }, ) self.assertEqual(response.status_code, 400) diff --git a/authentik/providers/oauth2/views/token.py b/authentik/providers/oauth2/views/token.py index 5a9d2c8ce4c5..225ea087971f 100644 --- a/authentik/providers/oauth2/views/token.py +++ b/authentik/providers/oauth2/views/token.py @@ -230,7 +230,7 @@ def __post_init_code(self, raw_code: str, request: HttpRequest): if self.authorization_code.code_challenge: # Authorization code had PKCE but we didn't get one if not self.code_verifier: - raise TokenError("invalid_request") + raise TokenError("invalid_grant") if self.authorization_code.code_challenge_method == PKCE_METHOD_S256: new_code_challenge = ( urlsafe_b64encode(sha256(self.code_verifier.encode("ascii")).digest()) @@ -243,6 +243,10 @@ def __post_init_code(self, raw_code: str, request: HttpRequest): if new_code_challenge != self.authorization_code.code_challenge: LOGGER.warning("Code challenge not matching") raise TokenError("invalid_grant") + # Token request had a code_verifier but code did not have a code challenge + # Prevent downgrade + if not self.authorization_code.code_challenge and self.code_verifier: + raise TokenError("invalid_grant") def __post_init_refresh(self, raw_token: str, request: HttpRequest): if not raw_token: diff --git a/website/docs/security/CVE-2024-23647.md b/website/docs/security/CVE-2024-23647.md new file mode 100644 index 000000000000..837b535c8200 --- /dev/null +++ b/website/docs/security/CVE-2024-23647.md @@ -0,0 +1,27 @@ +# CVE-2024-23647 + +_Reported by [@pieterphilippaerts](https://github.com/pieterphilippaerts)_ + +## PKCE downgrade attack in authentik + +## Summary + +PKCE is a very important countermeasure in OAuth2 , both for public and confidential clients. It protects against CSRF attacks and code injection attacks. Because of this bug, an attacker can circumvent the protection PKCE offers. + +## Patches + +authentik 2023.8.7 and 2023.10.7 fix this issue. + +## Details + +There is a bug in our implementation of PKCE that allows an attacker to circumvent the protection that PKCE offers. PKCE adds the `code_challenge’ parameter to the authorization request and adds the `code_verifier’ parameter to the token request. We recently fixed a downgrade attack (in v2023.8.5 and 2023.10.4) where if the attacker removed the `code_verifier’ parameter in the token request, authentik would allow the request to pass, thus circumventing PKCE’s protection. However, in the latest version of the software, another downgrade scenario is still possible: if the attacker removes the `code_challenge’ parameter from the authorization request, authentik will also not do the PKCE check. + +Note that this type of downgrade enables an attacker to perform a code injection attack, even if the OAuth client is using PKCE (which is supposed to protect against code injection attacks). To start the attack, the attacker must initiate the authorization process without that `code_challenge’ parameter in the authorization request. But this is easy to do (just use a phishing site or email to trick the user into clicking on a link that the attacker controls – the authorization link without that `code_challenge’ parameter). + +The OAuth BCP (https://datatracker.ietf.org/doc/html/draft-ietf-oauth-security-topics) explicitly mentions this particular attack in section 2.1.1: “Authorization servers MUST mitigate PKCE Downgrade Attacks by ensuring that a token request containing a code_verifier parameter is accepted only if a code_challenge parameter was present in the authorization request, see Section 4.8.2 for details.” + +## For more information + +If you have any questions or comments about this advisory: + +- Email us at [security@goauthentik.io](mailto:security@goauthentik.io) diff --git a/website/sidebars.js b/website/sidebars.js index 196ea0d08b4f..122a7e000ed1 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -362,6 +362,7 @@ const docsSidebar = { }, items: [ "security/policy", + "security/CVE-2024-23647", "security/CVE-2024-21637", "security/CVE-2023-48228", "security/GHSA-rjvp-29xq-f62w",