New issue
Advanced search Search tips

Issue 887637 link

Starred by 2 users

Issue metadata

Status: Available
Owner: ----
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: ----
Pri: 3
Type: Bug



Sign in to add a comment

v8 code caching will not work for service workers that serve scripts from cache_storage using a different Request URL key

Project Member Reported by wanderview@chromium.org, Sep 20

Issue description

When a cache_storage Response is returned from a service worker fetch event handler for a script, the v8 code cache may trigger this code:

https://cs.chromium.org/chromium/src/third_party/blink/renderer/platform/loader/fetch/resource.cc?l=178&rcl=70c422048c104b20c547bd4da7b3f4e52aa82234

This sends a message back to the browser with the code cache metadata, the cache name, and the ResponseResponse::Url().  That cache name and URL are then used to look up a DiskCacheEntry in cache_storage to store the metadata in.

This works well for the common case where the fetch event handler does something like:

  evt.respondWith(caches.match(evt.request));

However, its possible for the service worker script to run arbitrary js and return Response objects not generated in this way.

For example, consider a service worker that stores different Responses for different modes:

  let mode = GetMode();
  evt.respondWith(caches.match(evt.request.url + '?mode=' + mode));

In this case the message to store the cached metadata will not find an entry.  What is worse, though, is its possible there will be an entry, but for a different script than what was actually returned to the page.

I have not been able to explicitly trigger the v8 code cache heuristics to test this, but I did instrument ServiceWorkerCachedMetadataSender.  I can see that it uses the wrong URL on this page:

  https://sw-cache-switcheroo.glitch.me/

Even if the URL was updated, though, there would still be potential for problems.  For example, a new script could be written to the same URL and result in the cached metadata being wrong.

To fix this we should probably plumb a unique identifier for the cache_storage entry that can be used to store against only the specific entry that the v8 compiler saw.

Marking this restrict-view for now since I'm unsure if its a security issue.
 
Cc: wanderview@chromium.org
Components: -Blink>Storage>CacheStorage
Labels: -Pri-3 Pri-1
Temporarily marking this only ServiceWorker to signal that we'd like them to triage it.  Please add CacheStorage back after you look at.

What do you all think of priority and potential fixes here?

As another data point, Jeff Posnick suggests in chat that all sw-precache sites would hit the case where v8 code cache is never stored (but not the really bad case of code cache stored with wrong Response):

  "it might be similar to what sw-precache does?
   say at https://googlechromelabs.github.io/sw-precache/

   there's a cache entry with the URL https://googlechromelabs.github.io/sw-precache/js/service-worker-registration.js?_sw-precache=0b4c35226075896152de214f8860b76e
   and that's used in the response for https://googlechromelabs.github.io/sw-precache/js/service-worker-registration.js"
We are using both the URL and the response time which is recorded when Chrome received the response from the server. So it is almost impossible to write the metadata against wrong cache_storage entry.
But yes, using the response time is not an ideal solution.
I think we should have an unique identifier for the cache_storage entry.
Oh, I see now.  Thanks, I totally missed that.

So I think then the only issue here is that if the URL does not match then the cached metadata won't be written.  So sites using code like sw-precache won't get that optimization.

Does that sound correct?
Yes.
Components: Blink>Storage>CacheStorage
Labels: -Restrict-View-Google -Pri-1 Pri-3
Adding back to Storage team. No security issue. We can file a new bug for the perfmance issue or keep this issue.
Summary: v8 code caching will not work for service workers that serve scripts from cache_storage using a different Request URL key (was: service worker can cause v8 code cache metadata to be stored against wrong cache_storage entry)
Status: Available (was: Untriaged)

Sign in to add a comment