🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!
Security: refuse OIDC login when the userinfo subject differs from the ID token (AUTH-11)
Omega (QA)cc15054cc8c4
diff --git a/backend/oidc_handler.py b/backend/oidc_handler.pyindex 367653f..da36cda 100644--- a/backend/oidc_handler.py+++ b/backend/oidc_handler.py@@ -23,6 +23,21 @@ logger = logging.getLogger(__name__) # Or use current_app.logger inside routesoidc_bp = Blueprint('oidc', __name__) # url_prefix will be set when registering in app.py+def _userinfo_subject_matches(id_token_claims, userinfo):+ """True when the userinfo subject agrees with the ID token (AUTH-11).++ The userinfo endpoint is a second source of claims, and the callback+ falls back to it for email, names, and groups. If it names a+ different subject than the ID token, its claims describe a different+ user than the one who authenticated, so the login must be refused.+ A missing userinfo subject is tolerated, for providers that omit+ it, and the ID token claims stand on their own.+ """+ userinfo_subject = userinfo.get('sub')+ if not userinfo_subject:+ return True+ return userinfo_subject == id_token_claims.get('sub')+def _provider_supports_pkce(issuer_url):"""True when the provider discovery document advertises PKCE S256.@@ -199,6 +214,13 @@ def oidc_callback_route():frontend_login_url = os.environ.get('FRONTEND_URL', 'http://localhost:8080').rstrip('/') + "/login.html"return redirect(f"{frontend_login_url}?oidc_error=subject_missing")+ if not _userinfo_subject_matches(token_id_claims, userinfo):+ logger.error("[OIDC_HANDLER] OIDC callback: userinfo subject does not match the ID token subject; refusing login.")+ frontend_login_url = os.environ.get('FRONTEND_URL', 'http://localhost:8080').rstrip('/') + "/login.html"+ return redirect(f"{frontend_login_url}?oidc_error=subject_mismatch")+ if not userinfo.get('sub'):+ logger.warning("[OIDC_HANDLER] OIDC callback: userinfo response carries no subject; continuing on the ID token claims.")+email = token_id_claims.get('email') or userinfo.get('email')first_name = token_id_claims.get('given_name') or userinfo.get('given_name', '')diff --git a/tests/test_oidc_userinfo_subject.py b/tests/test_oidc_userinfo_subject.pynew file mode 100644index 0000000..333310a--- /dev/null+++ b/tests/test_oidc_userinfo_subject.py@@ -0,0 +1,37 @@+"""OIDC userinfo claims must describe the authenticated subject (AUTH-11).++The callback falls back to the userinfo endpoint for email, names, and+groups. If the userinfo subject differs from the ID token subject, the+claims belong to a different user and the login must be refused.+"""+from backend.oidc_handler import _userinfo_subject_matches+++def test_matching_subjects_agree():+ assert _userinfo_subject_matches(+ {"sub": "user-1", "iss": "https://idp.test"},+ {"sub": "user-1", "email": "[email protected]", "groups": ["admins"]},+ ) is True+++def test_divergent_userinfo_subject_rejected():+ # The audit's mock provider: an admin group and a different subject+ # carried only by userinfo, with a group-free ID token.+ assert _userinfo_subject_matches(+ {"sub": "user-1", "iss": "https://idp.test"},+ {"sub": "user-2", "email": "[email protected]", "groups": ["admins"]},+ ) is False+++def test_missing_userinfo_subject_tolerated():+ assert _userinfo_subject_matches(+ {"sub": "user-1", "iss": "https://idp.test"},+ {"email": "[email protected]"},+ ) is True+++def test_missing_id_token_subject_never_matches_a_userinfo_subject():+ assert _userinfo_subject_matches(+ {"iss": "https://idp.test"},+ {"sub": "user-1"},+ ) is False