You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ez...@apache.org on 2026/10/19 22:03:23 UTC
(trafficserver) 19/30: regex_map: anchor host matching to the full hostname
This is an automated email from the ASF dual-hosted git repository.
ezelkow1 pushed a commit to branch 9.2.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
commit 2bf140ed3822396f27210b8d77ce735c81f11e9b
Author: Evan Zelkowitz <e_...@apple.com>
AuthorDate: Fri Aug 28 21:51:54 2026 -0600
regex_map: anchor host matching to the full hostname
regex_map compiled its host pattern unanchored, so a rule for
"cdn.example.com" also matched hosts that merely contain it, such as
"prefix.cdn.example.com" and "cdn.example.com.test", and routed them to that
rule's to-host. Compile with RE_ANCHORED | RE_ENDANCHORED so a match must
span the entire host.
PCRE1 has no end-anchor compile option, so RE_ENDANCHORED is implemented by
wrapping the pattern in a non-capturing group followed by \z. Letting the
engine enforce the anchor keeps backtracking correct, which a post-match span
check would not, and the non-capturing group both preserves the pattern's
capture numbering for the to-URL substitutions and keeps the anchor from
binding to only the last branch of a top level alternation.
---
include/tscore/Regex.h | 1 +
proxy/http/remap/RemapConfig.cc | 6 +-
proxy/http/remap/UrlRewrite.cc | 2 +
src/tscore/Regex.cc | 15 ++
src/tscore/unit_tests/test_Regex.cc | 58 ++++++
tests/gold_tests/remap/regex_map_anchor.test.py | 35 ++++
.../remap/replay/regex_map_anchor.replay.yaml | 203 +++++++++++++++++++++
7 files changed, 318 insertions(+), 2 deletions(-)
diff --git a/include/tscore/Regex.h b/include/tscore/Regex.h
index 62dd210e4b..83fa93dbb1 100644
--- a/include/tscore/Regex.h
+++ b/include/tscore/Regex.h
@@ -42,6 +42,7 @@ enum REFlags {
RE_CASE_INSENSITIVE = 0x0001, ///< Ignore case (default: case sensitive).
RE_UNANCHORED = 0x0002, ///< Unanchored (DFA defaults to anchored).
RE_ANCHORED = 0x0004, ///< Anchored (Regex defaults to unanchored).
+ RE_ENDANCHORED = 0x0008, ///< Anchored at the subject end; with RE_ANCHORED, requires a whole-subject match.
};
/** Wrapper for PCRE evaluation.
diff --git a/proxy/http/remap/RemapConfig.cc b/proxy/http/remap/RemapConfig.cc
index 2d92533bd7..c6d7e07666 100644
--- a/proxy/http/remap/RemapConfig.cc
+++ b/proxy/http/remap/RemapConfig.cc
@@ -877,8 +877,10 @@ process_regex_mapping_config(const char *from_host_lower, url_mapping *new_mappi
reg_map->url_map = new_mapping;
// using from_host_lower (and not new_mapping->fromURL.host_get())
- // as this one will be nullptr-terminated (required by pcre_compile)
- if (reg_map->regular_expression.compile(from_host_lower) == false) {
+ // as this one will be nullptr-terminated (required by pcre_compile).
+ // Anchored at both ends so a rule for "cdn.example.com" matches the entire host and not a
+ // leading or trailing substring of it (e.g. "prefix.cdn.example.com" or "cdn.example.com.test").
+ if (reg_map->regular_expression.compile(from_host_lower, RE_ANCHORED | RE_ENDANCHORED) == false) {
Warning("pcre_compile failed! Regex has error starting at %s", from_host_lower);
goto lFail;
}
diff --git a/proxy/http/remap/UrlRewrite.cc b/proxy/http/remap/UrlRewrite.cc
index c33bbe4544..7d35302e04 100644
--- a/proxy/http/remap/UrlRewrite.cc
+++ b/proxy/http/remap/UrlRewrite.cc
@@ -937,6 +937,8 @@ UrlRewrite::_regexMappingLookup(RegexMappingList ®ex_mappings, URL *request_u
}
int matches_info[MAX_REGEX_SUBS * 3];
+ // The regex is compiled anchored at both ends (see process_regex_mapping_config), so a
+ // successful match spans the entire request host, never a leading or trailing substring.
bool match_result =
list_iter->regular_expression.exec(std::string_view(request_host, request_host_len), matches_info, countof(matches_info));
diff --git a/src/tscore/Regex.cc b/src/tscore/Regex.cc
index 0c6aaa436f..bb43caef96 100644
--- a/src/tscore/Regex.cc
+++ b/src/tscore/Regex.cc
@@ -22,6 +22,7 @@
*/
#include <array>
+#include <string>
#include "tscore/ink_platform.h"
#include "tscore/ink_thread.h"
@@ -72,6 +73,20 @@ Regex::compile(const char *pattern, const unsigned flags)
options |= PCRE_ANCHORED;
}
+ // PCRE1 has no end-anchor compile option, so express it in the pattern and let
+ // the engine enforce it. A post-match span check is not equivalent: it would
+ // reject a subject the pattern can match in full whenever the first path the
+ // engine takes ends early, as a lazy quantifier does. The non-capturing group
+ // preserves the pattern's own capture numbering and keeps \z from binding to
+ // just the last branch of a top level alternation.
+ std::string end_anchored_pattern;
+
+ if (flags & RE_ENDANCHORED) {
+ end_anchored_pattern.reserve(strlen(pattern) + 6);
+ end_anchored_pattern.assign("(?:").append(pattern).append(R"()\z)");
+ pattern = end_anchored_pattern.c_str();
+ }
+
regex = pcre_compile(pattern, options, &error, &erroffset, nullptr);
if (error) {
regex = nullptr;
diff --git a/src/tscore/unit_tests/test_Regex.cc b/src/tscore/unit_tests/test_Regex.cc
index e457345fd8..1f274c8021 100644
--- a/src/tscore/unit_tests/test_Regex.cc
+++ b/src/tscore/unit_tests/test_Regex.cc
@@ -52,3 +52,61 @@ TEST_CASE("Regex", "[libts][Regex]")
}
}
}
+
+// PCRE1 has no end-anchor compile option, so RE_ENDANCHORED is implemented by
+// rewriting the pattern. These cases pin the parts of that rewrite that are easy
+// to get wrong.
+TEST_CASE("Regex end anchored", "[libts][Regex]")
+{
+ SECTION("both anchors require a whole subject match")
+ {
+ Regex r;
+
+ REQUIRE(r.compile("cdn\\.example\\.com", RE_ANCHORED | RE_ENDANCHORED) == true);
+ CHECK(r.exec(std::string_view{"cdn.example.com"}) == true);
+ CHECK(r.exec(std::string_view{"cdn.example.com.test"}) == false);
+ CHECK(r.exec(std::string_view{"prefix.cdn.example.com"}) == false);
+ }
+
+ SECTION("the end anchor applies to every branch of a top level alternation")
+ {
+ Regex r;
+
+ REQUIRE(r.compile("foo|bar", RE_ANCHORED | RE_ENDANCHORED) == true);
+ CHECK(r.exec(std::string_view{"foo"}) == true);
+ CHECK(r.exec(std::string_view{"bar"}) == true);
+ CHECK(r.exec(std::string_view{"foobaz"}) == false);
+ CHECK(r.exec(std::string_view{"barbaz"}) == false);
+ }
+
+ SECTION("capture numbering is unchanged")
+ {
+ Regex r;
+ std::array<int, Regex::DEFAULT_GROUP_COUNT * 3> ovector = {{0}};
+ const std::string_view subject{"host.test"};
+
+ REQUIRE(r.compile("([a-z]+)\\.([a-z]+)", RE_ANCHORED | RE_ENDANCHORED) == true);
+ REQUIRE(r.get_capture_count() == 2);
+ REQUIRE(r.exec(subject, ovector.data(), int(ovector.size())) == true);
+ CHECK(subject.substr(ovector[2], ovector[3] - ovector[2]) == "host");
+ CHECK(subject.substr(ovector[4], ovector[5] - ovector[4]) == "test");
+ }
+
+ SECTION("a lazy quantifier still matches the full subject")
+ {
+ Regex r;
+
+ // The engine has to backtrack to satisfy the end anchor. Checking the match
+ // span after the fact instead would reject this.
+ REQUIRE(r.compile("a+?", RE_ANCHORED | RE_ENDANCHORED) == true);
+ CHECK(r.exec(std::string_view{"aaa"}) == true);
+ }
+
+ SECTION("RE_ANCHORED alone still permits a trailing substring")
+ {
+ Regex r;
+
+ REQUIRE(r.compile("cdn\\.example\\.com", RE_ANCHORED) == true);
+ CHECK(r.exec(std::string_view{"cdn.example.com.test"}) == true);
+ }
+}
diff --git a/tests/gold_tests/remap/regex_map_anchor.test.py b/tests/gold_tests/remap/regex_map_anchor.test.py
new file mode 100644
index 0000000000..8e8d9926cf
--- /dev/null
+++ b/tests/gold_tests/remap/regex_map_anchor.test.py
@@ -0,0 +1,35 @@
+'''
+Verify regex_map performs full-hostname matching.
+
+A regex_map rule must match the entire request hostname, never a host in
+which the configured value appears only as a substring. For a rule covering
+"cdn.example.com":
+ - "prefix.cdn.example.com" must NOT match: rejected by the start anchor.
+ - "cdn.example.com.test" must NOT match: rejected by full-hostname matching
+ (start-anchoring alone would still match it).
+ - "cdn.example.computer" must NOT match: shared prefix, no label boundary.
+ - "cdn.example.com." must NOT match: trailing-dot FQDN.
+ - "cdn.example.com" must match (exact), and reach the origin.
+ - "CDN.EXAMPLE.COM" must match (case-insensitive), and reach the origin.
+'''
+# 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.
+
+Test.Summary = '''
+Verify regex_map performs full-hostname matching.
+'''
+
+Test.ATSReplayTest(replay_file='replay/regex_map_anchor.replay.yaml')
diff --git a/tests/gold_tests/remap/replay/regex_map_anchor.replay.yaml b/tests/gold_tests/remap/replay/regex_map_anchor.replay.yaml
new file mode 100644
index 0000000000..9e1ff24e9c
--- /dev/null
+++ b/tests/gold_tests/remap/replay/regex_map_anchor.replay.yaml
@@ -0,0 +1,203 @@
+# 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.
+
+meta:
+ version: '1.0'
+
+autest:
+ description: 'Verify regex_map performs full-hostname matching'
+
+ dns:
+ name: 'dns'
+
+ server:
+ name: 'server'
+
+ client:
+ name: 'client'
+
+ ats:
+ name: 'ts'
+
+ process_config:
+ enable_cache: false
+
+ records_config:
+ proxy.config.diags.debug.enabled: 1
+ proxy.config.diags.debug.tags: 'http|url_rewrite_regex'
+ proxy.config.url_remap.remap_required: 1
+
+ remap_config:
+ # The rule must match ONLY the exact host "cdn.example.com", never a host
+ # in which that string appears as a leading or trailing substring.
+ - 'regex_map http://cdn\.example\.com/ http://127.0.0.1:{SERVER_HTTP_PORT}/'
+
+sessions:
+- transactions:
+
+ # The configured host appears only after a leading label. Rejected by the start
+ # anchor (RE_ANCHORED) alone: the match cannot begin at offset 0. ATS must
+ # return 404 and never forward to the origin.
+ - client-request:
+ method: GET
+ url: /path/test.html
+ version: '1.1'
+ headers:
+ fields:
+ - [Host, prefix.cdn.example.com]
+ - [uuid, leading-label]
+
+ proxy-request:
+ expect: absent
+
+ proxy-response:
+ status: 404
+
+ server-response:
+ status: 200
+ reason: OK
+ headers:
+ fields:
+ - [Content-Length, 0]
+
+ # The host starts with the configured value but continues past it.
+ # Start-anchoring alone would still match this; it is rejected only by
+ # full-hostname matching (RE_ENDANCHORED). ATS must return 404 and not forward.
+ - client-request:
+ method: GET
+ url: /path/test.html
+ version: '1.1'
+ headers:
+ fields:
+ - [Host, cdn.example.com.test]
+ - [uuid, trailing-content]
+
+ proxy-request:
+ expect: absent
+
+ proxy-response:
+ status: 404
+
+ server-response:
+ status: 200
+ reason: OK
+ headers:
+ fields:
+ - [Content-Length, 0]
+
+ # Shared prefix with no label boundary ("cdn.example.com" + "puter").
+ # Distinguishes true full-string matching from a naive label-boundary
+ # heuristic. Rejected by full-hostname matching; ATS must return 404.
+ - client-request:
+ method: GET
+ url: /path/test.html
+ version: '1.1'
+ headers:
+ fields:
+ - [Host, cdn.example.computer]
+ - [uuid, shared-prefix]
+
+ proxy-request:
+ expect: absent
+
+ proxy-response:
+ status: 404
+
+ server-response:
+ status: 200
+ reason: OK
+ headers:
+ fields:
+ - [Content-Length, 0]
+
+ # Trailing-dot FQDN: "cdn.example.com." is the same name to DNS but carries a
+ # trailing label separator. With anchored full-hostname matching it does not
+ # match the rule, so ATS returns 404 and does not forward. This transaction
+ # pins that behavior against silent drift.
+ - client-request:
+ method: GET
+ url: /path/test.html
+ version: '1.1'
+ headers:
+ fields:
+ - [Host, cdn.example.com.]
+ - [uuid, trailing-dot]
+
+ proxy-request:
+ expect: absent
+
+ proxy-response:
+ status: 404
+
+ server-response:
+ status: 200
+ reason: OK
+ headers:
+ fields:
+ - [Content-Length, 0]
+
+ # Exact match: the request host is exactly the configured host. It must match,
+ # be forwarded to the origin, and return 200. The origin stamps a marker header
+ # that the client verifies, proving the 200 came from the origin and not from a
+ # synthesized/error response.
+ - client-request:
+ method: GET
+ url: /path/test.html
+ version: '1.1'
+ headers:
+ fields:
+ - [Host, cdn.example.com]
+ - [uuid, exact-match]
+
+ server-response:
+ status: 200
+ reason: OK
+ headers:
+ fields:
+ - [Content-Length, 0]
+ - [X-Origin-Marker, served-by-origin]
+
+ proxy-response:
+ status: 200
+ headers:
+ fields:
+ - [X-Origin-Marker, {value: served-by-origin, as: equal}]
+
+ # Case-insensitive exact match: the request host differs only in case. The
+ # lookup lowercases the host before matching, so this must still match, be
+ # forwarded, and return the origin's 200 (verified via the marker header).
+ - client-request:
+ method: GET
+ url: /path/test.html
+ version: '1.1'
+ headers:
+ fields:
+ - [Host, CDN.EXAMPLE.COM]
+ - [uuid, case-insensitive-match]
+
+ server-response:
+ status: 200
+ reason: OK
+ headers:
+ fields:
+ - [Content-Length, 0]
+ - [X-Origin-Marker, served-by-origin]
+
+ proxy-response:
+ status: 200
+ headers:
+ fields:
+ - [X-Origin-Marker, {value: served-by-origin, as: equal}]