fix(fetch): reuse eligible same-origin memory cache responses

Apply force-cache and only-if-cached stale reuse after representation and
Vary selection. Give cors and same-origin requests the same raw response
identity only for untainted same-origin URLs, retaining origin, credential,
partition, cookie and redirect-handling boundaries. Current consumers still
validate every cached redirect and response before exposing it.

Add four I/O regression tests for stale reuse, Vary and authority isolation,
cached same-origin redirects and denial of cached cross-origin redirects.
Validation: workspace fmt, all-target/all-feature clippy, and full nextest
(25,043 passed; 19 skipped).
This commit is contained in:
ldm0
2026-11-03 18:08:21 +08:00
parent dda4c52234
commit c900668cd0
3 changed files with 273 additions and 9 deletions
@@ -10,7 +10,8 @@ use std::sync::{Arc, Weak};
use indexmap::IndexMap;
use moli_fetch::{
BrowserRequestMetadata, RawResponse, Request, RequestHeaders, RequestResourceType, Response,
BrowserRequestMetadata, RawResponse, Request, RequestHeaders, RequestMode, RequestResourceType,
Response,
};
use moli_http_cache::{
HttpCacheRequestPolicy, cacheable_response_parts_policy, response_date, unix_now_ms,
@@ -598,13 +599,14 @@ impl SharedMemoryResourceCache {
.map(|(index, _)| index)?;
let policy = HttpCacheRequestPolicy::from_headers(request_headers);
let variant = &variants[index];
let reusable = policy.allows_reuse_at(
&variant.response.headers,
variant.stored_at_unix_ms,
Some(variant.expires_at_unix_ms),
request.cache_mode().requires_http_cache_validation(),
unix_now_ms(),
);
let reusable = request.cache_mode().allows_stale_http_cache_response()
|| policy.allows_reuse_at(
&variant.response.headers,
variant.stored_at_unix_ms,
Some(variant.expires_at_unix_ms),
request.cache_mode().requires_http_cache_validation(),
unix_now_ms(),
);
if !reusable && policy.only_if_cached {
// A cache-only miss must not retire a representation that a
// subsequent request with different constraints can still use.
@@ -867,6 +869,19 @@ pub(in crate::network) fn raw_subresource_memory_cache_key(
}
let mut target_url = request.url.clone();
target_url.set_fragment(None);
// These policies authorize the same untainted same-origin HTTP hop. Each
// consumer still checks its redirects and response policy, and Vary still
// distinguishes their mode-specific wire headers.
let request_mode = if request.request_mode == RequestMode::Cors
&& request
.request_origin()
.is_some_and(|origin| origin.same_origin_url(&request.url))
&& !request.has_cross_origin_url(&request.url)
{
RequestMode::SameOrigin
} else {
request.request_mode
};
Some(RawSubresourceCacheKey {
page_cache_partition_id: 0,
url: target_url.into(),
@@ -876,7 +891,7 @@ pub(in crate::network) fn raw_subresource_memory_cache_key(
request_origin: request
.request_origin()
.map(|origin| origin.ascii_serialization().to_owned()),
request_mode: request.request_mode.as_ref().to_owned(),
request_mode: request_mode.as_ref().to_owned(),
// Policies and explicit Fetch referrers can select different headers
// without changing the origin or cookie initiator in this cache key.
referrer: request.referrer_header_value(&request.url),
@@ -16,6 +16,10 @@ mod redirect_tests;
#[path = "async_fetch/preflight_cache_tests.rs"]
mod preflight_cache_tests;
#[cfg(test)]
#[path = "async_fetch/cache_mode_tests.rs"]
mod cache_mode_tests;
/// Script fetches need CORS authorization before following every redirect,
/// even though their browser-generated GET requests do not require preflight.
pub(crate) async fn fetch_cors_script_text(
@@ -0,0 +1,245 @@
use super::*;
use anyhow::Result;
use moli_fetch::{FetchConfig, RawResponse, RequestCacheMode, ResponseCacheState};
use std::time::Duration;
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::TcpListener,
task::JoinHandle,
time::timeout,
};
fn reply(status: u16, headers: &[(&str, &str)], body: &[u8]) -> Vec<u8> {
let mut response = format!(
"HTTP/1.1 {status} Test\r\nContent-Length: {}\r\nConnection: close\r\n",
body.len()
)
.into_bytes();
for (name, value) in headers {
response.extend_from_slice(format!("{name}: {value}\r\n").as_bytes());
}
response.extend_from_slice(b"\r\n");
response.extend_from_slice(body);
response
}
type Server = JoinHandle<Result<Vec<String>>>;
async fn server(responses: Vec<Vec<u8>>) -> Result<(url::Url, Server)> {
let listener = TcpListener::bind("127.0.0.1:0").await?;
let url = url::Url::parse(&format!("http://{}/start", listener.local_addr()?))?;
let task = tokio::spawn(async move {
let mut requests = Vec::new();
for response in responses {
let (mut stream, _) = timeout(Duration::from_secs(5), listener.accept()).await??;
let mut head = Vec::new();
while !head.ends_with(b"\r\n\r\n") {
anyhow::ensure!(head.len() < 8192, "request head is too large");
head.push(timeout(Duration::from_secs(5), stream.read_u8()).await??);
}
requests.push(String::from_utf8(head)?);
stream.write_all(&response).await?;
}
Ok(requests)
});
Ok((url, task))
}
fn client() -> Result<crate::network::ResourceRequestClientOwner> {
let mut config = FetchConfig::default();
config.set_http_no_proxy(Some("*".to_owned()));
crate::network::ResourceRequestClient::new(&config)
}
fn request(url: &url::Url, mode: RequestMode) -> Request {
Request::new_browser("GET", url.clone(), None, Vec::new(), url.into())
.with_initiator_url(url)
.with_request_mode(mode)
.with_credentials_mode(RequestCredentialsMode::SameOrigin)
.with_browser_request_metadata(BrowserRequestMetadata::Fetch)
}
async fn load(
loader: &ResourceRequestClient,
request: Request,
) -> Result<(RawResponse, NetworkObservationJournal)> {
let observed = fetch_browser_subresource_raw_stream_with_cors_redirect_checks(
loader,
request,
None,
Vec::new(),
None,
None,
)
.await
.map_err(anyhow::Error::msg)?;
let (response, journal) = observed.into_parts_with_observation_journal();
Ok((response.into_materialized_raw_response().await?, journal))
}
#[tokio::test]
async fn cache_modes_reuse_stale_same_origin_hops_without_network() -> Result<()> {
for control in ["max-age=0, must-revalidate", "no-cache"] {
let (url, server) = server(vec![reply(
200,
&[("Cache-Control", control), ("ETag", "\"version\"")],
b"stored",
)])
.await?;
let owner = client()?;
let loader = owner.handle();
let seed = request(&url, RequestMode::Cors);
assert_eq!(load(&loader, seed.clone()).await?.0.body_bytes(), b"stored");
// The listening socket is gone before these reads: revalidation or a
// cache miss cannot accidentally pass by receiving another response.
assert_eq!(server.await??.len(), 1);
for (mode, cache) in [
(RequestMode::Cors, RequestCacheMode::ForceCache),
(RequestMode::SameOrigin, RequestCacheMode::ForceCache),
(RequestMode::SameOrigin, RequestCacheMode::OnlyIfCached),
] {
let (hit, journal) = load(
&loader,
seed.clone().with_request_mode(mode).with_cache_mode(cache),
)
.await?;
assert_eq!(hit.body_bytes(), b"stored");
assert_eq!(hit.cache_state, ResponseCacheState::Local);
assert!(journal.is_empty());
}
}
Ok(())
}
#[tokio::test]
async fn cache_modes_keep_vary_credentials_origin_and_partition_boundaries() -> Result<()> {
let (url, server) = server(vec![reply(
200,
&[
("Cache-Control", "max-age=600"),
("Vary", "X-Representation"),
],
b"first representation",
)])
.await?;
let owner = client()?;
let loader = owner.handle();
let mut seed = request(&url, RequestMode::Cors);
seed.request_headers = vec![("X-Representation".to_owned(), "first".to_owned())].into();
load(&loader, seed.clone()).await?;
assert_eq!(server.await??.len(), 1);
let cache_only = seed
.with_request_mode(RequestMode::SameOrigin)
.with_cache_mode(RequestCacheMode::OnlyIfCached);
let mut varied = cache_only.clone();
varied.request_headers = vec![("X-Representation".to_owned(), "second".to_owned())].into();
for isolated in [
varied,
cache_only
.clone()
.with_network_partition_key(Some("peer".to_owned())),
cache_only
.clone()
.with_credentials_mode(RequestCredentialsMode::Omit),
] {
let error = load(&loader, isolated).await.unwrap_err();
assert!(
error.to_string().contains("net::ERR_CACHE_MISS"),
"{error:#}"
);
}
let error = load(
&loader,
cache_only
.clone()
.with_request_origin(moli_url::WebOrigin::Opaque),
)
.await
.unwrap_err();
assert!(error.to_string().contains("same-origin"), "{error:#}");
let (retained, journal) = load(&loader, cache_only).await?;
assert_eq!(retained.body_bytes(), b"first representation");
assert_eq!(retained.cache_state, ResponseCacheState::Local);
assert!(journal.is_empty());
Ok(())
}
#[tokio::test]
async fn cache_modes_cached_same_origin_redirects_rebuild_the_consumer_url_list() -> Result<()> {
for control in ["max-age=600", "max-age=0, must-revalidate"] {
let (url, server) = server(vec![
reply(
302,
&[
("Cache-Control", control),
("ETag", "\"redirect\""),
("Location", "/final"),
],
b"",
),
reply(
200,
&[("Cache-Control", control), ("ETag", "\"body\"")],
b"final body",
),
])
.await?;
let owner = client()?;
let loader = owner.handle();
let (seed, _) = load(&loader, request(&url, RequestMode::Cors)).await?;
assert!(seed.redirected);
assert_eq!(seed.redirect_chain.len(), 1);
assert_eq!(server.await??.len(), 2);
let (hit, journal) = load(
&loader,
request(&url, RequestMode::SameOrigin).with_cache_mode(RequestCacheMode::OnlyIfCached),
)
.await?;
assert_eq!(hit.body_bytes(), b"final body");
assert_eq!(hit.final_url.path(), "/final");
assert!(hit.redirected);
assert_eq!(hit.redirect_chain.len(), 1);
assert_eq!(hit.cache_state, ResponseCacheState::Local);
assert!(journal.is_empty());
}
Ok(())
}
#[tokio::test]
async fn cache_modes_cached_redirects_cannot_bypass_same_origin_policy() -> Result<()> {
let (target, target_server) = server(vec![reply(
200,
&[
("Cache-Control", "max-age=600"),
("Access-Control-Allow-Origin", "*"),
],
b"foreign body",
)])
.await?;
let (source, source_server) = server(vec![reply(
302,
&[
("Cache-Control", "max-age=600"),
("Location", target.as_str()),
],
b"",
)])
.await?;
let owner = client()?;
let loader = owner.handle();
let (seed, _) = load(&loader, request(&source, RequestMode::Cors)).await?;
assert_eq!(seed.body_bytes(), b"foreign body");
assert_eq!(source_server.await??.len(), 1);
assert_eq!(target_server.await??.len(), 1);
let error = load(
&loader,
request(&source, RequestMode::SameOrigin).with_cache_mode(RequestCacheMode::OnlyIfCached),
)
.await
.unwrap_err();
// The redirect itself must have hit the cache; the target is rejected by
// the current consumer's policy before any cache lookup or network access.
assert!(error.to_string().contains("same-origin"), "{error:#}");
assert!(!error.to_string().contains("ERR_CACHE_MISS"), "{error:#}");
Ok(())
}