]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: cache: don't store a close-delimited response cut by an error
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 2 Dec 2026 16:45:28 +0000 (17:45 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Thu, 3 Dec 2026 12:41:06 +0000 (13:41 +0100)
A response without content-length and not chunked ends with the close of
the server connection. In that case, the HTTP analyzer considers the end
of the message is reached as soon as the end of stream or an abort is
reported on the server side, whatever the reason. So the http_end callback
of the cache filter was called and the cache entry was marked as complete,
even if the response was cut by a timeout, a reset or a client abort. The
truncated object was then served to every client until it expired.

Now, for a response without transfer length, the cache entry is only
committed if the end of input was reached on the server side, without
error. Otherwise, the entry is released.

A reg-test was added to check a clean close and a server timeout. The
reset case cannot be tested with vtest.

This patch must be backported to all stable versions.

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

diff --git a/reg-tests/cache/close_delimited.vtc b/reg-tests/cache/close_delimited.vtc
new file mode 100644 (file)
index 0000000..dd694be
--- /dev/null
@@ -0,0 +1,101 @@
+varnishtest "Caching of close-delimited responses"
+
+# A response without content-length and not chunked ends with the close of
+# the server connection. It must only be cached if the connection is cleanly
+# closed. If the response is interrupted by a timeout, it is truncated and
+# must not be cached. Each server is called once per expected request to the
+# origin. So, if a truncated response is cached, the second request on
+# /timeout is served from the cache and the test fails.
+
+feature ignore_unknown_macro
+
+# Synchronizes the clients with the end of each s2 iteration. vtest accepts
+# the next connection only once the previous iteration is finished.
+barrier b1 cond 2 -cyclic
+
+# Clean close: the response must be cached, the server is only called once
+server s1 {
+       rxreq
+       expect req.url == "/clean"
+       txresp -nolen -hdr "Cache-Control: max-age=60" -hdr "Connection: close" \
+           -body "0123456789"
+} -start
+
+# Server timeout: the response must not be cached, the server is called twice
+server s2 -repeat 2 {
+       rxreq
+       expect req.url == "/timeout"
+       txresp -nolen -hdr "Cache-Control: max-age=60" -hdr "Connection: close" \
+           -body "0123456789"
+       delay 1
+       barrier b1 sync
+} -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  500ms
+
+    cache my_cache
+       total-max-size 3
+       max-age 60
+
+    frontend fe
+       bind "fd@${fe}"
+       http-after-response set-header x-cache-hit %[res.cache_hit]
+       use_backend be_clean if { path /clean }
+       default_backend be_timeout
+
+    backend be_clean
+       http-request cache-use my_cache
+       http-response cache-store my_cache
+       server s1 ${s1_addr}:${s1_port}
+
+    backend be_timeout
+       http-request cache-use my_cache
+       http-response cache-store my_cache
+       server s2 ${s2_addr}:${s2_port}
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+       txreq -url "/clean"
+       rxresp
+       expect resp.status == 200
+       expect resp.bodylen == 10
+       expect resp.http.x-cache-hit == 0
+} -run
+
+client c2 -connect ${h1_fe_sock} {
+       txreq -url "/clean"
+       rxresp
+       expect resp.status == 200
+       expect resp.bodylen == 10
+       expect resp.http.x-cache-hit == 1
+} -run
+
+client c3 -connect ${h1_fe_sock} {
+       txreq -url "/timeout"
+       rxresp
+       expect resp.status == 200
+       expect resp.bodylen == 10
+       expect resp.http.x-cache-hit == 0
+       barrier b1 sync
+} -run
+
+client c4 -connect ${h1_fe_sock} {
+       txreq -url "/timeout"
+       rxresp
+       expect resp.status == 200
+       expect resp.bodylen == 10
+       expect resp.http.x-cache-hit == 0
+       barrier b1 sync
+} -run
+
+server s2 -wait
index 525c1b3c1bc41f92854bf7792b35b4294117579e..22ae3dc0dd1fcc11d8c1b5379f6f011060e0c902 100644 (file)
@@ -994,6 +994,16 @@ cache_store_http_end(struct stream *s, struct filter *filter,
                return 1;
 
        if (st && st->first_block) {
+               /* Without transfer length, the end of the response is the
+                * close of the server connection. It is only complete on a
+                * clean close. On error or on abort (a timeout for instance),
+                * it is truncated and must not be cached.
+                */
+               if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
+                   (s->scb->flags & (SC_FL_EOI|SC_FL_ERROR)) != SC_FL_EOI) {
+                       disable_cache_entry(st, filter, shctx);
+                       return 1;
+               }
 
                object = (struct cache_entry *)st->first_block->data;