Skip to content

Commit 9fc7b21

Browse files
TMHSDigitalclaude
andcommitted
Refuse CNAME lookups of names with invalid labels (#344)
dnsmessage.NewName checks only the 255-byte total, so a name with an empty label or one over 63 bytes got past the invalid-name branch of lookupCNAME, dialled, could take a rate slot and count a query, then failed building the question and was reported as OutcomeOther. lookupCNAME now checks every label's length before dialling and returns the same not-found error as for an over-long name. TestCNAMEInvalidNameSendsNoQuery pins the outcome and that no connection is made. Fixes #344 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 80cf0d3 commit 9fc7b21

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6767
- Less memory on large wordlists and busy scans: `-sources`, `-ct`, `-axfr` and `-permute` check their names against the wordlist without building a set as large as it (about 200 MB per feature and target on a 5M-line list), a `-simulate-zone` lab scan no longer keeps a count per queried name (about 170 MB on a 1M-line list), and a `CNAME` lookup reads a UDP answer into a buffer sized for the 1232-byte EDNS payload it advertises instead of 64 KB. Results are unchanged (#321).
6868

6969
### Fixed
70+
- A `CNAME` lookup of a name with an empty label or one over 63 bytes is reported as not found without a query, as a name over 255 bytes already was, instead of dialling, counting a query and reporting a failure that the reliability guard counts (#344).
7071
- Under `-no-abort`, the reliability guard's warning is printed as `Warning: ...` (level `warn` with `-log-format json`) instead of `Error: warning: ...`, and a target that finished no longer carries it as its `error` in the `-stats` report next to `"status": "ok"`. The TUI shows it as a warning too (#308).
7172
- The resolver preflight and wildcard probes made before a target's first name no longer use up `-max-duration`. Under a low `-rate` they could take all of a short budget, so the run tested no names and each `-resume` probed again and made no progress. A budget that runs out before any name was tested now says so (#340).
7273
- Release archives and the container image carry the licence notices of the Go standard library and every module linked into the binary, in a `THIRD_PARTY_LICENSES` file generated at release time by `tools/licenses` from the module cache. The image also carries the project's `LICENSE`; both are in `/usr/share/doc/subenum/` (#326).

‎internal/dns/cname.go‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ func lookupCNAME(ctx context.Context, resolver *net.Resolver, name string) (stri
3030
if resolver == nil || resolver.Dial == nil {
3131
return resolver.LookupCNAME(ctx, name)
3232
}
33+
// NewName checks only the 255-byte total; a label of 0 or over 63 bytes
34+
// fails later, when the query is built, after a dial, a rate slot and a
35+
// counted query (#344). No such name can exist, so neither is asked.
3336
qname, err := dnsmessage.NewName(name)
34-
if err != nil {
37+
if err != nil || !validLabels(name) {
3538
return "", &net.DNSError{Err: "no such host", Name: name, IsNotFound: true}
3639
}
3740
msg, err := cnameExchange(ctx, resolver, "udp", qname)
@@ -156,6 +159,17 @@ const ednsPayload = 1232
156159

157160
// cnameQuery builds a recursive CNAME query with an EDNS(0) OPT record
158161
// advertising 1232-byte UDP answers, as Go's resolver sends.
162+
// validLabels reports whether every label of name, which may end in a dot,
163+
// is 1 to 63 bytes long.
164+
func validLabels(name string) bool {
165+
for label := range strings.SplitSeq(strings.TrimSuffix(name, "."), ".") {
166+
if len(label) == 0 || len(label) > 63 {
167+
return false
168+
}
169+
}
170+
return true
171+
}
172+
159173
func cnameQuery(id uint16, qname dnsmessage.Name) ([]byte, error) {
160174
b := dnsmessage.NewBuilder(nil, dnsmessage.Header{ID: id, RecursionDesired: true})
161175
b.EnableCompression()

‎internal/dns/cname_test.go‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,16 @@ func TestCNAMEInvalidNameSendsNoQuery(t *testing.T) {
394394
t.Errorf("name over 255 bytes: %d connections, want 0", n)
395395
}
396396

397-
// A label over 63 bytes passes NewName and fails when the query is built.
398-
label := strings.Repeat("a", 64) + ".example.com."
399-
if got, err := lookupCNAME(contextWithTimeout(t, 5*time.Second), r, label); err == nil {
400-
t.Errorf("label over 63 bytes: lookupCNAME = %q, want an error", got)
397+
// A label over 63 bytes, or an empty one, passes NewName; it is not
398+
// found either, without a connection (#344).
399+
for _, name := range []string{strings.Repeat("a", 64) + ".example.com.", "a..example.com."} {
400+
_, err := lookupCNAME(contextWithTimeout(t, 5*time.Second), r, name)
401+
if !errors.As(err, &de) || !de.IsNotFound || Classify(err) != OutcomeNXDomain {
402+
t.Errorf("%s: %v, want a not-found error", name, err)
403+
}
404+
}
405+
if n := dials.Load(); n != 0 {
406+
t.Errorf("invalid labels: %d connections, want 0", n)
401407
}
402408
if w, q := writes.Load(), queries.Load(); w != 0 || q != 0 {
403409
t.Errorf("invalid names: %d writes and %d queries on the wire, want 0", w, q)

0 commit comments

Comments
 (0)