PSA: Service Workers are Coming
IF YOU DO NOT RUN A SITE THAT HOSTS UNTRUSTED/USER-PROVIDED FILES OVER SSL/TLS, YOU CAN STOP READING NOW
This post describes the potential amplification of existing risks that Service Workers bring for multi-user origins where the origin may not fully trust the content or, in which, users should not be able to modify each other's content.
Sites hosting multiple-user content in separate directories, e.g. /~alice/index.html
and /~bob/index.html
, are not exposed to new risks by Service Workers. See below for details.
Sites which host content from many users on the same origin at the same level of path separation (e.g. https://example.com/alice.html
and https://example.com/bob.html
) may need to take precaution to disable Service Workers. These sites already rely on extraordinary cooperation between actors and are likely to find their security assumptions astonished by future changes to browsers.
Discussion
Service Workers are a new feature that are coming to the Web Platform very soon.
Like AppCache, Service Workers are available without user prompts and enable developers to create meaningful offline experiences for web sites. They are, however, strictly more powerful than AppCache.
To mitigate the risks associated with request interception, Service Workers are only available to use under the following restrictions:
- Service Workers are restricted to secure origins. E.g.,
http://acme.com/
can never have a Service Worker installed, whereashttps://acme.com
can. If you do not serve over SSL/TLS, service workers do not impact your site. - Service Worker scripts must be hosted at the same origin. E.g.,
https://acme.com/index.html
can only register a Service Worker script if that script is also hosted athttps://acme.com
. Scripts included by the root Service Worker viaimportScripts()
may come from other origins, but the root script itself cannot be registered against another origin. Redirects are also treated as errors for the purposes of SW script fetching to ensure that attackers cannot turn transient ownership into long-term control. - Service Workers are restricted by the path of the Service Worker script unless the
Service-Worker-Scope: ...
header is set.- Service Workers intercept requests for documents and their sub-resources. These documents are married to SW's based on longest-prefix-match of the path component of the script which is registered with the scopes.
- For example, if
https://acme.com/thinger/index.com
registers a SW hosted athttps://acme.com/thinger/sw.js
, it cannot by default intercept requests forhttps://acme.com/index.html
- This example may, however, respond for more-specific document requests like
https://acme.com/thinger/blargity/index.html
. - If the script is instead located at
https://acme.com/sw.js
, the registration will allow interception for all navigations athttps://acme.com/
. - This means that sites hosting multiple-user content in separated directories, e.g.
/~alice/
and/~bob/
, are not exposed to new risks by Service Workers. - Sites which host multiple user's content in the same directories may wish to consider disabling Service Workers (see below).
- Servers can break this restriction on allowed scope by sending a
Service-Worker-Scope: ...
header, where the value of the header is the allowed path (e.g.,/
). This feature will not arrive for Chrome until version 41 (6 weeks after the original release which adds support for Service Workers).
- Service Worker scripts must be served with valid JavaScript mime types, e.g.
text/javascript
. Resources served with marginalContent-Type
values, e.g.text/plain
, will NOT be respected as valid Service Worker scripts.
In addition to these restrictions, Service Workers include features to help site operators understand Service Worker usage on their origins. The most important of these is the Service-Worker: script
header which is appended to every request for script files which are intended for use as Service Workers.
This feature allows site owners, via logs and server-side directives, to:
- Audit use of Service Workers on an origin
- Control or disable Service Workers, either globally or by enforcing whitelists
Disabling Service Workers is straightforward. Here's an example snippet for an Apache .htaccess
file:
<IfModule mod_setenvif.c>
SetEnvIf Service-Worker script swrequest
<RequireAll>
Require all granted
Require not env swrequest
</RequireAll>
</IfModule>
For Nginx the recipe might be:
location / {
if ($http_service_worker) {
return 403;
}
...
}
Recommendations
If you run a site which hosts untrusted third-party content on a single origin over SSL/TLS, you should ensure that you:
- Disable Service Workers at your origin by blocking requests which include the
Service-Worker: script
header. This is easily accomplished using global server configuration (e.g.httpd.conf
directives). - If you wish to allow Service Workers, Begin auditing use of Service Workers on your origin as requests which include
Service-Worker: script
may indicate other problems with content hosting (e.g., if you do not mean to be hosting active HTML content but are doing so incidentally). - Move to a sub-domain-per-user model as soon as possible, e.g.
https://alice.example.com
instead ofhttps://example.com/~alice
. The browser-enforced same-origin model is fundamentally incompatible with serving content from multiple entities at the same origin. For instance, sites which can run on the same origin are susceptible to easy-to-make mixups withCookie
paths and storage poisoning attacks via Local Storage, WebSQL, IndexedDB, the Filesystem API, etc. The browser's model for how to separate principals relies almost exclusively on origins and we strongly recommend that you separate users by sub-domain (which is a different origin) so that future changes to browsers do not cause harmful interactions with your hosting setup.
Thanks to Kenji Baheux, Joel Weinberger, Devdatta Akhawe, and Matt Falkenhagen for their review and suggestions. All errors are mine alone, however.