Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

test(security): pin every URL form ExtractHost resolves through urls.EnsureScheme, and trim its comment to the ownership line (PIN-301)

Luigi Agosti1d376fcb7eb2
diff --git a/internal/security/allowlist.go b/internal/security/allowlist.go
index 5fecd655..fb165078 100644
--- a/internal/security/allowlist.go
+++ b/internal/security/allowlist.go
@@ -51,28 +51,7 @@ func HostMatchesPatterns(host string, patterns []string) bool {
return false
}
-// ExtractHost is the ONE extractor that answers "which host is this security
-// decision about". navguard and internal/urls each kept a private copy with its
-// own hand-rolled bare-URL branch, and the three disagreed: a single-label name
-// like "intranet" was a host to one and nothing to another, and every scheme-less
-// "host:port" — "localhost:9222" included — was nothing to all of them, because
-// url.Parse reads the leading label as a scheme. Normalizing the scheme first
-// (urls.EnsureScheme) leaves this with nothing to guess at, so it is a parse and
-// a lowercase and there is no branch left to diverge.
-//
-// This widens what the allowlist matches, deliberately. Every consumer of this
-// primitive — the IDPI domain check, the response-forgery rule, download policy,
-// route interception — now sees a host for two forms that used to yield "":
-//
-// - a single-label name ("intranet"), which the operator can only have put in
-// security.allowedDomains on purpose, and which the docs say the list matches;
-// - any "host:port" without a scheme, which was refused BY the allowlist for
-// every host, listed or not.
-//
-// The forgery rule reads the same answer inverted, so a host that starts matching
-// there starts being protected rather than starting to be permitted. A bare host
-// defaults to https:// — the assumption the CLI and MCP already make — so the
-// scheme-less form is upgraded, never downgraded.
+// urls.EnsureScheme owns the normalisation that decides which forms carry a host.
func ExtractHost(rawURL string) string {
parsed, err := url.Parse(urls.EnsureScheme(rawURL))
if err != nil {
diff --git a/internal/security/allowlist_test.go b/internal/security/allowlist_test.go
index d854effb..953c040c 100644
--- a/internal/security/allowlist_test.go
+++ b/internal/security/allowlist_test.go
@@ -64,21 +64,23 @@ func TestHostAllowed_CaseInsensitive(t *testing.T) {
}
}
-func TestExtractHost_BareHostname(t *testing.T) {
- if got := ExtractHost("example.com/path"); got != "example.com" {
- t.Errorf("ExtractHost('example.com/path') = %q, want 'example.com'", got)
- }
-}
-
-func TestExtractHost_FullURL(t *testing.T) {
- if got := ExtractHost("https://example.com:8080/path?q=1"); got != "example.com" {
- t.Errorf("ExtractHost full URL = %q, want 'example.com'", got)
- }
-}
-
-func TestExtractHost_NoHost(t *testing.T) {
- if got := ExtractHost("about:blank"); got != "" {
- t.Errorf("ExtractHost('about:blank') = %q, want ''", got)
+func TestExtractHostResolvesEveryFormTheNormaliserWidened(t *testing.T) {
+ for raw, want := range map[string]string{
+ "example.com/path": "example.com",
+ "https://example.com:8080/path?q=1": "example.com",
+ "intranet": "intranet",
+ "localhost:9222": "localhost",
+ "//evil.com/x": "evil.com",
+ "/evil.com/x": "evil.com",
+ "https:evil.com/x": "evil.com",
+ "https:/evil.com/x": "evil.com",
+ "https:////evil.com/x": "evil.com",
+ `https:\\evil.com\x`: "evil.com",
+ "about:blank": "",
+ } {
+ if got := ExtractHost(raw); got != want {
+ t.Errorf("ExtractHost(%q) = %q, want %q. This primitive is read in BOTH directions: the IDPI domain check and download policy read a match as permission, the response-forgery rule inverts it, so a form that stops yielding a host re-opens surface rather than tightening it. urls.EnsureScheme owns the normalisation that decides which forms carry a host", raw, got, want)
+ }
}
}