Skip to content

Truly independent web browser

LibHTTP: Do not let max-stale reuse a must-revalidate response

Andreas Klingbdc8ca4ab3f7
diff --git a/Libraries/LibHTTP/Cache/Utilities.cpp b/Libraries/LibHTTP/Cache/Utilities.cpp
index 0b100e4021e..a3457339724 100644
--- a/Libraries/LibHTTP/Cache/Utilities.cpp
+++ b/Libraries/LibHTTP/Cache/Utilities.cpp
@@ -662,7 +662,14 @@ CacheLifetimeStatus cache_lifetime_status(HeaderList const& request_headers, Hea
if (freshness_lifetime > current_age)
return CacheLifetimeStatus::Fresh;
- if (request_cache_control.has_value()) {
+ // https://httpwg.org/specs/rfc9111.html#cache-response-directive.must-revalidate
+ // The must-revalidate response directive indicates that once the response has become stale, a cache MUST NOT reuse
+ // that response to satisfy another request until it has been successfully validated by the origin
+ //
+ // NB: A max-stale request directive does not permit reuse of such a response either, so check this first.
+ bool has_must_revalidate = response_cache_control.has_value() && contains_cache_control_directive(*response_cache_control, "must-revalidate"sv);
+
+ if (request_cache_control.has_value() && !has_must_revalidate) {
// https://httpwg.org/specs/rfc9111.html#cache-request-directive.max-stale
// The max-stale request directive indicates that the client will accept a response that has exceeded its
// freshness lifetime. If a value is present, then the client is willing to accept a response that has exceeded
@@ -680,16 +687,12 @@ CacheLifetimeStatus cache_lifetime_status(HeaderList const& request_headers, Hea
if (!response_cache_control.has_value())
return revalidation_status(CacheLifetimeStatus::MustRevalidate);
- // https://httpwg.org/specs/rfc9111.html#cache-response-directive.must-revalidate
- // The must-revalidate response directive indicates that once the response has become stale, a cache MUST NOT reuse
- // that response to satisfy another request until it has been successfully validated by the origin
- //
- // NB: This takes precedence over stale-while-revalidate, as per
+ // NB: must-revalidate takes precedence over stale-while-revalidate, as per
// https://httpwg.org/specs/rfc9111.html#serving.stale.responses:
// A cache MUST NOT generate a stale response if it is prohibited by an explicit in-protocol directive (e.g., by
// a no-cache response directive, a must-revalidate response directive, or an applicable s-maxage or
// proxy-revalidate response directive; see Section 5.2.2).
- if (contains_cache_control_directive(*response_cache_control, "must-revalidate"sv))
+ if (has_must_revalidate)
return revalidation_status(CacheLifetimeStatus::MustRevalidate);
// https://httpwg.org/specs/rfc5861.html#n-the-stale-while-revalidate-cache-control-extension
diff --git a/Tests/LibWeb/Text/input/Cache/http-disk-cache.html b/Tests/LibWeb/Text/input/Cache/http-disk-cache.html
index ba2e1a600b9..0278fbca980 100644
--- a/Tests/LibWeb/Text/input/Cache/http-disk-cache.html
+++ b/Tests/LibWeb/Text/input/Cache/http-disk-cache.html
@@ -411,6 +411,26 @@
expectCacheStatus(url, response, "written-to-cache");
})();
+ // Stale responses with a must-revalidate directive are not reused for a request with a max-stale directive.
+ await (async () => {
+ url = await createRequest("/cache-test/request/max-stale-must-revalidate", {
+ headers: {
+ "Cache-Control": "max-age=5, must-revalidate",
+ },
+ });
+
+ response = await cacheFetch(url);
+ expectCacheStatus(url, response, "written-to-cache");
+
+ response = await cacheFetch(url, {
+ headers: {
+ "Cache-Control": "max-stale=60",
+ [TEST_CACHE_REQUEST_TIME_OFFSET]: "10",
+ },
+ });
+ expectCacheStatus(url, response, "written-to-cache");
+ })();
+
// Requests with a valueluess max-stale directive are cached.
await (async () => {
url = await createRequest("/cache-test/request/max-stale-valueless", {