Security rules
NGB023: Missing OAuth state validation
Reports hand-written OAuth 2.0 / OpenID Connect authorization-code flows in which the callback redeems the code (grant_type=authorization_code) without reading and comparing the state parameter, and authorization requests (response_type=code) that do not send a state.
| Property | Value |
|---|---|
| Rule ID | NGB023 |
| Category | Navigation & authentication |
| Default severity | high |
| CWE | CWE-352, CWE-345 |
| Built-in presets | recommended: error, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Reports hand-written OAuth 2.0 / OpenID Connect authorization-code flows in which the callback redeems the code (grant_type=authorization_code) without reading and comparing the state parameter, and authorization requests (response_type=code) that do not send a state. The analysis looks at the class (or file) that performs the exchange. When the exchange sends a PKCE code_verifier, confidence is lowered because PKCE also binds the code to the browser session.
Why it matters
Without state, an attacker can complete their own authorization flow up to the callback and make the victim's browser load /callback?code=<attacker's code>: the victim is then signed in to the attacker's account (login CSRF), or the attacker's account gets linked to the victim's profile. state (or PKCE) ties the response to the request that this browser started.
Detection behavior and supported patterns
- Exchange:
{ grant_type: 'authorization_code', … },params.set('grant_type', 'authorization_code'), or'grant_type=authorization_code'in a request body string - Authorization request:
response_type=codein a URL string,{ response_type: 'code', client_id, … }(wire parameter names; camelCase library configs are ignored),params.set('response_type', 'code') stateread:queryParamMap.get('state'),searchParams.get('state'),params['state'],params.state,const { code, state } = …statevalidated: an equality comparison involving astatevalue in the same class/file- PKCE:
code_verifiersent with the exchange (confidence lowered to low)
Example
const code = this.route.snapshot.queryParamMap.get('code')!;this.http.post(TOKEN_URL, { grant_type: 'authorization_code', code, redirect_uri, client_id });Safer alternative
const q = this.route.snapshot.queryParamMap;if (q.get('state') !== sessionStorage.getItem('oauth_state')) throw new Error('state mismatch');this.http.post(TOKEN_URL, { grant_type: 'authorization_code', code: q.get('code'), code_verifier, redirect_uri, client_id });Remediation
Generate a random state (crypto.getRandomValues) per authorization request, store it in sessionStorage, send it, and in the callback reject the response unless the returned state equals the stored value, then delete it. Prefer a maintained OIDC client library, and use PKCE (S256) in addition.
False-positive considerations
- Backends-for-frontend that validate
stateon the server while the SPA only forwards the code can be reported; suppress with a justification.
Known limitations
- Flows handled by OAuth libraries (angular-oauth2-oidc, angular-auth-oidc-client, MSAL) are not analyzed here; those libraries validate
statethemselves (see NGB024 for settings that weaken them). - State handling spread over several services (callback parsing in one class, comparison in another) can be missed when the exchange class neither reads nor compares the parameter.
- Whether the stored state is unguessable and single-use is not assessed.
References
Run it
ngbastion explain NGB023