Skip to content

fix: reject malformed intercept domains and align the IDN profile - #831

Open
BaaRai wants to merge 2 commits into
mainfrom
sec/intercept-domain-validation
Open

BaaRai wants to merge 2 commits into
mainfrom
sec/intercept-domain-validation

Conversation

@BaaRai

@BaaRai BaaRai commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Follow-up to #782 on the validation of domain patterns in asset interception rules, and on the IDN profile shared by the rule side and the target side.

What and why

db.CanonicalInterceptDomain is the comparison form of both a stored rule and a target. It never refuses a name, which is right for matching (a malformed target must still meet the rules: the built-in fuzzy .gov.cn must still hit x..gov.cn), but the rule APIs also used it as their only check before storing a pattern. So malformed patterns were stored in a form no target ever has, and the rule silently matched nothing:

Pattern Stored before Now
exact_domain example.com.. example.com. 400
exact_domain a..example.com a..example.com 400
exact_domain a b.example.com, a\tb..., a\x00b... as written (NUL: 500 from PostgreSQL) 400
exact_domain com / domain_suffix *.com com 400, a TLD-wide suffix is out of scope
exact_domain 192.0.2.1, 2001:db8::1, 127.1 as written 400 (use exact_ip / cidr)
exact_domain _bücher.example.com _bücher.example.com (Unicode) 400
exact_domain https://example.com, *.example.com, example.com:443 as written 400
fuzzy_domain a..b, .gov .cn as written 400
fuzzy_domain a。。example.com, example.com.. a..example.com, example.com.. 400

The check is a new function, db.ValidInterceptDomain, called at storage time by validateAssetInterceptRuleReq (and db.ValidDomainSuffixPattern / db.ValidFuzzyDomainPattern). CanonicalInterceptDomain itself is unchanged and stays lenient for matching. For an accepted pattern, the stored value is always exactly what CanonicalInterceptDomain returned before (the tests assert it).

For exact_domain and domain_suffix (after the *. / . prefix of a suffix is dropped), it refuses:

  • whitespace or a control character inside the name;
  • an empty label: leading dot, doubled dot, or more than one trailing dot;
  • a single label;
  • an IP literal, or a name whose last label is all digits (127.1, which the intercept layer reads as an address in a URL host);
  • a URL or wildcard character (*, /, :): exact_domain used to accept https://example.com, *.example.com and example.com:443, all of which matched no target;
  • a name with non-ASCII characters, or with an xn-- label, that the IDNA profile refuses.

fuzzy_domain is a substring, so leading/trailing dots and single labels stay valid (.gov, .gov.cn, example, the built-in rules); it refuses whitespace, control characters and a doubled dot, including the ideographic and fullwidth dots once canonicalisation turns them into ..

Decisions

Trailing dots: refuse, not normalize. One trailing dot is the DNS root and stays accepted (Example.COM. is still stored as example.com). A second one is an empty label, not a spelling of the same name; resolvers refuse it, and intercept.normalizeHost drops only one dot. Normalizing every trailing dot would hide a typo and add a spelling rule that the target side does not have; refusing keeps one rule ("no empty label") for the leading, inner and trailing cases.

TLD-wide suffix rules: refused. A single label is meaningless as an exact_domain, and as a domain_suffix it covers a whole TLD. Both are refused, so a domain_suffix rule names an organisation or a site, not a public suffix. fuzzy_domain remains the substring escape hatch, with its false positives documented.

URL and wildcard characters: refused. exact_domain and domain_suffix take a host name. Accepting https://example.com or example.com:443 stored a rule that could never match, and hid a copy-paste mistake behind a silent no-op.

