# Service Worker Security FAQ [TOC] This FAQ is specifically about service workers. Also see the [general security FAQ](faq.md). Like the general security FAQ, this document is a collaborative effort by many Chromium developers. (rsesek, estark, falken, slightlyoff, jakearchibald, evn, raymes, ainslie, mek, lgarron, elawrence, kinuko, palmer, your name here...) Last updated 12 May 2017. If you see an error or have an additional question, and have a Chromium account, go ahead and fix it. If you don't have a Chromium account, email security-dev@chromium.org for a fix. ## Service Workers seem extremely risky! Why are they OK? Service Workers (SW) are indeed powerful. They support compelling web applications that can run offline or with intermittent connectivity. You can edit documents, browse and buy from catalogs, send social media messages, write email, etc. even in the subway! Service Workers can make the web platform more viable than ever before, enabling web apps to better compete with native apps even while essentially retaining the browse-to-use, sandboxed nature of the [Open Web Platform](https://www.w3.org/wiki/Open_Web_Platform) (OWP) that we all love. The rest of this FAQ will explain how the SW designers and implementers have mitigated the risks that necessarily come with this functionality. Service Workers are a replacement for and an improvement on the legacy [Application Cache API](https://developer.mozilla.org/en-US/docs/Web/HTML/Using_the_application_cache), which has been available in the OWP for a very long time. For more background on Service Workers, see [Service Workers Explained](https://github.com/w3c/ServiceWorker/blob/master/explainer.md). ## Do Service Workers run in a sandbox? Yes, SWs run in renderer processes. When Chrome starts a SW, it chooses a renderer process that is associated with the SW’s origin. If one does not exist, the browser creates a new one using a new [`SiteInstance`](https://cs.chromium.org/chromium/src/content/public/browser/site_instance.h) for the origin. ## What APIs can Service Workers access? The [HTML specification partially enumerates the API surface available to Workers](https://html.spec.whatwg.org/#apis-available-to-workers). See also [`Client`](https://developer.mozilla.org/en-US/docs/Web/API/Client), and [`ServiceWorkerGlobalScope`](https://w3c.github.io/ServiceWorker/#serviceworkerglobalscope-interface). (Note that SWs do not have access to synchronous APIs.) However, other web platform specifications can add new API surface. For example, the Permissions API exposes a permissions attribute to workers. Generally, SWs have access to a subset of the web platform APIs, although there are some Worker- and Service Worker-specific APIs that do not make sense for in-page JavaScript. (`[Service]WorkerGlobalScope` is of course not necessarily a strict subset of `Window`, and similarly `WorkerNavigator` is not necessarily a strict subset of `Navigator`. And the various SW events are of course only exposed to SWs.) ## Do Service Workers obey the same-origin policy? Service Worker registration specifies that [Service Workers must run in the same origin as their callers](https://w3c.github.io/ServiceWorker/#register-algorithm). The origin comparison for finding a Service Worker registration for a request is [specified](https://w3c.github.io/ServiceWorker/#scope-match-algorithm) to be to be a longest-prefix match of serialized URLs, including their path. (E.g. `https://example.com/` != `https://example.com.evil.com/`.) This specification gap seems fragile to us, [and should be fixed to be specified and implemented as actual origin equality](https://github.com/w3c/ServiceWorker/issues/1118), but doesn’t currently seem exploitable. Only [Secure Contexts can register or use Service Workers](https://w3c.github.io/webappsec-secure-contexts/#example-c52936fc). Because SWs can call `importScripts` to import scripts (from any other origin), it is a good idea for site operators to set a Content-Security-Policy response header on the ServiceWorker’s JavaScript response, instructing the browser what sources of script the origin considers trustworthy. That would reduce an XSS attacker’s ability to pull in their own code. ## Do Service Workers live forever? There are two concepts of “live” here. One is about the installed registration and one is about the running Service Worker thread. The installed registration lasts indefinitely, similar to origin-scoped storage like [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API). The browser performs an update check after any navigation using the Service Worker, invalidating the HTTP cache every 24 hours. Additionally, [browsers will revalidate the HTTP cache for SW scripts](https://w3c.github.io/ServiceWorker/#dfn-use-cache) unless the site opts into using the cache. The browser also performs an update check whenever the SW starts and periodically while the worker is running, if it has not checked in the last 24 hours (86,400 seconds, [as specified in the Handle Functional Event algorithm](https://w3c.github.io/ServiceWorker/#handle-functional-event-algorithm)). The browser can terminate a running SW thread at almost any time. Chrome terminates a SW if the SW has been idle for 30 seconds. Chrome also detects long-running workers and terminates them. It does this if an event takes more than 5 minutes to settle, or if the worker is busy running synchronous JavaScript and does not respond to a ping within 30 seconds. When a SW is not running, Developer Tools and chrome://serviceworker-internals show its status as STOPPED. ## How can I see Service Workers in Chrome? You can see them in the **Service Workers** field in the **Application** tab of **Developer Tools**. You can also look at [chrome://serviceworker-internals](chrome://serviceworker-internals). ## Do Service Workers keep running after I close the tab? If an origin has any Service Workers running, each worker will be shut down soon after it processes the last event. Events that can keep a worker alive include push notifications. (Note that the [push notifications will trigger a user-visible notification if the SW does not create one](https://cs.chromium.org/chromium/src/chrome/browser/push_messaging/push_messaging_notification_manager.cc?type=cs&l=270), and they also require the person to grant the origin permission in a prompt. you can see that in action in this [push notifications demo app](https://gauntface.github.io/simple-push-demo/).) ## Can attackers use Service Workers to trigger attacks developed after SW registration? For example, could an attacker convince users to visit a malicious website, then wait for (e.g.) a V8 bug to show up in Chrome's repository, then write an exploit, and then somehow run that exploit on the machines of everyone who visited the malicious website in the last month or so? Without explicit permission from the user, the browser won't let the SW poll for/receive any push notification events the attacker's server may (try to) send, and hence the SW won't get a chance to handle the events. Similarly, you might imagine a SW that tries to use `importScripts` to periodically (re-)load `maybe-v8-payload.js`. But, the SW would only get to do that as part of an event handler. And if the SW isn't getting any events (because the person is not browsing or navigating to attacker.com, and because the person never granted attacker.com push notification permission), then it will never get to run its event handlers, and so again the SW won't get a chance to attack. If the person is currently browsing attacker.com, then the attacker doesn't gain any additional attack benefit from a Service Worker. They can just `