Skip to content

Commit

Permalink
providers/oauth2: fix CVE-2024-21637 (cherry-pick #8104) (#8106)
Browse files Browse the repository at this point in the history
* providers/oauth2: fix CVE-2024-21637 (#8104)

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* update changelog

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

---------

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
Co-authored-by: Jens L <jens@goauthentik.io>
  • Loading branch information
gcp-cherry-pick-bot[bot] and BeryJu authored Jan 9, 2024
1 parent 1516fe8 commit d9aab79
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
19 changes: 19 additions & 0 deletions authentik/providers/oauth2/tests/test_authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ def test_invalid_redirect_uri(self):
)
OAuthAuthorizationParams.from_request(request)

def test_blocked_redirect_uri(self):
"""test missing/invalid redirect URI"""
OAuth2Provider.objects.create(
name=generate_id(),
client_id="test",
authorization_flow=create_test_flow(),
redirect_uris="data:local.invalid",
)
with self.assertRaises(RedirectUriError):
request = self.factory.get(
"/",
data={
"response_type": "code",
"client_id": "test",
"redirect_uri": "data:localhost",
},
)
OAuthAuthorizationParams.from_request(request)

def test_invalid_redirect_uri_empty(self):
"""test missing/invalid redirect URI"""
provider = OAuth2Provider.objects.create(
Expand Down
12 changes: 8 additions & 4 deletions authentik/providers/oauth2/views/authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
SESSION_KEY_LAST_LOGIN_UID = "authentik/providers/oauth2/last_login_uid"

ALLOWED_PROMPT_PARAMS = {PROMPT_NONE, PROMPT_CONSENT, PROMPT_LOGIN}
FORBIDDEN_URI_SCHEMES = {"javascript", "data", "vbscript"}


@dataclass(slots=True)
Expand Down Expand Up @@ -174,6 +175,10 @@ def __post_init__(self):
self.check_scope()
self.check_nonce()
self.check_code_challenge()
if self.request:
raise AuthorizeError(
self.redirect_uri, "request_not_supported", self.grant_type, self.state
)

def check_redirect_uri(self):
"""Redirect URI validation."""
Expand Down Expand Up @@ -211,10 +216,9 @@ def check_redirect_uri(self):
expected=allowed_redirect_urls,
)
raise RedirectUriError(self.redirect_uri, allowed_redirect_urls)
if self.request:
raise AuthorizeError(
self.redirect_uri, "request_not_supported", self.grant_type, self.state
)
# Check against forbidden schemes
if urlparse(self.redirect_uri).scheme in FORBIDDEN_URI_SCHEMES:
raise RedirectUriError(self.redirect_uri, allowed_redirect_urls)

def check_scope(self):
"""Ensure openid scope is set in Hybrid flows, or when requesting an id_token"""
Expand Down
6 changes: 6 additions & 0 deletions authentik/providers/oauth2/views/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from re import error as RegexError
from re import fullmatch
from typing import Any, Optional
from urllib.parse import urlparse

from django.http import HttpRequest, HttpResponse
from django.utils import timezone
Expand Down Expand Up @@ -53,6 +54,7 @@
RefreshToken,
)
from authentik.providers.oauth2.utils import TokenResponse, cors_allow, extract_client_auth
from authentik.providers.oauth2.views.authorize import FORBIDDEN_URI_SCHEMES
from authentik.sources.oauth.models import OAuthSource
from authentik.stages.password.stage import PLAN_CONTEXT_METHOD, PLAN_CONTEXT_METHOD_ARGS

Expand Down Expand Up @@ -204,6 +206,10 @@ def __post_init_code(self, raw_code: str, request: HttpRequest):
).from_http(request)
raise TokenError("invalid_client")

# Check against forbidden schemes
if urlparse(self.redirect_uri).scheme in FORBIDDEN_URI_SCHEMES:
raise TokenError("invalid_request")

self.authorization_code = AuthorizationCode.objects.filter(code=raw_code).first()
if not self.authorization_code:
LOGGER.warning("Code does not exist", code=raw_code)
Expand Down
8 changes: 8 additions & 0 deletions website/docs/releases/2023/v2023.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ image:
- \*: fix [GHSA-rjvp-29xq-f62w](../security/GHSA-rjvp-29xq-f62w), Reported by [@devSparkle](https://github.com/devSparkle)
## Fixed in 2023.8.5
- security: fix [CVE-2023-48228](../../security/CVE-2023-48228.md), Reported by [@Sapd](https://github.com/Sapd) (#7666)
## Fixed in 2023.8.6
- providers/oauth2: fix [CVE-2024-21637](../../security/CVE-2024-21637.md), Reported by [@lauritzh](https://github.com/lauritzh) (#8104)
## API Changes
#### What's New
Expand Down
39 changes: 39 additions & 0 deletions website/docs/security/CVE-2024-21637.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# CVE-2024-21637

_Reported by [@lauritzh](https://github.com/lauritzh)_

## XSS in Authentik via JavaScript-URI as Redirect URI and form_post Response Mode

### Summary

Given an OAuth2 provider configured with allowed redirect URIs set to `*` or `.*`, an attacker can send an OAuth Authorization request using `response_mode=form_post` and setting `redirect_uri` to a malicious URI, to capture authentik's session token.

### Patches

authentik 2023.8.6 and 2023.10.6 fix this issue.

### Impact

The impact depends on the attack scenario. In the following I will describe the two scenario that were identified for Authentik.

#### Redirect URI Misconfiguration

While advising that this may cause security issues, Authentik generally allows wildcards as Redirect URI. Therefore, using a wildcard-only effectively allowing arbitrary URLS is possible misconfiguration that may be present in real-world instances.

In such cases, unauthenticated and unprivileged attackers can perform the above described actions.

### User with (only) App Administration Permissions

A more likely scenario is an administrative user (e.g. a normal developer) having only permissions to manage applications.

This relatively user could use the described attacks to perform a privilege escalation.

### Workaround

It is recommended to upgrade to the patched version of authentik. If not possible, ensure that OAuth2 providers do not use a wildcard (`*` or `.*`) value as allowed redirect URI setting. (This is _not_ exploitable if part of the redirect URI has a wildcard, for example `https://foo-.*\.bar\.com`)

### For more information

If you have any questions or comments about this advisory:

- Email us at [security@goauthentik.io](mailto:security@goauthentik.io)
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-21637",
"security/CVE-2023-48228",
"security/GHSA-rjvp-29xq-f62w",
"security/CVE-2023-39522",
Expand Down

0 comments on commit d9aab79

Please sign in to comment.