From ef7becd7fa3ce0739c69b93ffd0026d2174b2083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 25 Jul 2026 08:54:00 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=E2=9A=A1=20compare=20If-None-Match=20we?= =?UTF-8?q?akly,=20as=20RFC=209110=20requires?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 276904c compared validators strongly, but RFC 9110 §13.1.2 says a recipient MUST use the weak comparison function for If-None-Match. A `W/""` request got a full 200 where a 304 was required, so any intermediary that weakens ETags on compression — nginx does — silently turned the whole feature off and every poll re-sent its payload. `matches()` now strips a leading `W/` from both the candidate and the stored tag before comparing the opaque parts. Everything else is untouched: `*`, comma lists, unquoted and malformed values, the layer position, and the 304 wire form all behaved correctly already. Vary: Cookie deliberately not added — CorsLayer already owns that header, so doing it right needs append-and-dedup rather than an insert, and `private` plus a body-hash validator already means a cross-user 304 can only fire on byte-identical bodies. --- src/web/etag.rs | 41 +++++++++++++++++++++++++++++++++++++---- tests/etag.rs | 23 +++++++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/web/etag.rs b/src/web/etag.rs index 49b5f5d..a01de0f 100644 --- a/src/web/etag.rs +++ b/src/web/etag.rs @@ -108,9 +108,11 @@ fn compute_etag(bytes: &[u8]) -> String { format!("\"{:016x}\"", hasher.finish()) } -/// Strong `If-None-Match` compare: `*` matches anything, otherwise the (comma- -/// separated) candidate list must contain our exact quoted tag. A weak -/// validator `W/"…"` never equals our strong tag, so it correctly misses. +/// `If-None-Match` compare: `*` matches anything, otherwise the (comma- +/// separated) candidate list must contain our tag. RFC 9110 §13.1.2 mandates the +/// *weak* comparison function here, so the `W/` prefix is stripped from both +/// sides and only the opaque quoted strings are compared — an intermediary that +/// weakens our ETag (nginx does this when it gzips) must still get its `304`. fn matches(if_none_match: Option<&HeaderValue>, tag: &str) -> bool { let Some(value) = if_none_match.and_then(|v| v.to_str().ok()) else { return false; @@ -120,5 +122,36 @@ fn matches(if_none_match: Option<&HeaderValue>, tag: &str) -> bool { || value .split(',') .map(str::trim) - .any(|candidate| candidate == tag) + .any(|candidate| opaque(candidate) == opaque(tag)) +} + +/// The opaque part of a validator — the quoted string without any weak prefix. +fn opaque(validator: &str) -> &str { + validator.strip_prefix("W/").unwrap_or(validator) +} + +#[cfg(test)] +mod tests { + use super::matches; + use axum::http::HeaderValue; + + fn m(header: &str, tag: &str) -> bool { + matches(Some(&HeaderValue::from_str(header).unwrap()), tag) + } + + #[test] + fn weak_comparison_is_symmetric() { + // Both directions and both-weak: the opaque strings decide (RFC 9110 + // §13.1.2), never the `W/` prefix. + assert!(m("W/\"abc\"", "\"abc\""), "weak candidate vs strong tag"); + assert!(m("\"abc\"", "W/\"abc\""), "strong candidate vs weak tag"); + assert!(m("W/\"abc\"", "W/\"abc\""), "both weak"); + assert!(m("\"abc\"", "\"abc\""), "both strong"); + // A different opaque string still misses, weak prefix or not. + assert!(!m("W/\"abd\"", "\"abc\"")); + // List membership and `*` keep working through the strip. + assert!(m("\"x\", W/\"abc\"", "\"abc\"")); + assert!(m("*", "\"abc\"")); + assert!(!m("garbage,,,\"", "\"abc\"")); + } } diff --git a/tests/etag.rs b/tests/etag.rs index 1767de8..22a40a3 100644 --- a/tests/etag.rs +++ b/tests/etag.rs @@ -78,6 +78,29 @@ async fn matching_if_none_match_gets_304_empty() { assert!(!body.is_empty()); } +/// RFC 9110 §13.1.2: `If-None-Match` uses the *weak* comparison function, so a +/// weakened copy of our own tag — what a gzipping reverse proxy sends back — +/// must still revalidate to a `304`. +#[tokio::test] +async fn weak_if_none_match_gets_304_empty() { + let app = mem_app().await; + let (_, headers, _) = get(&app, "/health", None).await; + let etag = headers.get("etag").unwrap().to_str().unwrap().to_string(); + + let (status, _, body) = get(&app, "/health", Some(&format!("W/{etag}"))).await; + assert_eq!( + status, + StatusCode::NOT_MODIFIED, + "W/ prefix must not defeat the match" + ); + assert!(body.is_empty(), "a 304 carries no body"); + + // A weakened *different* tag is still a miss. + let (status, _, body) = get(&app, "/health", Some("W/\"deadbeef\"")).await; + assert_eq!(status, StatusCode::OK); + assert!(!body.is_empty()); +} + #[tokio::test] async fn handler_cache_control_is_not_overwritten() { async fn handler() -> impl IntoResponse {