ARTEX/server/asset_intercept_validate_test.go
BaaRai a50008822f fix: reject malformed intercept domains and align the IDN profile
exact_domain and domain_suffix patterns are now checked before they are
stored: whitespace or control characters, empty labels (including more
than one trailing dot), a single label, an IP literal or all-digit last
label, and a non-ASCII or xn-- label the IDNA profile refuses are answered
400 on the global and task rule APIs. fuzzy_domain refuses whitespace,
control characters and a doubled dot. Valid domains keep the canonical
form they had, and CanonicalInterceptDomain stays lenient for matching.

intercept/target.go now uses idna.Lookup, the profile the rules use. The
previous profile had the same options in x/net v0.62.0; its comment said
it accepted underscore labels, which it did not.
2026-12-02 12:28:31 +01:00

154 lines
7.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package server
import "testing"
// A block rule stored with a non-canonical spelling used to never match the
// canonical target, silently failing open (GHSA-3vq4/GHSA-v6j2). Saving must
// normalize the pattern to the spelling the matcher compares, or refuse it.
func TestValidateAssetInterceptRuleCanonicalizes(t *testing.T) {
cases := []struct {
name string
kind string
pattern string
want string
wantErr bool
}{
{"expanded IPv6 is canonicalized", "exact_ip", "0:0:0:0:0:0:0:1", "::1", false},
{"mapped IPv4 is unmapped", "exact_ip", "::FFFF:203.0.113.5", "203.0.113.5", false},
{"IPv6 zone is dropped", "exact_ip", "fe80::1%eth0", "fe80::1", false},
{"leading-zero IPv4 is refused", "exact_ip", "01.02.03.04", "", true},
{"junk exact IP is refused", "exact_ip", "not-an-ip", "", true},
{"uppercase domain with trailing dot", "exact_domain", "Example.COM.", "example.com", false},
{"unicode domain is punycoded", "exact_domain", "bücher.example", "xn--bcher-kva.example", false},
{"bare dot domain is refused", "exact_domain", ".", "", true},
{"fuzzy domain is canonicalized", "fuzzy_domain", " .GOV.CN. ", ".gov.cn", false},
{"domain suffix is canonicalized", "domain_suffix", " Example.COM. ", "example.com", false},
{"domain suffix drops a wildcard label", "domain_suffix", "*.example.com", "example.com", false},
{"domain suffix drops a leading dot", "domain_suffix", ".example.com", "example.com", false},
{"unicode domain suffix is punycoded", "domain_suffix", "*.bücher.example", "xn--bcher-kva.example", false},
{"domain suffix wildcard inside a label is refused", "domain_suffix", "*example.com", "", true},
{"domain suffix deeper wildcard is refused", "domain_suffix", "api.*.example.com", "", true},
{"domain suffix double dot is refused", "domain_suffix", "..example.com", "", true},
{"domain suffix URL is refused", "domain_suffix", "https://example.com", "", true},
{"bare wildcard domain suffix is refused", "domain_suffix", "*.", "", true},
{"fuzzy IP is lower-cased", "fuzzy_ip", "FE80::", "fe80::", false},
{"CIDR is masked", "cidr", "192.168.1.5/16", "192.168.0.0/16", false},
{"invalid CIDR is refused", "cidr", "not-a-cidr", "", true},
{"URL pattern is left verbatim", "exact_url", "HTTPS://Example.com/A", "HTTPS://Example.com/A", false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
req := assetInterceptRuleReq{Enabled: true, Kind: c.kind, Pattern: c.pattern}
err := validateAssetInterceptRuleReq(&req)
if c.wantErr {
if err == nil {
t.Fatalf("pattern %q was accepted as %q, want an error", c.pattern, req.Pattern)
}
return
}
if err != nil {
t.Fatalf("pattern %q was refused: %v", c.pattern, err)
}
if req.Pattern != c.want {
t.Fatalf("pattern %q normalized to %q, want %q", c.pattern, req.Pattern, c.want)
}
})
}
}
// Malformed domain patterns are refused at the API instead of being stored in
// a form no target has ("example.com.." used to be stored as "example.com.").
func TestValidateAssetInterceptRuleRefusesMalformedDomains(t *testing.T) {
cases := []struct {
name string
kind string
pattern string
}{
{"exact two trailing dots", "exact_domain", "example.com.."},
{"exact doubled dot", "exact_domain", "a..example.com"},
{"exact leading dot", "exact_domain", ".example.com"},
{"exact space inside", "exact_domain", "a b.example.com"},
{"exact tab inside", "exact_domain", "a\tb.example.com"},
{"exact NUL inside", "exact_domain", "a\x00b.example.com"},
{"exact newline inside", "exact_domain", "example.com\nevil.net"},
{"exact single label", "exact_domain", "com"},
{"exact single label with trailing dot", "exact_domain", "Com."},
{"exact IPv4 literal", "exact_domain", "192.0.2.1"},
{"exact IPv6 literal", "exact_domain", "2001:db8::1"},
{"exact short IPv4 form", "exact_domain", "127.1"},
{"exact underscore in a Unicode label", "exact_domain", "_bücher.example.com"},
{"exact invalid punycode label", "exact_domain", "xn--zz.example.com"},
{"suffix two trailing dots", "domain_suffix", "example.com.."},
{"suffix wildcard and two trailing dots", "domain_suffix", "*.example.com.."},
{"suffix doubled dot", "domain_suffix", "a..example.com"},
{"suffix space inside", "domain_suffix", "a b.example.com"},
{"suffix control inside", "domain_suffix", "example\x01.com"},
{"suffix single label", "domain_suffix", "com"},
{"suffix wildcard single label", "domain_suffix", "*.com"},
{"suffix IPv4 literal", "domain_suffix", "192.0.2.1"},
{"fuzzy doubled dot", "fuzzy_domain", "a..example.com"},
{"fuzzy two trailing dots", "fuzzy_domain", "example.com.."},
{"fuzzy space inside", "fuzzy_domain", ".gov .cn"},
{"fuzzy control inside", "fuzzy_domain", ".gov\x00"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
req := assetInterceptRuleReq{Enabled: true, Kind: c.kind, Pattern: c.pattern}
if err := validateAssetInterceptRuleReq(&req); err == nil {
t.Fatalf("%s %q was accepted as %q, want an error", c.kind, c.pattern, req.Pattern)
}
})
}
}
// Valid domains keep the canonical form they were stored with before.
func TestValidateAssetInterceptRuleKeepsValidDomains(t *testing.T) {
cases := []struct {
kind string
pattern string
want string
}{
{"exact_domain", "example.com", "example.com"},
{"exact_domain", "Example.COM.", "example.com"},
{"exact_domain", "api.v2.Example.co.uk", "api.v2.example.co.uk"},
{"exact_domain", "bücher.example", "xn--bcher-kva.example"},
{"exact_domain", "XN--BCHER-KVA.example", "xn--bcher-kva.example"},
{"exact_domain", "example.com", "example.com"},
{"exact_domain", "_dmarc.Example.com", "_dmarc.example.com"},
{"exact_domain", "my_host.internal", "my_host.internal"},
{"domain_suffix", "*.Example.COM.", "example.com"},
{"domain_suffix", ".shop.example.com", "shop.example.com"},
{"domain_suffix", "xn--bcher-kva.example", "xn--bcher-kva.example"},
{"fuzzy_domain", ".gov", ".gov"},
{"fuzzy_domain", ".edu.cn", ".edu.cn"},
{"fuzzy_domain", "Example", "example"},
{"fuzzy_domain", "EXAMPLE.COM.", "example.com"},
}
for _, c := range cases {
t.Run(c.kind+" "+c.pattern, func(t *testing.T) {
req := assetInterceptRuleReq{Enabled: true, Kind: c.kind, Pattern: c.pattern}
if err := validateAssetInterceptRuleReq(&req); err != nil {
t.Fatalf("%s %q was refused: %v", c.kind, c.pattern, err)
}
if req.Pattern != c.want {
t.Fatalf("%s %q normalized to %q, want %q", c.kind, c.pattern, req.Pattern, c.want)
}
})
}
}
// The task-level rules reuse the same validator (task create, rule create and
// update, templates).
func TestValidateTaskInterceptRuleRefusesMalformedDomains(t *testing.T) {
for _, p := range []string{"example.com..", "a..example.com", "com", "192.0.2.1", "a b.example.com"} {
for _, action := range []string{"block", "allow"} {
req := taskInterceptRuleReq{Enabled: true, Action: action, Kind: "domain_suffix", Pattern: p}
if err := validateTaskInterceptRuleReq(&req); err == nil {
t.Errorf("task %s domain_suffix %q was accepted as %q", action, p, req.Pattern)
}
}
}
if _, err := buildTaskInterceptRules([]taskInterceptRuleReq{{Enabled: true, Action: "allow", Kind: "exact_domain", Pattern: "example.com.."}}); err == nil {
t.Error("buildTaskInterceptRules accepted exact_domain example.com..")
}
}