Skip to content

Skip MQTT brokers whose login holds an unresolved substitution - #2981

Merged
J. Nick Koston (bdraco) merged 3 commits into
esphome:mainfrom
sebo-b:fix-mqtt-unresolved-login
Nov 5, 2026
Merged

J. Nick Koston (bdraco) merged 3 commits into
esphome:mainfrom
sebo-b:fix-mqtt-unresolved-login

Conversation

@sebo-b

@sebo-b Sebastian Baberowski (sebo-b) commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What does this implement/fix?

MQTT discovery skips a broker whose username or password still holds an unresolved substitution, the same way it already does for broker: (#1643). Before, the monitor logged in with the literal token and the broker rejected it every 5 s, forever.

A resolved block can't tell a !secret value from a literal (esphome's loader has already inlined the secret on the package path), so the check is on the token shape: the value contains ${, or is entirely one $name. A real password that merely contains $ (pa$sword from a secret, a literal pa$$word) still logs in on both the inline and the package path, as esphome compiles it. Known gap: a password that is exactly $word is treated as a token.

Tests: unresolved ${x} / $x / Jinja-style logins yield no broker; passwords containing $ are kept; a package-sourced login is judged like an inline one (test_seed_login_matches_the_main_path); the coordinator test for an unresolved host also covers the username.

Overlaps with #2965 in _broker_from_block and tests/test_mqtt_monitor.py; whichever lands second needs a small rebase. Happy to rebase this one.

Related issue or feature (if applicable):

Types of changes

  • Bugfix (non-breaking change which fixes an issue) — bugfix
  • New feature (non-breaking change which adds functionality) — new-feature
  • Enhancement to an existing feature — enhancement
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) — breaking-change
  • Refactor (no behaviour change) — refactor
  • Documentation only — docs
  • Maintenance / chore — maintenance
  • CI / workflow change — ci
  • Dependencies bump — dependencies

Frontend coordination

  • No frontend change needed
  • Companion frontend PR: esphome/device-builder-frontend#

