Skip to content

Commit

Permalink
Don't serve responses service worker initiated requests from NS memor…
Browse files Browse the repository at this point in the history
…y cache

Service worker interception happens after checking network service
memory cache checks. Don't serve responses from the in-memory cache
when requests are originated from service workers so that service
workers can intercept them.

Fixes service-worker-interception-late.js in
http/tests/inspector-protocol/fetch/.

Bug: 1339708
Change-Id: I79c70663509209f5a52b7174662d3fd10afabf15
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3777800
Commit-Queue: Kenichi Ishibashi <bashi@chromium.org>
Reviewed-by: Yutaka Hirano <yhirano@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1026613}
  • Loading branch information
bashi authored and Chromium LUCI CQ committed Jul 21, 2022
1 parent afd0f6a commit 235e668
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion services/network/network_service_memory_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ enum class EntryStatus {
kUsed = 2,
kVaryMismatch = 3,
kBlockedByRequestHeaders = 4,
kMaxValue = kBlockedByRequestHeaders,
kBlockedServiceWorkerOriginatedRequest = 5,
kMaxValue = kBlockedServiceWorkerOriginatedRequest,
};

void RecordEntryStatus(EntryStatus result) {
Expand Down Expand Up @@ -464,6 +465,15 @@ absl::optional<std::string> NetworkServiceMemoryCache::CanServe(
return absl::nullopt;
}

// A service worker may be activated after this method and may want to
// intercept the request.
// TODO(https://crbug.com/1346152): Support service worker originated
// requests.
if (resource_request.originated_from_service_worker) {
RecordEntryStatus(EntryStatus::kBlockedServiceWorkerOriginatedRequest);
return absl::nullopt;
}

if (!CheckPrivateNetworkAccess(load_options, resource_request,
client_security_state,
it->second->transport_info)) {
Expand Down
10 changes: 10 additions & 0 deletions services/network/network_service_memory_cache_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,16 @@ TEST_F(NetworkServiceMemoryCacheTest, CanServe_DisableCache) {
ASSERT_FALSE(CanServeFromMemoryCache(request));
}

TEST_F(NetworkServiceMemoryCacheTest, CanServe_FromServiceWorker) {
ResourceRequest request = CreateRequest("/cacheable");
StoreResponseToMemoryCache(request);

// TODO(https://crbug.com/1346152): Support service worker originated
// requests and change the expectation.
request.originated_from_service_worker = true;
ASSERT_FALSE(CanServeFromMemoryCache(request));
}

TEST_F(NetworkServiceMemoryCacheTest, CanServe_ValidateCache) {
ResourceRequest request = CreateRequest("/cacheable");
request.load_flags |= net::LOAD_VALIDATE_CACHE;
Expand Down
1 change: 1 addition & 0 deletions tools/metrics/histograms/enums.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69707,6 +69707,7 @@ Called by update_net_trust_anchors.py.-->
<int value="2" label="Used"/>
<int value="3" label="VaryMismatch"/>
<int value="4" label="BlockedByRequestHeaders"/>
<int value="5" label="BlockedServiceWorkerOriginatedRequest"/>
</enum>

<enum name="NetworkServiceSandboxGrantResult">
Expand Down

0 comments on commit 235e668

Please sign in to comment.