-
Notifications
You must be signed in to change notification settings - Fork 100
Add support for 0x80 prefix compression in DNS domain name encoding #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: clairernovotny <1427284+clairernovotny@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for non-standard 0x80 prefix compression in DNS domain name encoding to handle malformed packets from customer systems. While DNS RFCs only define 0xc0 (suffix compression) and normal labels, some systems use 0x80 to indicate prefix compression where the pointer references the beginning of a domain name rather than a complete replacement.
Key changes:
- Added detection and handling for 0x80 compression pattern in
ReadDomainName() - Implemented prefix+suffix combination logic to properly decode these non-standard domain names
- Maintained backward compatibility with existing 0xc0 compression and normal label parsing
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| else | ||
| { | ||
| // Combine prefix and suffix, removing the trailing dot from prefix | ||
| result = prefix.TrimEnd('.') + "." + suffix; |
Copilot
AI
Oct 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String concatenation with dots could produce malformed domain names if either prefix or suffix contains internal dots or empty segments. Consider validating that both prefix and suffix are well-formed domain names before combining, or use a more robust joining method that handles edge cases like empty labels between dots.
| if (bytes.Count > 0) | ||
| { | ||
| return Encoding.UTF8.GetString(bytes.ToArray(), 0, bytes.Count) + result; | ||
| } |
Copilot
AI
Oct 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] This logic for prepending accumulated bytes is duplicated from the 0xc0 compression block above (lines 85-88). Consider extracting this pattern into a helper method to reduce code duplication and improve maintainability.
| var prefix = newRecordReader.ReadDomainName(); | ||
|
|
||
| // Continue reading suffix at current position | ||
| var suffix = ReadDomainName(); |
Copilot
AI
Oct 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recursive call to ReadDomainName() could lead to infinite recursion or stack overflow if the DNS packet contains circular references with 0x80 pointers. Consider adding depth tracking or visited offset checking to prevent infinite loops, similar to how the 0xc0 compression should be protected.
Problem
Some DNS/mDNS systems encode domain names using a non-standard compression pattern with the 0x80 prefix (binary
10xxxxxx) instead of the standard 0xc0 pattern (binary11xxxxxx). According to DNS RFCs, the upper 2 bits of a domain name length byte should be:00: Normal label length (0-63 bytes)11: Compression pointer (standard suffix compression)10or01: Reserved for future useHowever, customer systems have been observed using the
10pattern (0x80) for a variant of compression that references a prefix rather than a complete replacement, causing decoding failures in Zeroconf.Example from Issue
In the reported packet, a PTR record at offset 0x74 contains:
0x80: Prefix compression marker (binary10000000)0x0c: Pointer to offset 12 where_services._dns-sd._udp.local.is encoded0x00: Terminator indicating no suffixThis should decode to
_services._dns-sd._udp.local.but was previously failing.Solution
Modified
Zeroconf/Dns/RecordReader.csto add support for the 0x80 prefix compression pattern in theReadDomainName()method:Changes
else ifbranch to handle(length & 0xc0) == 0x80patternTesting
Validated with comprehensive unit tests covering:
All tests pass, confirming the fix correctly handles the 0x80 pattern while maintaining full backward compatibility with existing behavior.
Fixes issue regarding "Weird domain name encoding - 0x80 prefix instead of 0xc0"
Original prompt
Fixes #226
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.