Hi,

Here is a fix for the cache, against current master (6bf3333). The patch is
attached (git format-patch).

http_check_response_for_cacheability() walks the Cache-Control values in
order. "no-store", "private", "no-cache" and "s-maxage=0" clear
TX_CACHEABLE and TX_CACHE_COOK, but a later "public" sets both flags again
unconditionally. So "public, no-store" is not cached while "no-store,
public" is, and the same holds for "private, public" and for "public" in a
second Cache-Control header line. A no-cache="set-cookie" followed by
"public" also gets TX_CACHE_COOK back. RFC 9111 says a cache must not store
a no-store response and a shared cache must not store a private one, so an
origin or middleware that appends "public" to a response already marked
private gets per-user responses served to other clients.

With "http-request cache-use" and "http-response cache-store" on a small
cache, and a backend that counts requests per path, the second GET of each
path gives:

backend Cache-Control master with the patch expected
no-store + public (two lines) cached not cached not cached
no-store, public cached not cached not cached
private, public cached not cached not cached
no-store not cached not cached not cached
public, no-store not cached not cached not cached
public cached cached cached

The configuration, the backend and the script are attached (cache.cfg,
backend.py, run.sh). Without the patch it reproduces 3/3 on 3.5-dev12 and
on 3.5-dev9.

The patch remembers whether a restrictive directive was seen and does not
let a later "public" set the flags again. It adds
reg-tests/cache/cache_control_restrictive_first.vtc, which fails without
the fix (X-Cache-Hit (1) == "0") and passes with it; the 19 existing cache
reg-tests still pass. It should be backported to the stable branches.

Found by an LLM-assisted tool, verified manually.

Regards,
Martin Mirchev
cache test
    total-max-size 4
    max-object-size 100000
    max-age 60

frontend fe
    mode http
    bind 127.0.0.1:18193
    timeout client 5s
    http-request cache-use test
    default_backend be

backend be
    mode http
    timeout connect 5s
    timeout server 5s
    http-response cache-store test
    server s1 127.0.0.1:18194
From a80999b716ca0a32f93c214fe954c76f8848634e Mon Sep 17 00:00:00 2001
From: Martin Mirchev <[email protected]>
Date: Tue, 17 Nov 2026 18:27:24 +0800
Subject: [PATCH] BUG/MEDIUM: cache: do not let "public" override
 no-store/private/no-cache

http_check_response_for_cacheability() walks the Cache-Control values
in order. "no-store", "private", "no-cache" and "s-maxage=0" clear
TX_CACHEABLE and TX_CACHE_COOK, but a later "public" sets both flags
again unconditionally. The result therefore depends on the order of the
directives: "public, no-store" is not cached while "no-store, public"
is, and the same holds for "private, public" and for "public" arriving
in a second Cache-Control header line. A no-cache="set-cookie" followed
by "public" also gets TX_CACHE_COOK restored.

RFC 9111 says that a cache must not store a no-store response and that
a shared cache must not store a private one, whatever other directives
come with them. So an origin or middleware appending "public" to a
response already marked private gets its per-user responses served to
other clients.

This patch remembers whether a restrictive directive was seen and does
not let a later "public" set the flags again. The new reg-test
reg-tests/cache/cache_control_restrictive_first.vtc fails without the
fix (X-Cache-Hit (1) == "0") and passes with it, and the 19 existing
cache reg-tests still pass.

This should be backported to all stable branches.
---
 .../cache/cache_control_restrictive_first.vtc | 87 +++++++++++++++++++
 src/http_ana.c                                | 10 ++-
 2 files changed, 96 insertions(+), 1 deletion(-)
 create mode 100644 reg-tests/cache/cache_control_restrictive_first.vtc

