Security rules

NGB071: Service worker scope expansion

Reports Service-Worker-Allowed response headers that are sent for more than the service-worker script itself — for path globs or all responses in firebase.json, vercel.json, staticwebapp.config.json (globalHeaders / routes) and _headers files, or set by server middleware outside a route for a specific .js file — and navigator.serviceWorker.register(url, { scope }) calls whose scope comes from untrusted data..

Property Value
Rule ID NGB071
Category Security configuration
Default severity medium
CWE CWE-668, CWE-284
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 Service-Worker-Allowed response headers that are sent for more than the service-worker script itself — for path globs or all responses in firebase.json, vercel.json, staticwebapp.config.json (globalHeaders / routes) and _headers files, or set by server middleware outside a route for a specific .js file — and navigator.serviceWorker.register(url, { scope }) calls whose scope comes from untrusted data.

Why it matters

A service worker may only control pages below its script's directory unless the script response carries Service-Worker-Allowed. When that header is attached to every response (or every *.js file), any script an attacker can get served from your origin — an uploaded file, a JSONP endpoint, a user-controlled path on a CDN folder — can be registered as a worker for the whole site and then intercept every request, persistently.

Detection behavior and supported patterns

  • Hosting configs: { "source": "**", "headers": [{ "key": "Service-Worker-Allowed", "value": "/" }] } and any source that is not a literal script file (medium for /, low for narrower scopes)
  • _headers blocks for wildcard paths containing Service-Worker-Allowed
  • Server code: res.setHeader('Service-Worker-Allowed', '/') without a condition and outside app.get('/sw.js', …)
  • navigator.serviceWorker.register(url, { scope }) with a traced untrusted scope

Example

TypeScript
{ "source": "**", "headers": [{ "key": "Service-Worker-Allowed", "value": "/" }] }

Safer alternative

TypeScript
{ "source": "/assets/sw.js", "headers": [{ "key": "Service-Worker-Allowed", "value": "/" }] }

Remediation

Serve the service worker from the scope it controls (usually the site root) so the header is not needed. If it must live in a sub-folder, send Service-Worker-Allowed only for that one script path ("source": "/assets/sw.js"), never for globs or all responses.

False-positive considerations

  • A wildcard source that in practice only matches the worker script (e.g. a folder that only contains sw.js) is reported; narrow the source to the script path.

Known limitations

  • Reverse-proxy configurations (nginx, Apache, CDN rules) are not read.
  • Whether the origin actually serves attacker-influenced JavaScript is not assessed; the finding reports the widened precondition.

References

Run it

Shell
ngbastion explain NGB071