[whatwg/fetch] A service worker that passes a navigation through should not change its request headers (Issue #1984)

fbuecklers created an issue (whatwg/fetch#1984)

### What problem are you trying to solve?

Web features that act on a navigation add headers and leave the existing ones alone:

- navigation preload adds `Service-Worker-Navigation-Preload`,
- speculation rules add `Sec-Purpose` and `Sec-Speculation-Tags` and keep `Sec-Fetch-Dest: document` and `Sec-Fetch-Mode: navigate`,
- the worker script fetch adds `Service-Worker: script`,
- storage access adds `Sec-Fetch-Storage-Access`.

A pass-through worker, `event.respondWith(fetch(event.request))`, changes existing headers instead. Compared with the same navigation without a worker, the latest browsers send ([measurements](https://github.com/w3c/ServiceWorker/issues/1803#issuecomment-6944803505)):

| | `Sec-Fetch-Dest` | `Sec-Fetch-User` | `Sec-Fetch-Site` | Other |
|---|---|---|---|---|
| Chrome | `empty` | missing | `cross-site` for a typed URL | header order |
| Firefox | `empty` | missing | `same-origin` for cross-site | `Sec-Fetch-Mode: same-origin`, `Priority` |
| Safari | `empty` | (not sent) | `same-origin` for cross-site | `Referer` = worker URL, `Priority`, header order |

This is also unpredictable. In these cases the browser sends the unchanged navigation request and gives its response to the worker:

- when the worker isn't running yet: Chrome AutoPreload (top-level only) and Safari,
- with a `race-network-and-fetch-handler` route (Chrome 123+, Safari 27+),
- with navigation preload, through `event.preloadResponse` (plus `Service-Worker-Navigation-Preload`),
- depending on the browser version: Chrome's AutoPreload was off in 143–146 and has been on by default since 148.

Web features should behave in a way developers and servers can rely on. Here, neither can control which case applies, so neither can rely on the headers. In practice this breaks bot detection (Akamai Bot Manager), Shopify Markets and Fetch Metadata policies (w3c/ServiceWorker#1803).


### What solutions exist today?

None that keep the worker in charge. Navigation preload sends an extra request on every navigation, even when the worker answers from cache. A race route lets a fast network response replace the worker's answer. A `network` route bypasses the worker.

The changed headers don't protect anything either. The concern in #48 and w3c/ServiceWorker#1803 is that the worker could replay a navigation response elsewhere, but all the cases above already give it that response.

The current model also doesn't scale. As long as the worker's request isn't the navigation's own request, every new header for navigations needs its own rule for service workers, and each engine fills that gap differently:

- Fetch Metadata: the wrong `Sec-Fetch-Site` lets cross-site navigations through isolation policies in Firefox and Safari,
- `Priority`, `Referer` and the header order already differ,
- newer headers such as `Sec-Fetch-Storage-Access` will hit the same question.

If the worker's request is the navigation's request, new headers work through service workers by design.

### How would you solve it?

When a worker calls `fetch()` with the navigation's `event.request` unchanged and without `init`, once and while the fetch event is pending, the browser sends the navigation's own request: same headers, values and order. A new `Request`, any `init` or a later `fetch()` of the same object keeps today's behaviour.

This can't be used to forge navigations. The Request constructor rejects `mode: "navigate"` in `init`, and any `init` turns `navigate` into `same-origin`. So an unchanged `event.request` only exists while a real navigation is in progress.

If servers should be able to tell that a worker handled the navigation, use an additional header, like navigation preload and speculation rules do, not a change to the existing ones.

The change could go into the Request constructor (keep the destination and the navigation's initiator data when `init` is empty and the input is a navigation request) or into Handle Fetch.

The same principle applies to pass-through subresource requests. We start with navigations because that's where browsers already send the unchanged request, and where `mode: "navigate"` can't be forged.

### Anything else?

**Test**
- Tentative WPT: web-platform-tests/wpt#65569

**Related discussions**
- #1323: proxying concept for service workers
- #48: why the destination isn't developer-controlled
- w3c/ServiceWorker#1803: preserve `Sec-Fetch-Dest` for passed navigations, with field measurements
- w3c/ServiceWorker#1756: ServiceWorkerAutoPreload

**Bugs under the current spec, tracked separately**
- [Mozilla 2035038](https://bugzilla.mozilla.org/show_bug.cgi?id=2035038): Firefox computes `Origin`, `Sec-Fetch-Site` and `Sec-Fetch-Mode` from the worker
- [WebKit 334340](https://bugs.webkit.org/show_bug.cgi?id=334340): Safari sends the worker's `Sec-Fetch-Site`
- [Mozilla 1626616](https://bugzilla.mozilla.org/show_bug.cgi?id=1626616): Firefox leaves `document.referrer` empty
- [WebKit 332815](https://bugs.webkit.org/show_bug.cgi?id=332815): Safari sends the worker URL as `Referer`

**Questions**
- @annevk: would the Request constructor be the right place for this, or Handle Fetch in the Service Worker spec?
- @mikewest: does keeping the navigation's `Sec-Fetch-*` for an unchanged pass-through fit Fetch Metadata?
- @asutherland @yoshisatoyanagisawa @youennf: is there implementer interest from Gecko, Chromium and WebKit?

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/1984
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/fetch/issues/1984@github.com>

Received on Friday, 4 December 2026 15:52:17 UTC