Copilot commented on code in PR #13826:
URL: https://github.com/apache/trafficserver/pull/13826#discussion_r4361583134


##########
tests/fuzzing/fuzz_header_conversion.cc:
##########
@@ -0,0 +1,205 @@
+/** @file
+
+  Fuzzing HTTP header validation and version conversion.
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ */
+
+#include "proxy/hdrs/HTTP.h"
+#include "proxy/hdrs/HeaderValidator.h"
+#include "proxy/hdrs/VersionConverter.h"
+#include "tscore/Diags.h"
+#include "tsutil/PostScript.h"
+
+#include <fuzzer/FuzzedDataProvider.h>
+
+#include <array>
+#include <cstddef>
+#include <cstdint>
+#include <string>
+#include <string_view>
+#include <vector>
+
+extern int cmd_disable_pfreelist;
+
+namespace
+{
+constexpr size_t                           MAX_INPUT_LENGTH = 64 * 1024;
+constexpr std::array<std::string_view, 17> FIELD_NAMES      = {
+  ":method",
+  ":scheme",
+  ":authority",
+  ":path",
+  ":status",
+  "host",
+  "cookie",
+  "connection",
+  "keep-alive",
+  "proxy-connection",
+  "upgrade",
+  "transfer-encoding",
+  "te",
+  "content-length",
+  "content-type",
+  "x-test",
+  "",
+};
+
+// Bit flags in the first input byte controlling the harness behavior.
+constexpr uint8_t FLAG_IS_RESPONSE    = 1 << 0; // Decode as a response 
instead of a request.
+constexpr uint8_t FLAG_IS_TEXT        = 1 << 1; // Consume the payload as 
HTTP/1.1 text.
+constexpr uint8_t FLAG_HTTP_3         = 1 << 2; // Target HTTP/3 instead of 
HTTP/2.
+constexpr uint8_t FLAG_PSEUDO_HEADERS = 1 << 3; // Add the required 
pseudo-headers.
+constexpr uint8_t FLAG_CONNECT        = 1 << 4; // Use the CONNECT method 
(requests only).
+constexpr uint8_t FLAG_TRAILER        = 1 << 5; // Treat the structured fields 
as trailers.
+
+void
+add_field(HTTPHdr &header, std::string_view name, std::string_view value)
+{
+  // An empty name allocates an unnamed field for validator rejection coverage.
+  MIMEField *field = header.field_create(name);
+
+  header.field_value_set(field, value);
+  header.field_attach(field);
+}
+
+void
+print_header(const HTTPHdr &header)
+{
+  std::array<char, 32> small_buffer{};
+  int                  buffer_index = 0;
+  int                  dump_offset  = 0;
+  int                  length       = header.length_get();
+
+  header.print(small_buffer.data(), small_buffer.size(), &buffer_index, 
&dump_offset);
+  if (length >= 0 && length <= 2 * static_cast<int>(MAX_INPUT_LENGTH)) {
+    std::vector<char> buffer(static_cast<size_t>(length) + 1);
+    buffer_index = 0;
+    dump_offset  = 0;
+    header.print(buffer.data(), static_cast<int>(buffer.size()), 
&buffer_index, &dump_offset);
+  }
+}
+
+void
+convert_decoded_header(const HTTPHdr &header, int version, bool is_response, 
bool is_trailer)
+{
+  if (!HeaderValidator::is_h2_h3_header_valid(header, is_response, 
is_trailer)) {
+    return;
+  }
+
+  // Trailers lack the request/status pseudo headers required by full-header 
conversion.
+  if (is_trailer) {
+    return;
+  }
+
+  HTTPHdr          converted;
+  ts::PostScript   cleanup([&]() { converted.destroy(); });
+  VersionConverter converter;
+
+  converted.create(is_response ? HTTPType::RESPONSE : HTTPType::REQUEST);

Review Comment:
   `convert_decoded_header` converts from `version` (2/3) to 1, but `converted` 
is created without specifying an H2/H3 version before copying an allegedly 
H2/H3 header into it. If `HTTPHdr::create(type)` defaults to an HTTP/1-style 
internal structure, this copy+convert sequence may behave incorrectly. Create 
`converted` with the matching H2/H3 version (or derive it from `header`) before 
`copy()` so the subsequent `converter.convert(converted, version, 1)` runs on a 
header with the correct internal representation.



##########
tests/fuzzing/fuzz_header_conversion.cc:
##########
@@ -0,0 +1,205 @@
+/** @file
+
+  Fuzzing HTTP header validation and version conversion.
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ */
+
+#include "proxy/hdrs/HTTP.h"
+#include "proxy/hdrs/HeaderValidator.h"
+#include "proxy/hdrs/VersionConverter.h"
+#include "tscore/Diags.h"
+#include "tsutil/PostScript.h"
+
+#include <fuzzer/FuzzedDataProvider.h>
+
+#include <array>
+#include <cstddef>
+#include <cstdint>
+#include <string>
+#include <string_view>
+#include <vector>
+
+extern int cmd_disable_pfreelist;
+
+namespace
+{
+constexpr size_t                           MAX_INPUT_LENGTH = 64 * 1024;
+constexpr std::array<std::string_view, 17> FIELD_NAMES      = {
+  ":method",
+  ":scheme",
+  ":authority",
+  ":path",
+  ":status",
+  "host",
+  "cookie",
+  "connection",
+  "keep-alive",
+  "proxy-connection",
+  "upgrade",
+  "transfer-encoding",
+  "te",
+  "content-length",
+  "content-type",
+  "x-test",
+  "",
+};
+
+// Bit flags in the first input byte controlling the harness behavior.
+constexpr uint8_t FLAG_IS_RESPONSE    = 1 << 0; // Decode as a response 
instead of a request.
+constexpr uint8_t FLAG_IS_TEXT        = 1 << 1; // Consume the payload as 
HTTP/1.1 text.
+constexpr uint8_t FLAG_HTTP_3         = 1 << 2; // Target HTTP/3 instead of 
HTTP/2.
+constexpr uint8_t FLAG_PSEUDO_HEADERS = 1 << 3; // Add the required 
pseudo-headers.
+constexpr uint8_t FLAG_CONNECT        = 1 << 4; // Use the CONNECT method 
(requests only).
+constexpr uint8_t FLAG_TRAILER        = 1 << 5; // Treat the structured fields 
as trailers.
+
+void
+add_field(HTTPHdr &header, std::string_view name, std::string_view value)
+{
+  // An empty name allocates an unnamed field for validator rejection coverage.
+  MIMEField *field = header.field_create(name);
+
+  header.field_value_set(field, value);
+  header.field_attach(field);
+}
+
+void
+print_header(const HTTPHdr &header)
+{
+  std::array<char, 32> small_buffer{};
+  int                  buffer_index = 0;
+  int                  dump_offset  = 0;
+  int                  length       = header.length_get();
+
+  header.print(small_buffer.data(), small_buffer.size(), &buffer_index, 
&dump_offset);
+  if (length >= 0 && length <= 2 * static_cast<int>(MAX_INPUT_LENGTH)) {
+    std::vector<char> buffer(static_cast<size_t>(length) + 1);
+    buffer_index = 0;
+    dump_offset  = 0;
+    header.print(buffer.data(), static_cast<int>(buffer.size()), 
&buffer_index, &dump_offset);
+  }
+}
+
+void
+convert_decoded_header(const HTTPHdr &header, int version, bool is_response, 
bool is_trailer)
+{
+  if (!HeaderValidator::is_h2_h3_header_valid(header, is_response, 
is_trailer)) {
+    return;
+  }
+
+  // Trailers lack the request/status pseudo headers required by full-header 
conversion.
+  if (is_trailer) {
+    return;
+  }
+
+  HTTPHdr          converted;
+  ts::PostScript   cleanup([&]() { converted.destroy(); });
+  VersionConverter converter;
+
+  converted.create(is_response ? HTTPType::RESPONSE : HTTPType::REQUEST);
+  converted.copy(&header);
+  if (converter.convert(converted, version, 1) == 0) {
+    print_header(converted);
+  }
+}
+} // namespace
+
+extern "C" int
+LLVMFuzzerInitialize(int *, char ***)
+{
+  cmd_disable_pfreelist = true;
+  DiagsPtr::set(new Diags("fuzzing", "", "", nullptr));
+  http_init();
+  return 0;
+}
+
+extern "C" int
+LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+  if (size == 0 || size > MAX_INPUT_LENGTH) {
+    return 0;
+  }
+
+  uint8_t          flags       = data[0];
+  bool             is_response = (flags & FLAG_IS_RESPONSE) != 0;
+  bool             is_text     = (flags & FLAG_IS_TEXT) != 0;
+  int              version     = (flags & FLAG_HTTP_3) != 0 ? 3 : 2;
+  HTTPType         type        = is_response ? HTTPType::RESPONSE : 
HTTPType::REQUEST;
+  HTTPHdr          header;
+  ts::PostScript   cleanup([&]() { header.destroy(); });
+  VersionConverter converter;
+
+  if (is_text) {
+    std::string    input(reinterpret_cast<const char *>(data + 1), size - 1);
+    HTTPParser     parser;
+    ts::PostScript parser_cleanup([&]() { http_parser_clear(&parser); });
+    const char    *start = input.data();
+    const char    *end   = start + input.size();
+
+    // Reserve pseudo-header slots required by HTTP/1-to-HTTP/2 or HTTP/3 
conversion.
+    header.create(type, version == 2 ? HTTP_2_0 : HTTP_3_0);
+    http_parser_init(&parser);
+    ParseResult result = is_response ? header.parse_resp(&parser, &start, end, 
true) : header.parse_req(&parser, &start, end, true);
+    if (result == ParseResult::DONE && converter.convert(header, 1, version) 
== 0) {
+      convert_decoded_header(header, version, is_response, false);
+    }
+  } else {
+    FuzzedDataProvider provider(data + 1, size - 1);
+
+    header.create(type);

Review Comment:
   In the structured-field path, the harness builds an H2/H3-style header 
(pseudo-headers like `:method`, `:status`) but calls `header.create(type)` 
without specifying H2/H3. This can leave `HTTPHdr` in an HTTP/1-oriented layout 
while later code validates/converts as H2/H3, which can lead to incorrect 
behavior or missed coverage. Initialize the header with the intended protocol 
version (HTTP_2_0 vs HTTP_3_0) based on `version`.



##########
tests/fuzzing/fuzz_header_conversion.cc:
##########
@@ -0,0 +1,205 @@
+/** @file
+
+  Fuzzing HTTP header validation and version conversion.
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ */
+
+#include "proxy/hdrs/HTTP.h"
+#include "proxy/hdrs/HeaderValidator.h"
+#include "proxy/hdrs/VersionConverter.h"
+#include "tscore/Diags.h"
+#include "tsutil/PostScript.h"
+
+#include <fuzzer/FuzzedDataProvider.h>
+
+#include <array>
+#include <cstddef>
+#include <cstdint>
+#include <string>
+#include <string_view>
+#include <vector>
+
+extern int cmd_disable_pfreelist;
+
+namespace
+{
+constexpr size_t                           MAX_INPUT_LENGTH = 64 * 1024;
+constexpr std::array<std::string_view, 17> FIELD_NAMES      = {
+  ":method",
+  ":scheme",
+  ":authority",
+  ":path",
+  ":status",
+  "host",
+  "cookie",
+  "connection",
+  "keep-alive",
+  "proxy-connection",
+  "upgrade",
+  "transfer-encoding",
+  "te",
+  "content-length",
+  "content-type",
+  "x-test",
+  "",
+};
+
+// Bit flags in the first input byte controlling the harness behavior.
+constexpr uint8_t FLAG_IS_RESPONSE    = 1 << 0; // Decode as a response 
instead of a request.
+constexpr uint8_t FLAG_IS_TEXT        = 1 << 1; // Consume the payload as 
HTTP/1.1 text.
+constexpr uint8_t FLAG_HTTP_3         = 1 << 2; // Target HTTP/3 instead of 
HTTP/2.
+constexpr uint8_t FLAG_PSEUDO_HEADERS = 1 << 3; // Add the required 
pseudo-headers.
+constexpr uint8_t FLAG_CONNECT        = 1 << 4; // Use the CONNECT method 
(requests only).
+constexpr uint8_t FLAG_TRAILER        = 1 << 5; // Treat the structured fields 
as trailers.
+
+void
+add_field(HTTPHdr &header, std::string_view name, std::string_view value)
+{
+  // An empty name allocates an unnamed field for validator rejection coverage.
+  MIMEField *field = header.field_create(name);
+
+  header.field_value_set(field, value);
+  header.field_attach(field);
+}
+
+void
+print_header(const HTTPHdr &header)
+{
+  std::array<char, 32> small_buffer{};
+  int                  buffer_index = 0;
+  int                  dump_offset  = 0;
+  int                  length       = header.length_get();
+
+  header.print(small_buffer.data(), small_buffer.size(), &buffer_index, 
&dump_offset);
+  if (length >= 0 && length <= 2 * static_cast<int>(MAX_INPUT_LENGTH)) {
+    std::vector<char> buffer(static_cast<size_t>(length) + 1);
+    buffer_index = 0;
+    dump_offset  = 0;
+    header.print(buffer.data(), static_cast<int>(buffer.size()), 
&buffer_index, &dump_offset);
+  }
+}
+
+void
+convert_decoded_header(const HTTPHdr &header, int version, bool is_response, 
bool is_trailer)
+{
+  if (!HeaderValidator::is_h2_h3_header_valid(header, is_response, 
is_trailer)) {
+    return;
+  }
+
+  // Trailers lack the request/status pseudo headers required by full-header 
conversion.
+  if (is_trailer) {
+    return;
+  }
+
+  HTTPHdr          converted;
+  ts::PostScript   cleanup([&]() { converted.destroy(); });
+  VersionConverter converter;
+
+  converted.create(is_response ? HTTPType::RESPONSE : HTTPType::REQUEST);
+  converted.copy(&header);
+  if (converter.convert(converted, version, 1) == 0) {
+    print_header(converted);
+  }
+}
+} // namespace
+
+extern "C" int
+LLVMFuzzerInitialize(int *, char ***)
+{
+  cmd_disable_pfreelist = true;
+  DiagsPtr::set(new Diags("fuzzing", "", "", nullptr));
+  http_init();
+  return 0;
+}
+
+extern "C" int
+LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+  if (size == 0 || size > MAX_INPUT_LENGTH) {
+    return 0;
+  }
+
+  uint8_t          flags       = data[0];
+  bool             is_response = (flags & FLAG_IS_RESPONSE) != 0;
+  bool             is_text     = (flags & FLAG_IS_TEXT) != 0;
+  int              version     = (flags & FLAG_HTTP_3) != 0 ? 3 : 2;
+  HTTPType         type        = is_response ? HTTPType::RESPONSE : 
HTTPType::REQUEST;
+  HTTPHdr          header;
+  ts::PostScript   cleanup([&]() { header.destroy(); });
+  VersionConverter converter;
+
+  if (is_text) {
+    std::string    input(reinterpret_cast<const char *>(data + 1), size - 1);
+    HTTPParser     parser;
+    ts::PostScript parser_cleanup([&]() { http_parser_clear(&parser); });
+    const char    *start = input.data();
+    const char    *end   = start + input.size();
+
+    // Reserve pseudo-header slots required by HTTP/1-to-HTTP/2 or HTTP/3 
conversion.
+    header.create(type, version == 2 ? HTTP_2_0 : HTTP_3_0);

Review Comment:
   The HTTP/1 text parsing path initializes `HTTPHdr` with 
`HTTP_2_0`/`HTTP_3_0` and then calls `parse_req`/`parse_resp`. Those parsers 
operate on HTTP/1.x wire format, so pre-creating the header as H2/H3 risks 
corrupt internal state or producing inconsistent parse results. Create the 
header in an HTTP/1-compatible mode for parsing (e.g., default/HTTP_1_1), then 
perform the version conversion after parsing.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to