Revision: 1 Last modified: 2026-12-04T18:15:21Z
This is a library. It is sourced, never executed. It has no shebang and is not marked executable; the
file mode is rw-rw-r--. Running it directly does nothing useful and is not a supported use.
scripts/forge/lib/common.sh is the shared layer of scripts/forge/forge.sh and is also sourced by
scripts/forge/live_probe.sh for the output sanitiser. It owns four things:
- Environment hygiene — building the environment of every child process positively (allow-list) instead of stripping a denylist, by re-executing the caller once with an empty environment.
- The one output sanitiser — every line the tool prints passes through it, so no token value can reach a terminal or a log.
- Output and exit helpers — the
RESULT:/REASON:protocol and the exit-code vocabulary. - Validators for every string that can reach a URL path or the output.
bash4+ (compgen -e, arrays,${var,,}).- Linux
/procforfg_env_clean(it verifies the re-executed state from/proc/$$/cmdlineand/proc/$$/environ). - Callers must have set nothing in particular in advance, but
fg_secret_hitandfg_scrubare only useful afterfg_secrets_load(inlib/credentials.sh) has filledFG_SEC_VALS/FG_SEC_NAMES.
# how a caller loads it
. "$FG_DIR/lib/common.sh" || { printf 'RESULT: USAGE\n'; exit 2; }
# build the clean child environment (call FIRST, before any external command); re-executes once
fg_env_clean "$@"; rc=$?
case "$rc" in 0) ;; *) printf 'RESULT: GAP\n'; exit 3 ;; esac
# output
fg_line "PROVIDER: $FG_PROVIDER" # stdout, scrubbed
fg_msg "no answer" # stderr, prefixed "forge: ", scrubbed
fg_end OK 0 # prints "RESULT: OK" and exits 0
fg_end FAIL 1 tag_sha_mismatch # prints "REASON: ..." then "RESULT: FAIL", exits 1
# the typed error helpers (each ends the run)
fg_usage_err "a command is required" # exit 2, RESULT: USAGE
fg_gap credentials_absent:GH_TOKEN "..." # exit 3, RESULT: GAP
fg_api_err auth_failed "..." # exit 4, RESULT: API
fg_fail present_mismatch "..." # exit 1, RESULT: FAIL
# validators (return status, never exit)
fg_ok_segment "my-repo" && echo safe
fg_ok_tag "v1.2.3"
fg_ok_sha "0123456789abcdef0123456789abcdef01234567"
fg_ok_id "12345" # numeric ids and UUIDs; never "/" or "."
fg_ok_port "8443"
fg_ok_varname "GITHUB_TOKEN" # must match [A-Z][A-Z0-9_]*TOKENcommon.sh itself does not exit; it returns statuses, and the fg_* helpers it defines exit the process.
| Helper | Status / exit | Meaning |
|---|---|---|
fg_env_clean |
0 |
environment is clean, values restored, ready to run |
fg_env_clean |
3 |
/proc/$$ unreadable — state cannot be checked, so the caller must refuse |
fg_env_clean |
4 |
the re-exec failed |
fg_env_clean |
5 |
the re-executed process still has a non-empty environment (refused; no loop) |
fg_end WORD RC |
exits RC |
prints optional REASON:, then RESULT: WORD |
fg_usage_err |
2 |
RESULT: USAGE |
fg_gap |
3 |
RESULT: GAP (precondition / forge state) |
fg_api_err |
4 |
RESULT: API (network or unexpected HTTP) |
fg_fail |
1 |
RESULT: FAIL |
fg_ok_* |
0 / 1 |
valid / not valid |
fg_secret_hit |
0 / 1 |
the text contains a secret window / it does not |
fg_bool |
prints | true / false / unknown (never fails) |
Must be available before the relevant calls:
FG_DIR— thescripts/forgedirectory, solib/*.shandproviders/*.shcan be sourced off it._FG_*state set byfg_env_cleanitself; do not pre-set them.
May read after the call:
_FG_STATE(clean|dirty|blind),_FG_A0.FG_SEC_NAMES[]/FG_SEC_VALS[]— filled byfg_secrets_load(credentials.sh), consumed here.FG_SECRET_NAME— set byfg_secret_hitto the name of the variable whose value was matched. Never the value.
Exported to children (the whole list, nothing else): PATH HOME USER LOGNAME LANG LANGUAGE TZ TMPDIR
SSH_AUTH_SOCK XDG_RUNTIME_DIR; LC_ALL is then forced to C. Carried as unexported shell variables
(no child sees them): every name matching [A-Z][A-Z0-9_]*TOKEN and every FORGE_* setting. Everything else
is dropped — including SHELLOPTS/BASHOPTS (an inherited xtrace or allexport), BASH_ENV, exported
functions, proxy variables, and any name that is not a shell identifier.
| Function | Purpose |
|---|---|
fg_env_clean "$@" |
Build the clean child environment; re-executes the script once with exec -c, argv[0] = forge-clean-env, values over fd 9 (a pipe written by builtins — never an argv, never a file). |
fg_secret_hit TEXT |
Status 0 when TEXT holds a window of any known token value: the whole value when shorter than 12 characters, otherwise any 12-character piece of it. Sets FG_SECRET_NAME. Text longer than 4096 bytes is pre-filtered through one grep -qF and then confirmed by the exact loop — a grep that cannot run (status 2) falls through to the exact loop rather than being read as "no secret". |
fg_scrub TEXT |
Returns TEXT with every space-separated word holding a secret window replaced by unsafe; key=value words keep a clean key (request_id=unsafe). Multi-line input is scrubbed line by line and every line is kept. |
fg_msg / fg_line |
scrubbed write to stderr (forge: … prefix) / stdout |
fg_end WORD RC [REASON] |
the only sanctioned way to end a run |
fg_*_err / fg_gap / fg_fail |
typed endings, see the table above |
fg_ok_segment |
^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$ and no .. |
fg_ok_tag |
^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$ and no .. |
fg_ok_sha |
exactly 40 lower-case hex characters |
fg_ok_id |
^[A-Za-z0-9][A-Za-z0-9-]{0,63}$ — numeric ids and UUIDs; never /, never . |
fg_ok_port |
decimal 1–65535 |
fg_ok_varname |
^[A-Z][A-Z0-9_]{0,62}TOKEN$ |
fg_bool |
true/false passthrough, anything else unknown |
Internal helpers (_fg_reserved, _fg_carry_shell, _fg_env_dump, _fg_env_restore, _fg_env_state,
_fg_windows) are not part of the caller contract.
- No
/proc(non-Linux, hardened container):fg_env_cleanreturns 3 and the caller must refuse. It does not silently continue with an unverified environment. - A second re-exec attempt with the marker already set returns 5 instead of looping.
- Empty
FG_SEC_VALSmakesfg_secret_hitreturn 1 immediately — no secret can be detected, so callers that need assurance must check thatfg_secrets_loadsucceeded (seecredentials.sh). - A multi-line message is scrubbed per line; lines are never dropped.
grepunavailable in the fast path is treated as "could not decide" and the exact loop still runs.
_fg_env_statereads/proc/$$/cmdlineforargv[0]and counts entries in/proc/$$/environ.cleanrequiresargv[0] == forge-clean-envand zero environment entries.clean→_fg_env_restorereads thee|s NUL NAME NUL VALUE NULrecords from fd 9, re-exports the allow-list, sets the rest as plain variables, forcesLC_ALL=C.blind→ return 3. Already marked but not clean → 5. Otherwiseexec -c -a forge-clean-env "$BASH" "$0" "$@" 9< <(_fg_env_dump), where_fg_env_dumpruns in a forked subshell using builtins only.fg_scrubfirst asksfg_secret_hitabout the whole text; if there is no hit it returns the text unchanged. On a hit it splits into words, tests each, and rewrites only the offending ones.
scripts/forge/forge.sh— the main consumer.scripts/forge/lib/credentials.sh— fills the secret set this file scrubs with.scripts/forge/lib/http.sh— the HTTP client that relies on these traps and the temp dir.scripts/forge/live_probe.sh— sources this file forfg_secret_hitin its leak check.
2026-12-04 — bash -n scripts/forge/lib/common.sh → exit 0 (parses; it is a library so it is not executed).
Sourcing check that a reader can repeat:
bash -c '. scripts/forge/lib/common.sh && fg_ok_sha 0123456789abcdef0123456789abcdef01234567 && echo validator-ok'Expected: validator-ok.