Skip to content

fix: escape dots and backslashes in labels read from packets - #540

Merged
keepsimple1 merged 1 commit into
keepsimple1:mainfrom
meliodaff:fix/536-escape-labels-on-read
Nov 10, 2026
Merged

keepsimple1 merged 1 commit into
keepsimple1:mainfrom
meliodaff:fix/536-escape-labels-on-read

Conversation

@meliodaff

Copy link
Copy Markdown
Contributor

Fixes #536

write_name and ServiceInfo::new treat \. as a dot inside a label (RFC 6763 section 4.3), but read_labels appended each label unescaped. A question for an instance name with dots therefore never matched the registered service, and SRV, TXT and ANY
questions for it went unanswered.

read_labels now escapes . and \ inside a label, so a name read from a packet uses the same form that write_name parses. The answer owner (add_answer_of_service reuses the question's name) is then written as the original single label too.

Two things to check in review

  • Behavior change: a browsed fullname now keeps its escapes, so it equals the registered fullname. An app that read L.A.R.A Hub._lara._tcp.local. from a browse event now gets L\.A\.R\.A Hub._lara._tcp.local.. test_rfc6763_utf8_network_integration expected the old form, so I changed it.
  • Name limit: the check against MAX_NAME_BYTES used name.len(), which escaping makes longer than the name on the wire. A legal name with many dots would have been rejected and its whole packet dropped, so read_labels and follow_pointer now carry a
    wire_len count. The limit itself is unchanged.
    Tests
  • New test_incoming_label_with_dot_is_escaped: aara._tcp.local (one label with dots) reads back asL.A.R.A Hub._lara._tcp.local.`.
  • New test_incoming_name_limit_ignores_escapes: ytes once escaped still parses.
  • New test_incoming_name_over_limit_is_rejected: a name above 255 bytes on the wire is still rejected.
  • test_incoming_name_with_merged_labels_does_not_panic (Panic in write_utf8 (assert s.len() < 64) kills mDNS_daemon thread permanently on a ≥64-byte DNS label from the LAN #483): with the backslash escaped, the two labels no longer merge, so the
    test now checks that the question is re-emitted by
  • test_rfc6763_utf8_network_integration: checks that the browsed fullname equals the registered fullname exactly.

cargo test passes on Windows 11 against main ((mdns_test), 8 shutdown, 3 address-parsing and 4
doc tests. --all-features passes too (90 unit). cargo fmt --check, both CI clippy commands and cargo doc are clean.

Checked live (0.21.6 with the escaping change, wire_len count): a QU SRV query for the single
label L.A.R.A Hub is answered with the SRV and A records. Without the change there was no response.

write_name and ServiceInfo::new treat "\." as a dot inside a label
(RFC 6763 section 4.3), but read_labels appended each label unescaped. A
question for an instance name with dots never matched the registered
service, so SRV, TXT and ANY questions for it went unanswered.

read_labels now escapes "." and "\" inside a label. A browsed fullname
therefore keeps its escapes and equals the registered fullname.

Escaping makes the text longer than the name on the wire, so the
MAX_NAME_BYTES check now counts wire bytes in a separate wire_len. Without
that, a legal name with many dots was rejected and its packet dropped.

Fixes keepsimple1#536

@keepsimple1 keepsimple1 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR! The fix itself looks good.

There's one gap this fix exposes. Before the fix, a peer's record name never matched our escaped probe name for an instance with dots, so conflict detection never fired for such an instance. Now it does, and the conflict path renames the record with name_change (and hostname_change for A/AAAA). Both split the name on every ., including escaped ones:

name_change(r"L\.A\.R\.A Hub._lara._tcp.local.")
  split('.') → ["L\", "A\", "R\", "A Hub", "_lara", "_tcp", "local", ""]
  → r"L\ (2).A\.R\.A Hub._lara._tcp.local."
  on the wire: ["L\ (2)", "A.R.A Hub", "_lara", "_tcp", "local"]

The expected result is L\.A\.R\.A Hub (2)._lara._tcp.local..

Could you include a fix and a test in this PR? Or, if you would like to have it fixed in a separate PR, then open a new issue to track that.

@keepsimple1

Copy link
Copy Markdown
Owner

Could you include a fix and a test in this PR? Or, if you would like to have it fixed in a separate PR, then open a new issue to track that.

To move forward, I've opened #546 to track the remaining gap. I will merge this PR for now.

@keepsimple1
keepsimple1 merged commit 8eb02e5 into keepsimple1:main Nov 10, 2026
6 checks passed
keepsimple1 added a commit that referenced this pull request Nov 16, 2026
Since #540, a fullname read from the network keeps its RFC 6763 escapes,
e.g. "L\.A\.R\.A Hub._lara._tcp.local.". Add a public
instance_name_unescaped() that returns the first label of a fullname
unescaped ("L.A.R.A Hub").

Co-Authored-By: Claude Opus 5.5 <[email protected]>
keepsimple1 added a commit that referenced this pull request Nov 17, 2026
…552)

Since #540, a fullname read from the network keeps its RFC 6763 escapes, e.g. "L\.A\.R\.A Hub._lara._tcp.local.". Add a public instance_name_unescaped() that returns the first label of a fullname unescaped ("L.A.R.A Hub").
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.

Queries for an instance name with dots get no answer (names read from packets are not escaped)

2 participants