You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by bc...@apache.org on 2026/09/04 23:15:28 UTC

(trafficserver) branch master updated: Normalize repeated carriage returns in headers (#13595)

This is an automated email from the ASF dual-hosted git repository.

bryancall pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new a27e9465e2 Normalize repeated carriage returns in headers (#13595)
a27e9465e2 is described below

commit a27e9465e2465f53e9839e69a3d259cea1338a3f
Author: Raja Muhammad Awais <11...@users.noreply.github.com>
AuthorDate: Sat Sep 5 04:15:22 2026 +0500

    Normalize repeated carriage returns in headers (#13595)
    
    mime_parser_parse() marked a field line as safe for raw preservation whenever
    it ended in CRLF, so a value such as "Extra-CRs: \r\r\r\r\n" was parsed and
    trimmed to a normalized form but serialized from the untouched input buffer,
    sending the extra CR bytes to the next hop. Also disable raw preservation when
    the byte before the final CRLF is itself a CR, so the field is printed in
    canonical form. Correctly formed lines keep the existing fast path. Adds unit
    coverage.
---
 src/proxy/hdrs/MIME.cc                 |  3 ++-
 src/proxy/hdrs/unit_tests/test_Hdrs.cc | 26 ++++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/proxy/hdrs/MIME.cc b/src/proxy/hdrs/MIME.cc
index 41c752ce58..a83bd35067 100644
--- a/src/proxy/hdrs/MIME.cc
+++ b/src/proxy/hdrs/MIME.cc
@@ -2478,7 +2478,8 @@ mime_parser_parse(MIMEParser *parser, HdrHeap *heap, MIMEHdrImpl *mh, const char
       }
       field_name.rtrim_if(&ParseRules::is_ws);
       raw_print_field = false;
-    } else if (parsed.suffix(2) != "\r\n") {
+    } else if (parsed.suffix(2) != "\r\n" || (parsed.size() > 2 && parsed[parsed.size() - 3] == '\r')) {
+      // Do not preserve malformed line endings when forwarding the field.
       raw_print_field = false;
     }
 
diff --git a/src/proxy/hdrs/unit_tests/test_Hdrs.cc b/src/proxy/hdrs/unit_tests/test_Hdrs.cc
index a94f81a405..193b7072fb 100644
--- a/src/proxy/hdrs/unit_tests/test_Hdrs.cc
+++ b/src/proxy/hdrs/unit_tests/test_Hdrs.cc
@@ -2985,6 +2985,32 @@ TEST_CASE("HTTP parser tolerates high-bit bytes without UB", "[proxy][hdrtest]")
   req_hdr.destroy();
 }
 
+TEST_CASE("HTTP parser normalizes repeated carriage returns in header line endings", "[proxy][hdrtest]")
+{
+  constexpr std::string_view message = "GET / HTTP/1.1\r\nHost: example.com\r\nExtra-CRs: \r\r\r\r\n\r\n"sv;
+
+  HTTPParser parser;
+  http_parser_init(&parser);
+
+  HTTPHdr  req_hdr;
+  HdrHeap *heap = new_HdrHeap(HdrHeap::DEFAULT_SIZE + 64);
+  req_hdr.create(HTTPType::REQUEST, HTTP_1_1, heap);
+
+  auto start = message.data();
+  REQUIRE(req_hdr.parse_req(&parser, &start, message.data() + message.size(), true) == ParseResult::DONE);
+
+  std::string serialized(static_cast<size_t>(req_hdr.length_get()), '\0');
+  int         index  = 0;
+  int         offset = 0;
+  req_hdr.print(serialized.data(), static_cast<int>(serialized.size()), &index, &offset);
+  serialized.resize(static_cast<size_t>(index));
+
+  CHECK(serialized.find("Extra-CRs: \r\n") != std::string::npos);
+  CHECK(serialized.find("Extra-CRs: \r\r") == std::string::npos);
+
+  req_hdr.destroy();
+}
+
 TEST_CASE("HTTP response parser tolerates high-bit bytes without UB", "[proxy][hdrtest]")
 {
   struct Test {