Security rules
NGB058: ID token sent as API bearer token
Reports Authorization headers whose value is an OpenID Connect ID token: getIdToken() of an OIDC client library (angular-oauth2-oidc, angular-auth-oidc-client, oidc-client-ts, Auth0, Keycloak, MSAL — high confidence), or values named idToken / id_token (medium; low when the name only appears along the traced data flow).
| Property | Value |
|---|---|
| Rule ID | NGB058 |
| Category | Navigation & authentication |
| Default severity | medium |
| CWE | CWE-287, CWE-863 |
| Built-in presets | recommended: warn, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Reports Authorization headers whose value is an OpenID Connect ID token: getIdToken() of an OIDC client library (angular-oauth2-oidc, angular-auth-oidc-client, oidc-client-ts, Auth0, Keycloak, MSAL — high confidence), or values named idToken / id_token (medium; low when the name only appears along the traced data flow). Firebase, Amplify / Cognito and Google identity tokens, which those platforms are designed to send to your backend, are not reported.
Why it matters
An ID token tells the client who signed in; its audience is the client application, not your API. An API that accepts ID tokens as bearer tokens cannot tell which client a token was issued to (any app of the same identity provider gets valid ID tokens for its users), has no scopes to authorize against, and usually accepts tokens with a longer lifetime that are logged and stored more freely. OAuth 2.0 access tokens — audience-restricted to the API — exist for this.
Detection behavior and supported patterns
- Header values:
{ Authorization:Bearer ${v}}inHttpHeaders,setHeaders, Fetch / Axios options;headers.set('Authorization', v)/.append(…) vcontainsoauthService.getIdToken()(OIDC client library) → highvis or contains an identifier / key namedidToken,id_token,IdToken→ medium; only along the traced flow → low- Excluded: Firebase / AngularFire
user.getIdToken(), Amplify / Cognito, Google auth libraries
Example
req.clone({ setHeaders: { Authorization: `Bearer ${this.oauth.getIdToken()}` } })Safer alternative
req.clone({ setHeaders: { Authorization: `Bearer ${this.oauth.getAccessToken()}` } })Remediation
Request an access token for the API (an audience / resource scope) and send getAccessToken() instead. Have the API validate the token audience. Keep the ID token in the client for display and session information only.
False-positive considerations
- Backends designed around ID tokens of their own client (some Google Cloud and serverless setups) are reported; document an exception.
Known limitations
- Tokens stored under a generic name (
token) are not recognized as ID tokens. - Whether the backend validates the audience is not visible; a backend that only accepts its own ID tokens (same client) is not exploitable through other clients.
References
Run it
ngbastion explain NGB058