Skip to content

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

fix: three host extractors decide the same security question and disagree on the forms an intranet host is written in (PIN-273)

Luigi Agosti9ffa40f213dc
diff --git a/internal/handlers/audit.go b/internal/handlers/audit.go
index 2b3e0aa9..37be1752 100644
--- a/internal/handlers/audit.go
+++ b/internal/handlers/audit.go
@@ -63,7 +63,7 @@ func (h *Handlers) HandleAudit(w http.ResponseWriter, r *http.Request) {
if err != nil {
return audit.NewPageAuditError(url, err)
}
- return h.auditPage(r.Context(), url, opts, effectiveCfg, targets)
+ return h.auditPage(r.Context(), targets.url, opts, effectiveCfg, targets)
}
report, err := audit.RunAudit(
diff --git a/internal/handlers/audit_page.go b/internal/handlers/audit_page.go
index 5c75156a..756ebe86 100644
--- a/internal/handlers/audit_page.go
+++ b/internal/handlers/audit_page.go
@@ -14,6 +14,7 @@ import (
"github.com/pinchtab/pinchtab/internal/config"
"github.com/pinchtab/pinchtab/internal/httpx"
"github.com/pinchtab/pinchtab/internal/navguard"
+ "github.com/pinchtab/pinchtab/internal/urls"
)
const (
@@ -86,6 +87,7 @@ func (h *Handlers) HandleAuditPage(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
+ req.URL = targets.url
if !h.ensureBrowserOrRespond(w, routing.EffectiveCfg) {
return
}
@@ -98,6 +100,7 @@ func (h *Handlers) HandleAuditPage(w http.ResponseWriter, r *http.Request) {
// for batch audits: the same URL/IDPI/SSRF validation, but failures come back
// as errors so the caller can turn them into per-page report entries.
func (h *Handlers) validateAuditTarget(url string, cfg *config.RuntimeConfig) (navTargets, error) {
+ url = urls.EnsureScheme(url)
allowFile := cfg != nil && cfg.AllowFileScheme
if err := validateNavigateURL(url, allowFile); err != nil {
return navTargets{}, err
@@ -107,13 +110,13 @@ func (h *Handlers) validateAuditTarget(url string, cfg *config.RuntimeConfig) (n
return navTargets{}, fmt.Errorf("navigation blocked by IDPI: %s", domainResult.Reason)
}
if allowFile && navguard.IsFileURL(url) {
- return navTargets{target: &validatedNavigateTarget{AllowInternal: true}, trustedCIDRs: buildNavigateTrustedProxyCIDRs(cfg)}, nil
+ return navTargets{target: &validatedNavigateTarget{AllowInternal: true}, trustedCIDRs: buildNavigateTrustedProxyCIDRs(cfg), url: url}, nil
}
target, err := validateNavigateTarget(url, h.IDPIGuard.DomainAllowed(url), parseCIDRs(cfg.TrustedResolveCIDRs))
if err != nil {
return navTargets{}, err
}
- return navTargets{target: target, trustedCIDRs: buildNavigateTrustedProxyCIDRs(cfg)}, nil
+ return navTargets{target: target, trustedCIDRs: buildNavigateTrustedProxyCIDRs(cfg), url: url}, nil
}
// auditPage navigates a fresh tab to url and assembles the single-page
diff --git a/internal/handlers/idpi_domain_block.go b/internal/handlers/idpi_domain_block.go
index 7f9febc1..2ab0f8af 100644
--- a/internal/handlers/idpi_domain_block.go
+++ b/internal/handlers/idpi_domain_block.go
@@ -2,11 +2,10 @@ package handlers
import (
"net/http"
- "strings"
"github.com/pinchtab/pinchtab/internal/httpx"
- "github.com/pinchtab/pinchtab/internal/navguard"
"github.com/pinchtab/pinchtab/internal/remedy"
+ "github.com/pinchtab/pinchtab/internal/security"
)
// idpiDomainBlockedCode is the one code every IDPI domain block reports, so a
@@ -50,7 +49,7 @@ func idpiBlockDetails(url, hint string, r remedy.Remedy) map[string]any {
details["url"] = url
// The domain is a discrete field as well as prose so MCP and dashboard
// consumers do not have to parse the sentence to learn what was blocked.
- if host, ok := navguard.ExtractHost(url); ok && strings.TrimSpace(host) != "" {
+ if host := security.ExtractHost(url); host != "" {
details["domain"] = host
}
return details
@@ -65,8 +64,8 @@ var allowlistWidening = remedy.Declare(
// (about:blank) cannot be allowlisted at all, so it gets no remedy instead of one that
// cannot work.
func idpiAllowlistRemedy(url string) remedy.Remedy {
- host, ok := navguard.ExtractHost(url)
- if !ok || strings.TrimSpace(host) == "" {
+ host := security.ExtractHost(url)
+ if host == "" {
return remedy.None
}
return allowlistWidening.Fill(host)
diff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.go
index 1cf7db75..f3efa304 100644
--- a/internal/handlers/navigation.go
+++ b/internal/handlers/navigation.go
@@ -18,6 +18,7 @@ import (
"github.com/pinchtab/pinchtab/internal/httpx"
"github.com/pinchtab/pinchtab/internal/navguard"
"github.com/pinchtab/pinchtab/internal/remedy"
+ "github.com/pinchtab/pinchtab/internal/urls"
)
// HandleNavigate navigates a tab to a URL or creates a new tab.
@@ -111,6 +112,11 @@ func decodeNavigateRequest(w http.ResponseWriter, r *http.Request) (navigateRequ
type navTargets struct {
target *validatedNavigateTarget
trustedCIDRs []*net.IPNet
+ // url is the normalized target every decision above was made about. The
+ // caller navigates to THIS, not to what it decoded: a decision taken on
+ // "https://intranet" and a navigation issued for "intranet" are a decision
+ // about a different string than the one that ran.
+ url string
}
// navigateToURL runs the full navigate pipeline on an already-decoded request:
@@ -129,6 +135,7 @@ func (h *Handlers) navigateToURL(w http.ResponseWriter, r *http.Request, req nav
if !ok {
return
}
+ req.URL = targets.url
navRoute := h.recordNavigateRoute(r, routing)
@@ -204,6 +211,10 @@ func idpiScannerHint() string {
// target resolution, recording the navigate request on both the blocked and
// accepted paths. On success it returns the resolved target and trusted-proxy CIDRs.
func (h *Handlers) validateNavigateTargets(w http.ResponseWriter, r *http.Request, tabID, url string, effectiveCfg *config.RuntimeConfig) (navTargets, bool) {
+ // The raw HTTP path is the only one that reaches here scheme-less: the CLI
+ // normalizes through urls.Normalize and MCP through urls.Sanitize. Doing it
+ // here as well is what lets the host extractor below be a plain parse.
+ url = urls.EnsureScheme(url)
allowFile := effectiveCfg != nil && effectiveCfg.AllowFileScheme
if err := validateNavigateURL(url, allowFile); err != nil {
httpx.Error(w, 400, err)
@@ -227,7 +238,7 @@ func (h *Handlers) validateNavigateTargets(w http.ResponseWriter, r *http.Reques
// guard above (strict-mode allowlists block it via the empty-host path).
if allowFile && navguard.IsFileURL(url) {
h.recordNavigateRequest(r, tabID, url)
- return navTargets{target: &validatedNavigateTarget{AllowInternal: true}, trustedCIDRs: buildNavigateTrustedProxyCIDRs(effectiveCfg)}, true
+ return navTargets{target: &validatedNavigateTarget{AllowInternal: true}, trustedCIDRs: buildNavigateTrustedProxyCIDRs(effectiveCfg), url: url}, true
}
trustedResolveCIDRs := parseCIDRs(effectiveCfg.TrustedResolveCIDRs)
@@ -238,7 +249,7 @@ func (h *Handlers) validateNavigateTargets(w http.ResponseWriter, r *http.Reques
}
trustedCIDRs := buildNavigateTrustedProxyCIDRs(effectiveCfg)
h.recordNavigateRequest(r, tabID, url)
- return navTargets{target: target, trustedCIDRs: trustedCIDRs}, true
+ return navTargets{target: target, trustedCIDRs: trustedCIDRs, url: url}, true
}
// recordNavigateRoute builds the single-browser route metadata for this navigate,
diff --git a/internal/handlers/navigation_host_forms_test.go b/internal/handlers/navigation_host_forms_test.go
new file mode 100644
index 00000000..91e28c37
--- /dev/null
+++ b/internal/handlers/navigation_host_forms_test.go
@@ -0,0 +1,95 @@
+package handlers
+
+import (
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/pinchtab/pinchtab/internal/config"
+)
+
+func hostFormConfig(allowedDomains []string) *config.RuntimeConfig {
+ return &config.RuntimeConfig{
+ ActionTimeout: time.Second,
+ AllowedDomains: allowedDomains,
+ IDPI: config.IDPIConfig{Enabled: true, StrictMode: true},
+ }
+}
+
+// The raw HTTP path is the only one that reaches validation without a scheme:
+// the CLI normalizes through urls.Normalize and MCP through urls.Sanitize. So it
+// is the one that has to normalize here, and the target it hands on must be the
+// normalized one — a decision taken about "https://localhost:9222" and a
+// navigation issued for "localhost:9222" are decisions about different strings.
+func TestNavigateTargetsAreNormalizedBeforeTheSecurityDecision(t *testing.T) {
+ cases := []struct {
+ name string
+ rawURL string
+ wantURL string
+ }{
+ {"loopback with the cdp port", "localhost:9222", "https://localhost:9222"},
+ {"loopback bare", "localhost", "https://localhost"},
+ {"loopback with path", "localhost/status", "https://localhost/status"},
+ {"loopback literal ip with port", "127.0.0.1:9222", "https://127.0.0.1:9222"},
+ {"already scheme-ful is untouched", "http://localhost:9222/x", "http://localhost:9222/x"},
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ h := New(&policyMockBridge{}, hostFormConfig([]string{"localhost", "127.0.0.1"}), nil, nil, nil)
+ w := httptest.NewRecorder()
+
+ targets, ok := h.validateNavigateTargets(w, httptest.NewRequest("GET", "/navigate", nil), "", tc.rawURL, h.Config)
+
+ if !ok {
+ t.Fatalf("a listed host was refused: %d %s", w.Code, w.Body.String())
+ }
+ if targets.url != tc.wantURL {
+ t.Errorf("target url = %q, want %q; the caller navigates to this, not to what it decoded", targets.url, tc.wantURL)
+ }
+ })
+ }
+}
+
+// The allowlist used to refuse every scheme-less "host:port" — listed or not,
+// including the canonical CDP spelling — because url.Parse read the leading label
+// as a scheme and the empty host lost. The refusal even named the wrong cause:
+// "URL has no domain component".
+func TestAListedHostWrittenWithAPortIsNotRefusedByTheAllowlist(t *testing.T) {
+ h := New(&policyMockBridge{}, hostFormConfig([]string{"localhost"}), nil, nil, nil)
+ w := httptest.NewRecorder()
+
+ if _, ok := h.validateNavigateTargets(w, httptest.NewRequest("GET", "/navigate", nil), "", "localhost:9222", h.Config); !ok {
+ t.Fatalf("localhost:9222 refused under an allowlist that lists localhost: %d %s", w.Code, w.Body.String())
+ }
+}
+
+// A host outside the list is still refused, and the refusal names the allowlist
+// rather than reporting an SSRF or resolution failure — the misdiagnosis this
+// card started from, where the operator was told the target was a private-IP
+// threat when the real answer was that it was not on the list.
+func TestAnUnlistedSingleLabelHostIsRefusedNamingTheAllowlist(t *testing.T) {
+ h := New(&policyMockBridge{}, hostFormConfig([]string{"example.com"}), nil, nil, nil)
+ w := httptest.NewRecorder()
+
+ if _, ok := h.validateNavigateTargets(w, httptest.NewRequest("GET", "/navigate", nil), "", "intranet", h.Config); ok {
+ t.Fatal("an unlisted host was accepted under an active allowlist")
+ }
+
+ resp := decodeBlocked(t, w)
+ if !strings.Contains(resp.Error, "allowlist") {
+ t.Errorf("refusal = %q; it must name the list that refused, not a network cause", resp.Error)
+ }
+ if got := detailString(t, resp.Details, "hint"); !strings.Contains(got, "security.allowedDomains") {
+ t.Errorf("hint = %q; it does not name the setting that refused", got)
+ }
+ for _, misdiagnosis := range []string{"private", "internal IP", "resolve", "no domain component"} {
+ if strings.Contains(resp.Error, misdiagnosis) {
+ t.Errorf("refusal = %q; it reports %q, which is not why this was refused", resp.Error, misdiagnosis)
+ }
+ }
+ if got := detailString(t, resp.Details, "remedy"); !strings.Contains(got, "intranet") {
+ t.Errorf("remedy = %q; it does not name the host the caller must list", got)
+ }
+}
diff --git a/internal/handlers/scrape.go b/internal/handlers/scrape.go
index 26102459..d12aabdd 100644
--- a/internal/handlers/scrape.go
+++ b/internal/handlers/scrape.go
@@ -97,7 +97,7 @@ func (h *Handlers) HandleScrape(w http.ResponseWriter, r *http.Request) {
if err != nil {
return "", err
}
- return h.renderPageHTML(runCtx, url, routing.EffectiveCfg, targets)
+ return h.renderPageHTML(runCtx, targets.url, routing.EffectiveCfg, targets)
}
// Expand a chosen URL set instead of discovering the site when --only is
diff --git a/internal/navguard/navguard_test.go b/internal/navguard/navguard_test.go
index 3cebdd00..e2913509 100644
--- a/internal/navguard/navguard_test.go
+++ b/internal/navguard/navguard_test.go
@@ -8,6 +8,7 @@ import (
"testing"
"github.com/pinchtab/pinchtab/internal/netguard"
+ "github.com/pinchtab/pinchtab/internal/security"
)
func stubHostResolution(t *testing.T, fn func(context.Context, string, string) ([]net.IP, error)) {
@@ -148,7 +149,6 @@ func TestValidateTarget_AllowsNoHostSafeInputs(t *testing.T) {
v := &Validator{}
for _, rawURL := range []string{
"about:blank",
- "some/relative/path",
"",
} {
if _, err := v.ValidateTarget(context.Background(), rawURL, false); err != nil {
@@ -157,6 +157,40 @@ func TestValidateTarget_AllowsNoHostSafeInputs(t *testing.T) {
}
}
+// "some/relative/path" used to sit in the list above, treated as hostless because
+// the private bare-URL branch here demanded a dot, a colon, an IP or localhost
+// before it would call a single label a host. That is the same rule that made
+// "intranet/path" hostless, so an operator's allowlisted intranet name was never
+// resolved and never got the internal-IP override the allowlist had already
+// granted it. One extractor now answers for both, and a leading single label is a
+// host — so this input is resolved and refused when it does not exist, rather than
+// waved through as a relative path the HTTP API never actually accepts.
+func TestValidateTarget_TreatsALeadingSingleLabelAsAHost(t *testing.T) {
+ var asked []string
+ stubHostResolution(t, func(_ context.Context, _, host string) ([]net.IP, error) {
+ asked = append(asked, host)
+ return []net.IP{net.ParseIP("192.168.1.10")}, nil
+ })
+
+ v := &Validator{}
+ if _, err := v.ValidateTarget(context.Background(), "intranet/path", false); err == nil {
+ t.Fatal("a single-label host resolving to a private IP was allowed without the allowlist override")
+ }
+ if len(asked) != 1 || asked[0] != "intranet" {
+ t.Fatalf("resolved hosts = %v, want exactly [intranet]; the host was not extracted from the bare form", asked)
+ }
+
+ // With the allowlist override the same target is permitted — the override the
+ // old bare-host rule silently discarded.
+ target, err := v.ValidateTarget(context.Background(), "intranet/path", true)
+ if err != nil {
+ t.Fatalf("allowlisted single-label host refused: %v", err)
+ }
+ if len(target.TrustedResolvedIP) == 0 {
+ t.Error("the override returned no resolved IP, so the target was never resolved")
+ }
+}
+
func TestValidateTarget_RejectsPrivateLiteralIP(t *testing.T) {
v := &Validator{}
if _, err := v.ValidateTarget(context.Background(), "http://192.168.1.10/app", false); err == nil {
@@ -424,3 +458,66 @@ func cidrStrings(cidrs []*net.IPNet) []string {
}
return out
}
+
+// The defect this card started from, stated as the property that prevents it:
+// the allowlist decision and the host this guard resolves must come from the same
+// extractor. They did not, so security.HostAllowed answered "yes, intranet is
+// listed" while this guard saw no host at all, dropped the override on the floor,
+// and the operator got an SSRF refusal on a target they had explicitly allowed.
+func TestTheAllowlistAnswerAndTheResolvedHostComeFromOneExtractor(t *testing.T) {
+ allowed := []string{"intranet", "wiki", "example.com"}
+
+ var resolved []string
+ stubHostResolution(t, func(_ context.Context, _, host string) ([]net.IP, error) {
+ resolved = append(resolved, host)
+ return []net.IP{net.ParseIP("10.0.0.7")}, nil
+ })
+
+ for _, rawURL := range []string{
+ "intranet",
+ "intranet/wiki",
+ "intranet:8080",
+ "https://intranet/wiki",
+ "wiki",
+ "example.com",
+ } {
+ t.Run(rawURL, func(t *testing.T) {
+ resolved = nil
+ listed := security.HostAllowed(rawURL, allowed)
+ if !listed {
+ t.Fatalf("HostAllowed(%q) = false; this table only carries listed hosts", rawURL)
+ }
+
+ target, err := ValidateTarget(context.Background(), rawURL, listed, nil)
+ if err != nil {
+ t.Fatalf("a listed host resolving to a private IP was refused: %v — the allowlist override did not reach this guard", err)
+ }
+ if len(resolved) != 1 {
+ t.Fatalf("resolved %v; the guard did not resolve exactly the host the allowlist matched", resolved)
+ }
+ if got, want := resolved[0], security.ExtractHost(rawURL); got != want {
+ t.Errorf("guard resolved %q, allowlist matched %q — two answers to one question", got, want)
+ }
+ if len(target.TrustedResolvedIP) == 0 {
+ t.Error("the override produced no resolved IP, so nothing was actually checked")
+ }
+ })
+ }
+}
+
+// The converse: the override is the allowlist's to grant, not a side effect of
+// the host form. An unlisted single-label host resolving to a private IP is still
+// refused.
+func TestAnUnlistedSingleLabelHostIsStillRefused(t *testing.T) {
+ stubHostResolution(t, func(context.Context, string, string) ([]net.IP, error) {
+ return []net.IP{net.ParseIP("10.0.0.7")}, nil
+ })
+
+ listed := security.HostAllowed("intranet", []string{"example.com"})
+ if listed {
+ t.Fatal("the fixture allowlist matched a host it does not carry")
+ }
+ if _, err := ValidateTarget(context.Background(), "intranet", listed, nil); err == nil {
+ t.Fatal("an unlisted single-label host resolving to a private IP was allowed")
+ }
+}
diff --git a/internal/navguard/target.go b/internal/navguard/target.go
index fbe4b0da..0ae41336 100644
--- a/internal/navguard/target.go
+++ b/internal/navguard/target.go
@@ -11,6 +11,7 @@ import (
"time"
"github.com/pinchtab/pinchtab/internal/netguard"
+ "github.com/pinchtab/pinchtab/internal/security"
)
// ValidateTarget resolves a navigation URL's target host and enforces SSRF
@@ -29,8 +30,12 @@ func ValidateTarget(ctx context.Context, raw string, allowExplicitInternal bool,
return &ValidatedTarget{AllowInternal: true}, nil
}
- host, hasHost := ExtractHost(raw)
- if !hasHost {
+ // One extractor answers this for the whole process: the host the allowlist
+ // matched upstream (allowExplicitInternal) and the host resolved and
+ // SSRF-checked here must be the same string, or the override silently misses
+ // exactly the single-label intranet names it exists for.
+ host := security.ExtractHost(raw)
+ if host == "" {
// No resolvable host means the IP/SSRF checks below can't run, so a
// caller that relies on ValidateTarget alone would otherwise allow any
// scheme. Enforce scheme safety here too (defense in depth) so opaque
diff --git a/internal/navguard/url.go b/internal/navguard/url.go
index b31d8faa..658b691a 100644
--- a/internal/navguard/url.go
+++ b/internal/navguard/url.go
@@ -2,7 +2,6 @@ package navguard
import (
"fmt"
- "net"
"net/url"
"strings"
)
@@ -59,38 +58,3 @@ func IsFileURL(raw string) bool {
}
return strings.EqualFold(parsed.Scheme, "file")
}
-
-// ExtractHost extracts the hostname from a raw URL string.
-// Returns the lowercase, dot-trimmed host and true if found.
-func ExtractHost(raw string) (string, bool) {
- parsed, err := url.Parse(strings.TrimSpace(raw))
- if err != nil {
- return "", false
- }
- if host := strings.TrimSuffix(strings.ToLower(parsed.Hostname()), "."); host != "" {
- return host, true
- }
- if parsed.Scheme != "" {
- return "", false
- }
-
- bare := parsed.Path
- bare = strings.SplitN(bare, "/", 2)[0]
- bare = strings.SplitN(bare, "?", 2)[0]
- bare = strings.SplitN(bare, "#", 2)[0]
- bare = strings.TrimSpace(bare)
- if bare == "" || strings.HasPrefix(bare, "/") || strings.HasPrefix(bare, ".") {
- return "", false
- }
- if h, _, err := net.SplitHostPort(bare); err == nil {
- bare = h
- }
- host := strings.TrimSuffix(strings.ToLower(bare), ".")
- if host == "" {
- return "", false
- }
- if host == "localhost" || strings.HasSuffix(host, ".localhost") || net.ParseIP(host) != nil || strings.Contains(host, ".") || strings.Contains(host, ":") {
- return host, true
- }
- return "", false
-}
diff --git a/internal/security/allowlist.go b/internal/security/allowlist.go
index 8b59d30a..5fecd655 100644
--- a/internal/security/allowlist.go
+++ b/internal/security/allowlist.go
@@ -5,9 +5,10 @@
package security
import (
- "net"
"net/url"
"strings"
+
+ "github.com/pinchtab/pinchtab/internal/urls"
)
// HostAllowed reports whether rawURL's host matches an entry in allowedDomains.
@@ -50,37 +51,41 @@ func HostMatchesPatterns(host string, patterns []string) bool {
return false
}
-// ExtractHost parses rawURL and returns the lowercase bare hostname (no port).
-// It handles both fully-qualified URLs ("https://example.com:8080/path") and
-// bare hostnames ("example.com" or "example.com/path").
+// 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.
func ExtractHost(rawURL string) string {
- parsed, err := url.Parse(strings.TrimSpace(rawURL))
+ parsed, err := url.Parse(urls.EnsureScheme(rawURL))
if err != nil {
return ""
}
-
- host := parsed.Hostname()
-
- if host == "" {
- bare := parsed.Path
- bare = strings.SplitN(bare, "/", 2)[0]
- bare = strings.SplitN(bare, "?", 2)[0]
- bare = strings.SplitN(bare, "#", 2)[0]
- if h, _, err := net.SplitHostPort(bare); err == nil {
- host = h
- } else {
- host = bare
- }
- }
-
// The DNS root label is silent: "example.com." and "example.com" name the
- // same host and the browser fetches both. Leaving it on made the two answers
+ // same host and the browser fetches both. Leaving it on made two answers
// differ, and this primitive is read in both directions — a listed host that
// stops matching is a refusal on a target the operator allowed, and the
// response-forgery rule reads the same answer inverted, where a listed host
// that stops matching PERMITS forgery on exactly the sensitive origin the
- // list marks. navguard's host extractor already trimmed it.
- return strings.TrimSuffix(strings.ToLower(strings.TrimSpace(host)), ".")
+ // list marks.
+ return strings.TrimSuffix(strings.ToLower(parsed.Hostname()), ".")
}
// IsAllowedSpecialURL reports whether rawURL is a non-routable URL that bypasses
diff --git a/internal/security/allowlist_test.go b/internal/security/allowlist_test.go
index 13d1d985..5e0d4f59 100644
--- a/internal/security/allowlist_test.go
+++ b/internal/security/allowlist_test.go
@@ -1,6 +1,12 @@
package security
-import "testing"
+import (
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/srccensus"
+)
func TestHostAllowed_EmptyAllowlistMeansNoEnforcement(t *testing.T) {
if !HostAllowed("https://anything.test/", nil) {
@@ -112,3 +118,99 @@ func TestHostAllowed_TrailingRootLabelDoesNotWidenTheList(t *testing.T) {
t.Errorf("ExtractHost trimmed more than the single root label: %q", got)
}
}
+
+// The forms a host is written in, driven through the ALLOWLIST DECISION rather
+// than the parse, because the decision is what the operator gets. Three
+// extractors used to answer this question and they disagreed on the first two
+// groups below: a single-label intranet name was a host to one and nothing to
+// another, and every scheme-less "host:port" was nothing to all of them, so a
+// listed host written with a port was refused by the allowlist itself.
+func TestHostAllowedAcrossTheFormsAHostIsWrittenIn(t *testing.T) {
+ allowed := []string{"intranet", "example.com", "localhost", "192.168.1.5", "*.corp.example.com"}
+
+ cases := []struct {
+ name string
+ rawURL string
+ want bool
+ }{
+ {"single-label bare host", "intranet", true},
+ {"single-label with path", "intranet/wiki", true},
+ {"single-label with query", "intranet?q=1", true},
+ {"single-label with port", "intranet:8080", true},
+ {"single-label unlisted", "wiki", false},
+
+ {"bare host with port", "example.com:8080", true},
+ {"bare host with port and path", "example.com:8080/x", true},
+ {"loopback with the cdp port", "localhost:9222", true},
+ {"literal ip with port", "192.168.1.5:9000", true},
+
+ {"scheme-ful", "https://example.com/path", true},
+ {"scheme-ful with port", "https://example.com:8443/path?q=1", true},
+ {"scheme-ful uppercase", "HTTPS://EXAMPLE.COM", true},
+ {"bare host", "example.com", true},
+ {"literal ip", "192.168.1.5", true},
+ {"localhost", "localhost", true},
+
+ {"trailing root label", "example.com./path", true},
+ {"trailing root label with port", "example.com.:8443", true},
+ {"wildcard subdomain", "https://api.corp.example.com/v1", true},
+ {"wildcard subdomain bare with port", "api.corp.example.com:8443", true},
+
+ {"unlisted host", "https://evil.com", false},
+ {"unlisted host with port", "evil.com:8080", false},
+ {"unlisted subdomain of a listed host", "https://evil.example.com.attacker.test", false},
+ {"about:blank has no host to verify", "about:blank", true},
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := HostAllowed(tc.rawURL, allowed); got != tc.want {
+ t.Errorf("HostAllowed(%q, %v) = %v, want %v (host extracted: %q)",
+ tc.rawURL, allowed, got, tc.want, ExtractHost(tc.rawURL))
+ }
+ })
+ }
+}
+
+// The case both surviving extractors used to get wrong, kept as its own test
+// because it fails for EVERY host rather than for an unlucky one: url.Parse reads
+// "localhost" in "localhost:9222" as a scheme, so the host came back empty and
+// HostAllowed refused on the empty host — the canonical CDP spelling refused by
+// the allowlist that lists it.
+func TestAListedHostWrittenWithAPortIsAllowed(t *testing.T) {
+ for _, rawURL := range []string{"localhost:9222", "example.com:8080", "192.168.1.5:9000", "intranet:8080"} {
+ host := ExtractHost(rawURL)
+ if host == "" {
+ t.Errorf("ExtractHost(%q) = %q; an empty host is refused by the allowlist whatever it lists", rawURL, host)
+ continue
+ }
+ if !HostAllowed(rawURL, []string{host}) {
+ t.Errorf("HostAllowed(%q, [%q]) = false; the host it names is the one listed", rawURL, host)
+ }
+ }
+}
+
+// The rule the card is named for, kept enforceable: one extractor answers "which
+// host is this security decision about". Three used to, they disagreed on exactly
+// the forms an intranet host is written in, and the direction of the disagreement
+// was luck rather than design. A fourth copy — or a resurrection of one of the
+// two deleted — reds here by file and line rather than by whichever consumer
+// happens to read the losing answer.
+func TestOnePackageOwnsTheSecurityHostExtractor(t *testing.T) {
+ files := srccensus.Tree(t, "../..", 200)
+
+ var declarations []string
+ for _, file := range files {
+ if strings.Contains(file.Text, "func ExtractHost(") {
+ declarations = append(declarations, file.Name)
+ }
+ }
+
+ if len(declarations) != 1 {
+ t.Fatalf("ExtractHost is declared in %d files, want exactly 1:\n %s",
+ len(declarations), strings.Join(declarations, "\n "))
+ }
+ if filepath.Base(filepath.Dir(declarations[0])) != "security" {
+ t.Errorf("the one extractor lives in %s; internal/security owns this primitive because every consumer already imports it and it imports none of them", declarations[0])
+ }
+}
diff --git a/internal/urls/urls.go b/internal/urls/urls.go
index 7c3f8160..7793ce5d 100644
--- a/internal/urls/urls.go
+++ b/internal/urls/urls.go
@@ -22,17 +22,11 @@ func Sanitize(rawURL string) (string, error) {
return "", fmt.Errorf("empty URL")
}
- if strings.Contains(rawURL, "://") ||
- strings.HasPrefix(rawURL, "javascript:") ||
- strings.HasPrefix(rawURL, "vbscript:") ||
- strings.HasPrefix(rawURL, "about:") ||
- strings.HasPrefix(rawURL, "data:") {
+ normalized := EnsureScheme(rawURL)
+ if normalized == rawURL {
return rawURL, nil
}
- // Bare hostname — normalize to https://
- normalized := Normalize(rawURL)
-
parsed, err := url.Parse(normalized)
if err != nil {
return "", fmt.Errorf("invalid URL: %w", err)
@@ -51,18 +45,43 @@ func IsValid(rawURL string) bool {
return err == nil
}
-// ExtractHost returns the lowercase hostname without port. Empty string on failure.
-func ExtractHost(rawURL string) string {
- // url.Parse puts bare hostnames into Path when no scheme is present
- if !strings.Contains(rawURL, "://") {
- rawURL = "https://" + rawURL
+// EnsureScheme is the ONE place a scheme-less target acquires one, so no consumer
+// has to hand-roll a bare-URL branch to find the host inside it. url.Parse reads a
+// scheme-less string's leading label as a scheme, which is right for the opaque
+// forms ("about:blank", "data:...", "javascript:...") and wrong for "intranet:8080",
+// where the label is a host and the opaque part is a port — that is the one case
+// this has to tell apart, and a port is what tells it.
+//
+// https:// is the default a bare host gets. It is the assumption Normalize already
+// makes for the CLI and Sanitize for MCP, and it is the safer of the two: a target
+// written without a scheme is upgraded rather than downgraded.
+func EnsureScheme(rawURL string) string {
+ trimmed := strings.TrimSpace(rawURL)
+ if trimmed == "" {
+ return trimmed
}
-
- parsed, err := url.Parse(rawURL)
- if err != nil {
- return ""
+ if strings.Contains(trimmed, "://") {
+ return trimmed
+ }
+ parsed, err := url.Parse(trimmed)
+ if err == nil && parsed.Scheme != "" && !opaqueStartsWithPort(parsed.Opaque) {
+ return trimmed
}
+ return "https://" + trimmed
+}
- host := parsed.Hostname() // strips port
- return strings.ToLower(host)
+// opaqueStartsWithPort reports whether an opaque remainder begins with a port —
+// "8080" or "8080/path" — which is what makes "intranet:8080" a host and a port
+// rather than a scheme and a body.
+func opaqueStartsWithPort(opaque string) bool {
+ digits := strings.SplitN(opaque, "/", 2)[0]
+ if digits == "" {
+ return false
+ }
+ for _, r := range digits {
+ if r < '0' || r > '9' {
+ return false
+ }
+ }
+ return true
}
diff --git a/internal/urls/urls_test.go b/internal/urls/urls_test.go
index 1577af4c..736ad38a 100644
--- a/internal/urls/urls_test.go
+++ b/internal/urls/urls_test.go
@@ -81,24 +81,35 @@ func TestIsValid(t *testing.T) {
}
}
-func TestExtractHost(t *testing.T) {
+func TestEnsureScheme(t *testing.T) {
tests := []struct {
+ name string
input string
expected string
}{
- {"https://example.com", "example.com"},
- {"https://Example.COM/path", "example.com"},
- {"http://sub.example.com:8080/path", "sub.example.com"},
- {"example.com/path", "example.com"},
- {"EXAMPLE.COM", "example.com"},
- {"", ""},
+ {"bare host", "example.com", "https://example.com"},
+ {"single-label host", "intranet", "https://intranet"},
+ {"bare host with path", "example.com/path", "https://example.com/path"},
+ // The case url.Parse gets wrong on its own: it reads "intranet" as a
+ // scheme, so the host disappears entirely unless the port is recognised.
+ {"bare host with port", "intranet:8080", "https://intranet:8080"},
+ {"bare host with port and path", "example.com:8080/x", "https://example.com:8080/x"},
+ {"loopback with cdp port", "localhost:9222", "https://localhost:9222"},
+ {"scheme-ful url is untouched", "http://example.com", "http://example.com"},
+ {"file url is untouched", "file:///tmp/x", "file:///tmp/x"},
+ // Opaque schemes keep their meaning: prefixing these would invent a host
+ // where the form has none.
+ {"about is untouched", "about:blank", "about:blank"},
+ {"data is untouched", "data:text/plain,hi", "data:text/plain,hi"},
+ {"javascript is untouched", "javascript:alert(1)", "javascript:alert(1)"},
+ {"mailto is untouched", "mailto:[email protected]", "mailto:[email protected]"},
+ {"empty stays empty", "", ""},
}
for _, tt := range tests {
- t.Run(tt.input, func(t *testing.T) {
- result := ExtractHost(tt.input)
- if result != tt.expected {
- t.Errorf("ExtractHost(%q) = %q, want %q", tt.input, result, tt.expected)
+ t.Run(tt.name, func(t *testing.T) {
+ if got := EnsureScheme(tt.input); got != tt.expected {
+ t.Errorf("EnsureScheme(%q) = %q, want %q", tt.input, got, tt.expected)
}
})
}