forge
Explore
Log in

Don't you dare go Hollow

⚡ compare If-None-Match weakly, as RFC 9110 requires

Browse files at this commit

ef7becd7fa3ce0739c69b93ffd0026d2174b2083Ahmet Kaan GümüşParent: b005e15
  • fix
    • ⚡ compare If-None-Match weakly, as RFC 9110 requires
Show full message
fix: ⚡ compare If-None-Match weakly, as RFC 9110 requires

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/"<tag>"` 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.rsmodified+37 -4
@@ -108,9 +108,11 @@ fn compute_etag(bytes: &[u8]) -> String {
108108 format!("\"{:016x}\"", hasher.finish())
109109 }
110110
111-/// Strong `If-None-Match` compare: `*` matches anything, otherwise the (comma-
112-/// separated) candidate list must contain our exact quoted tag. A weak
113-/// validator `W/"…"` never equals our strong tag, so it correctly misses.
111+/// `If-None-Match` compare: `*` matches anything, otherwise the (comma-
112+/// separated) candidate list must contain our tag. RFC 9110 §13.1.2 mandates the
113+/// *weak* comparison function here, so the `W/` prefix is stripped from both
114+/// sides and only the opaque quoted strings are compared — an intermediary that
115+/// weakens our ETag (nginx does this when it gzips) must still get its `304`.
114116 fn matches(if_none_match: Option<&HeaderValue>, tag: &str) -> bool {
115117 let Some(value) = if_none_match.and_then(|v| v.to_str().ok()) else {
116118 return false;
@@ -120,5 +122,36 @@ fn matches(if_none_match: Option<&HeaderValue>, tag: &str) -> bool {
120122 || value
121123 .split(',')
122124 .map(str::trim)
123- .any(|candidate| candidate == tag)
125+ .any(|candidate| opaque(candidate) == opaque(tag))
126+}
127+
128+/// The opaque part of a validator — the quoted string without any weak prefix.
129+fn opaque(validator: &str) -> &str {
130+ validator.strip_prefix("W/").unwrap_or(validator)
131+}
132+
133+#[cfg(test)]
134+mod tests {
135+ use super::matches;
136+ use axum::http::HeaderValue;
137+
138+ fn m(header: &str, tag: &str) -> bool {
139+ matches(Some(&HeaderValue::from_str(header).unwrap()), tag)
140+ }
141+
142+ #[test]
143+ fn weak_comparison_is_symmetric() {
144+ // Both directions and both-weak: the opaque strings decide (RFC 9110
145+ // §13.1.2), never the `W/` prefix.
146+ assert!(m("W/\"abc\"", "\"abc\""), "weak candidate vs strong tag");
147+ assert!(m("\"abc\"", "W/\"abc\""), "strong candidate vs weak tag");
148+ assert!(m("W/\"abc\"", "W/\"abc\""), "both weak");
149+ assert!(m("\"abc\"", "\"abc\""), "both strong");
150+ // A different opaque string still misses, weak prefix or not.
151+ assert!(!m("W/\"abd\"", "\"abc\""));
152+ // List membership and `*` keep working through the strip.
153+ assert!(m("\"x\", W/\"abc\"", "\"abc\""));
154+ assert!(m("*", "\"abc\""));
155+ assert!(!m("garbage,,,\"", "\"abc\""));
156+ }
124157 }
tests/etag.rsmodified+23 -0
@@ -78,6 +78,29 @@ async fn matching_if_none_match_gets_304_empty() {
7878 assert!(!body.is_empty());
7979 }
8080
81+/// RFC 9110 §13.1.2: `If-None-Match` uses the *weak* comparison function, so a
82+/// weakened copy of our own tag — what a gzipping reverse proxy sends back —
83+/// must still revalidate to a `304`.
84+#[tokio::test]
85+async fn weak_if_none_match_gets_304_empty() {
86+ let app = mem_app().await;
87+ let (_, headers, _) = get(&app, "/health", None).await;
88+ let etag = headers.get("etag").unwrap().to_str().unwrap().to_string();
89+
90+ let (status, _, body) = get(&app, "/health", Some(&format!("W/{etag}"))).await;
91+ assert_eq!(
92+ status,
93+ StatusCode::NOT_MODIFIED,
94+ "W/ prefix must not defeat the match"
95+ );
96+ assert!(body.is_empty(), "a 304 carries no body");
97+
98+ // A weakened *different* tag is still a miss.
99+ let (status, _, body) = get(&app, "/health", Some("W/\"deadbeef\"")).await;
100+ assert_eq!(status, StatusCode::OK);
101+ assert!(!body.is_empty());
102+}
103+
81104 #[tokio::test]
82105 async fn handler_cache_control_is_not_overwritten() {
83106 async fn handler() -> impl IntoResponse {