|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Regenerate the vendored Node.js release signing keyring from the canonical |
| 3 | +# nodejs/release-keys repository. |
| 4 | +# |
| 5 | +# The keyring is the trust anchor used by `vite_js_runtime` to verify the PGP |
| 6 | +# signature on Node.js `SHASUMS256.txt` (see rfcs/verify-node-shasums-signature.md). |
| 7 | +# Run from the repository root: |
| 8 | +# |
| 9 | +# .github/scripts/update-node-release-keys.sh |
| 10 | +# |
| 11 | +# When KEYRING_SUMMARY_FILE is set, a Markdown pull-request body describing the |
| 12 | +# before/after key changes (added, removed, modified, by fingerprint) is written |
| 13 | +# to that path. The accompanying workflow |
| 14 | +# (.github/workflows/update-node-release-keys.yml) runs this weekly and opens a |
| 15 | +# PR when the keyring changes. The change must be reviewed by a human before |
| 16 | +# merging, since it alters which keys are trusted. |
| 17 | +set -euo pipefail |
| 18 | + |
| 19 | +DEST="crates/vite_js_runtime/src/assets/node-release-keys.asc" |
| 20 | +SUMMARY_FILE="${KEYRING_SUMMARY_FILE:-}" |
| 21 | + |
| 22 | +if [[ ! -f "$DEST" ]]; then |
| 23 | + echo "error: run from the repository root (missing $DEST)" >&2 |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | + |
| 27 | +TMP="$(mktemp -d)" |
| 28 | +trap 'rm -rf "$TMP"' EXIT |
| 29 | + |
| 30 | +# Snapshot the current keyring so we can diff it after regenerating. |
| 31 | +cp "$DEST" "$TMP/before.asc" |
| 32 | + |
| 33 | +git clone --depth 1 https://github.com/nodejs/release-keys.git "$TMP/release-keys" |
| 34 | + |
| 35 | +# Concatenate every armored key block, sorted by filename and separated by a |
| 36 | +# single blank-free newline, so a key whose file lacks a trailing newline does |
| 37 | +# not get its END line merged with the next BEGIN line. |
| 38 | +python3 - "$TMP/release-keys/keys" "$DEST" <<'PY' |
| 39 | +import glob |
| 40 | +import os |
| 41 | +import sys |
| 42 | +
|
| 43 | +keys_dir, dest = sys.argv[1], sys.argv[2] |
| 44 | +files = sorted(glob.glob(os.path.join(keys_dir, "*.asc"))) |
| 45 | +if not files: |
| 46 | + sys.exit("error: no key files found in nodejs/release-keys") |
| 47 | +
|
| 48 | +blocks = [ |
| 49 | + open(path, encoding="utf-8").read().replace("\r\n", "\n").replace("\r", "\n").strip("\n") |
| 50 | + for path in files |
| 51 | +] |
| 52 | +with open(dest, "w", encoding="utf-8") as out: |
| 53 | + out.write("\n".join(blocks) + "\n") |
| 54 | +print(f"wrote {len(files)} release keys to {dest}") |
| 55 | +PY |
| 56 | + |
| 57 | +# Sanity check: balanced, non-trivial keyring. |
| 58 | +begin=$(grep -c '^-----BEGIN PGP PUBLIC KEY BLOCK-----$' "$DEST" || true) |
| 59 | +end=$(grep -c '^-----END PGP PUBLIC KEY BLOCK-----$' "$DEST" || true) |
| 60 | +if [[ "$begin" != "$end" || "$begin" -lt 8 ]]; then |
| 61 | + echo "error: unexpected keyring (begin=$begin end=$end)" >&2 |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | +echo "keyring has $begin key blocks" |
| 65 | + |
| 66 | +# Write the PR body with a before/after comparison of the changed keys. Best |
| 67 | +# effort: if the diff can't be produced (e.g. gpg missing), fall back to a plain |
| 68 | +# body so the PR is still created. |
| 69 | +if [[ -n "$SUMMARY_FILE" ]]; then |
| 70 | + if ! python3 - "$TMP/before.asc" "$DEST" > "$SUMMARY_FILE" <<'PY' |
| 71 | +import hashlib |
| 72 | +import re |
| 73 | +import shutil |
| 74 | +import subprocess |
| 75 | +import sys |
| 76 | +import tempfile |
| 77 | +
|
| 78 | +PREAMBLE = ( |
| 79 | + "Automated refresh of the vendored Node.js release signing keys from " |
| 80 | + "[nodejs/release-keys](https://github.com/nodejs/release-keys).\n\n" |
| 81 | + "This keyring is the trust anchor used to verify the PGP signature on " |
| 82 | + "Node.js `SHASUMS256.txt` (see `rfcs/verify-node-shasums-signature.md`).\n\n" |
| 83 | + "**Review the key changes below before merging.** PR CI runs the " |
| 84 | + "`vite_js_runtime` tests, which confirm every vendored key still parses; a " |
| 85 | + "new key that fails to parse will surface as a failing test.\n" |
| 86 | +) |
| 87 | +
|
| 88 | +
|
| 89 | +def unescape(value): |
| 90 | + # gpg --with-colons backslash-escapes field-conflicting bytes as \xHH |
| 91 | + # (e.g. a literal ':' in a uid becomes '\x3a'); decode them for display. |
| 92 | + return re.sub(r"\\x([0-9a-fA-F]{2})", lambda m: chr(int(m.group(1), 16)), value) |
| 93 | +
|
| 94 | +
|
| 95 | +def load_keys(asc_path): |
| 96 | + """Map primary fingerprint -> {uid, digest of its colon record}.""" |
| 97 | + home = tempfile.mkdtemp() |
| 98 | + try: |
| 99 | + out = subprocess.run( |
| 100 | + ["gpg", "--homedir", home, "--quiet", "--batch", |
| 101 | + "--show-keys", "--with-colons", asc_path], |
| 102 | + capture_output=True, text=True, check=True, |
| 103 | + ).stdout |
| 104 | + finally: |
| 105 | + shutil.rmtree(home, ignore_errors=True) |
| 106 | +
|
| 107 | + keys, fpr, pending = {}, None, [] |
| 108 | + for line in out.splitlines(): |
| 109 | + field = line.split(":") |
| 110 | + record = field[0] |
| 111 | + if record == "pub": |
| 112 | + fpr, pending = None, [line] |
| 113 | + elif record == "fpr" and fpr is None: |
| 114 | + fpr = field[9] |
| 115 | + keys[fpr] = {"uid": "", "lines": pending + [line]} |
| 116 | + pending = [] |
| 117 | + elif fpr is not None: |
| 118 | + keys[fpr]["lines"].append(line) |
| 119 | + if record == "uid" and not keys[fpr]["uid"]: |
| 120 | + keys[fpr]["uid"] = unescape(field[9]) |
| 121 | + else: |
| 122 | + pending.append(line) |
| 123 | +
|
| 124 | + for entry in keys.values(): |
| 125 | + entry["digest"] = hashlib.sha256("\n".join(entry["lines"]).encode()).hexdigest() |
| 126 | + return keys |
| 127 | +
|
| 128 | +
|
| 129 | +before = load_keys(sys.argv[1]) |
| 130 | +after = load_keys(sys.argv[2]) |
| 131 | +
|
| 132 | +added = sorted(set(after) - set(before)) |
| 133 | +removed = sorted(set(before) - set(after)) |
| 134 | +modified = sorted(f for f in set(after) & set(before) |
| 135 | + if after[f]["digest"] != before[f]["digest"]) |
| 136 | +
|
| 137 | +
|
| 138 | +def bullets(fingerprints, table): |
| 139 | + if not fingerprints: |
| 140 | + return ["- _none_"] |
| 141 | + out = [] |
| 142 | + for fpr in fingerprints: |
| 143 | + uid = table[fpr]["uid"] or "(no user id)" |
| 144 | + out.append(f"- {uid} `{fpr}`") |
| 145 | + return out |
| 146 | +
|
| 147 | +
|
| 148 | +lines = [PREAMBLE, "## Key changes", ""] |
| 149 | +lines.append(f"Release keyring: **{len(before)} → {len(after)} keys**.") |
| 150 | +lines += ["", f"### Added ({len(added)})", *bullets(added, after)] |
| 151 | +lines += ["", f"### Removed ({len(removed)})", *bullets(removed, before)] |
| 152 | +lines += ["", f"### Modified ({len(modified)})", *bullets(modified, after)] |
| 153 | +print("\n".join(lines)) |
| 154 | +PY |
| 155 | + then |
| 156 | + echo "warning: could not build key diff; writing a plain PR body" >&2 |
| 157 | + cat > "$SUMMARY_FILE" <<'EOF' |
| 158 | +Automated refresh of the vendored Node.js release signing keys from |
| 159 | +[nodejs/release-keys](https://github.com/nodejs/release-keys). |
| 160 | +
|
| 161 | +This keyring is the trust anchor used to verify the PGP signature on Node.js |
| 162 | +`SHASUMS256.txt` (see `rfcs/verify-node-shasums-signature.md`). Review which keys |
| 163 | +changed (`git diff`) before merging. |
| 164 | +EOF |
| 165 | + fi |
| 166 | + echo "wrote PR body to $SUMMARY_FILE" |
| 167 | +fi |
0 commit comments