IDN profile: aligned on idna.Lookup, and the underscore limit documented. intercept/target.go used idna.New(idna.MapForLookup(), idna.BidiRule()), with a comment saying it avoided the strict rules that refuse an underscore label. In the pinned golang.org/x/net v0.62.0, MapForLookup() sets StrictDomainName(true) (STD3) and ValidateLabels(true), so that profile has the same options as idna.Lookup (transitionalLookup is false). A probe over 37 names (underscore, uppercase, fullwidth, Unicode, punycode, invalid punycode, hyphen rules, ZWJ, bidi, IPs, wildcards) gave the same output and the same refusals for both profiles. target.go now uses idna.Lookup, the profile the rule side, db/company_scope.go and egress/egress.go already use, so they cannot drift if x/net changes one of them; the wrong comment is fixed, and the comments on both sides now say how underscores are handled.

Behaviour that follows, unchanged by this PR except where noted:

  • Underscore: the profile refuses a label with _. An ASCII name is then compared lower-cased as written on both sides (_DMARC.Example.com and _dmarc.example.com meet). Lower-casing is all the profile does to ASCII, so this is not ambiguous, and such patterns stay accepted. The same goes for the hyphen-rule refusals (ab--c, -a).
  • Uppercase and fullwidth: mapped by the profile (EXAMPLE.com -> example.com), same as before.
  • Unicode: mapped to punycode (bücher.example -> xn--bcher-kva.example), same as before. Changed: a non-ASCII name that the profile refuses (for example _bücher.example.com) was stored as Unicode, while the same target written in punycode stays punycode, so the rule missed it. It has no single canonical spelling and is now refused.
  • Punycode: a valid xn-- label is kept, as before. Changed: an xn-- label that the profile refuses (invalid punycode, or the punycode of an underscore label) is now refused, for the same reason.

Callers checked

  • db/asset_intercept_match.go: matchOne (exact_domain, domain_suffix via DomainSuffixPattern, fuzzy_domain) calls CanonicalInterceptDomain on both sides. Unchanged, still lenient.
  • server/asset_intercept.go validateAssetInterceptRuleReq: the only write-side caller. It is used by POST/PUT /api/asset-intercept/rules, by server/task_intercept.go (validateTaskInterceptRuleReq: POST/PUT /api/tasks/{id}/intercept-rules, and buildTaskInterceptRules for task creation in server/server.go and task templates in server/task_templates.go). All of them answer 400 before any database write.
  • intercept/target.go (normalizeHost) and intercept/target_gate.go (blockRule, URL rules): target side, matching only. The block-only target gate is not touched.
  • agent/tools_insert.go: reads and matches rules (ListAssetInterceptRules, TaskInterceptRulesSplit), never writes them. No agent tool creates asset interception rules.
  • server/orchestration.go: a spawned child task copies the stored block rules of the calling task as they are. They are not checked again, so an old malformed row is copied rather than dropped (dropping a block rule would fail open).
  • server/sandbox.go: reads stored exact_domain / domain_suffix allow rules as scope domains. A single-label domain_suffix allow rule (com) can no longer be created.

Tests

