Skip to content

Commit

Permalink
security: fix CVE-2024-23647 (cherry-pick #8345) (#8346)
Browse files Browse the repository at this point in the history
security: fix CVE-2024-23647 (#8345)

* security: fix CVE-2024-23647



* add tests



* add website



---------

Signed-off-by: Jens Langhammer <[email protected]>
Co-authored-by: Jens L <[email protected]>
  • Loading branch information
gcp-cherry-pick-bot[bot] and BeryJu committed Jan 29, 2024
1 parent 2a3d2cd commit 9591c4d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 4 deletions.
77 changes: 74 additions & 3 deletions authentik/providers/oauth2/tests/test_token_pkce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion authentik/providers/oauth2/views/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions website/docs/security/CVE-2024-23647.md
Original file line number Diff line number Diff line change
@@ -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 [[email protected]](mailto:[email protected])
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9591c4d

Please sign in to comment.