Skip to content

Commit 56133d3

Browse files
TMHSDigitalclaude
andcommitted
Refuse CNAME targets that are not host names
The CNAME lookup reads the answer itself and took the alias target's bytes as they came. A target with characters a host name cannot have is now refused with the error Go's own lookups give for such an answer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ccbc40e commit 56133d3

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎internal/dns/cname.go‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,30 @@ func lookupCNAME(ctx context.Context, resolver *net.Resolver, name string) (stri
4141
if err != nil {
4242
return "", err
4343
}
44-
return canonicalName(msg, name), nil
44+
target := canonicalName(msg, name)
45+
if !validTarget(target) {
46+
// Go's own lookups refuse such an answer with this error.
47+
return "", &net.DNSError{Err: "DNS response contained records which contain invalid names", Name: name}
48+
}
49+
return target, nil
50+
}
51+
52+
// validTarget reports whether name, which may end in a dot, is a host name
53+
// in presentation form: labels of 1 to 63 letters, digits, hyphens or
54+
// underscores, 253 bytes at most. dnsmessage hands back a name's raw bytes,
55+
// so a hostile answer could otherwise carry control bytes into results.
56+
func validTarget(name string) bool {
57+
name = strings.TrimSuffix(name, ".")
58+
if name == "" || len(name) > 253 || !validLabels(name) {
59+
return false
60+
}
61+
for i := 0; i < len(name); i++ {
62+
c := name[i]
63+
if c != '.' && c != '-' && c != '_' && (c < '0' || c > '9') && (c|0x20 < 'a' || c|0x20 > 'z') {
64+
return false
65+
}
66+
}
67+
return true
4568
}
4669

4770
// rawLookup sends one qtype query for name over resolver.Dial, asking again

‎internal/dns/cname_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,37 @@ func TestCNAMEOneQuery(t *testing.T) {
6767
}
6868
}
6969

70+
// TestCNAMEInvalidTarget: an alias target that is not a host name (control
71+
// bytes, spaces, markup) is refused, as Go's own lookups refuse it, and
72+
// never reaches the results.
73+
func TestCNAMEInvalidTarget(t *testing.T) {
74+
targets := map[string]string{
75+
"esc.example.com": "x\x1b[2J.example.net",
76+
"space.example.com": "a b.example.net",
77+
"tag.example.com": "<b>.example.net",
78+
"ok.example.com": "_svc.edge-1.example.net",
79+
}
80+
srv := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply {
81+
if target, ok := targets[q.Name]; ok {
82+
return dnstest.Reply{CNAME: target}
83+
}
84+
return dnstest.NXDomain
85+
})
86+
l := Lookup{Resolver: NewResolver(time.Second, srv.Addr), Timeout: time.Second, Attempts: 1, Types: []string{"CNAME"}}
87+
for name, target := range targets {
88+
recs, outcome := l.Resolve(context.Background(), name)
89+
if name == "ok.example.com" {
90+
if outcome != OutcomeFound || len(recs) != 1 || recs[0].Value != target {
91+
t.Errorf("%s: %v %v, want CNAME %s", name, outcome, recs, target)
92+
}
93+
continue
94+
}
95+
if outcome == OutcomeFound || len(recs) != 0 {
96+
t.Errorf("%s: %v %q, want the invalid target refused", name, outcome, recs)
97+
}
98+
}
99+
}
100+
70101
// TestCNAMETimeout: a server that never answers is a timeout, not a
71102
// negative, and a cancelled scan is cancelled.
72103
func TestCNAMETimeout(t *testing.T) {

0 commit comments

Comments
 (0)