From fe7a7783eaf942d36cbbe76a148079b4079ad6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20L=C3=B3pez=20de=20la=20Franca=20Beltran?= Date: Mon, 18 Mar 2024 13:34:33 +0100 Subject: [PATCH] Make options.hosts case insensitive --- lib/types/hosts.go | 10 +++++++++- lib/types/hosts_test.go | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/types/hosts.go b/lib/types/hosts.go index ddd084704f1..b92e2109abc 100644 --- a/lib/types/hosts.go +++ b/lib/types/hosts.go @@ -97,7 +97,7 @@ type Hosts struct { // NewHosts returns new Hosts from given addresses. func NewHosts(source map[string]Host) (*Hosts, error) { h := &Hosts{ - source: source, + source: toLowerKeys(source), n: &trieNode{ children: make(map[rune]*trieNode), }, @@ -113,6 +113,14 @@ func NewHosts(source map[string]Host) (*Hosts, error) { return h, nil } +func toLowerKeys(source map[string]Host) map[string]Host { + result := make(map[string]Host, len(source)) + for k, v := range source { + result[strings.ToLower(k)] = v + } + return result +} + // Regex description of domain(:port)? pattern to enforce blocks by. // Global var to avoid compilation penalty at runtime. // Based on regex from https://stackoverflow.com/a/106223/5427244 diff --git a/lib/types/hosts_test.go b/lib/types/hosts_test.go index b6eb46b289e..13f1564ef9e 100644 --- a/lib/types/hosts_test.go +++ b/lib/types/hosts_test.go @@ -49,6 +49,7 @@ func TestHosts(t *testing.T) { "specific.wildcard.io": {IP: net.ParseIP("90.100.110.120")}, "*wildcard-2.io": {IP: net.ParseIP("13.14.15.16")}, "specific.wildcard-2.io": {IP: net.ParseIP("130.140.150.160")}, + "with-UPPER-case.io": {IP: net.ParseIP("17.18.19.20")}, // IPv6 "simple-ipv6.io": {IP: net.ParseIP("aa::bb")}, @@ -77,6 +78,8 @@ func TestHosts(t *testing.T) { {"only-port.io", "", NotInHosts}, {"only-port.io:443", "5.6.7.8:8443", DifferentPortMapping}, {"only-port.io:9999", "", NotGivenPortMapping}, + {"with-upper-case.io", "17.18.19.20:0", EmptyPortMapping}, + {"with-UPPER-case.io", "17.18.19.20:0", EmptyPortMapping}, } runTcs(t, hosts, tcs) })