]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: http-ana: do not store responses to requests with "no-store" master
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 21 Oct 2026 09:05:01 +0000 (09:05 +0000)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 27 Nov 2026 15:50:22 +0000 (16:50 +0100)
A request carrying "Cache-Control: no-store" normally prevents its
response from being stored in the cache. But when the response carries
"Cache-Control: public", it is stored anyway and delivered to other
clients, while RFC 9111#5.2.1.5 says that a cache must not store any part
of the request or of its response, whatever the response says. A client
asking for its response not to be stored, for privacy reasons, may thus
see it delivered to others, though the server marked it as public.

http_check_request_for_cacheability() handles "no-store" by clearing
TX_CACHEABLE and TX_CACHE_COOK, but http_check_response_for_cacheability()
sets them again on "public", since this directive may legitimately make
cacheable a response which is not by default.

The request's directive is now remembered in the TX_CACHE_NOSTORE flag
added by the previous patch, and the response is never considered
cacheable when it is set, whatever its own directives.

The reg-test reg-tests/cache/request_nostore.vtc sends a request with
"no-store" receiving a public response, and checks that it was not
stored. It fails without the fix.

The request's "no-store" was handled since commit 0ad8e0dfe ("MINOR: http:
add a function to check request's cache-control header field") in 1.8,
and the response's "public" could always override it. The fix may be
backported to all supported versions, along with the previous commit
("MINOR: http-ana: add a transaction flag for requests forbidding
storage").

reg-tests/cache/request_nostore.vtc [new file with mode: 0644]
src/http_ana.c

diff --git a/reg-tests/cache/request_nostore.vtc b/reg-tests/cache/request_nostore.vtc
new file mode 100644 (file)
index 0000000..7bbcad9
--- /dev/null
@@ -0,0 +1,63 @@
+varnishtest "A request with \"no-store\" is never stored, even if the response is public"
+
+# RFC 9111#5.2.1.5: the no-store request directive forbids caches from storing
+# any part of the request or of its response, whatever the response says.
+
+feature ignore_unknown_macro
+
+server s1 {
+    rxreq
+    expect req.url == "/a"
+    txresp -hdr "Cache-Control: public, max-age=60" -bodylen 10
+
+    rxreq
+    expect req.url == "/a"
+    txresp -hdr "Cache-Control: public, max-age=60" -bodylen 11
+} -start
+
+haproxy h1 -conf {
+    global
+    .if feature(THREAD)
+        thread-groups 1
+    .endif
+
+    defaults
+        mode http
+        timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
+        timeout client  "${HAPROXY_TEST_TIMEOUT-5s}"
+        timeout server  "${HAPROXY_TEST_TIMEOUT-5s}"
+
+    frontend fe
+        bind "fd@${fe}"
+        http-request cache-use c1
+        http-response cache-store c1
+        http-response set-header X-Cache-Hit %[res.cache_hit]
+        default_backend be
+
+    backend be
+        server s1 ${s1_addr}:${s1_port}
+
+    cache c1
+        total-max-size 4
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+    txreq -url "/a" -hdr "Cache-Control: no-store"
+    rxresp
+    expect resp.status == 200
+    expect resp.bodylen == 10
+
+    # the response to the no-store request must not have been stored
+    txreq -url "/a"
+    rxresp
+    expect resp.status == 200
+    expect resp.bodylen == 11
+    expect resp.http.X-Cache-Hit == 0
+
+    # while this one was
+    txreq -url "/a"
+    rxresp
+    expect resp.status == 200
+    expect resp.bodylen == 11
+    expect resp.http.X-Cache-Hit == 1
+} -run
index f529bec23a4bca49606a896b6060a8c9a47d4808..ca103dc503e220bf67dc4df5b76dcb6f794433f0 100644 (file)
@@ -4033,7 +4033,11 @@ void http_check_request_for_cacheability(struct stream *s, struct channel *req)
                        continue;
                }
                if (istmatchi(ctx.value, ist("no-store"))) {
                        continue;
                }
                if (istmatchi(ctx.value, ist("no-store"))) {
+                       /* remembered, the response must not be able to
+                        * make itself cacheable again.
+                        */
                        txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
                        txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
+                       txn->flags |= TX_CACHE_NOSTORE;
                        continue;
                }
        }
                        continue;
                }
        }
@@ -4123,7 +4127,8 @@ void http_check_response_for_cacheability(struct stream *s, struct channel *res)
 
        /* We had a 'max-age=0' directive but no extra s-maxage, do not cache
         * the response. */
 
        /* We had a 'max-age=0' directive but no extra s-maxage, do not cache
         * the response. */
-       if ((has_null_maxage && !has_smaxage) || forbidden) {
+       if ((has_null_maxage && !has_smaxage) || forbidden ||
+           (txn->flags & TX_CACHE_NOSTORE)) {
                txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
        }
 
                txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
        }