Checklist

  • The code change is tested and works locally.
  • Pre-commit hooks pass (ruff, codespell, yaml/json/python checks).
  • Tests have been added or updated under tests/ where applicable.
  • components.index.json / definitions/components/*.json have not been hand-edited (regenerate via script/sync_components.py if a sync is needed).
  • Architecture-level changes are reflected in docs/ARCHITECTURE.md and/or docs/API.md.

Copilot AI balanced review requested due to automatic review settings October 26, 2026 22:11
@github-actions github-actions Bot added the bugfix Bug fix label Oct 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused shared guard implements the stated token policy with regression coverage for both extraction paths and no blocking issues identified.

No open issues 🎉

What changed in this PR

This PR extends MQTT discovery’s unresolved-substitution guard to usernames and passwords, preventing repeated rejected logins with literal substitution tokens.

Changes:

  • Skip brokers with unresolved ${…} or whole-value $name credentials.
  • Preserve passwords that merely contain dollar signs.
  • Add regression coverage for inline credentials, package-sourced credentials, and coordinator behavior.
File Description
tests/​test_mqtt_monitor.py Tests unresolved login rejection and dollar-containing password preservation.
tests/​test_mqtt_coordinator_extract.py Tests consistent credential handling for package-sourced MQTT configuration.
esphome_device_builder/​controllers/​_device_mqtt_coordinator.py Adds credential-token detection to shared broker extraction.

Review effort: Balanced

@codspeed

codspeed Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 28 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing sebo-b:fix-mqtt-unresolved-login (c4110a9) with main (4f11f0a)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (4f11f0a) to head (c4110a9).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2981   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files         300      300           
  Lines       26480    26484    +4     
=======================================
+ Hits        26419    26423    +4     
  Misses         61       61           
Flag Coverage Δ
py3.12 99.73% <100.00%> (+<0.01%) ⬆️
py3.14 99.62% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ce_builder/controllers/_device_mqtt_coordinator.py 99.03% <100.00%> (+<0.01%) ⬆️
...ome_device_builder/helpers/device_yaml/__init__.py 100.00% <ø> (ø)
...ome_device_builder/helpers/device_yaml/_parsing.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@esphbot

ESPHome Bot (esphbot) commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Previous review — superseded by a newer review below.

@esphbot ESPHome Bot (esphbot) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tip

No blocking issues found — ready to merge.

@bdraco

Copy link
Copy Markdown
Member

Simplify pass in 21fa4a1:

  • The strict regex now lives beside _UNRESOLVED_SUBSTITUTION_RE in helpers/device_yaml/_parsing.py as _UNRESOLVED_WHOLE_TOKEN_RE, exported the same way. _SUBSTITUTION_RE.fullmatch isn't equivalent (it accepts $1x), so it stays a separate regex.
  • _broker_from_block adds a _LOGIN_FIELDS constant and binds username/password once, with an isinstance(v, str) guard like the host check's, and reuses them when building the config.
  • The parse_mqtt_block docstring now says an unresolved login also returns None.
  • Bot suggestion: the unresolved-broker warning now lists "unresolved substitution" among its causes.
  • Tests: the unresolved-login parametrize is down to ${x} / $x (the Jinja form hits the same branch). The secret dollar case is folded into test_parse_mqtt_block_resolves_secrets. The seed-path test asserts whether a monitor started. The coordinator test comment no longer says DNS, and the username row stays so warn-once stays pinned for the login.

Skipped: refusing a declared login whose secret resolves to empty. An empty password can be a legitimate config, so that would change behavior.

Note: #2965 turns the host check in this same function into a per-field loop. Whichever PR lands second should fold _LOGIN_FIELDS into that loop, with the whole-token regex for the login fields.

ESPHome Bot (@esphbot) review

# Conflicts:
#	esphome_device_builder/controllers/_device_mqtt_coordinator.py
#	tests/test_mqtt_coordinator_extract.py
#	tests/test_mqtt_monitor.py
@bdraco

Copy link
Copy Markdown
Member

Merged main (post #2965) in c4110a9. The login check now rides #2965's per-field loop: _LOGIN_FIELDS joins the endpoint fields with the whole-token regex, and there is no declared-but-empty refusal for logins, since an empty password is valid. The warning text keeps 'unresolved substitution'.

ESPHome Bot (@esphbot) review

@esphbot

ESPHome Bot (esphbot) commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

PR Review — Skip MQTT brokers whose login holds an unresolved substitution

Ready to merge. The login guard is correct after the rebase onto #2965, and nothing is outstanding.

What the PR does well:

  • Login check shares the endpoint loop. _LOGIN_FIELDS now goes through Adds the ability for to connect to an MQTT broker via WS/WSS #2965's per-field loop in _broker_from_block (_device_mqtt_coordinator.py:579-589). Login fields use _UNRESOLVED_WHOLE_TOKEN_RE and skip the declared-but-empty refusal, so an empty password still connects. This matches what bdraco described in the merge note.
  • Both login fields go through substitution resolution. username and password are in _BROKER_FIELDS (:63-69), so both pass through secret and substitution resolution before the token check. Values are only ever str | None, so str(x) if x else None gives the same result as the old or None form.
  • The regex is narrow and sits beside its sibling in _parsing.py. It rejects ${…} anywhere and a value that is exactly one $name. It keeps pa$sword and pa$$word. The one known false positive (a password that is exactly $word) is stated in the PR description.
  • Both config paths are tested. Inline logins are tested in test_mqtt_monitor.py. Package-sourced logins are tested in test_seed_login_matches_the_main_path. The coordinator test is parametrized, so the warn-once log is checked for an unresolved username as well as an unresolved host.
  • The warning text is updated. The unresolved-broker warning now lists "unresolved substitution" among its causes.

No outstanding issues.



Checklist

  • No hardcoded secrets
  • Input validation at boundaries
  • New branches covered by tests
  • Diff matches PR description

Automated review by Kōan (Claude) HEAD=c4110a9 17s

@esphbot ESPHome Bot (esphbot) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tip

No blocking issues found — ready to merge.

@bdraco
J. Nick Koston (bdraco) merged commit bf2b8cf into esphome:main Nov 5, 2026
23 checks passed
@esphbot

Copy link
Copy Markdown
Contributor

J. Nick Koston (@bdraco): Merged main (post #2965) in c4110a9. The login check now rides #2965's per-field loop: `_LOGIN_FIE...

Rebase verified. Login fields now go through the shared loop (_device_mqtt_coordinator.py:579-589). They use the whole-token regex and skip the empty-value refusal. No further changes needed.

@esphbot ESPHome Bot (esphbot) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tip

No blocking issues found — ready to merge.

@github-actions github-actions Bot locked and limited conversation to collaborators Nov 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bugfix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] MQTT discovery logs in with an unresolved ${…} username/password and retries forever

4 participants