Security rules

NGB013: Credential forwarding risk

Analyzes Angular HTTP interceptors (functional HttpInterceptorFn, functions registered with withInterceptors(), and classes implementing HttpInterceptor) and reports req.clone() calls that attach Authorization/API-key/token headers or withCredentials: true without first checking that the request goes to a trusted destination..

Property Value
Rule ID NGB013
Category Navigation & authentication
Default severity high
CWE CWE-522, CWE-200
Built-in presets recommended: error, strict: error, payments: error

Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.

What it checks

Analyzes Angular HTTP interceptors (functional HttpInterceptorFn, functions registered with withInterceptors(), and classes implementing HttpInterceptor) and reports req.clone() calls that attach Authorization/API-key/token headers or withCredentials: true without first checking that the request goes to a trusted destination.

Why it matters

Interceptors run for every HttpClient request, including calls to third-party APIs, CDNs and user-supplied URLs. An unconditional Authorization header leaks the user's bearer token to every one of those hosts.

Detection behavior and supported patterns

  • req.clone({ setHeaders: { Authorization: … } }), req.clone({ headers: req.headers.set("Authorization", …) }), sensitive headers such as X-Api-Key, X-Auth-Token, Cookie, or names classified as credentials
  • req.clone({ withCredentials: true }) (reported with medium severity)
  • Recognized destination checks (if / early return / ternary, including const isApi = … variables): req.url.startsWith(environment.apiUrl) or a constant https://host/, relative URLs (startsWith("/") and not //), new URL(req.url).origin === …, allowlists (ALLOWED.some(u => req.url.startsWith(u)), .includes(new URL(req.url).origin))
  • Weak checks (req.url.includes("api"), startsWith("http"), unanchored regexes) lower the confidence to medium; unknown project validators lower it to low

Example

TypeScript
export const authInterceptor: HttpInterceptorFn = (req, next) =>  next(req.clone({ setHeaders: { Authorization: `Bearer ${inject(Auth).token()}` } }));

Safer alternative

TypeScript
export const authInterceptor: HttpInterceptorFn = (req, next) => {  if (!req.url.startsWith(environment.apiUrl)) return next(req);  return next(req.clone({ setHeaders: { Authorization: `Bearer ${inject(Auth).token()}` } }));};

Remediation

Attach credentials only when the request URL targets your API origin: if (!req.url.startsWith(environment.apiUrl)) return next(req); before cloning, or compare new URL(req.url, location.origin).origin against an allowlist.

False-positive considerations

  • Applications that only ever call a single backend may still be reported; adding an explicit destination check documents that assumption and protects future third-party calls.

Known limitations

  • Checks performed outside the interceptor (e.g. a separate HttpClient instance via HttpBackend) are not seen; such interceptors may still be reported.
  • Header names built dynamically are not evaluated.
  • CSRF/XSRF token headers are intentionally not reported.

References

Run it

Shell
ngbastion explain NGB013