Files
sumbrella/tests/check_secrets.py
T
Andre Neto 5d4b62c40b Build the two sketches with PlatformIO, add offline checks and rewrite the README
Sumbrella had no build definition: it was two .ino files to open in the Arduino
IDE. This adds one PlatformIO project per sketch (PlatformIO only converts a
*.ino sitting at the top of its own src_dir) and there is now a real, repeated
compile for the boards the project actually used:

  Projeto.ino       -> Arduino Nano        206 B RAM, 3508 B flash of 30720
  Fetch_rain.ino    -> ESP8266 ESP-01     28972 B RAM, 280119 B of 434160 flash

The boards come from the course poster kept in the repository (Sumbrella.pdf,
"Listagem de material": Arduino Nano and ESP8226 ESP-01), not from the code:
neither sketch names its board. The ESP-01 flash size is not given anywhere, so
the 512 KB variant is targeted.

Dependencies are now reproducible. LiquidCrystal_I2C comes from the PlatformIO
registry by name and version. DS3231 1.01 cannot: all 87 registry hits were
checked and none exposes class Time / getTimeStr() / DS3231(data, sclk), so it is
pinned to tag v1.01 of a mirror whose files were verified byte-for-byte equal to
the libs/DS3231 removed in 009066d. A clean clone compiles as it is.

The unused LCD stays unused and no sensor is invented: the only thing this commit
does to the sketches is compile them.

tests/ holds the checks that need no board, each with a negative selftest that
proves the check bites: secrets hygiene (secrets.h not tracked and ignored, the
example complete and placeholder-only, no credential literal in any tracked file)
and pin validity against the Nano and ESP-01 pin maps. run.sh builds both targets
and then runs them, exiting non-zero on any failure. .gitignore now covers the
PlatformIO artefacts (.pio/, *.bin, *.elf and friends).

README.md is rewritten in the common format, with the real build and test output.
Two things it no longer says, because they are not true of this repository: no
revision reachable from main ever contained a credential literal (the deleted
ESP8266_Fetching_Data.ino carried the string "<REDACTED>"), and the DS3231
dependency is not installable from the PlatformIO registry.

Known, left unfixed on purpose: the LCD is constructed and never written to; the
rain values are printed and discarded; loop() calls setup() on failure; and
libs/DS3231 is still reachable in the history before 009066d.
2026-10-08 16:11:49 +01:00

197 lines
7.0 KiB
Python

