Repository navigation
chore: a wizard for the npm publish token #2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,312 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # A wizard walks a human through a manual procedure, step by step. | ||
| # Generated by the /wizard skill. | ||
| # | ||
| # Everything above the "STAGES" marker is the wizard library: do not hand-edit | ||
| # it. Author the per-step stages below the marker. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # ────────────────────────────────────────────────────────────────────────── | ||
| # Wizard library: delightful, consistent UX, identical across every wizard. | ||
| # ────────────────────────────────────────────────────────────────────────── | ||
|
|
||
| if [[ -t 1 ]] && command -v tput >/dev/null 2>&1 && [[ "$(tput colors 2>/dev/null || echo 0)" -ge 8 ]]; then | ||
| BOLD=$(tput bold); DIM=$(tput dim); RESET=$(tput sgr0) | ||
| BLUE=$(tput setaf 4); GREEN=$(tput setaf 2); YELLOW=$(tput setaf 3); RED=$(tput setaf 1) | ||
| else | ||
| BOLD=""; DIM=""; RESET=""; BLUE=""; GREEN=""; YELLOW=""; RED="" | ||
| fi | ||
|
|
||
| # Author sets this at the top of the stages section. | ||
| TOTAL_STAGES=0 | ||
|
|
||
| _STAGE_INDEX=0 | ||
| ENV_FILE="${ENV_FILE:-.env}" | ||
| WRITTEN_ENV=() # KEYs written to ENV_FILE this run | ||
| WRITTEN_SECRET=() # secret NAMEs set this run | ||
| SKIPPED=() # things we couldn't do (e.g. gh missing) | ||
|
|
||
| # _clear wipes the terminal so only the current step is on screen. No-op when | ||
| # output isn't a terminal, so piped logs stay readable. | ||
| _clear() { | ||
| [[ -t 1 ]] || return 0 | ||
| if command -v tput >/dev/null 2>&1; then tput clear; else printf '\033[2J\033[3J\033[H'; fi | ||
| } | ||
|
|
||
| # banner "Title" shows the opening frame: what this wizard does. | ||
| banner() { | ||
| _clear | ||
| printf '\n%s%s %s%s\n' "$BOLD" "$BLUE" "$1" "$RESET" | ||
| printf '%s %s stages%s\n\n' "$DIM" "$TOTAL_STAGES" "$RESET" | ||
| printf '%s You drive the browser; this wizard tells you exactly what to do and\n' "$DIM" | ||
| printf ' captures the values you copy back. Stop any time with Ctrl-C and re-run\n' | ||
| printf ' later, since it remembers values already saved.%s\n' "$RESET" | ||
| pause "Ready to start?" | ||
| } | ||
|
|
||
| # stage "Name" clears the screen, then announces a stage and shows progress. | ||
| # Clearing keeps only the current step on screen. | ||
| stage() { | ||
| _clear | ||
| _STAGE_INDEX=$((_STAGE_INDEX + 1)) | ||
| printf '\n%s%s▸ Stage %s/%s · %s%s\n' \ | ||
| "$BOLD" "$BLUE" "$_STAGE_INDEX" "$TOTAL_STAGES" "$1" "$RESET" | ||
| } | ||
|
|
||
| # say "..." prints a plain instruction line. | ||
| say() { printf ' %s\n' "$1"; } | ||
| # step "..." is a numbered-feeling action the human takes in the browser. | ||
| step() { printf ' %s•%s %s\n' "$BLUE" "$RESET" "$1"; } | ||
| note() { printf ' %s%s%s\n' "$DIM" "$1" "$RESET"; } | ||
| warn() { printf ' %s⚠ %s%s\n' "$YELLOW" "$1" "$RESET"; } | ||
|
|
||
| # open_url URL opens it in the human's browser, cross-platform incl. WSL. | ||
| open_url() { | ||
| local url="$1" | ||
| printf ' %s↗ opening%s %s\n' "$GREEN" "$RESET" "$url" | ||
| { if command -v wslview >/dev/null 2>&1; then wslview "$url" | ||
| elif command -v explorer.exe >/dev/null 2>&1; then explorer.exe "$url" | ||
| elif command -v xdg-open >/dev/null 2>&1; then xdg-open "$url" | ||
| elif command -v open >/dev/null 2>&1; then open "$url" | ||
| else warn "couldn't open a browser; visit it manually: $url"; fi | ||
| } >/dev/null 2>&1 || warn "couldn't open a browser, so visit it manually: $url" | ||
| } | ||
|
|
||
| # pause "msg" waits for the human to confirm they've done the manual part. | ||
| pause() { | ||
| printf ' %s%s%s ' "$DIM" "${1:-Press Enter to continue}" "$RESET" | ||
| read -r _ || true | ||
| } | ||
|
|
||
| # confirm "question" is a y/N gate; returns success on yes. | ||
| confirm() { | ||
| local reply="" | ||
| printf ' %s? %s [y/N] ' "$YELLOW" "$1" | ||
| read -r reply || true | ||
| [[ "$reply" =~ ^[Yy] ]] | ||
| } | ||
|
|
||
| # _existing KEY: current value of KEY in ENV_FILE, if any. | ||
| _existing() { | ||
| [[ -f "$ENV_FILE" ]] || return 1 | ||
| local line; line=$(grep -E "^${1}=" "$ENV_FILE" | tail -n1) || return 1 | ||
| printf '%s' "${line#*=}" | ||
| } | ||
|
|
||
| # ask KEY "Prompt" reads a value into $KEY. Offers the existing .env value as | ||
| # a default on re-runs (Enter keeps it). Visible input (non-secret). | ||
| ask() { | ||
| local key="$1" prompt="$2" current input | ||
| current=$(_existing "$key" || true) | ||
| if [[ -n "$current" ]]; then | ||
| printf ' %s%s%s %s[Enter keeps current]%s ' "$BOLD" "$prompt" "$RESET" "$DIM" "$RESET" | ||
| else | ||
| printf ' %s%s%s ' "$BOLD" "$prompt" "$RESET" | ||
| fi | ||
| read -r input || true | ||
| [[ -z "$input" && -n "$current" ]] && input="$current" | ||
| printf -v "$key" '%s' "$input" | ||
| } | ||
|
|
||
| # ask_secret KEY "Prompt" is like ask, but input is hidden. | ||
| ask_secret() { | ||
| local key="$1" prompt="$2" current input | ||
| current=$(_existing "$key" || true) | ||
| if [[ -n "$current" ]]; then | ||
| printf ' %s%s%s %s[Enter keeps current]%s ' "$BOLD" "$prompt" "$RESET" "$DIM" "$RESET" | ||
| else | ||
| printf ' %s%s%s ' "$BOLD" "$prompt" "$RESET" | ||
| fi | ||
| read -rs input || true | ||
| printf '\n' | ||
| [[ -z "$input" && -n "$current" ]] && input="$current" | ||
| printf -v "$key" '%s' "$input" | ||
| } | ||
|
|
||
| # write_env KEY VALUE upserts KEY=VALUE into ENV_FILE (creates it; replaces | ||
| # any existing line). Idempotent. | ||
| write_env() { | ||
| local key="$1" value="$2" tmp | ||
| touch "$ENV_FILE" | ||
| tmp=$(mktemp) | ||
| grep -vE "^${key}=" "$ENV_FILE" > "$tmp" || true | ||
| printf '%s=%s\n' "$key" "$value" >> "$tmp" | ||
| mv "$tmp" "$ENV_FILE" | ||
| WRITTEN_ENV+=("$key") | ||
| printf ' %s✓ wrote%s %s → %s\n' "$GREEN" "$RESET" "$key" "$ENV_FILE" | ||
| } | ||
|
|
||
| # set_secret NAME VALUE sets a GitHub Actions repo secret via gh. Falls back | ||
| # to a warning (and records it) if gh is unavailable or unauthenticated. | ||
| set_secret() { | ||
| local name="$1" value="$2" | ||
| if command -v gh >/dev/null 2>&1 && gh auth status >/dev/null 2>&1; then | ||
| if printf '%s' "$value" | gh secret set "$name" >/dev/null 2>&1; then | ||
| WRITTEN_SECRET+=("$name") | ||
| printf ' %s✓ set%s GitHub secret %s\n' "$GREEN" "$RESET" "$name" | ||
| return | ||
| fi | ||
| fi | ||
| SKIPPED+=("GitHub secret $name (set it manually: gh secret set $name)") | ||
| warn "skipped GitHub secret $name: gh not ready; set it later" | ||
| } | ||
|
|
||
| # set_var NAME VALUE sets a GitHub Actions repo variable (non-secret). | ||
| set_var() { | ||
| local name="$1" value="$2" | ||
| if command -v gh >/dev/null 2>&1 && gh auth status >/dev/null 2>&1; then | ||
| if gh variable set "$name" --body "$value" >/dev/null 2>&1; then | ||
| printf ' %s✓ set%s GitHub variable %s\n' "$GREEN" "$RESET" "$name" | ||
| return | ||
| fi | ||
| fi | ||
| SKIPPED+=("GitHub variable $name") | ||
| warn "skipped GitHub variable $name, gh not ready; set it later" | ||
| } | ||
|
|
||
| # finish clears, then shows a closing summary of everything configured. | ||
| finish() { | ||
| _clear | ||
| printf '\n%s%s ✓ Setup complete%s\n' "$BOLD" "$GREEN" "$RESET" | ||
| (( ${#WRITTEN_ENV[@]} )) && note "wrote ${#WRITTEN_ENV[@]} value(s) to $ENV_FILE: ${WRITTEN_ENV[*]}" | ||
| (( ${#WRITTEN_SECRET[@]} )) && note "set ${#WRITTEN_SECRET[@]} GitHub secret(s): ${WRITTEN_SECRET[*]}" | ||
| if (( ${#SKIPPED[@]} )); then | ||
| printf '\n'; warn "still to do by hand:" | ||
| for s in "${SKIPPED[@]}"; do note " - $s"; done | ||
| fi | ||
| printf '\n' | ||
| } | ||
|
|
||
|
|
||
| # ────────────────────────────────────────────────────────────────────────── | ||
| # STAGES: author this section. | ||
| # ────────────────────────────────────────────────────────────────────────── | ||
|
|
||
| # The wizard's own knobs, overridable from the environment so this script can be | ||
| # driven by a test or a future CI job rather than a person at a keyboard. | ||
| TOKEN_FILE="${TOKEN_FILE:-npm_release.token}" | ||
| EXPECTED_USER="${EXPECTED_USER:-botharness}" | ||
| # npm's error text is how a rejected token is told from a missing one, and the | ||
| # difference decides whether we prompt or warn. Both spellings below were observed | ||
| # against registry.npmjs.org: | ||
| # rejected → E401 / 401 Unauthorized | ||
| # no creds → "need auth This command requires you to be logged in" | ||
| # The network cases are here so an offline machine asks for a token instead of | ||
| # silently rejecting a perfectly good saved one. | ||
| REJECTED_PATTERN='401|E401|Unauthorized|ENEEDAUTH|ENOTFOUND|ETIMEDOUT|ENETUNREACH|EAI_AGAIN' | ||
|
|
||
| # token_is_valid TOKEN — succeed only if the registry accepts this token AND it | ||
| # belongs to the account we publish as. Checking the user matters: a valid token | ||
| # for someone else would sail past a bare `npm whoami` and publish as them. | ||
| token_is_valid() { | ||
| local candidate="$1" whoami_out | ||
| whoami_out=$(npm whoami --//registry.npmjs.org/:_authToken="$candidate" 2>&1) || true | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🛡️ Detected with Advanced Tier | 🟠 Major | ⚡ Quick win Sensitive Data Exposure Reachability: Internal Stop passing the token on the Line 205 passes the candidate token as Line 309 and Line 310 tell the user that the token is "never passed on a command line". Fix: put the token in a temporary user config file with mode 600, and pass only the file path to npm. 🔒️ Proposed fix token_is_valid() {
- local candidate="$1" whoami_out
- whoami_out=$(npm whoami --//registry.npmjs.org/:_authToken="$candidate" 2>&1) || true
+ local candidate="$1" whoami_out rc
+ rc=$(umask 077; mktemp)
+ printf '//registry.npmjs.org/:_authToken=%s\n' "$candidate" > "$rc"
+ whoami_out=$(npm whoami --userconfig "$rc" 2>&1) || true
+ rm -f "$rc"🤖 Prompt for AI Agents |
||
| printf '%s' "$whoami_out" | grep -qE "$REJECTED_PATTERN" && return 1 | ||
| # `npm whoami` is the one call that actually discriminates: a fake token gets | ||
| # E401 and a real one gets the username. Nothing here can prove the token may | ||
| # PUBLISH, and the obvious candidates cannot be trusted to — `npm access` and | ||
| # `npm profile` are unknown subcommands that exit 0, so they "succeed" for a | ||
| # token that is pure fiction. Only whoami's output was verified to differ. | ||
| [[ "$whoami_out" == "$EXPECTED_USER" ]] | ||
| } | ||
|
|
||
| read_token() { | ||
| [[ -f "$TOKEN_FILE" ]] || return 1 | ||
| # trailing whitespace/newlines are easy to paste in and silently break the header | ||
| tr -d '[:space:]' < "$TOKEN_FILE" | ||
| } | ||
|
|
||
| store_token() { | ||
| local value="$1" dir tmp | ||
| dir=$(dirname "$TOKEN_FILE") | ||
| mkdir -p "$dir" | ||
| tmp=$(mktemp "${dir}/.npmtok.XXXXXX") | ||
| # written via a temp file in the same directory so the secret is never briefly | ||
| # world-readable, then moved into place; umask covers the window before chmod | ||
| ( umask 077; printf '%s\n' "$value" > "$tmp" ) | ||
| chmod 600 "$tmp" | ||
| mv "$tmp" "$TOKEN_FILE" | ||
| printf ' %s✓ saved%s %s (mode 600)\n' "$GREEN" "$RESET" "$TOKEN_FILE" | ||
| } | ||
|
|
||
| TOTAL_STAGES=2 | ||
|
|
||
| banner "npm release token" | ||
|
|
||
| # ── Stage 1: reuse a saved token, or ask for one ─────────────────────────── | ||
| stage "The publish token" | ||
| say "Releasing to npm needs a token with write access, because this account's 2FA" | ||
| say "is a passkey and the CLI cannot answer a WebAuthn challenge." | ||
| note "A saved token is reused automatically; you are only asked when there isn't" | ||
| note "a usable one. Ctrl-C at any time — nothing is lost but this run." | ||
|
|
||
| SAVED="" | ||
| if [[ -f "$TOKEN_FILE" ]]; then | ||
| note "found $TOKEN_FILE" | ||
| CANDIDATE=$(read_token || true) | ||
| if [[ -n "$CANDIDATE" ]] && token_is_valid "$CANDIDATE"; then | ||
| say "That token still works as $EXPECTED_USER." | ||
| SAVED="$CANDIDATE" | ||
| else | ||
| warn "the saved token was rejected — expired, revoked, or from another account." | ||
| note "npm tokens can be listed at https://www.npmjs.com/settings/access-tokens" | ||
| fi | ||
| else | ||
| note "no $TOKEN_FILE yet" | ||
| fi | ||
|
|
||
| if [[ -z "$SAVED" ]]; then | ||
| say "" | ||
| say "If you don't have one yet, create it here:" | ||
| open_url "https://www.npmjs.com/settings/access-tokens/new" | ||
| step "Packages: scope @botharness (or the three package names, one per row)" | ||
| step "Permissions: Read and write" | ||
| step "Tick 'Bypass 2FA' — without it the token cannot publish, and npm says so" | ||
| step "Set the shortest expiry you can live with; bypass tokens lose direct" | ||
| step " publishing in January 2027, so plan to move to OIDC trusted publishing" | ||
| step "Generate token, then copy it (it is shown only once)" | ||
| say "" | ||
| ask_secret TOKEN "Paste the token (input hidden):" | ||
| say "" | ||
| if [[ -z "$TOKEN" ]]; then | ||
| say "Nothing pasted. Re-run this script when you have one:" >&2 | ||
| note " bash scripts/npm-token.sh" >&2 | ||
| exit 1 | ||
| fi | ||
| if ! token_is_valid "$TOKEN"; then | ||
| warn "npm rejected that token." | ||
| say "Usual causes, in the order worth checking:" >&2 | ||
| note " - 'Bypass 2FA' was left unticked (the default is OFF)" >&2 | ||
| note " - the token was copied with a trailing space or newline" >&2 | ||
| note " - it belongs to a different npm account than $EXPECTED_USER" >&2 | ||
| note " - it has already expired" >&2 | ||
| exit 1 | ||
| fi | ||
| store_token "$TOKEN" | ||
| SAVED="$TOKEN" | ||
| fi | ||
|
|
||
| # ── Stage 2: report what we know, and what we cannot know ───────────────── | ||
| stage "Ready to publish" | ||
| printf ' %s✓%s token accepted by the registry as %s\n' "$GREEN" "$RESET" "$EXPECTED_USER" | ||
| say "" | ||
| say "What this script could NOT check, so you are not misled:" | ||
| note " Whether the token can publish. The registry has no such endpoint, and the" | ||
| note " commands that look like they would answer it (npm access, npm profile)" | ||
| note " are unknown commands that exit 0 — a fake token passes them. Only a real" | ||
| note " publish proves it, which is why the release publishes core first: a" | ||
| note " permission failure lands on the first package, before anything is" | ||
| note " half-released." | ||
| note "" | ||
| warn "If 'Bypass 2FA' was unticked, the publish fails with a 403. Re-create the" | ||
| warn "token with it ticked if that happens." | ||
| say "" | ||
| say "Publish with it:" | ||
| printf ' %spnpm release -- --yes%s\n' "$BOLD" "$RESET" | ||
| note "scripts/release.mjs reads $TOKEN_FILE on its own — no env var, no flag." | ||
| note "The token is never printed, never passed on a command line (so it cannot" | ||
| note "leak into ps or a CI log), and never written outside $TOKEN_FILE." | ||
|
|
||
| finish | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Make the token path agree with
release.mjs.The two scripts do not find the token file the same way:
TOKEN_FILEdefaults to a path relative to the current directory (cwd).scripts/release.mjsLine 73 readsjoin(root, "npm_release.token"), and its override variable isNPM_TOKEN_FILE, notTOKEN_FILE.If the user runs the wizard from
scripts/, or overrides only one of the two variables, the token is saved in one place and the release reads another. The release then publishes without a token.Fix: resolve the default path from the repository root, and honor
NPM_TOKEN_FILEin both scripts.📝 Committable suggestion
🤖 Prompt for AI Agents