Security rules

NGB074: Service worker caches unvalidated responses

Reports cache.put(request, response) in custom service workers (sw.js, service-worker.ts, *-sw.js) when the surrounding handler never checks the response (response.ok, response.status, response.type / 'opaque').

Property Value
Rule ID NGB074
Category Security configuration
Default severity medium
CWE CWE-349, CWE-524
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 cache.put(request, response) in custom service workers (sw.js, service-worker.ts, *-sw.js) when the surrounding handler never checks the response (response.ok, response.status, response.type / 'opaque'). cache.add() / addAll() reject failed responses and are not reported.

Why it matters

A runtime cache serves its entries instead of the network. Without a response check, whatever came back once — a 500 page, a captive-portal or proxy-injected page, an attacker-influenced error response, or an opaque cross-origin response the worker cannot even inspect — is stored and replayed on every later visit, offline-first, until the cache is cleared. A single bad response poisons the application for that user.

Detection behavior and supported patterns

  • cache.put(req, res) / cache.put(req, res.clone()) in a service-worker file whose enclosing handler contains no .ok, .status or .type check and no 'opaque' comparison
  • Responses the worker creates itself (new Response(…)) are not reported
  • JavaScript workers are parsed syntactically; the generated ngsw-worker.js (which validates responses) and build output are ignored

Example

TypeScript
const res = await fetch(event.request);cache.put(event.request, res.clone());

Safer alternative

TypeScript
const res = await fetch(event.request);if (res.ok && res.type === 'basic') cache.put(event.request, res.clone());

Remediation

Only cache successful, non-opaque responses: if (response.ok && response.type === 'basic') cache.put(request, response.clone());. With Workbox use CacheableResponsePlugin({ statuses: [200] }). Keep cache-first strategies for versioned static assets.

False-positive considerations

  • Caches whose entries are short-lived or always revalidated still store the bad response for that period; the finding is about the missing check.

Known limitations

  • Checks in helper functions outside the handler are not followed; any .ok / .status / .type mention counts as a check.
  • Workbox strategies and plugins (CacheableResponsePlugin) are not analyzed.
  • Workers are found by file name only.

References

Run it

Shell
ngbastion explain NGB074