Ignore malformed mDNS service names in browser callbacks - #2621
Conversation
|
@esphbot review |
Merging this PR will not alter performance
Comparing Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2621 +/- ##
=======================================
Coverage 99.72% 99.72%
=======================================
Files 275 275
Lines 23364 23384 +20
=======================================
+ Hits 23300 23320 +20
Misses 64 64
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR hardens the project’s zeroconf/mDNS browsing callbacks against malformed service instance names that can cause ServiceInfo/AsyncServiceInfo construction to raise, preventing log spam and callback crashes when third-party devices advertise invalid _http._tcp (and related) records on the LAN.
Changes:
- Add a shared
valid_mdns_service_name()helper and apply it as an early guard in mDNS browser callbacks (device state monitor, remote-build peer discovery) and thediscoverCLI. - Update/extend tests to assert malformed instance names are dropped (no state created, no CLI output) rather than raising.
- Add a focused test module covering the shared browser dispatch guard behavior.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
esphome_device_builder/helpers/hostname.py |
Introduces valid_mdns_service_name() utility for validating raw wire service names before constructing ServiceInfo. |
esphome_device_builder/controllers/_device_state_monitor/mdns.py |
Adds early validation guard in the shared browser dispatch to skip malformed names before invoking handlers. |
esphome_device_builder/controllers/remote_build/discovery.py |
Adds early validation guard in the remote-build peer browser callback to ignore malformed names. |
esphome_device_builder/discover.py |
Adds early validation guard in the CLI browse callback so malformed names produce no output and don’t raise. |
tests/test_remote_build_controller.py |
Adds regression coverage ensuring malformed remote-build service names are ignored without mutating peer state. |
tests/test_discover.py |
Updates the hostile-name test to assert the entire row is dropped when the instance name is invalid. |
tests/test_mdns_malformed_names.py |
New test module validating valid_mdns_service_name() and asserting dispatch drops malformed names before any handler runs. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
PR Review — Ignore malformed mDNS service names in browser callbacksCorrect, well-placed fix — the guard sits on all three browser registrations and nothing is left uncovered. Merge-ready; two non-blocking follow-ups. Specific things done well:
What's worth a follow-up:
Checklist
Silent Failure Analysis
🟠 **1. HIGH** — guard validates the wrong predicate (crash class still reachable)
|
esphbot
left a comment
There was a problem hiding this comment.
Tip
No blocking issues found — ready to merge.
What does this implement/fix?
A third party device on the LAN (an IRIS alarm module) advertises a
_http._tcpservice whose instance name contains ASCII control characters. The zeroconf browser hands us the raw wire name, butServiceInforejects it withBadTypeInNameException, so every announce raised inside our browser callback and flooded the log with tracebacks.Adds
valid_mdns_service_name, a cached wrapper over zeroconf's ownservice_type_namevalidation, and checks it at the top of each browser callback: the device state monitor's shared dispatch (which also covers the upstreamDashboardImportDiscoverywe forward to), the remote build peer browser, and thediscoverCLI. A name that fails validation is dropped with a debug log; it never created any state, soRemovedis skipped too.Related issue or feature (if applicable):
Types of changes
bugfixnew-featureenhancementbreaking-changerefactordocsmaintenancecidependenciesFrontend coordination
Checklist
ruff,codespell, yaml/json/python checks).tests/where applicable.components.index.json/definitions/components/*.jsonhave not been hand-edited (regenerate viascript/sync_components.pyif a sync is needed).docs/ARCHITECTURE.mdand/ordocs/API.md.