Skip to content

Truly independent web browser

LibHTTP: Treat an invalid Expires header as already expired

Andreas Kling4625db216816
diff --git a/Libraries/LibHTTP/Cache/Utilities.cpp b/Libraries/LibHTTP/Cache/Utilities.cpp
index bc3d5f9f687..ab96e7ae872 100644
--- a/Libraries/LibHTTP/Cache/Utilities.cpp
+++ b/Libraries/LibHTTP/Cache/Utilities.cpp
@@ -552,7 +552,14 @@ AK::Duration calculate_freshness_lifetime(u32 status_code, HeaderList const& hea
// * If the Expires response header field (Section 5.3) is present, use its value minus the value of the Date response
// header field (using the time the message was received if it is not present, as per Section 6.6.1 of [HTTP]), or
- if (auto expires = parse_http_date(headers.get("Expires"sv)); expires.has_value()) {
+ if (auto expires_header = headers.get("Expires"sv); expires_header.has_value()) {
+ // https://httpwg.org/specs/rfc9111.html#field.expires
+ // A cache recipient MUST interpret invalid date formats, especially the value "0", as representing a time in
+ // the past (i.e., "already expired").
+ auto expires = parse_http_date(*expires_header);
+ if (!expires.has_value())
+ return {};
+
auto date = parse_http_date(headers.get("Date"sv)).value_or_lazy_evaluated([&]() {
return UnixDateTime::now() + current_time_offset_for_testing;
});
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/http-cache/freshness.any.sharedworker.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/http-cache/freshness.any.sharedworker.txt
index 340488ee522..a92d46f553c 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/http-cache/freshness.any.sharedworker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/http-cache/freshness.any.sharedworker.txt
@@ -2,14 +2,14 @@ Harness status: OK
Found 17 tests
-14 Pass
-3 Fail
+15 Pass
+2 Fail
Pass HTTP cache reuses a response with a future Expires
Pass HTTP cache does not reuse a response with a past Expires
Pass HTTP cache does not reuse a response with a present Expires
Pass HTTP cache does not reuse a response with an invalid Expires
Pass HTTP cache does not reuse a response with an invalid Expires with Last-Modified now
-Fail HTTP cache does not reuse a response with an invalid Expires with past Last-Modified
+Pass HTTP cache does not reuse a response with an invalid Expires with past Last-Modified
Pass HTTP cache reuses a response with positive Cache-Control: max-age
Pass HTTP cache does not reuse a response with Cache-Control: max-age=0
Pass HTTP cache reuses a response with positive Cache-Control: max-age and a past Expires
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/http-cache/freshness.any.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/http-cache/freshness.any.txt
index 340488ee522..a92d46f553c 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/http-cache/freshness.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/http-cache/freshness.any.txt
@@ -2,14 +2,14 @@ Harness status: OK
Found 17 tests
-14 Pass
-3 Fail
+15 Pass
+2 Fail
Pass HTTP cache reuses a response with a future Expires
Pass HTTP cache does not reuse a response with a past Expires
Pass HTTP cache does not reuse a response with a present Expires
Pass HTTP cache does not reuse a response with an invalid Expires
Pass HTTP cache does not reuse a response with an invalid Expires with Last-Modified now
-Fail HTTP cache does not reuse a response with an invalid Expires with past Last-Modified
+Pass HTTP cache does not reuse a response with an invalid Expires with past Last-Modified
Pass HTTP cache reuses a response with positive Cache-Control: max-age
Pass HTTP cache does not reuse a response with Cache-Control: max-age=0
Pass HTTP cache reuses a response with positive Cache-Control: max-age and a past Expires
diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/http-cache/freshness.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/http-cache/freshness.any.worker.txt
index 340488ee522..a92d46f553c 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/http-cache/freshness.any.worker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/http-cache/freshness.any.worker.txt
@@ -2,14 +2,14 @@ Harness status: OK
Found 17 tests
-14 Pass
-3 Fail
+15 Pass
+2 Fail
Pass HTTP cache reuses a response with a future Expires
Pass HTTP cache does not reuse a response with a past Expires
Pass HTTP cache does not reuse a response with a present Expires
Pass HTTP cache does not reuse a response with an invalid Expires
Pass HTTP cache does not reuse a response with an invalid Expires with Last-Modified now
-Fail HTTP cache does not reuse a response with an invalid Expires with past Last-Modified
+Pass HTTP cache does not reuse a response with an invalid Expires with past Last-Modified
Pass HTTP cache reuses a response with positive Cache-Control: max-age
Pass HTTP cache does not reuse a response with Cache-Control: max-age=0
Pass HTTP cache reuses a response with positive Cache-Control: max-age and a past Expires