Security rules

NGB072: Service worker attaches credentials to any origin

Reports custom service-worker fetch handlers (sw.js, service-worker.ts, *-sw.js) that add an Authorization / API-key header or credentials: 'include' to intercepted requests without first checking the request's destination (new URL(request.url).origin, url.startsWith(API), request.mode === 'same-origin')..

Property Value
Rule ID NGB072
Category Sensitive data & payment security
Default severity high
CWE CWE-522, CWE-200
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 custom service-worker fetch handlers (sw.js, service-worker.ts, *-sw.js) that add an Authorization / API-key header or credentials: 'include' to intercepted requests without first checking the request's destination (new URL(request.url).origin, url.startsWith(API), request.mode === 'same-origin').

Why it matters

A service worker sees every request the page makes — your API, but also third-party images, fonts, analytics and any URL an attacker can get the page to load (an <img> in user content). A handler that injects the bearer token into all of them sends it to every one of those hosts. Angular interceptors are covered by NGB013; this is the same mistake one layer lower, where it also affects requests the application never made itself.

Detection behavior and supported patterns

  • self.addEventListener('fetch', e => …) / self.onfetch = … handlers that create headers with Authorization / X-Api-Key … ({ Authorization: … }, headers.set('Authorization', …)) or credentials: 'include'
  • Not reported when the handler inspects the destination: .origin, .host, .hostname, .startsWith(…), new URL(…), request.mode === "same-origin", URL regex / includes tests
  • JavaScript workers are parsed syntactically; the generated ngsw-worker.js and build output are ignored

Example

TypeScript
self.addEventListener('fetch', (e) => {  e.respondWith(fetch(new Request(e.request, { headers: { Authorization: `Bearer ${token}` } })));});

Safer alternative

TypeScript
self.addEventListener('fetch', (e) => {  if (new URL(e.request.url).origin !== API_ORIGIN) return;  e.respondWith(fetch(new Request(e.request, { headers: { Authorization: `Bearer ${token}` } })));});

Remediation

Attach credentials only to your own API: const url = new URL(event.request.url); if (url.origin !== API_ORIGIN) return; before cloning the request with the token. Prefer HttpOnly cookies with SameSite over tokens in a service worker.

False-positive considerations

  • A worker deployed only for an API-only origin, where every request is first-party, is reported; add an explicit origin check to make that guarantee visible.

Known limitations

  • Only inline handler functions are analyzed; handlers defined elsewhere and passed by name are not followed.
  • Any destination check is accepted; a check that is present but wrong (substring match) is not detected.
  • Workers are found by file name only.

References

Run it

Shell
ngbastion explain NGB072