Skip to content

Commit ae6a252

Browse files
talnordandnoe
authored andcommitted
router: fix matching when all domains have wildcards (envoyproxy#4326)
When all domains of all virtual hosts had wildcard characters and there was a default virtual host, RouteMatcher::findVirtualHost() used to erroneously ignore the wildcard suffixes. This patch fixes the issue and introduces a unit test that covers this case. Risk level: Medium. The fix is straightforward, but users who rely on the erroneous behavior might be affected. Testing: Introduced TestRoutesWithWildcardAndDefaultOnly Signed-off-by: Tal Nordan tal.nordan@solo.io
1 parent aa06142 commit ae6a252

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

source/common/router/config_impl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ RouteConstSharedPtr VirtualHostImpl::getRouteFromEntries(const Http::HeaderMap&
851851

852852
const VirtualHostImpl* RouteMatcher::findVirtualHost(const Http::HeaderMap& headers) const {
853853
// Fast path the case where we only have a default virtual host.
854-
if (virtual_hosts_.empty() && default_virtual_host_) {
854+
if (virtual_hosts_.empty() && wildcard_virtual_host_suffixes_.empty() && default_virtual_host_) {
855855
return default_virtual_host_.get();
856856
}
857857

test/common/router/config_impl_test.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,31 @@ TEST(RouteMatcherTest, TestRoutes) {
535535
}
536536
}
537537

538+
TEST(RouteMatcherTest, TestRoutesWithWildcardAndDefaultOnly) {
539+
std::string yaml = R"EOF(
540+
virtual_hosts:
541+
- name: wildcard
542+
domains: ["*.solo.io"]
543+
routes:
544+
- match: { prefix: "/" }
545+
route: { cluster: "wildcard" }
546+
- name: default
547+
domains: ["*"]
548+
routes:
549+
- match: { prefix: "/" }
550+
route: { cluster: "default" }
551+
)EOF";
552+
553+
const auto proto_config = parseRouteConfigurationFromV2Yaml(yaml);
554+
NiceMock<Server::Configuration::MockFactoryContext> factory_context;
555+
TestConfigImpl config(proto_config, factory_context, true);
556+
557+
EXPECT_EQ("wildcard",
558+
config.route(genHeaders("gloo.solo.io", "/", "GET"), 0)->routeEntry()->clusterName());
559+
EXPECT_EQ("default",
560+
config.route(genHeaders("example.com", "/", "GET"), 0)->routeEntry()->clusterName());
561+
}
562+
538563
TEST(RouteMatcherTest, TestRoutesWithInvalidRegex) {
539564
std::string invalid_route = R"EOF(
540565
virtual_hosts:

0 commit comments

Comments
 (0)