Skip to content

DNS: reject a label over 63 bytes before the CNAME query - #350

Closed
bfs2021 wants to merge 1 commit into
TMHSDigital:mainfrom
bfs2021:fix/cname-label-length
Closed

bfs2021 wants to merge 1 commit into
TMHSDigital:mainfrom
bfs2021:fix/cname-label-length

Conversation

@bfs2021

@bfs2021 bfs2021 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Problem

Fixes #344. A name with a label over 63 bytes is meant to be skipped as not found without any query, but the guard in lookupCNAME only caught names over the 255 bytes dnsmessage.NewName rejects. A 64-byte label passed it, so the lookup dialed, took a rate slot and a query, then failed building the question and was counted as OutcomeOther — an infrastructure failure — instead of not-found.

Root cause

dnsmessage.NewName validates the 255-byte total length of a name, not the 63-byte limit of a single label. The invalid-name branch in internal/dns/cname.go returned the not-found error only when NewName failed, so the over-long-label case fell through to cnameExchange, which dialed before cnameQuery refused to build the message.

Fix

lookupCNAME now also runs longLabel on the name before dialing: any label over 63 bytes returns the same not-found *net.DNSError the other invalid names get, so Classify reports OutcomeNXDomain and no connection, write or query happens. The check splits the name on dots, so a trailing dot and the existing name forms are unaffected.

Testing

  • go test ./internal/dns/ -run TestCNAMEInvalidNameSendsNoQuery -v — the acceptance case for CNAME lookup of a 64-byte label dials, then reports an error, not not-found #344 is now pinned in TestCNAMEInvalidNameSendsNoQuery: a 64-byte label returns not-found, Classify gives OutcomeNXDomain, and the connection, write and query counters stay at 0. Without the source change the test fails with 1 connections, want 0, so it guards the branch.
  • go test ./internal/dns/ and go test ./... — pass.
  • gofmt -l and go vet ./internal/dns/ — clean.

dnsmessage.NewName only checks the 255-byte total, so a name with one
64-byte label passed the invalid-name branch, took a connection and a
query slot from the rate budget, and only failed when the message was
built. Classify then reported OutcomeOther instead of not-found, which
nudged the reliability guard for a name that should simply be skipped.

The branch now splits the name and rejects any label over 63 bytes, so
the lookup returns a not-found *net.DNSError without dialing. The
existing test pinned only the over-255-byte case; it now pins this one
too, including the connection and wire counters.
@TMHSDigital

Copy link
Copy Markdown
Owner

Thanks for this, and for the clear write-up. Your diagnosis is right: dnsmessage.NewName only bounds the 255-byte total, so a 64-byte label dialed before it failed.

#344 was fixed on main in 9fc7b21 while this PR was open, so this branch now conflicts. That fix does the same thing. It adds a validLabels check before the dial, which also rejects empty labels (a..example.com). Its test asserts what yours does: not-found, OutcomeNXDomain, and zero connections, writes and queries, for both the long and the empty label.

Main already has everything this PR adds, so I'm closing it rather than asking for a rebase. Thanks again. Contributions like this are very welcome.

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.

CNAME lookup of a 64-byte label dials, then reports an error, not not-found

2 participants