Table-driven:

  • db/asset_intercept_domain_valid_test.go: refusals (32 cases, including https://example.com, example.com:443, foo*.example.com, a。。example.com, example.com..), a regression table of 24 valid domains whose output must equal CanonicalInterceptDomain (subdomains, uppercase, single trailing dot, Unicode, punycode, fullwidth, underscore, hyphen-rule names), domain_suffix and fuzzy_domain tables.
  • db/asset_intercept_canonical_test.go: CanonicalInterceptDomain output unchanged, and it is still lenient for malformed targets.
  • server/asset_intercept_validate_test.go: refusals on the API validator for the three kinds, valid domains unchanged, task rules (block and allow) and buildTaskInterceptRules.
  • server/asset_intercept_domain_pg_test.go (PostgreSQL): POST /api/asset-intercept/rules answers 400 for malformed patterns and stores nothing, a valid one is stored canonical, a malformed PUT is refused and leaves the row. The same file now also covers task rules on a real task: POST /api/tasks/{id}/intercept-rules with a valid domain returns 200 and stores the canonical form, a malformed PUT returns 400 and leaves the row, and a valid PUT returns 200 and canonicalises.
  • intercept/target_idn_test.go: TestTargetAndRuleCanonicalizationMatch compares NormalizeTarget with db.CanonicalInterceptDomain on 17 names (Unicode, fullwidth, punycode valid and invalid, underscores, hyphen rules, ZWSP); TestRuleAndTargetSpellingsMeet checks that a rule stored in any spelling of a valid name blocks the target written in any other spelling of it.

RED on the base, with the new tests that compile there (8f91643, the main tip when the branch was cut; the branch was then rebased on e38f58d, which changes no file this diff touches except one CHANGELOG line next to the new entry):

asset_intercept_validate_test.go:98: exact_domain "example.com.." was accepted as "example.com.", want an error
asset_intercept_validate_test.go:98: exact_domain "a..example.com" was accepted as "a..example.com", want an error
asset_intercept_validate_test.go:98: exact_domain "com" was accepted as "com", want an error
asset_intercept_validate_test.go:98: exact_domain "192.0.2.1" was accepted as "192.0.2.1", want an error
asset_intercept_validate_test.go:98: exact_domain "_bücher.example.com" was accepted as "_bücher.example.com", want an error
asset_intercept_validate_test.go:98: domain_suffix "*.com" was accepted as "com", want an error
asset_intercept_validate_test.go:147: task allow domain_suffix "com" was accepted as "com"
asset_intercept_domain_pg_test.go:75: POST asset rule exact_domain "ivx782.example.com..": 200 {..."pattern":"ivx782.example.com."...}
asset_intercept_domain_pg_test.go:84: 7 malformed rule(s) were stored
--- FAIL: TestValidateAssetInterceptRuleRefusesMalformedDomains
--- FAIL: TestValidateTaskInterceptRuleRefusesMalformedDomains
--- FAIL: TestAssetInterceptRuleAPIRefusesMalformedDomainsPG

The db validator tests do not build on the base (undefined: ValidInterceptDomain). Guards that already pass on the base and must keep passing: TestValidateAssetInterceptRuleKeepsValidDomains, TestCanonicalInterceptDomainUnchanged, TestRuleAndTargetSpellingsMeet, and the existing TestValidateAssetInterceptRuleCanonicalizes.

GREEN on the branch, on PostgreSQL 16 in CI and in the audit environment, with go1.26.11:

gofmt -l (changed files): empty
go build ./...: ok
go vet ./db ./intercept ./server: ok
go test ./db ./intercept ./server -count=1 -p 1 (with ARTEX_PG_DSN):
ok   github.com/Autumn-27/artex/db
ok   github.com/Autumn-27/artex/intercept
ok   github.com/Autumn-27/artex/server

The *AsRoot tests need real root and skip in a non-root environment; the diff touches no file or symbol related to execpriv, isolation, rundir or spill. No test was skipped in the suites above, and the PG test ran.

Not verified / limits

  • No migration: rows stored before this change keep their old value, including malformed ones. Task templates saved earlier are not checked again when a task is created from them, and a spawned child copies its caller's block rules as stored.
  • A last label like 0x1 (0x7f.0x1) is not recognized as an IPv4 form. Out of scope here.
  • No length check (253 / 63).
  • The web UI was not exercised; it receives the 400 and its message like any other validation error.

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.
…pt domains

A fuzzy_domain pattern written with ideographic or fullwidth dots became a
doubled dot only after canonicalisation, so "a。。example.com" was stored as
"a..example.com" and "example.com.." as "example.com..". Check the canonical
form as well.

ValidInterceptDomain now rejects "*", "/" and ":" too: exact_domain accepted
"https://example.com" and "*.example.com", which were stored and never matched.

Replace the tautological IDN test, which compared hostProfile with itself, with
a NormalizeTarget versus db.CanonicalInterceptDomain comparison, and cover task
rule create and update in the PostgreSQL API test.
@BaaRai
BaaRai marked this pull request as ready for review December 2, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant