Skip to content

Ignore meter keys that are not 32 hex characters in web save and config import - #1311

Merged
gskjold merged 1 commit into
mainfrom
fix-web-meter-key-length
Nov 5, 2026
Merged

gskjold merged 1 commit into
mainfrom
fix-web-meter-key-length

Conversation

@gskjold

@gskjold gskjold commented Nov 5, 2026

Copy link
Copy Markdown
Member

Why

Follow-up to #1310, same bug in the other two places that take meter keys. The GCM encryption and authentication keys are 16 bytes, i.e. 32 hex characters, but:

  • Web save (AmsWebServer::handleSave, mek/mea): the form's pattern only required hex characters ([0-9A-Fa-f]+), not a length. A short or mistyped key such as ABCD was accepted, and fromHex(..., 16) read 32 characters from it, past the end of the string. A key with an extra character was cut off without a warning.
  • Config file import (configFileParse, meterEncryptionKey / meterAuthenticationKey): the same unconditional fromHex(..., 16). readBytesUntil doesn't null-terminate and buf isn't cleared between lines, so a short key line was filled up with leftover bytes from earlier, longer lines.

Changes

  • handleSave: after removing 0x, parse the key only if it is exactly sizeof(key) * 2 characters. Otherwise keep the stored key. An empty field still clears the key, as before.
  • configFileParse: parse the key only if the line length (size, not the string length, which could include those leftover bytes) is the prefix plus 32 characters.
  • UI: the two key inputs require exactly 32 hex characters (meterKeyPattern replaces hexPattern, which only these two inputs used), so the browser reports a wrong length before submitting. The config JSON always sends 32 characters (zeros when no key is set), so existing configs still validate. ui/dist is rebuilt.

Verification

  • pio run -e esp32 -e esp8266 builds; npm run build in ui/ builds.
  • Not tested on a device: saving a short and a correct key through the web form, and importing a config file with a short key.

🤖 Generated with Claude Code

…ig import

The web configuration form only checked that the encryption and
authentication keys were hex, not how long they were, and handleSave and
the config file import both had fromHex read 32 characters regardless.
A short key was parsed past the end of the string, and in the config
file import past the end of the line into leftovers from earlier lines.

Only accept a key of exactly twice the key size and keep the stored key
otherwise; an empty key in the web form still clears it. The form now
requires 32 hex characters, so a wrong length is reported before saving.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

🔧 PR Build Artifacts

Version: 3cbf4ca

All environments built successfully. Download the zip files:

Artifacts expire after 7 days. View workflow run

@gskjold
gskjold merged commit a60e145 into main Nov 5, 2026
13 of 16 checks passed
@gskjold
gskjold deleted the fix-web-meter-key-length branch November 5, 2026 15:48
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.

1 participant