⚡ compare If-None-Match weakly, as RFC 9110 requires
Browse files at this commitef7becd7fa3ce0739c69b93ffd0026d2174b2083Parent: b005e15- fix
- ⚡ compare If-None-Match weakly, as RFC 9110 requires
2 changed files +60 -4
- src/web/etag.rs +37 -4
- tests/etag.rs +23 -0
src/web/etag.rsmodified+37 -4
| @@ -108,9 +108,11 @@ fn compute_etag(bytes: &[u8]) -> String { | |||
| 108 | format!("\"{:016x}\"", hasher.finish()) | 108 | format!("\"{:016x}\"", hasher.finish()) |
| 109 | } | 109 | } |
| 110 | 110 | ||
| 111 | -/// Strong `If-None-Match` compare: `*` matches anything, otherwise the (comma- | 111 | +/// `If-None-Match` compare: `*` matches anything, otherwise the (comma- |
| 112 | -/// separated) candidate list must contain our exact quoted tag. A weak | 112 | +/// separated) candidate list must contain our tag. RFC 9110 §13.1.2 mandates the |
| 113 | -/// validator `W/"…"` never equals our strong tag, so it correctly misses. | 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`. | ||
| 114 | fn matches(if_none_match: Option<&HeaderValue>, tag: &str) -> bool { | 116 | fn matches(if_none_match: Option<&HeaderValue>, tag: &str) -> bool { |
| 115 | let Some(value) = if_none_match.and_then(|v| v.to_str().ok()) else { | 117 | let Some(value) = if_none_match.and_then(|v| v.to_str().ok()) else { |
| 116 | return false; | 118 | return false; |
| @@ -120,5 +122,36 @@ fn matches(if_none_match: Option<&HeaderValue>, tag: &str) -> bool { | |||
| 120 | || value | 122 | || value |
| 121 | .split(',') | 123 | .split(',') |
| 122 | .map(str::trim) | 124 | .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 | + } | ||
| 124 | } | 157 | } |
tests/etag.rsmodified+23 -0
| @@ -78,6 +78,29 @@ async fn matching_if_none_match_gets_304_empty() { | |||
| 78 | assert!(!body.is_empty()); | 78 | assert!(!body.is_empty()); |
| 79 | } | 79 | } |
| 80 | 80 | ||
| 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 | + | ||
| 81 | #[tokio::test] | 104 | #[tokio::test] |
| 82 | async fn handler_cache_control_is_not_overwritten() { | 105 | async fn handler_cache_control_is_not_overwritten() { |
| 83 | async fn handler() -> impl IntoResponse { | 106 | async fn handler() -> impl IntoResponse { |