Security rules

NGB021: Disabled XSRF protection

Reports code that turns off Angular's built-in XSRF (CSRF) protection: withNoXsrfProtection() in provideHttpClient(...) and HttpClientXsrfModule.disable().

Property Value
Rule ID NGB021
Category Navigation & authentication
Default severity high
CWE CWE-352
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 code that turns off Angular's built-in XSRF (CSRF) protection: withNoXsrfProtection() in provideHttpClient(...) and HttpClientXsrfModule.disable(). Confidence depends on how the application authenticates: high when the project sends cookies with requests (withCredentials: true, credentials: 'include'), low when it authenticates with Authorization headers only, medium when neither is visible.

Why it matters

With cookie-based sessions the browser attaches the session cookie to any request to your API, including ones triggered by other sites. Angular's HttpXsrfInterceptor copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header for mutating same-origin requests, which a cross-site attacker cannot do. Disabling it removes the client half of that defence; the server must then rely solely on SameSite cookies or other checks.

Detection behavior and supported patterns

  • provideHttpClient(withNoXsrfProtection()) (function from @angular/common/http)
  • HttpClientXsrfModule.disable() in NgModule imports
  • Cookie-auth evidence: withCredentials: true or credentials: "include" anywhere in the project (raises confidence)
  • Bearer-auth evidence: Authorization headers attached in code (lowers confidence: header-authenticated APIs are not CSRF-prone)

Example

TypeScript
provideHttpClient(withInterceptors([auth]), withNoXsrfProtection())

Safer alternative

TypeScript
provideHttpClient(withInterceptors([auth]), withXsrfConfiguration({ cookieName: 'XSRF-TOKEN', headerName: 'X-XSRF-TOKEN' }))

Remediation

Remove withNoXsrfProtection() / HttpClientXsrfModule.disable(). If your backend uses different names, configure them with withXsrfConfiguration({ cookieName: '…', headerName: '…' }) and make the server reject mutating requests without a matching token.

False-positive considerations

  • Applications that authenticate exclusively with bearer tokens in headers do not need XSRF tokens; such findings are low confidence.

Known limitations

  • The server side is not visible: whether the backend validates the XSRF header, uses SameSite cookies or other CSRF defences cannot be assessed.
  • Angular only adds the XSRF header to mutating requests to relative / same-origin URLs; disabling it has no effect on cross-origin APIs, which need server-side CSRF protection anyway.
  • Custom HttpXsrfTokenExtractor providers that return no token are not detected.

References

Run it

Shell
ngbastion explain NGB021