Bug 242910 - A stopped service worker fails to pass header and referrer on fetch request
Summary: A stopped service worker fails to pass header and referrer on fetch request
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Service Workers (show other bugs)
Version: Safari 15
Hardware: All All
: P2 Blocker
Assignee: youenn fablet
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-07-19 11:22 PDT by Braden O'Guinn
Modified: 2022-12-04 18:03 PST (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Braden O'Guinn 2022-07-19 11:22:40 PDT
As of Safari 15.5/Webkit 613.2.7, when a service worker has just moved from "idle" to "active", any fetch requests it makes will lack custom headers and referrer.

I am able to reproduce this problem consistently via installing this sandbox service worker:

```
self.addEventListener("fetch", (event) => {
    const newRequest = new Request(event.request, {
        headers: { "X-My-Custom-Header": "1" },
    });
    event.respondWith(fetch(newRequest));
});

```

And then checking the contents of the "$_SERVER" variable on my my PHP server (or whatever equivalent you use):

```
    [HTTP_HOST] => <my test server url>
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [HTTP_COOKIE] => <omitted, but looks good>
    [HTTP_USER_AGENT] => Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1
    [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.9
    [HTTP_ACCEPT_ENCODING] => gzip, deflate, br
    [HTTP_CONNECTION] => keep-alive
    [HTTP_REFERER] =>  
```

And you can see that `HTTP_X_MY_CUSTOM_HEADER` is missing.

To reproduce:

1. Install the sandbox service worker I posted above.
2. Load a page and see that the custom header and referrer are sent correctly in a network request.
3. Allow the service worker to go to the idle state. Practically, this looks like closing the test domain's tab and visiting another site for roughly 10 seconds. You can check the `Develop > Service Workers` drop-down menu to see if the service worker has been idled; if it has, it will be missing from this list.
4. Re-visit your test domain and see that the new request is now missing the custom header and referrer.
5. Re-load the page and see that a page request from an active and started service worker always contains the custom header and referrer.

Thanks for your help, and let me know whatever other info you need to debug. This is a blocking bug for Etsy's use of service workers.
Comment 1 Braden O'Guinn 2022-07-19 11:26:46 PDT
Note also that this issue is not present in any other browser that our company has been able to detect.
Comment 2 Braden O'Guinn 2022-07-21 13:28:08 PDT
We're seeing data in production that puts this bug back as far as 13.1.2.
Comment 3 Radar WebKit Bug Importer 2022-07-26 11:23:16 PDT
<rdar://problem/97616866>
Comment 4 youenn fablet 2022-11-17 06:53:01 PST
We might be reusing the preload we might be doing as the service worker is not active.
We should probably not since we are trying to fetch newRequest and not event.request.
Comment 5 youenn fablet 2022-11-18 02:11:24 PST
Pull request: https://github.com/WebKit/WebKit/pull/6624
Comment 6 EWS 2022-11-29 01:47:52 PST
Committed 257110@main (157a46485c50): <https://commits.webkit.org/257110@main>

Reviewed commits have been landed. Closing PR #6624 and removing active labels.