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

(trafficserver) branch master updated: nexthop: reject a bare #include in strategies.yaml (#13837)

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

traeak 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 d6b47e7f44 nexthop: reject a bare #include in strategies.yaml (#13837)
d6b47e7f44 is described below

commit d6b47e7f447d9e07a7a6c67ea0a6e778c410d0e9
Author: Brian Olsen <br...@comcast.com>
AuthorDate: Thu Oct 15 15:51:17 2026 -0600

    nexthop: reject a bare #include in strategies.yaml (#13837)
    
    * nexthop: reject a bare #include in strategies.yaml
    
    A '#include' with no file name built a std::string from a null
    pointer, which crashes with libc++ and throws an unhelpful
    basic_string error with libstdc++. Reject it with a clear message,
    and keep the nested load error when an included file fails.
    
    * replace strtok_r calls with string_view operations for include parsing
---
 src/proxy/http/remap/NextHopStrategyFactory.cc     | 29 +++++++++------
 .../unit-tests/include-missing-file-strategy.yaml  | 42 ++++++++++++++++++++++
 .../remap/unit-tests/include-missing-strategy.yaml | 42 ++++++++++++++++++++++
 .../unit-tests/test_NextHopStrategyFactory.cc      | 26 ++++++++++++--
 4 files changed, 126 insertions(+), 13 deletions(-)

diff --git a/src/proxy/http/remap/NextHopStrategyFactory.cc b/src/proxy/http/remap/NextHopStrategyFactory.cc
index b0e6ba97f8..196d4bc1f0 100644
--- a/src/proxy/http/remap/NextHopStrategyFactory.cc
+++ b/src/proxy/http/remap/NextHopStrategyFactory.cc
@@ -25,6 +25,7 @@
 
 #include <fstream>
 #include <cstring>
+#include <string_view>
 
 #include "proxy/http/remap/NextHopStrategyFactory.h"
 #include "proxy/http/remap/NextHopConsistentHash.h"
@@ -117,7 +118,7 @@ done:
   if (!error_loading) {
     NH_Note("%s finished loading", basename);
   } else {
-    Error("%s failed to load", basename);
+    NH_Error("%s failed to load", basename);
   }
 }
 