diff --git a/reg-tests/cache/cache_control_restrictive_first.vtc b/reg-tests/cache/cache_control_restrictive_first.vtc
new file mode 100644
index 0000000..ccbf3a6
--- /dev/null
+++ b/reg-tests/cache/cache_control_restrictive_first.vtc
@@ -0,0 +1,87 @@
+varnishtest "Cache-Control restrictions are not overridden by a later 'public'"
+# A response carrying no-store, no-cache or private must not be stored even
+# when a "public" directive follows it in the same or a later Cache-Control
+# header.
+
+feature ignore_unknown_macro
+
+server s1 {
+    rxreq
+    expect req.url == "/no-store-public"
+    txresp -hdr "Cache-Control: no-store, public, max-age=60" -bodylen 110
+
+    rxreq
+    expect req.url == "/no-store-public"
+    txresp -hdr "Cache-Control: no-store, public, max-age=60" -bodylen 110
+
+    rxreq
+    expect req.url == "/private-public"
+    txresp -hdr "Cache-Control: private" \
+        -hdr "Cache-Control: public, max-age=60" -bodylen 120
+
+    rxreq
+    expect req.url == "/private-public"
+    txresp -hdr "Cache-Control: private" \
+        -hdr "Cache-Control: public, max-age=60" -bodylen 120
+
+    rxreq
+    expect req.url == "/public"
+    txresp -hdr "Cache-Control: public, max-age=60" -bodylen 130
+} -start
+
+haproxy h1 -conf {
+    global
+    .if feature(THREAD)
+        thread-groups 1
+    .endif
+        tune.idle-pool.shared off
+
+    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}"
+        default_backend test
+
+    backend test
+        http-request cache-use my_cache
+        server www ${s1_addr}:${s1_port}
+        http-response cache-store my_cache
+        http-response set-header X-Cache-Hit %[res.cache_hit]
+
+    cache my_cache
+        total-max-size 3
+        max-age 20
+        max-object-size 3072
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+    txreq -url "/no-store-public"
+    rxresp
+    expect resp.status == 200
+    expect resp.bodylen == 110
+    txreq -url "/no-store-public"
+    rxresp
+    expect resp.status == 200
+    expect resp.http.X-Cache-Hit == 0
+
+    txreq -url "/private-public"
+    rxresp
+    expect resp.status == 200
+    expect resp.bodylen == 120
+    txreq -url "/private-public"
+    rxresp
+    expect resp.status == 200
+    expect resp.http.X-Cache-Hit == 0
+
+    txreq -url "/public"
+    rxresp
+    expect resp.status == 200
+    txreq -url "/public"
+    rxresp
+    expect resp.status == 200
+    expect resp.http.X-Cache-Hit == 1
+} -run
diff --git a/src/http_ana.c b/src/http_ana.c
index 01a5975..8fb69bd 100644
--- a/src/http_ana.c
+++ b/src/http_ana.c
@@ -4019,6 +4019,8 @@ void http_check_response_for_cacheability(struct stream *s, struct channel *res)
 	int has_freshness_info = 0;
 	int has_validator = 0;
 	int has_null_maxage = 0;
+	int has_no_store = 0;
+	int has_no_cache_cookie = 0;
 
 	if (txn->status < 200) {
 		/* do not try to cache interim responses! */
@@ -4040,7 +4042,11 @@ void http_check_response_for_cacheability(struct stream *s, struct channel *res)
 	ctx.blk = NULL;
 	while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
 		if (isteqi(ctx.value, ist("public"))) {
-			txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
+			/* "public" must not override a restriction seen earlier */
+			if (!has_no_store)
+				txn->flags |= TX_CACHEABLE;
+			if (!has_no_store && !has_no_cache_cookie)
+				txn->flags |= TX_CACHE_COOK;
 			continue;
 		}
 		/* This max-age might be overridden by a s-maxage directive, do
@@ -4052,6 +4058,7 @@ void http_check_response_for_cacheability(struct stream *s, struct channel *res)
 
 		/* We might have a no-cache="set-cookie" form. */
 		if (isteqi(ctx.value, ist("no-cache=\"set-cookie\""))) {
+			has_no_cache_cookie = 1;
 			txn->flags &= ~TX_CACHE_COOK;
 			continue;
 		}
@@ -4060,6 +4067,7 @@ void http_check_response_for_cacheability(struct stream *s, struct channel *res)
 		    isteqi(ctx.value, ist("no-cache")) || istmatchi(ctx.value, ist("no-cache=")) ||
 		    isteqi(ctx.value, ist("no-store")) || istmatchi(ctx.value, ist("no-store=")) ||
 		    isteqi(ctx.value, ist("s-maxage=0"))) {
+			has_no_store = 1;
 			txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
 			continue;
 		}
-- 
2.50.1

import http.server, socketserver, sys
COUNTS = {}
MODES = {
  "/two-lines": [("Cache-Control", "no-store"), ("Cache-Control", "public")],
  "/one-line":  [("Cache-Control", "no-store, public")],
  "/private":   [("Cache-Control", "private, public")],
  "/ctrl":      [("Cache-Control", "no-store")],
  "/ctrl-rev":  [("Cache-Control", "public, no-store")],
  "/cacheable": [("Cache-Control", "public")],
}
class H(http.server.BaseHTTPRequestHandler):
    protocol_version = "HTTP/1.1"
    def do_GET(self):
        p = self.path
        COUNTS[p] = COUNTS.get(p, 0) + 1
        body = f"path={p} REQCOUNT={COUNTS[p]}\n".encode()
        self.send_response(200)
        for k, v in MODES.get(p, []):
            self.send_header(k, v)
        self.send_header("Expires", "Wed, 01 Jan 2031 00:00:00 GMT")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)
    def log_message(self, *a): pass
socketserver.ThreadingTCPServer.allow_reuse_address = True
socketserver.ThreadingTCPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()
#!/bin/bash
# usage: run.sh <haproxy-binary>; prints second-response body per path (REQCOUNT=1 => served from cache)
D=$(cd "$(dirname "$0")" && pwd)
python3 -I "$D/backend.py" 18194 2>/dev/null & BPID=$!
"$1" -f "$D/cache.cfg" -db > "$D/hap.log" 2>&1 & HPID=$!
sleep 1.5
for p in /two-lines /one-line /private /ctrl /ctrl-rev /cacheable; do
  curl -s http://127.0.0.1:18193$p > /dev/null
  printf '%-11s second GET -> %s' "$p" "$(curl -s http://127.0.0.1:18193$p)"; echo
done
kill $HPID $BPID 2>/dev/null; wait $HPID $BPID 2>/dev/null; true

Reply via email to