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


##########
src/proxy/hdrs/HTTP.cc:
##########
@@ -1292,6 +1293,40 @@ validate_hdr_content_length(HdrHeap *heap, HTTPHdrImpl 
*hh)
   return ParseResult::DONE;
 }
 
+bool
+HTTPHdr::parse_req_would_accept(int strict_uri_parsing, size_t 
max_request_line_size, size_t max_hdr_field_size)
+{
+  auto method{this->method_get()};
+  URL *url = this->url_get();
+
+  if (method.empty() || !std::all_of(method.begin(), method.end(), 
&ParseRules::is_token)) {
+    return false;
+  }

Review Comment:
   For a bodyless `:method: get`, the converter's case-insensitive 
`method_set()` assigns the GET index, and `method_get()` hides the original 
spelling from this gate. Skipping the reparse therefore lets HttpSM treat this 
extension method as a cacheable GET; the parser would instead assign index -1 
to the raw lowercase method. Compare the stored index with case-sensitive 
tokenization of the raw method and fall back when they differ.



##########
src/proxy/hdrs/HTTP.cc:
##########
@@ -1292,6 +1293,40 @@ validate_hdr_content_length(HdrHeap *heap, HTTPHdrImpl 
*hh)
   return ParseResult::DONE;
 }
 
+bool
+HTTPHdr::parse_req_would_accept(int strict_uri_parsing, size_t 
max_request_line_size, size_t max_hdr_field_size)
+{
+  auto method{this->method_get()};
+  URL *url = this->url_get();
+
+  if (method.empty() || !std::all_of(method.begin(), method.end(), 
&ParseRules::is_token)) {
+    return false;
+  }
+  // A CONNECT target is authority-form, so parse_req() checks its userinfo; 
it has no Host for validate_hdr_host().
+  if (!url_is_uri_compliant(strict_uri_parsing, url->user_get()) ||
+      !url_is_uri_compliant(strict_uri_parsing, url->password_get()) ||
+      !url_is_uri_compliant(strict_uri_parsing, this->path_get()) || 
!url_is_uri_compliant(strict_uri_parsing, this->query_get()) ||
+      !url_is_uri_compliant(strict_uri_parsing, this->fragment_get())) {
+    return false;
+  }
+  // Upper bound of the serialized "METHOD URL HTTP/1.1\r\n", so parse_req() 
makes the exact call.
+  if (method.size() + static_cast<size_t>(url->length_get()) + 12 > 
max_request_line_size) {
+    return false;
+  }
+  for (auto const &field : *this) {
+    auto name{field.name_get()};
+    auto value{field.value_get()};
+
+    // parse_req() drops or rejects a name with any non-field-name character, 
and trims values.
+    if (name.empty() || name.size() + value.size() > max_hdr_field_size ||
+        !std::all_of(name.begin(), name.end(), 
&ParseRules::is_http_field_name) ||
+        (!value.empty() && (ParseRules::is_ws(value.front()) || 
ParseRules::is_ws(value.back())))) {
+      return false;
+    }
+  }
+  return validate_hdr_host(m_http) == ParseResult::DONE && 
validate_hdr_content_length(m_heap, m_http) == ParseResult::DONE;

Review Comment:
   `validate_hdr_content_length()` deletes identical duplicates here, before 
HttpSM computes `client_request_hdr_bytes` with `length_get()`. A bodyless H2 
request containing many `Content-Length: 0` fields can consequently pass a 
configured `request_header_max_size` that the serialized path would exceed, and 
its header-byte accounting loses those fields. Preserve the pre-validation 
size, or conservatively fall back before mutating a header with duplicate 
Content-Length fields.



##########
src/proxy/hdrs/HTTP.cc:
##########
@@ -1292,6 +1293,40 @@ validate_hdr_content_length(HdrHeap *heap, HTTPHdrImpl 
*hh)
   return ParseResult::DONE;
 }
 
+bool
+HTTPHdr::parse_req_would_accept(int strict_uri_parsing, size_t 
max_request_line_size, size_t max_hdr_field_size)
+{
+  auto method{this->method_get()};
+  URL *url = this->url_get();
+
+  if (method.empty() || !std::all_of(method.begin(), method.end(), 
&ParseRules::is_token)) {
+    return false;
+  }
+  // A CONNECT target is authority-form, so parse_req() checks its userinfo; 
it has no Host for validate_hdr_host().
+  if (!url_is_uri_compliant(strict_uri_parsing, url->user_get()) ||

Review Comment:
   For a bodyless CONNECT with `:authority: example.com:443\x80` and 
`strict_uri_parsing=2`, the converter retains the non-ASCII port suffix while 
setting numeric port 443. This gate omits the raw port, and CONNECT has no 
synthesized Host to catch it, so the request bypasses the full-target URI check 
that would return 400. Include the stored port text in the compliance check so 
it falls back to the parser.



##########
src/proxy/http2/Http2Stream.cc:
##########
@@ -864,15 +925,35 @@ Http2Stream::update_write_request(bool call_update)
 
   // Process the new data
   if (!this->parsing_header_done) {
-    // Still parsing the request or response header
     int         bytes_used = 0;
     ParseResult state;
-    if (this->is_outbound_connection()) {
+    HTTPHdr    *send_hdr = this->_pending_send_header();
+
+    if (send_hdr != nullptr) {
+      // Field by field: copy() would wipe the pseudo-headers create(HTTP_2_0) 
reserved.
+      if (this->is_outbound_connection()) {
+        this->_send_header.method_set(send_hdr->method_get());

Review Comment:
   An HTTP/1.1 request using the extension method `get` or `head` reaches this 
copy with its original spelling and method index -1. Calling `method_set()` 
assigns the GET/HEAD index, causing the HTTP/2 converter to emit uppercase 
GET/HEAD to the origin and change the requested operation. Copy the source 
method index along with its spelling instead of re-tokenizing it.



##########
src/proxy/hdrs/VersionConverter.cc:
##########
@@ -229,14 +251,35 @@ VersionConverter::_convert_req_from_2_to_1(HTTPHdr 
&header) const
     // :path
     if (MIMEField *field = header.field_find(PSEUDO_HEADER_PATH);
         field != nullptr && field->value_is_valid(is_control_BIT | is_ws_BIT)) 
{
-      auto path{field->value_get()};
+      auto                  value{field->value_get()};
+      ts::LocalBuffer<char> buf(value.length());
+      std::string_view      path{buf.data(), value.length()};
+      auto                 *url = header.m_http->u.req.m_url_impl;
+
+      // Copy out, as for :authority.
+      std::copy(value.begin(), value.end(), buf.data());
+
+      // Split as url_parse_http() would, so cache keys and remap see the same 
fields.
+      auto hpos = path.find('#');
+
+      if (hpos != std::string_view::npos) {
+        url->set_fragment(header.m_heap, path.substr(hpos + 1), true);
+        path = path.substr(0, hpos);
+      }
+
+      auto qpos = path.find('?');
+
+      if (qpos != std::string_view::npos) {
+        url->set_query(header.m_heap, path.substr(qpos + 1), true);
+        path = path.substr(0, qpos);
+      }
 
-      // cut first '/' if there, because `url_print()` add '/' before printing 
path
-      if (path.starts_with("/"sv)) {
+      // url_parse_http() strips every leading '/'; url_print() re-adds 
exactly one.
+      while (path.starts_with("/"sv)) {
         path.remove_prefix(1);
       }

Review Comment:
   Removing all leading slashes here discards bytes before the request-line 
limit is checked. With `request_line_max_size: 1024`, a `:path` containing 
1,100 slashes followed by `x` previously returned 414, but now becomes `/x` and 
passes both the direct-path gate and serialized fallback. Preserve and check 
the pre-normalization request-line length on both paths, and cover this case in 
the request-line-limit replay.



-- 
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