@@ -195,10 +196,9 @@ void
 NextHopStrategyFactory::loadConfigFile(const std::string &fileName, std::stringstream &doc,
                                        std::unordered_set<std::string> &include_once)
 {
-  const char *sep = " \t";
-  char       *tok, *last;
-  struct stat buf;
-  std::string line;
+  constexpr std::string_view separators = " \t";
+  struct stat                buf;
+  std::string                line;
 
   if (stat(fileName.c_str(), &buf) == -1) {
     std::string err_msg = strerror(errno);
@@ -251,17 +251,24 @@ NextHopStrategyFactory::loadConfigFile(const std::string &fileName, std::strings
     std::ifstream file(fileName);
     if (file.is_open()) {
       while (std::getline(file, line)) {
-        if (line[0] == '#') {
-          tok = strtok_r(const_cast<char *>(line.c_str()), sep, &last);
-          if (tok != nullptr && strcmp(tok, "#include") == 0) {
-            std::string f = strtok_r(nullptr, sep, &last);
+        std::string_view line_view{line};
+        if (line_view.starts_with('#')) {
+          const auto directive_end = line_view.find_first_of(separators);
+          if (line_view.substr(0, directive_end) == "#include") {
+            const auto include_start = line_view.find_first_not_of(separators, directive_end);
+            if (include_start == std::string_view::npos) {
+              throw std::invalid_argument("'#include' without a file name in '" + fileName + "'");
+            }
+            const auto        include_line = line_view.substr(include_start);
+            const auto        include_end  = include_line.find_first_of(separators);
+            const std::string f{include_line.substr(0, include_end)};
             if (include_once.find(f) == include_once.end()) {
               include_once.insert(f);
               // try to load included file.
               try {
                 loadConfigFile(f, doc, include_once);
-              } catch (std::exception &ex) {
-                throw std::invalid_argument("Unable to open included file '" + f + "' from '" + fileName + "'");
+              } catch (const std::exception &ex) {
+                throw std::invalid_argument("Unable to load included file '" + f + "' from '" + fileName + "': " + ex.what());
               }
             }
           }
diff --git a/src/proxy/http/remap/unit-tests/include-missing-file-strategy.yaml b/src/proxy/http/remap/unit-tests/include-missing-file-strategy.yaml
new file mode 100644
index 0000000000..de954f349b
--- /dev/null
+++ b/src/proxy/http/remap/unit-tests/include-missing-file-strategy.yaml
@@ -0,0 +1,42 @@
+# @file
+#
+#  Unit test data strategies.yaml file for testing the NextHopStrategyFactory
+#
+#  @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.
+#
+#  @section details Details
+#
+#
+# unit test strategies.yaml whose '#include' names a file that does not exist
+#
+#include unit-tests/does-not-exist.yaml
+#
+hosts: &g1
+  - host: 'p1.foo.com'
+    protocol:
+      - scheme: http
+        port: 80
+strategies:
+  - strategy: 'strategy-1'
+    policy: consistent_hash
+    hash_key: cache_key
+    go_direct: false
+    groups:
+      - *g1
+    scheme: http
diff --git a/src/proxy/http/remap/unit-tests/include-missing-strategy.yaml b/src/proxy/http/remap/unit-tests/include-missing-strategy.yaml
new file mode 100644
index 0000000000..c0a0bd5df4
--- /dev/null
+++ b/src/proxy/http/remap/unit-tests/include-missing-strategy.yaml
@@ -0,0 +1,42 @@
+# @file
+#
+#  Unit test data strategies.yaml file for testing the NextHopStrategyFactory
+#
+#  @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.
+#
+#  @section details Details
+#
+#
+# unit test strategies.yaml with a '#include' directive missing its file name
+#
+#include
+#
+hosts: &g1
+  - host: 'p1.foo.com'
+    protocol:
+      - scheme: http
+        port: 80
+strategies:
+  - strategy: 'strategy-1'
+    policy: consistent_hash
+    hash_key: cache_key
+    go_direct: false
+    groups:
+      - *g1
+    scheme: http
diff --git a/src/proxy/http/remap/unit-tests/test_NextHopStrategyFactory.cc b/src/proxy/http/remap/unit-tests/test_NextHopStrategyFactory.cc
index 9ad20006a5..9605cfec7f 100644
--- a/src/proxy/http/remap/unit-tests/test_NextHopStrategyFactory.cc
+++ b/src/proxy/http/remap/unit-tests/test_NextHopStrategyFactory.cc
@@ -40,11 +40,33 @@
 
 SCENARIO("factory tests loading yaml configs", "[loadConfig]")
 {
+  GIVEN("Loading a strategies.yaml with a bare '#include' directive.")
+  {
+    REQUIRE_NOTHROW(YAML::LoadFile(TS_SRC_DIR "/include-missing-strategy.yaml"));
+    NextHopStrategyFactory nhf(TS_SRC_DIR "/include-missing-strategy.yaml");
+
+    THEN("the bare directive is rejected and no strategies load")
+    {
+      REQUIRE(nhf.strategies_loaded == false);
+      REQUIRE(nhf.strategyInstance("strategy-1") == nullptr);
+    }
+  }
+
+  GIVEN("Loading a strategies.yaml whose '#include' names a missing file.")
+  {
+    REQUIRE_NOTHROW(YAML::LoadFile(TS_SRC_DIR "/include-missing-file-strategy.yaml"));
+    NextHopStrategyFactory nhf(TS_SRC_DIR "/include-missing-file-strategy.yaml");
+
+    THEN("the nested load error is reported and no strategies load")
+    {
+      REQUIRE(nhf.strategies_loaded == false);
+      REQUIRE(nhf.strategyInstance("strategy-1") == nullptr);
+    }
+  }
+
   GIVEN("Loading the strategy.yaml with included 'hosts.yaml'.")
   {
-#ifdef TS_SRC_DIR
     REQUIRE(chdir(TS_SRC_DIR "/..") == 0);
-#endif
     NextHopStrategyFactory nhf(TS_SRC_DIR "/strategy.yaml");
 
     WHEN("the two files are loaded.")