-
Notifications
You must be signed in to change notification settings - Fork 1
chore(release): guard against credentials in published firmware #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
169b6d3
chore(release): guard against credentials in published firmware
iliabaranov 114b5a3
release_guard: brand private builds; bash-3.2 safe; Kconfig unescape;…
iliabaranov b24bc22
release_guard: portable grep (no -P/-z); brand definition before proj…
iliabaranov 689546e
release_guard: brand lives in a used object, not only a log string
iliabaranov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| #!/usr/bin/env bash | ||
| # SPDX-FileCopyrightText: 2026 Polymath Robotics | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # release_guard.sh — refuse to publish firmware artifacts that carry secrets. | ||
| # | ||
| # Every value in the git-ignored sdkconfig.credentials (Tailscale auth key, | ||
| # WiFi credentials, admin password, OTA URL/API key, management-server IP) is | ||
| # compiled into BOTH the .bin and the .elf as a plain string. Only a build made | ||
| # WITHOUT a credentials file may ever be attached to a public release. | ||
| # | ||
| # tools/release_guard.sh <artifact>... | ||
| # | ||
| # Three independent layers, so the verdict does not depend on which | ||
| # credentials file happens to be on the machine running the guard: | ||
| # 1. build brand — any image compiled with a credentials file present carries | ||
| # the ML-BUILD-WITH-CREDENTIALS marker (CMakeLists + ml_app.c) | ||
| # 2. local values — every value of a credentials file found in the tree | ||
| # 3. secret shapes — tskey-…, PEM private keys, literal HTTP auth tokens | ||
| # | ||
| # Exit 0 = every artifact clean. Exit 1 = a leak (pattern named, value never | ||
| # printed). Exit 2 = usage. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| [[ $# -ge 1 ]] || { echo "usage: $0 <artifact>..." >&2; exit 2; } | ||
|
|
||
| # Values that are already public (Kconfig defaults in the tracked tree, e.g. the | ||
| # documented default admin password) are not secrets and must not trip the guard. | ||
| public_defaults="$(grep -hoE 'default "[^"]+"' "$REPO"/components/microlink/Kconfig \ | ||
| "$REPO"/firmware/components/*/Kconfig 2>/dev/null | sed -E 's/^default "//; s/"$//' || true)" | ||
|
|
||
| # Kconfig writes strings with \" and \\ escaped; the compiler embeds the unescaped bytes. | ||
| kconfig_unescape() { printf '%s' "$1" | sed -E 's/\\(["\\])/\1/g'; } | ||
| # Whole-C-string view of an artifact: every NUL becomes a newline, so a | ||
| # NUL-terminated string literal is exactly one line. Portable — needs no | ||
| # grep -P / -z (absent from BSD grep). | ||
| cstrings() { tr '\0' '\n' < "$1"; } | ||
|
|
||
| # Layer 2 inputs: "KEY<TAB>value" lines from every credentials file in the tree. | ||
| values="" | ||
| for f in "$REPO"/firmware/sdkconfig.credentials "$REPO"/machn/sdkconfig.credentials; do | ||
| [[ -f "$f" ]] || continue | ||
| while IFS= read -r line; do | ||
| [[ "$line" =~ ^(CONFIG_[A-Z0-9_]+)=\"(.*)\"$ ]] || continue | ||
| k="${BASH_REMATCH[1]}"; v="$(kconfig_unescape "${BASH_REMATCH[2]}")" | ||
| [[ -n "$v" && "$v" != "y" && "$v" != "n" ]] || continue | ||
| grep -qxF -- "$v" <<< "$public_defaults" && continue | ||
| values+="$k"$'\t'"$v"$'\n' | ||
| done < "$f" | ||
| done | ||
| values="$(printf '%s' "$values" | sort -u)" | ||
|
|
||
| overall=0 | ||
| for art in "$@"; do | ||
| if [[ ! -f "$art" ]]; then echo "MISSING $art" >&2; overall=1; continue; fi | ||
| leak=0 | ||
|
|
||
| # 1. build brand | ||
| if grep -qF -- 'ML-BUILD-WITH-CREDENTIALS' "$art"; then | ||
| echo "LEAK $art: built with a credentials file (private build brand present)"; leak=1 | ||
| fi | ||
|
|
||
| # 2. local credential values. Short values would match by accident anywhere, | ||
| # so they are required as a whole NUL-terminated C string. | ||
| while IFS=$'\t' read -r k v; do | ||
| [[ -n "$k" ]] || continue | ||
| if [[ ${#v} -ge 6 ]]; then | ||
| grep -qF -- "$v" "$art" && { echo "LEAK $art: value of $k present"; leak=1; } | ||
| else | ||
| cstrings "$art" | grep -qaxF -- "$v" && { echo "LEAK $art: value of $k present"; leak=1; } | ||
| fi | ||
| done <<< "$values" | ||
|
|
||
| # 3. secret-shaped strings regardless of any credentials file | ||
| grep -qaE -- 'tskey-(auth|client|api)-[A-Za-z0-9]+' "$art" && { echo "LEAK $art: Tailscale key pattern present"; leak=1; } | ||
| # header followed by a base64 body — the bare header is an mbedTLS parser constant | ||
| tr '\n\r\0' ' ' < "$art" | grep -qaE -- '-----BEGIN [A-Z ]*PRIVATE KEY----- *[A-Za-z0-9+/=]{40,}' && { echo "LEAK $art: PEM private key present"; leak=1; } | ||
| grep -qaE -- '(^|[^A-Za-z])(Bearer|Basic) [A-Za-z0-9+/=_-]{16,}' "$art" && { echo "LEAK $art: literal HTTP auth token present"; leak=1; } | ||
|
|
||
| if [[ $leak -eq 0 ]]; then echo "clean $art"; else overall=1; fi | ||
| done | ||
|
|
||
| exit $overall |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.