Security rules

NGB031: Sensitive data in CacheStorage

Reports application code that stores authenticated or user-specific responses in the browser's CacheStorage (caches.open(…) → cache.put() / cache.add() / cache.addAll()): requests carrying an Authorization header or credentials, user/account/session/payment endpoints, and response bodies that contain credentials or payment data..

Property Value
Rule ID NGB031
Category Sensitive data & payment security
Default severity medium
CWE CWE-524, CWE-922
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 application code that stores authenticated or user-specific responses in the browser's CacheStorage (caches.open(…) → cache.put() / cache.add() / cache.addAll()): requests carrying an Authorization header or credentials, user/account/session/payment endpoints, and response bodies that contain credentials or payment data.

Why it matters

CacheStorage is persistent, origin-wide and survives logout and browser restarts unless the application deletes it. On shared or lost devices the next user (or any script running on the origin) can read cached account data, tokens or payment details with caches.match(). Unlike HTTP caching it ignores Cache-Control: no-store.

Detection behavior and supported patterns

  • cache.put(request, response), cache.add(request), cache.addAll([...]) on a DOM Cache (from caches.open(), typed or inferred)
  • Request evidence: new Request(url, { headers: { Authorization } }) or credentials: 'include' (high); constant URLs such as /api/me, /account, /session, /orders, /payments (medium)
  • Response evidence: credential or payment names in the cached response / body (new Response(JSON.stringify({ accessToken })))

Example

TypeScript
const cache = await caches.open('api');await cache.put('/api/account', await fetch('/api/account', { credentials: 'include' }));

Safer alternative

TypeScript
const cache = await caches.open('static-v1');await cache.addAll(['/assets/i18n/en.json', '/assets/logo.svg']);

Remediation

Do not put authenticated responses into CacheStorage. If offline access is required, cache only non-personal data, scope cache names per user, and delete them on logout (caches.delete(name)).

False-positive considerations

  • Endpoints matched by name (e.g. a public /api/orders/statuses list) can be reported; suppress with a justification.

Known limitations

  • Cache keys built dynamically without a constant prefix are not classified.
  • Code inside service workers is covered by NGB032, which also analyzes Angular ngsw-config.json.
  • Whether the application clears the cache on logout (caches.delete()) is not verified.

References

Run it

Shell
ngbastion explain NGB031