Skip to content

🐛 HTTP/2 Tunnel returns 520 for origin 304 responses with a valid Content-Length #1807

Backlog

Description

@XiaoMengXinX

Describe the bug

Through a Quick Tunnel using --protocol http2, Cloudflare returns 520 when the origin sends a bodyless 304 Not Modified with a nonzero Content-Length equal to the size of the corresponding 200 response. An unconditional GET succeeds. Both If-None-Match and If-Modified-Since reproduce the problem.

To Reproduce

  1. Save this synthetic origin as origin.py and run python3 origin.py.

    from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
    
    BODY = bytes(range(256)) * 117
    ETAG = '"tunnel-304-check"'
    LAST_MODIFIED = "Wed, 25 Nov 2026 02:17:24 GMT"
    
    class Origin(BaseHTTPRequestHandler):
        protocol_version = "HTTP/1.1"
    
        def do_GET(self):
            not_modified = (
                self.headers.get("If-None-Match") == ETAG
                or self.headers.get("If-Modified-Since") == LAST_MODIFIED
            )
            self.send_response_only(304 if not_modified else 200)
            self.send_header("Date", self.date_time_string())
            self.send_header("Cache-Control", "no-cache")
            self.send_header("Content-Type", "text/html")
            self.send_header("Content-Length", str(len(BODY)))
            self.send_header("ETag", ETAG)
            self.send_header("Last-Modified", LAST_MODIFIED)
            self.end_headers()
            if not not_modified:
                self.wfile.write(BODY)
    
    ThreadingHTTPServer(("127.0.0.1", 8080), Origin).serve_forever()
  2. Start a Quick Tunnel and wait for the HTTP/2 connection to register.

    printf '{}\n' > quick-tunnel-test.yml
    cloudflared tunnel --config ./quick-tunnel-test.yml --no-autoupdate \
      --protocol http2 --url http://127.0.0.1:8080
  3. Run the same conditional GET directly against the origin and through the hostname printed by the Quick Tunnel.

    curl -si -H 'Accept-Encoding: identity' \
      -H 'If-None-Match: "tunnel-304-check"' http://127.0.0.1:8080/page
    curl --http2 -si -H 'Accept-Encoding: identity' \
      -H 'If-None-Match: "tunnel-304-check"' https://YOUR-HOST.trycloudflare.com/page

    The origin returns 304 with Content-Length: 29952 and an empty body. The public endpoint returns 520 on stock cloudflared. For a control, send Content-Length only on the 200 response. The stock connector then forwards the 304 successfully.

Expected behavior

The public endpoint should return 304 with an empty body. RFC 9110 §8.6 permits this Content-Length value on a 304 to a conditional GET when it equals the length of the corresponding 200 content.

Environment and versions

  • Ubuntu Linux, x86_64
  • cloudflared 2026.11.0 (built 2026-11-12-12:17 UTC)
  • Two temporary Quick Tunnels forced to HTTP/2, sharing the same loopback origin
  • The header-copy logic is also present in 2026.11.2 by source inspection. The live comparison used 2026.11.0.

Logs and errors

Each pair of conditional requests ran three times on November 25, 2026.

Origin response Stock 2026.11.0 at the edge Patched 2026.11.0 at the edge
304 with Content-Length: 29952, empty body 520 on 6/6 requests 304 on 6/6 requests, empty body
304 with Content-Length omitted, empty body 304 with both validators 304 with both validators

Example edge Ray IDs from the synthetic Quick Tunnels are a5ffc58ae884fd69-SIN (stock 520) and a5ffc59528f1b216-SIN (patched 304).

Additional context

The experiment changes http2RespWriter.WriteRespHeaders to skip copying Content-Length into the outer HTTP/2 response for http.StatusNotModified. The origin header remains in the serialized user headers. Regular GET, HEAD, Range, POST, chunked responses, 204, and 404 matched the stock connector in the live comparison. A focused local test also checks that 200 and 206 retain the outer header and 304 retains the serialized header.

Would omitting the outer header for 304 fit the Tunnel protocol contract, or should the edge handle this case directly?

Activity

  1. PacketBreaker commented on Nov 28, 2026

    @PacketBreaker

    I can take this. The cause is in the HTTP/2 response path: http2RespWriter.WriteRespHeaders in connection/http2.go promotes the origin's Content-Length to a real HTTP/2 response header. For a response whose status forbids a body (304, but also 204 and 1xx), the declared length can never match the empty body, so the edge sees a malformed response and returns 520. The QUIC path passes headers as stream metadata instead, which is why it only fails with --protocol http2.

    I'll open a PR that keeps Content-Length in the serialized user headers but skips promoting it to an HTTP/2 response header when the status code has no body, with a unit test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions