Details
Vulnerable code
authlib/oidc/core/grants/implicit.py, lines 70-84 at c529a61af40e3efe05286140e00fbdced07d7987 (an excerpt; ... marks code left out of the captured snippet):
redirect_uri = super().validate_authorization_request() # safe: parent grant sets the filtered request.scope
if not is_openid_scope(self.request.payload.scope): # <-- sink (line 72): raw, client-supplied scope
raise InvalidScopeError("Missing 'openid' scope", ...)
...
return redirect_uri
Reproduction
Verified two ways on Authlib c529a61af40e3efe05286140e00fbdced07d7987 (v1.8.0):
- a pytest PoC using the Flask test client
- a standalone Flask OpenID Provider on localhost, driven with
curl
The Python and Flask versions were not recorded.
Setup: a client registered with scope='profile' only (no openid) and response types id_token / id_token token. It uses Authlib's built-in SQLAlchemy ClientMixin and the built-in scope-filtering BearerTokenGenerator.
- The client sends an authorization request with
scope=openid profileand anonce. ImplicitGrantsetsrequest.scope='profile'. A consent page built the way the docs show displays onlyprofile.OpenIDImplicitGrant.validate_authorization_request()checks the rawrequest.payload.scopeat line 72, so the request passes.- After the user approves, the provider returns an RS256-signed ID token with
iss,aud(the client),nonce,auth_time,at_hash,subandname, while the granted scope isprofile. The ID token's signature verified against the provider's JWKS.
Controls run during verification:
- The same request without
openidreturnsinvalid_scope("Missing 'openid' scope"). - Authlib's
UserInfoEndpointrejects this client's access token with 403insufficient_scope. - The OIDC authorization code flow for the same client issues no
id_token.
The sketch below shows the request shape against Authlib's Flask OIDC test app. The verification's own test script is not included here.
# Client metadata: scope='profile' (no openid), response_types=['id_token token'].
rv = client.get('/authorize?response_type=id_token%20token&client_id=client-id&redirect_uri=https%3A%2F%2Fclient.test%2Fauthorized&scope=openid%20profile&nonce=n')
# after the user grants consent:
assert 'id_token=' in rv.headers['Location'] # should have been rejected: openid is not an allowed scope
- Expected:
InvalidScopeError/invalid_scope("Missing 'openid' scope"), because the client's resolved allowed scope (request.scope == 'profile') does not includeopenid. - Actual: after consent, the redirect carries a signed ID token (including
subandname) while the granted scope isprofile.
Suggested remediation
Decide whether to issue OpenID tokens from the scope the grant actually authorized, never from the raw requested scope. The authorized scope is self.request.scope, which the parent grant has already filtered through client.get_allowed_scope().
redirect_uri = super().validate_authorization_request()
-if not is_openid_scope(self.request.payload.scope):
+if not is_openid_scope(self.request.scope):
raise InvalidScopeError("Missing 'openid' scope", ...)
During verification, this one-line change at line 72 blocked the exploit, and the existing OIDC implicit/hybrid tests still passed (23 passed).
The same rule should hold for the rest of OIDC issuance. create_granted_params() (`authlib/oidc/core/grants/implicit.py:112-129`) passes the raw payload.scope to generate_token. It should pass self.request.scope instead.
Notes:
- Adjacent raw-scope use, worth fixing in the same change:
create_granted_params()hands the rawpayload.scopetogenerate_token. So a deployment whose custom token generator does not filter scope also grants the client access-token scopes it is not allowed; verification notedopenidandemail. Authlib's built-inBearerTokenGeneratorre-filters the access-token scope, so this only shows up with a non-filtering generator. That re-filtering does not stop the ID token. - Other paths through the same raw value: beyond
create_granted_params(), we did not audit every scope read inprocess_implicit_token()(`authlib/oidc/core/grants/implicit.py:132-169`), or in other OIDC grants such as the hybrid grant. Whether they share the pattern is not verified. - Already safe, no change needed:
- the scope filtering in the parent
ImplicitGrantand inClientMixin UserInfoEndpoint, which rejects this client's access token with 403insufficient_scope- the OIDC authorization code flow, which issues no ID token for this client
Suggested regression test, alongside the existing Flask OIDC implicit-grant tests:
- Register a client with
scope='profile'andresponse_types=['id_token token']. - Send an authorization request with
scope=openid profileand anonce, and approve it. - Assert that the response is an
invalid_scopeerror ("Missing 'openid' scope") and that the redirect contains noid_token.
A second case should use a token generator that does not filter scope, and assert that the issued access token carries only the client's allowed scopes.