From: Christopher Faulet Date: Wed, 21 Oct 2026 09:03:58 +0000 (+0000) Subject: BUG/MEDIUM: http-ana: do not let "public" cancel directives forbidding storage X-Git-Url: https://git.kaiwu.me/http/%22../$%7BGITURL%7D/static/%7B@url%7D?a=commitdiff_plain;h=6abaa7ca517e969afb40bcf3f74375d2d8afb479;p=haproxy.git BUG/MEDIUM: http-ana: do not let "public" cancel directives forbidding storage A response carrying "Cache-Control: private, public, max-age=60" is stored in the cache and delivered to all clients, while "private" forbids a shared cache from storing it (RFC 9111#5.2.2.7). The same happens with "no-store" or "no-cache" followed by "public", and with "no-cache= "set-cookie"" followed by "public", where the response may then be stored with its Set-Cookie header. Contradictory directives are commonly met when several layers add their own Cache-Control header, for example an application emitting "private" for a personalized page behind a component adding "public, max-age=60" to everything. Personalized responses, or session cookies, of a user are then delivered from the cache to other users. This requires the server to emit such directives, so the severity is limited, but the consequence is a cross-user data leak. http_check_response_for_cacheability() processes the Cache-Control values in order. "private", "no-cache", "no-store" and "s-maxage=0" clear TX_CACHEABLE and TX_CACHE_COOK, and "no-cache="set-cookie"" clears TX_CACHE_COOK, but "public" sets both flags again, so the last one wins. The directives forbidding storage are now only recorded during the walk, and applied after it, so that they always prevail over "public", wherever it is. "public" still makes cacheable a response that was not cacheable by default, e.g. because of its status code or of an Authorization header in the request, as permitted by RFC 9111#3.5. The reg-test reg-tests/cache/public_order.vtc checks "private, public" in one header and "no-store" followed by "public" in two headers, as well as "public" alone. It fails without the fix. This has always been the case, the processing of the directives came from the legacy code, and was last reworked by commit 40ed97b04 ("BUG/MINOR: cache: Manage multiple values in cache-control header value") in 2.3-dev9. The fix may be backported to all supported versions. --- diff --git a/reg-tests/cache/public_order.vtc b/reg-tests/cache/public_order.vtc new file mode 100644 index 000000000..9becfdb47 --- /dev/null +++ b/reg-tests/cache/public_order.vtc @@ -0,0 +1,81 @@ +varnishtest "A \"public\" directive does not cancel the ones forbidding storage" + +# "private", "no-store" or "no-cache" forbid a shared cache from storing or +# reusing a response, whatever other directives are present. A "public" +# directive placed after them, possibly in another Cache-Control header added +# by another layer, must not make the response cacheable. + +feature ignore_unknown_macro + +server s1 { + rxreq + expect req.url == "/a" + txresp -hdr "Cache-Control: private, public, max-age=60" -bodylen 10 + rxreq + expect req.url == "/a" + txresp -hdr "Cache-Control: private, public, max-age=60" -bodylen 11 + + rxreq + expect req.url == "/b" + txresp -hdr "Cache-Control: no-store" -hdr "Cache-Control: public, max-age=60" -bodylen 20 + rxreq + expect req.url == "/b" + txresp -hdr "Cache-Control: no-store" -hdr "Cache-Control: public, max-age=60" -bodylen 21 + + rxreq + expect req.url == "/c" + txresp -hdr "Cache-Control: public, max-age=60" -bodylen 30 +} -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" + rxresp + expect resp.bodylen == 10 + txreq -url "/a" + rxresp + expect resp.bodylen == 11 + expect resp.http.X-Cache-Hit == 0 + + txreq -url "/b" + rxresp + expect resp.bodylen == 20 + txreq -url "/b" + rxresp + expect resp.bodylen == 21 + expect resp.http.X-Cache-Hit == 0 + + # "public" alone remains cacheable + txreq -url "/c" + rxresp + expect resp.bodylen == 30 + txreq -url "/c" + rxresp + expect resp.bodylen == 30 + expect resp.http.X-Cache-Hit == 1 +} -run diff --git a/src/http_ana.c b/src/http_ana.c index acbb1ecc9..f529bec23 100644 --- a/src/http_ana.c +++ b/src/http_ana.c @@ -4062,6 +4062,8 @@ void http_check_response_for_cacheability(struct stream *s, struct channel *res) int has_validator = 0; int has_null_maxage = 0; int has_smaxage = 0; + int forbidden = 0; + int cookie_forbidden = 0; if (txn->status < 200) { /* do not try to cache interim responses! */ @@ -4078,8 +4080,10 @@ void http_check_response_for_cacheability(struct stream *s, struct channel *res) } } - /* Look for "cache-control" header and iterate over all the values - * until we find one that specifies that caching is possible or not. */ + /* Look for "cache-control" header and iterate over all the values. The + * directives forbidding storage are only applied at the end so that a + * "public" directive, wherever it is, cannot cancel them. + */ ctx.blk = NULL; while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { if (isteqi(ctx.value, ist("public"))) { @@ -4095,7 +4099,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\""))) { - txn->flags &= ~TX_CACHE_COOK; + cookie_forbidden = 1; continue; } @@ -4103,7 +4107,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"))) { - txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; + forbidden = 1; continue; } if (istmatchi(ctx.value, ist("s-maxage"))) { @@ -4119,10 +4123,13 @@ 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. */ - if (has_null_maxage && !has_smaxage) { + if ((has_null_maxage && !has_smaxage) || forbidden) { txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; } + if (cookie_forbidden) + txn->flags &= ~TX_CACHE_COOK; + /* If no freshness information could be found in Cache-Control values, * look for an Expires header. */ if (!has_freshness_info) {