fix(fetch): ignore URL fragments in the raw memory cache

This commit is contained in:
ldm0
2026-11-22 02:02:03 +08:00
parent fd9ebc742a
commit e2bd2b0932
2 changed files with 87 additions and 3 deletions
@@ -714,12 +714,17 @@ impl SharedMemoryResourceCache {
// Retire the old entry before fetching its replacement. Failure,
// cancellation or a non-storable response must not resurrect it
// as a fresh hit for a later consumer.
let variant = variants.remove(index);
let mut variant = variants.remove(index);
variant
.response
.final_url
.set_fragment(request.url.fragment());
variants.retain(|other| !other.matches(request_headers));
self.insert_raw_subresource_variants(key.clone(), variants);
return Some(RawSubresourceCacheLookup::Revalidate(variant.response));
}
let hit = variants[index].response.clone();
let mut hit = variants[index].response.clone();
hit.final_url.set_fragment(request.url.fragment());
self.insert_raw_subresource_variants(key.clone(), variants);
Some(RawSubresourceCacheLookup::Local(hit))
}
@@ -953,9 +958,11 @@ pub(in crate::network) fn raw_subresource_memory_cache_key(
if !raw_subresource_request_is_memory_cacheable(request, request_headers) {
return None;
}
let mut target_url = request.url.clone();
target_url.set_fragment(None);
Some(RawSubresourceCacheKey {
page_cache_partition_id: 0,
url: request.url.as_str().to_owned(),
url: target_url.into(),
fetch_destination: request
.browser_request_metadata()
.map(BrowserRequestMetadata::fetch_destination),
@@ -537,3 +537,80 @@ async fn raw_memory_cache_revalidates_cors_redirect_hops_without_reusing_tainted
);
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn raw_memory_cache_fragments_share_representations_and_keep_each_requested_url() -> Result<()>
{
for observe in [false, true] {
let (url, server) = server(vec![
reply(
200,
&[
("Cache-Control", "max-age=600"),
("ETag", "\"fragment-version\""),
],
b"cached body",
),
reply(304, &[("Cache-Control", "max-age=600")], b""),
])
.await?;
let client = ResourceRequestClient::new(&FetchConfig::default())?;
for (index, fragment) in [
Some("first"),
Some("second"),
Some("validated"),
None,
Some(""),
Some("last"),
]
.into_iter()
.enumerate()
{
let target = fragment.map_or_else(|| url.clone(), |value| format!("{url}#{value}"));
let request = if index == 2 {
request(&target)?.with_cache_mode(RequestCacheMode::Validate)
} else {
request(&target)?
};
let (response, journal, cancel) = fetch(&client, request, observe).await?;
assert_eq!(response.final_url.as_str(), target);
assert_eq!(response.status, 200);
assert_eq!(response.body_bytes(), b"cached body");
assert_eq!(
response.cache_state,
match index {
0 => ResponseCacheState::None,
2 => ResponseCacheState::Validated,
_ => ResponseCacheState::Local,
}
);
if index == 2 {
assert!(
!cancel.response_timing().body_bytes_received(),
"304 is a bodyless wire response"
);
if observe {
assert_eq!(journal.final_response_observation().unwrap().status(), 304);
}
} else if index != 0 {
assert!(
journal.is_empty(),
"fragment-only changes must not start another transfer"
);
}
}
let requests = server.await??;
assert_eq!(requests.len(), 2);
assert!(
requests
.iter()
.all(|head| head.starts_with(b"GET /resource HTTP/"))
);
assert!(header(&requests[0], "if-none-match").is_none());
assert_eq!(
header(&requests[1], "if-none-match"),
Some(b"\"fragment-version\"".as_slice())
);
}
Ok(())
}