bneradt commented on code in PR #14128:
URL: https://github.com/apache/trafficserver/pull/14128#discussion_r4683167519


##########
src/iocore/net/YamlSNIConfig.cc:
##########
@@ -72,6 +75,92 @@ load_tunnel_alpn(std::vector<int> &dst, const YAML::Node 
&node)
   }
 }
 
+// The port text of a tunnel destination, split the way TLSTunnelSupport splits
+// it when it connects, or nullopt when the destination names no port. An
+// unbracketed IPv6 address has several colons and so names no port. Text after
+// a bracketed address that is not a port, or a bracket that never closes, 
comes
+// back as the port so that the check refuses it: the connection would read it
+// as no port and fall back to the inbound one.
+std::optional<std::string_view>
+tunnel_port_text(std::string_view destination)
+{
+  if (!destination.empty() && destination.front() == '[') {
+    auto close = destination.find(']');
+    if (close == std::string_view::npos) {
+      return destination;
+    }
+    auto tail = destination.substr(close + 1);
+    if (tail.empty()) {
+      return std::nullopt;
+    }
+    return tail.front() == ':' ? tail.substr(1) : tail;

Review Comment:
   **[P2] Reject a bracketed destination with a missing port separator.** The 
no-colon branch returns the raw tail, so `[::1]443` reaches 
`is_valid_tunnel_port()` as `443` and passes. The runtime endpoint tokenizer 
only recognizes a port after `]:`, so the tunnel falls back to the inbound 
port. Reject a nonempty tail without the separator. Aligning whitespace 
handling with the runtime tokenizer would also keep leading whitespace from 
bypassing the bracket branch.



##########
src/proxy/http/PreWarmManager.cc:
##########
@@ -76,16 +77,25 @@ alpn_name_for_stat(int alpn_id)
   }
 }
 
-void
+// Returns false when std::stoi cannot read the text after the first colon: a
+// port template, a port that starts with a match group, or a bracketed IPv6
+// address, whose first colon is inside the brackets. The sni.yaml loader has
+// already refused any other port std::stoi cannot read.
+[[nodiscard]] bool
 parse_authority(std::string &fqdn, int32_t &port, std::string_view authority)
 {
   if (auto pos = authority.find(':'); pos != std::string::npos) {
     fqdn = authority.substr(0, pos);
-    port = static_cast<in_port_t>(std::stoi(authority.substr(pos + 1).data()));
+    try {
+      port = static_cast<in_port_t>(std::stoi(authority.substr(pos + 
1).data()));

Review Comment:
   **[P2] Pre-warm only fixed endpoints.** `std::stoi` accepts a numeric 
prefix, so a valid dynamic port such as `4$1` registers a pool for port `4` 
even though the tunnel uses the expanded port. Splitting at the first colon 
also skips valid fixed IPv6 destinations such as `[::1]:443`. Parse the 
authority with the same host and port boundaries as the tunnel, then skip 
dynamic destinations and require the whole fixed port to parse. Please cover 
both cases in the pre-warm test.



##########
src/iocore/net/YamlSNIConfig.cc:
##########
@@ -72,6 +75,92 @@ load_tunnel_alpn(std::vector<int> &dst, const YAML::Node 
&node)
   }
 }
 
+// The port text of a tunnel destination, split the way TLSTunnelSupport splits
+// it when it connects, or nullopt when the destination names no port. An
+// unbracketed IPv6 address has several colons and so names no port. Text after
+// a bracketed address that is not a port, or a bracket that never closes, 
comes
+// back as the port so that the check refuses it: the connection would read it
+// as no port and fall back to the inbound one.
+std::optional<std::string_view>
+tunnel_port_text(std::string_view destination)
+{
+  if (!destination.empty() && destination.front() == '[') {
+    auto close = destination.find(']');
+    if (close == std::string_view::npos) {
+      return destination;
+    }
+    auto tail = destination.substr(close + 1);
+    if (tail.empty()) {
+      return std::nullopt;
+    }
+    return tail.front() == ':' ? tail.substr(1) : tail;
+  }
+  auto colon = destination.find(':');
+  if (colon == std::string_view::npos || colon != destination.rfind(':')) {
+    return std::nullopt;
+  }
+  return destination.substr(colon + 1);
+}
+
+bool
+is_digit(char c)
+{
+  return std::isdigit(static_cast<unsigned char>(c)) != 0;
+}
+
+// Digits with no leading zero, which svtou would read as octal.
+bool
+is_decimal(std::string_view text)
+{
+  return !text.empty() && text.front() != '0' && std::all_of(text.begin(), 
text.end(), is_digit);
+}
+
+// The number of match groups a connection to this fqdn captures: one for each
+// '*' when the fqdn is a wildcard that SNIConfigParams files as a glob, and
+// none for a name it matches exactly.
+size_t
+match_group_count(const std::string &fqdn)
+{
+  const ats_wildcard_matcher wildcard;
+  return wildcard.match(fqdn.c_str()) ? std::count(fqdn.begin(), fqdn.end(), 
'*') : 0;
+}
+
+// A port is a number from 1 to 65535, one of the port templates, or a match
+// group such as $1, optionally after some digits. A match group is filled in
+// from the fqdn for each connection, so its value is left for then, but it 
must
+// name a group the fqdn captures. In the port the group number runs to the end
+// of the destination, which is how TunnelDestination::replace_match_groups
+// reads it, and it is one digit because no fqdn captures ten groups.
+bool
+is_valid_tunnel_port(std::string_view port, size_t groups)
+{
+  if (port == TunnelDestination::MAP_WITH_RECV_PORT_STR || port == 
TunnelDestination::MAP_WITH_PROXY_PROTOCOL_PORT_STR) {
+    return true;
+  }
+  if (auto dollar = port.find('$'); dollar != std::string_view::npos) {
+    auto digits = port.substr(0, dollar);
+    auto group  = port.substr(dollar + 1);
+    return (digits.empty() || is_decimal(digits)) && group.size() == 1 && 
group.front() >= '1' &&

Review Comment:
   **[P2] Validate the expanded port before converting it.** This validates the 
capture reference, but the current runtime path still accepts a numeric prefix 
and narrows to `in_port_t` without checking the range. A capture that expands 
to a nonnumeric or out-of-range value can still pick the wrong port or fall 
back to the inbound port. Check the entire expanded port before connecting, 
while preserving the fallback for destinations configured without a port.



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