Skip to content

Add MD5 hash generate to the firmware for display and download - #2969

Merged
J. Nick Koston (bdraco) merged 17 commits into
esphome:mainfrom
dougiteixeira:md5_firmware
Nov 5, 2026
Merged

J. Nick Koston (bdraco) merged 17 commits into
esphome:mainfrom
dougiteixeira:md5_firmware

Conversation

@dougiteixeira

@dougiteixeira Douglas (dougiteixeira) commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

What does this implement/fix?

Related issue or feature (if applicable):
Add MD5 hash generate for the firmware to allow for display and download—very useful for updates via http_request.

esphome/device-builder-frontend#2046

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

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.

@github-actions github-actions Bot added the new-feature New feature label Oct 24, 2026
@dougiteixeira Douglas (dougiteixeira) changed the title Md5 firmware Add MD5 hash generation to the firmware for display and download Oct 24, 2026
@codspeed

codspeed Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 28 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing dougiteixeira:md5_firmware (0f96b49) with main (9217113)

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 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.77%. Comparing base (9217113) to head (0f96b49).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2969   +/-   ##
=======================================
  Coverage   99.76%   99.77%           
=======================================
  Files         300      300           
  Lines       26499    26522   +23     
=======================================
+ Hits        26438    26461   +23     
  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 Δ
...me_device_builder/controllers/firmware/download.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.

@dougiteixeira
Douglas (dougiteixeira) marked this pull request as ready for review October 24, 2026 17:36
@dougiteixeira Douglas (dougiteixeira) changed the title Add MD5 hash generation to the firmware for display and download Add MD5 hash generate to the firmware for display and download Oct 28, 2026
@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

Addressed in 67e47eb:

  1. docs/API.md now says the digest and .md5 row cover every artifact except firmware.elf (and .md5 files).
    2/3. Nothing is written to the build dir any more. get_binaries only hashes and lists the {file: "<x>.md5", type: "md5"} row; GET /api/firmware/download generates the md5sum line on request for a <artifact>.md5 that isn't on disk (a platform-provided .md5 still streams from disk). A platform-listed <file>.md5 is no longer listed twice. Tests cover the generated body, its Content-Disposition, no file on disk, and 404 for a missing or ELF base.

ESPHome Bot (@esphbot) review

@esphbot

Copy link
Copy Markdown
Contributor

J. Nick Koston (@bdraco): Addressed in 67e47eb:

Verified in 67e47eb. Docs match code now. No build-dir writes remain. Listed sidecars no longer duplicate. All three prior findings resolved.

@esphbot

ESPHome Bot (esphbot) commented Nov 5, 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

Follow-up in 1106286, which supersedes 67e47eb: get_binaries now writes nothing and lists no .md5 rows at all. Each artifact except firmware.elf carries only its md5 digest, hashed with hashlib.file_digest and cached per (path, size, mtime) so a rebuild rehashes. The HTTP download path is back to main's shape. docs/API.md states the ELF exception (findings 1, 2 and 3). The companion frontend (esphome/device-builder-frontend#2046) shows a per-artifact copy control for the digest.

ESPHome Bot (@esphbot) review

@esphbot

Copy link
Copy Markdown
Contributor

J. Nick Koston (@bdraco): Follow-up in 1106286, which supersedes 67e47eb: get_binaries now writes nothing and lists no `...

Verified in 1106286. No sidecar writes remain. Download path matches main. The (path, size, mtime_ns) cache key rehashes on rebuild. Prior findings resolved. One optional nit: guard the stat/hash, since md5 is optional.

@esphbot

ESPHome Bot (esphbot) commented Nov 5, 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.

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.

🟡 Changes recommended

An artifact removed during listing can trigger an uncaught FileNotFoundError, causing firmware/get_binaries to return INTERNAL_ERROR instead of the remaining available artifacts.

1 open issue
What changed in this PR

Adds firmware MD5 digests to the backend artifact listing, supporting checksum display and download in companion frontend PR #2046 for http_request updates.

Changes:

  • Computes and caches MD5 digests for downloadable artifacts, excluding ELF debug symbols.
  • Updates tests to verify digests and rehashing after an artifact changes.
  • Documents the optional md5 response field.
File Description
tests/​controllers/​firmware/​test_get_binaries.py Verifies artifact digests and cache invalidation after rebuilds.
esphome_device_builder/​controllers/​firmware/​download.py Adds cached MD5 calculation to artifact listings.
docs/​API.md Documents MD5 availability in firmware/get_binaries.

Review effort: Balanced

@bdraco

Copy link
Copy Markdown
Member

d745318: a stat, read or path-escape failure on one artifact now leaves that row without md5 (logged at debug) instead of failing get_binaries, with a test. The 67e47eb items (the stale on-disk .md5 MEDIUM and the double hash on token mint) went away in 1106286, which removed the generated .md5 download path. Main is merged in (f8f6864).

ESPHome Bot (@esphbot) review

@esphbot

Copy link
Copy Markdown
Contributor

J. Nick Koston (@bdraco): d745318: a stat, read or path-escape failure on one artifact now leaves that row without md5 (lo...

Verified at download.py:180-185. PathEscapeError subclasses ValueError, so the catch covers it. New test pins row kept without md5. Prior suggestion resolved.

@esphbot

ESPHome Bot (esphbot) commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Previous review — superseded by a newer review below.

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

No active review issues are reported for the artifact-listing changes that add download filenames and MD5 digests.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@esphbot

Copy link
Copy Markdown
Contributor

J. Nick Koston (@bdraco): 6179bab (MEDIUM, over-broad catch logged at debug): only a vanished file (FileNotFoundError) and...

Verified at download.py:184-187. FileNotFoundError and ValueError log at debug. Other OSErrors log a warning. Symlink case fixed too: download.py:291 names the file from the listed basename. Both findings resolved.

@esphbot

Copy link
Copy Markdown
Contributor

J. Nick Koston (@bdraco): a7caa0a adds a test for the quiet skip of a vanished artifact (FileNotFoundError stays at debug,...

Vanished-file test confirmed. It asserts no warning and that the row is kept without md5. Debug branch now covered.

@esphbot

ESPHome Bot (esphbot) commented Nov 5, 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

ac0cf7d answers Copilot's vanished-artifact thread: an artifact removed between the existence filter and the hash now drops out of the listing. An escaping or unreadable one keeps its row without md5. Tests cover both.

ESPHome Bot (@esphbot) review

@esphbot

ESPHome Bot (esphbot) commented Nov 5, 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.

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 firmware checksum metadata change has no active review issues.

No open issues 🎉

Review effort: Balanced

@bdraco

Copy link
Copy Markdown
Member

4561bb8:

  1. (MEDIUM) A PathEscapeError drops the artifact from the listing with a warning, since its download would be refused too. Any other ValueError or OSError keeps the row without md5, also with a warning. The test asserts the drop and the log line.
  2. (MEDIUM) The digest cache key is now size, mtime, inode and ctime, so a same-size rewrite within one mtime tick, or an atomic replace, rehashes.

ESPHome Bot (@esphbot) review

@bdraco

Copy link
Copy Markdown
Member

af29ff4 merges main on top of 4561bb8.

ESPHome Bot (@esphbot) review

@esphbot

Copy link
Copy Markdown
Contributor

J. Nick Koston (@bdraco): 4561bb8:

Verified both fixes. PathEscapeError drops the entry with a warning (download.py:191-193). It is caught before ValueError. Cache key now has size, mtime, inode and ctime (download.py:186-188). Escape test checks the drop and the log line.

@esphbot

ESPHome Bot (esphbot) commented Nov 5, 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

95d16af (MEDIUM, silent field omission): docs/API.md now says md5 is also absent when an artifact can't be read for hashing (logged as a warning), and that clients must treat a missing md5 as "no checksum available", not as verified. The companion frontend shows an "MD5 unavailable" note in that case. 0f96b49 merges main.

ESPHome Bot (@esphbot) review

@esphbot

Copy link
Copy Markdown
Contributor

J. Nick Koston (@bdraco): 95d16af (MEDIUM, silent field omission): docs/API.md now says md5 is also absent when an artifac...

Verified in docs/API.md. Missing md5 now documented. Clients told: no checksum available. Prior MEDIUM resolved.

@esphbot

Copy link
Copy Markdown
Contributor

PR Review — Add MD5 hash generate to the firmware for display and download

Ready to merge. Commit 95d16af fixes the last open finding (the missing md5 when hashing fails), and the merge of main in 0f96b49 does not touch this area.

What this PR does well:

  • The missing-md5 contract is documented. docs/API.md now says md5 is also absent when an artifact can't be read for hashing, and that clients must treat a missing md5 as "no checksum available", not as verified. The companion frontend shows an "MD5 unavailable" note in that case.
  • Hashing stays off the event loop. collect_download_entries runs inside run_in_executor(_get_types) (download.py:121), so reading a multi-MB file to hash it does not block the WS loop.
  • Each error case is handled per artifact. A vanished artifact drops out at debug level. An artifact resolving outside the build dir drops out with a warning, which matches the download refusing it. Any other read error keeps the row without md5 and logs a warning. A test covers each case.
  • A stale digest is not returned. The cache key is size, mtime_ns, inode and ctime_ns, so a rebuilt artifact gets a fresh digest. The rebuild test checks this.
  • The listing and the HTTP download use the same filename, because both go through _download_filename.

What needs attention:

  • Nothing.


Checklist

  • No hardcoded secrets
  • Path traversal guarded on hash path (resolve_under_root)
  • Hashing runs off the event loop
  • Digest cache key invalidates on rebuild
  • Missing md5 documented for clients
  • Listed filename matches served filename
  • Tests cover new fields and every error branch

Automated review by Kōan (Claude) HEAD=0f96b49 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 fbf212d into esphome:main Nov 5, 2026
22 checks passed
@dougiteixeira
Douglas (dougiteixeira) deleted the md5_firmware branch November 6, 2026 08:36
@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

new-feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants