mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
Block rules compared raw strings: exact_ip was a literal compare, exact domain was case-folded only, and fuzzy_ip was case-sensitive. Targets are canonical, so a rule written as 0:0:0:0:0:0:0:1, with a trailing dot or in upper case never matched and the block silently failed open (GHSA-3vq4 / GHSA-v6j2). Canonicalize both sides in matchOne (netip Unmap/zone-strip for IPs; lower-case, one trailing dot removed, IDNA for domains; lower-case fuzzy_ip) and canonicalize or reject the same variants at save time.
48 lines
2 KiB
Go
48 lines
2 KiB
Go
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},
|
|
{"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)
|
|
}
|
|
})
|
|
}
|
|
}
|