#!/usr/bin/env python3
"""Offline checks: no credential leaks and a complete secrets.h.example.
Failure conditions (each one makes this script exit non-zero):
1. Fetch_rain/secrets.h is tracked by git.
2. Fetch_rain/secrets.h is not covered by .gitignore.
3. Fetch_rain/secrets.h.example is missing, or does not define every
WIFI_*/THINGHTTP_KEY_* symbol that Fetch_rain.ino references.
4. Fetch_rain/secrets.h.example holds anything that does not look like a
placeholder.
5. Any git-tracked file holds a literal value for a credential-looking key
(api_key / password / passwd / pwd / ssid / secret / token), quoted or
not. A value that is a SCREAMING_SNAKE_CASE identifier is treated as a
reference to a symbol, not as a literal.
Run with --selftest to prove those checks actually bite: the checker is re-run
against deliberately broken copies and MUST fail on each one.
"""
import os
import re
import shutil
import subprocess
import sys
import tempfile
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SKETCH = os.path.join("Fetch_rain", "Fetch_rain.ino")
EXAMPLE = os.path.join("Fetch_rain", "secrets.h.example")
SECRETS = os.path.join("Fetch_rain", "secrets.h")
SYMBOL_RE = re.compile(r"\b((?:WIFI|THINGHTTP_KEY)_[A-Z0-9_]+)\b")
KEY = r"(api[_-]?key|password|passwd|pwd|ssid|secret|token)"
QUOTED_RE = re.compile(KEY + r"""\s*[=:]\s*["']([^"'\n]{6,})["']""", re.I)
BARE_RE = re.compile(KEY + r"\s*[=:]\s*([A-Za-z0-9_\-\.]{8,})", re.I)
SYMBOL_VALUE_RE = re.compile(r"^[A-Z0-9_]+$")
PLACEHOLDER_HINTS = ("your-", "your_", "example", "changeme", "change-me",
"xxx", "todo", "placeholder", "<", "$", "{}")
SKIP_SUFFIX = (".jpg", ".jpeg", ".png", ".pdf", ".gif", ".ico", ".zip")
def read(root, rel):
with open(os.path.join(root, rel), encoding="utf-8") as fh:
return fh.read()
def git(root, *args):
return subprocess.run(["git", "-C", root, *args], capture_output=True,
text=True)
def is_real(value):
"""True if `value` is a literal that must not be in a tracked file."""
if "(" in value or ")" in value or "+" in value or " " in value:
return False
if SYMBOL_VALUE_RE.match(value):
return False
return not any(h in value.lower() for h in PLACEHOLDER_HINTS)
def check(root):
errors = []
tracked = [p for p in git(root, "ls-files").stdout.splitlines() if p]
if not tracked:
return ["%s is not a git checkout with tracked files" % root]
if SECRETS in tracked:
errors.append("SECURITY: %s is tracked by git" % SECRETS)
if git(root, "check-ignore", "-q", SECRETS).returncode != 0:
errors.append("SECURITY: %s is not matched by .gitignore" % SECRETS)
if not os.path.isfile(os.path.join(root, EXAMPLE)):
errors.append("%s is missing" % EXAMPLE)
example_text = ""
else:
example_text = read(root, EXAMPLE)
used = set(SYMBOL_RE.findall(read(root, SKETCH)))
defined = set(re.findall(r"^\s*(?:const\s+char\s*\*\s*)?([A-Z0-9_]+)\s*=",
example_text, re.MULTILINE))
missing = sorted(used - defined)
if missing:
errors.append("%s does not define %s (used by %s)"
% (EXAMPLE, ", ".join(missing), SKETCH))
for name, value in re.findall(r"\b([A-Z0-9_]+)\s*=\s*\"([^\"]*)\"",
example_text):
if not any(h in value.lower() for h in PLACEHOLDER_HINTS):
errors.append("%s looks like a real value, not a placeholder: "
"%s = \"%s\"" % (EXAMPLE, name, value))
for rel in tracked:
if rel.endswith(SKIP_SUFFIX) or rel == EXAMPLE:
continue
try:
text = read(root, rel)
except (UnicodeDecodeError, IsADirectoryError):
continue
for lineno, line in enumerate(text.splitlines(), 1):
for regex in (QUOTED_RE, BARE_RE):
for key, value in regex.findall(line):
if is_real(value):
errors.append("SECURITY: literal %s in %s:%d (%s)"
% (key.lower(), rel, lineno, value))
return errors
def run_selftest():
failures = []
# The literal is assembled at run time so that this file itself does not
# contain the pattern the checker looks for.
fake_key = "Th1ngsP34k" + "RealKey" + "0001"
def temp_copy():
tmp = tempfile.mkdtemp(prefix="sumbrella-selftest-")
dst = os.path.join(tmp, "repo")
shutil.copytree(REPO, dst, symlinks=True)
return tmp, dst
def drop_symbol(dst):
p = os.path.join(dst, EXAMPLE)
lines = [l for l in open(p, encoding="utf-8")
if not l.startswith("const char* THINGHTTP_KEY_TMRW")]
open(p, "w", encoding="utf-8").writelines(lines)
shutil.copyfile(p, os.path.join(dst, SECRETS))
def drop_gitignore_rule(dst):
p = os.path.join(dst, ".gitignore")
lines = [l for l in open(p, encoding="utf-8")
if l.strip() != "secrets.h"]
open(p, "w", encoding="utf-8").writelines(lines)
def inject_literal(dst):
with open(os.path.join(dst, "README.md"), "a", encoding="utf-8") as fh:
fh.write("\n<!-- api_key=" + fake_key + " -->\n")
def remove_example(dst):
os.remove(os.path.join(dst, EXAMPLE))
cases = [
("secrets.h.example loses one symbol", drop_symbol),
(".gitignore loses the secrets.h rule", drop_gitignore_rule),
("a tracked file gains a literal api_key", inject_literal),
("secrets.h.example is deleted", remove_example),
]
for name, mutate in cases:
tmp, dst = temp_copy()
try:
mutate(dst)
if check(dst):
print(" PASS negative case detected: %s" % name)
else:
print(" FAIL negative case NOT detected: %s" % name)
failures.append(name)
finally:
shutil.rmtree(tmp, ignore_errors=True)
# Control: the untouched copy must be clean, otherwise the negative cases
# above prove nothing.
tmp, dst = temp_copy()
try:
errs = check(dst)
if errs:
print(" FAIL control copy is not clean: %s" % "; ".join(errs))
failures.append("control")
else:
print(" PASS control copy is clean")
finally:
shutil.rmtree(tmp, ignore_errors=True)
return failures
def main():
if "--selftest" in sys.argv:
print("check_secrets.py --selftest")
failures = run_selftest()
if failures:
print("SELFTEST FAILED: %s" % ", ".join(failures))
return 1
print("SELFTEST OK")
return 0
errors = check(REPO)
for err in errors:
print(" FAIL %s" % err)
if errors:
return 1
print(" PASS secrets.h untracked and ignored; secrets.h.example defines "
"every symbol the sketch uses and holds no real value; no tracked "
"file holds a literal credential")
return 0
if __name__ == "__main__":
sys.exit(main())