Skip to content

Truly independent web browser

LibHTTP: Let a request's no-cache directive win over force-cache

Andreas Kling4cd8f6fb9c6c
diff --git a/Libraries/LibHTTP/Cache/DiskCache.cpp b/Libraries/LibHTTP/Cache/DiskCache.cpp
index b5e5abe3d60..98722ba0e57 100644
--- a/Libraries/LibHTTP/Cache/DiskCache.cpp
+++ b/Libraries/LibHTTP/Cache/DiskCache.cpp
@@ -177,7 +177,7 @@ Variant<Optional<CacheEntryReader&>, DiskCache::CacheHasOpenEntry> DiskCache::op
break;
case CacheLifetimeStatus::Expired:
- if (cache_mode_permits_stale_responses(cache_mode)) {
+ if (request_permits_stale_responses(cache_mode, request_headers)) {
dbgln_if(HTTP_DISK_CACHE_DEBUG, "\033[36m[disk]\033[0m \033[32;1mOpened expired cache entry for\033[0m {} (lifetime={}s age={}s) ({} bytes)", url, freshness_lifetime.to_seconds(), current_age.to_seconds(), index_entry->data_size);
} else {
dbgln_if(HTTP_DISK_CACHE_DEBUG, "\033[36m[disk]\033[0m \033[33;1mCache entry expired for\033[0m {} (lifetime={}s age={}s)", url, freshness_lifetime.to_seconds(), current_age.to_seconds());
@@ -191,7 +191,7 @@ Variant<Optional<CacheEntryReader&>, DiskCache::CacheHasOpenEntry> DiskCache::op
case CacheLifetimeStatus::MustRevalidate:
// NB: The "force-cache" and "only-if-cached" cache modes use a stored response "not paying attention to
// staleness", which includes responses that must be revalidated. Other engines behave the same way.
- if (cache_mode_permits_stale_responses(cache_mode)) {
+ if (request_permits_stale_responses(cache_mode, request_headers)) {
dbgln_if(HTTP_DISK_CACHE_DEBUG, "\033[36m[disk]\033[0m \033[32;1mOpened expired cache entry for\033[0m {} (lifetime={}s age={}s) ({} bytes)", url, freshness_lifetime.to_seconds(), current_age.to_seconds(), index_entry->data_size);
} else if (open_mode == OpenMode::Read) {
TRY(revalidate_cache_entry());
@@ -202,7 +202,7 @@ Variant<Optional<CacheEntryReader&>, DiskCache::CacheHasOpenEntry> DiskCache::op
break;
case CacheLifetimeStatus::StaleWhileRevalidate:
- if (cache_mode_permits_stale_responses(cache_mode)) {
+ if (request_permits_stale_responses(cache_mode, request_headers)) {
dbgln_if(HTTP_DISK_CACHE_DEBUG, "\033[36m[disk]\033[0m \033[32;1mOpened expired cache entry for\033[0m {} (lifetime={}s age={}s) ({} bytes)", url, freshness_lifetime.to_seconds(), current_age.to_seconds(), index_entry->data_size);
} else if (open_mode == OpenMode::Read) {
dbgln_if(HTTP_DISK_CACHE_DEBUG, "\033[36m[disk]\033[0m \033[36;1mMust revalidate, but may use, cache entry for\033[0m {} (lifetime={}s age={}s)", url, freshness_lifetime.to_seconds(), current_age.to_seconds());
diff --git a/Libraries/LibHTTP/Cache/MemoryCache.cpp b/Libraries/LibHTTP/Cache/MemoryCache.cpp
index 051599013ca..e55850ec2b0 100644
--- a/Libraries/LibHTTP/Cache/MemoryCache.cpp
+++ b/Libraries/LibHTTP/Cache/MemoryCache.cpp
@@ -133,7 +133,7 @@ Optional<MemoryCache::Entry const&> MemoryCache::open_entry(URL::URL const& url,
case CacheLifetimeStatus::StaleWhileRevalidate:
// NB: The "force-cache" and "only-if-cached" cache modes use a stored response "not paying attention to
// staleness", which includes responses that must be revalidated. Other engines behave the same way.
- if (cache_mode_permits_stale_responses(cache_mode)) {
+ if (request_permits_stale_responses(cache_mode, request_headers)) {
dbgln_if(HTTP_MEMORY_CACHE_DEBUG, "\033[37m[memory]\033[0m \033[32;1mOpened expired cache entry for\033[0m {} (lifetime={}s age={}s) ({} bytes)", url, freshness_lifetime.to_seconds(), current_age.to_seconds(), cache_entry->response_body.size());
cache_entry->last_use = next_use();
return *cache_entry;
diff --git a/Libraries/LibHTTP/Cache/Utilities.cpp b/Libraries/LibHTTP/Cache/Utilities.cpp
index 55ddb1a6e46..0b100e4021e 100644
--- a/Libraries/LibHTTP/Cache/Utilities.cpp
+++ b/Libraries/LibHTTP/Cache/Utilities.cpp
@@ -243,6 +243,24 @@ bool has_preconditions(HeaderList const& request_headers)
|| request_headers.contains("If-Range"sv);
}
+// AD-HOC: The "force-cache" cache mode uses a stored response "not paying attention to staleness". But a request that
+// carries a no-cache directive of its own asks for a response that the origin validated, so it does not get a
+// stale one. This is checked when the cache is looked up, as only then does the request have its final headers.
+bool request_permits_stale_responses(CacheMode cache_mode, HeaderList const& request_headers)
+{
+ if (!cache_mode_permits_stale_responses(cache_mode))
+ return false;
+ if (cache_mode != CacheMode::ForceCache)
+ return true;
+
+ if (auto cache_control = request_headers.get("Cache-Control"sv); cache_control.has_value())
+ return !contains_cache_control_directive(*cache_control, "no-cache"sv);
+ if (auto pragma = request_headers.get("Pragma"sv); pragma.has_value())
+ return !contains_cache_control_directive(*pragma, "no-cache"sv);
+
+ return true;
+}
+
// https://datatracker.ietf.org/doc/html/rfc9110#name-overview-of-status-codes
static bool is_heuristically_cacheable_status(u32 status_code)
{
diff --git a/Libraries/LibHTTP/Cache/Utilities.h b/Libraries/LibHTTP/Cache/Utilities.h
index a25fe6fabda..44c1a809666 100644
--- a/Libraries/LibHTTP/Cache/Utilities.h
+++ b/Libraries/LibHTTP/Cache/Utilities.h
@@ -11,6 +11,7 @@
#include <AK/Time.h>
#include <AK/Types.h>
#include <LibHTTP/Cache/CacheKey.h>
+#include <LibHTTP/Cache/CacheMode.h>
#include <LibHTTP/Forward.h>
#include <LibHTTP/HeaderList.h>
#include <LibURL/Forward.h>
@@ -39,6 +40,7 @@ LexicalPath path_for_cache_entry(LexicalPath const& cache_directory, CacheDigest
bool is_cacheable(StringView method, HeaderList const&);
bool has_preconditions(HeaderList const& request_headers);
+bool request_permits_stale_responses(CacheMode, HeaderList const& request_headers);
bool is_cacheable(u32 status_code, HeaderList const&);
bool is_header_exempted_from_storage(StringView name);
NonnullRefPtr<HeaderList> remove_connection_specific_fields(HeaderList const&);
diff --git a/Tests/LibWeb/Text/input/Cache/http-disk-cache.html b/Tests/LibWeb/Text/input/Cache/http-disk-cache.html
index 96e438c7aad..ba2e1a600b9 100644
--- a/Tests/LibWeb/Text/input/Cache/http-disk-cache.html
+++ b/Tests/LibWeb/Text/input/Cache/http-disk-cache.html
@@ -14,6 +14,7 @@
const ACCESS_CONTROL_ALLOW_HEADERS = [
"Cache-Control",
"If-Modified-Since",
+ "Pragma",
"Range",
TEST_CACHE_ENABLED_HEADER,
TEST_CACHE_REQUEST_TIME_OFFSET,
@@ -265,6 +266,28 @@
expectCacheStatus(url, response, "read-from-cache");
})();
+ // Requests with a force-cache directive do not receive stale responses if they have a no-cache directive.
+ for (const [name, value] of [["Cache-Control", "no-cache"], ["Pragma", "no-cache"]]) {
+ url = await createRequest(`/cache-test/cache-mode/force-cache-with-${name.toLowerCase()}-no-cache`, {
+ headers: {
+ Age: "2",
+ "Cache-Control": "max-age=5",
+ },
+ });
+
+ response = await cacheFetch(url);
+ expectCacheStatus(url, response, "written-to-cache");
+
+ response = await cacheFetch(url, {
+ cache: "force-cache",
+ headers: {
+ [name]: value,
+ [TEST_CACHE_REQUEST_TIME_OFFSET]: "5",
+ },
+ });
+ expectCacheStatus(url, response, "written-to-cache");
+ }
+
// Responses with only a max-age directive are cached.
await (async () => {
url = await createRequest("/cache-test/response/max-age", {