From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id DEA681FF09F for ; Tue, 01 Dec 2026 11:20:37 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 0C0E5216A7; Tue, 01 Dec 2026 11:20:31 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH http-server v2 2/9] proxy: strip chunked framing headers from decoded responses Date: Tue, 1 Dec 2026 11:18:57 +0100 Message-ID: <20261201101913.1885484-3-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261201101913.1885484-1-t.lamprecht@proxmox.com> References: <20261201101913.1885484-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1796120358764 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.149 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: TMUJ765OM3NTGBDLKUQ42XA7NS7QGPCG X-Message-ID-Hash: TMUJ765OM3NTGBDLKUQ42XA7NS7QGPCG X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: AnyEvent::HTTP decodes chunked upstream responses but leaves the Transfer-Encoding and Trailer headers intact. Forwarding them alongside the Content-Length of the decoded body causes clients to misparse the body as chunked. In addition, response() checked Perl truthiness on $content rather than definedness and length, so a decoded empty body ("") omitted Content-Length on keep-alive 200 OK responses and a "0" body was dropped altogether. Strip Transfer-Encoding and Trailer before forwarding, disable HTTP::Headers underscore translation so an upstream Transfer_Encoding header cannot be normalized back into Transfer-Encoding, and frame non-stream responses whenever the status permits a body and $content is defined (keeping 1xx, 204, and 304 without Content-Length or body, and omitting Content-Length for file/pipe streams). This is needed once API2 endpoints stream chunked HTTP/1.1 responses (for example from pvedaemon through pveproxy). The proxy still buffers the full response rather than forwarding it incrementally. Signed-off-by: Thomas Lamprecht --- Changes in v2: - Scope local $HTTP::Headers::TRANSLATE_UNDERSCORE = 0 to a do block around HTTP::Headers->new() so it cannot leak across pipelined keep-alive responses while still preventing Transfer_Encoding from normalizing into Transfer-Encoding. - Strip any supplied body on HTTP 205 Reset Content while keeping Content-Length: 0 for persistent connections. src/PVE/APIServer/AnyEvent.pm | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/PVE/APIServer/AnyEvent.pm b/src/PVE/APIServer/AnyEvent.pm index fa4076d..8f77737 100644 --- a/src/PVE/APIServer/AnyEvent.pm +++ b/src/PVE/APIServer/AnyEvent.pm @@ -408,8 +408,10 @@ sub response { $content = ''; } - if ($code =~ /^(1\d\d|[23]04)$/) { - # make sure informational, no content and not modified response send no content + my $no_body = $code =~ /^(1[0-9][0-9]|[23]04)$/; + if ($no_body || $code == 205) { + # Informational, 204, 205, and 304 responses must not send a body (205 keeps + # Content-Length: 0 for persistent connections). $content = ""; } @@ -434,7 +436,7 @@ sub response { $resp->header('Server' => "pve-api-daemon/3.0"); my $content_length; - if ($content && !$stream_fh) { + if (!$stream_fh && !$no_body && defined($content)) { $content_length = length($content); @@ -469,7 +471,7 @@ sub response { #print "SEND(without content) $res\n" if $self->{debug}; $res .= "\015\012"; - $res .= $content if $content && !$stream_fh; + $res .= $content if !$stream_fh && !$no_body && defined($content) && length($content); $self->log_request($reqstate, $reqstate->{request}); @@ -959,7 +961,16 @@ sub proxy_request { my $stream = delete $hdr->{pvestreamfile}; delete $hdr->{URL}; delete $hdr->{HTTPVersion}; - my $header = HTTP::Headers->new(%$hdr); + # AnyEvent::HTTP already decoded a chunked body but keeps the framing headers. + delete $hdr->{'transfer-encoding'}; + delete $hdr->{trailer}; + # Keep '_' untranslated only while parsing upstream headers so + # Transfer_Encoding cannot become Transfer-Encoding without affecting + # any pipelined follow-up response on the same connection. + my $header = do { + local $HTTP::Headers::TRANSLATE_UNDERSCORE = 0; + HTTP::Headers->new(%$hdr); + }; if (my $location = $header->header('Location')) { $location =~ s|^http://localhost:85||; $header->header(Location => $location); -- 2.47.3