Revision: 1 Last modified: 2026-12-04T18:15:21Z
This is a library. It is sourced, never executed. No shebang, mode rw-rw-r--. Running it directly does
nothing useful and is not a supported use.
scripts/forge/lib/credentials.sh is token discovery for scripts/forge/forge.sh. It answers one question —
is there a usable API token for this host, and where did it come from? — and it answers it without ever
exposing a value. It also builds the set of every candidate token value the tool can know of, which the
output sanitiser in lib/common.sh uses to scrub what gets printed.
The design point worth understanding: nothing here prints a value, and no value is passed to a child as an
argument or written to a file. The keys file is read by a child bash that prints exactly one variable name
into a pipe; the .env is parsed, never executed.
bash4+,timeout(coreutils),statwith-c,id.- Called after
lib/common.sh(it usesfg_msg,fg_ok_varname,FG_SEC_NAMES/FG_SEC_VALS,FG_SEC_WIN). - The caller should already have
FG_REPO_ROOTandFG_DIR_PHYSset — they decide whether the repository.envis trusted at all.
. "$FG_DIR/lib/credentials.sh" || fg_end USAGE 2 install_broken
# 1. resolve a token by NAME, in the documented precedence
fg_token_resolve GITHUB_TOKEN GH_TOKEN
case "$FG_TOKEN_STATE" in
found) fg_line "AUTH: $FG_TOKEN_SOURCE:$FG_TOKEN_NAME" ;;
invalid) fg_gap "credentials_invalid:$FG_TOKEN_BAD" "value unusable; it is never sent" ;;
*) fg_line "AUTH: none" ;;
esac
# 2. load every candidate secret value (needed before any scrubbing)
fg_secrets_load; rc=$?
[ "$rc" -eq 0 ] || fg_msg "WARNING keys_file_incomplete: output is checked against what was read"
# 3. ask questions with the loaded state
fg_text_has_secret "$SOME_TEXT" && echo "contains $FG_SECRET_NAME"
fg_is_credential_file "$NOTES_PATH" && echo "that is the keys file or the .env — refuse to publish it"
# 4. inspect the paths the search would use
fg_keys_file_path # e.g. /home/<user>/api_keys.sh
fg_env_file_path # e.g. <repo>/.env, or empty when not applicableThe file never exits. Every function returns a status; the caller decides what to do.
| Function | Returns | Meaning |
|---|---|---|
fg_token_resolve NAME... |
0 |
a usable token was found (FG_TOKEN_STATE=found) |
fg_token_resolve |
1 |
nothing usable (absent, or invalid — see FG_TOKEN_BAD) |
fg_secrets_load |
0 |
the candidate set is complete |
fg_secrets_load |
1 |
the keys file could not be read to its end — the set may be incomplete, so the caller must not publish |
fg_text_has_secret TEXT |
0 / 1 |
the text holds a secret window / it does not |
fg_is_credential_file PATH |
0 / 1 |
PATH is (same device:inode, links followed) the keys file or the .env in use / it is not |
_fg_file_safe PATH |
0 / 1 / 2 |
safe to read / exists but unsafe (symlink, wrong owner, mode & 077 != 0) / absent (not a warning) |
State set for the caller: FG_TOKEN, FG_TOKEN_NAME, FG_TOKEN_SOURCE (env | keys_file | dotenv),
FG_TOKEN_STATE (found | absent | invalid), FG_TOKEN_BAD, FG_SEC_NAMES[], FG_SEC_VALS[],
FG_SECRET_NAME.
Must set before use: FG_DIR (to source the file), and FG_REPO_ROOT / FG_DIR_PHYS if the repository
.env is to be considered.
May read: FG_TOKEN* after fg_token_resolve; FG_SEC_NAMES / FG_SEC_VALS after fg_secrets_load;
FG_SECRET_NAME (a variable name, never a value) after a successful fg_secret_hit /
fg_text_has_secret.
Env vars consulted (names only — this guide never states values): FORGE_KEYS_FILE, FORGE_ENV_FILE,
FORGE_KEYS_TIMEOUT, HOME.
- The process environment.
fg_env_cleanhas already carried*TOKENnames over as unexported shell variables, so a child cannot see them but this shell can. - The keys file —
FORGE_KEYS_FILE, default$HOME/api_keys.sh. Sourced in a childbashbounded bytimeout(FORGE_KEYS_TIMEOUT, default 10 s), stdout and stderr redirected to/dev/null, and only the one requested name printed into a pipe. The main shell never sources it. - The project
.env—FORGE_ENV_FILE, default<repository root>/.env. Parsed, never executed:NAME=VALUElines, optionalexport, optional matching single or double quotes, last assignment wins. The default is used only whenforge.shlies inside that repository; the.envof any other repository the command is run from is not read, withNOTE dotenv_not_trusted. An explicitFORGE_ENV_FILEis always honoured (subject to the file checks).
Within one source, names are tried in the order the hosts map lists them.
File safety. A keys file or .env that is not a regular, non-symlink file owned by this user with
mode & 077 == 0 is refused with WARNING keys_file_insecure and skipped. It is never read.
What counts as absent. Empty, shorter than 8 characters, a single repeated character, or a known dummy word
(changeme, change-me, replace-me, placeholder, your-…, <…>, xxx…, todo, none, null, dummy,
test, example…).
What counts as invalid. A value outside [A-Za-z0-9._~+/=-] or longer than 400 characters. It is never
sent — it could otherwise break out of the curl config line. An invalid value stops the search: a lower
source is never substituted, because the operator set that variable and a silent substitute would hide the
mistake.
- Keys file missing —
_fg_file_safereturns 2 (absent), which is not a warning; the search simply continues to the.env. - Keys file times out —
fg_secrets_loadreturns 1 andforge.shrefuses to publish, because the candidate set would be incomplete and the leak scan could miss a value. .envoutside the vendored repo — not read;dotenv_not_trustedis printed so the behaviour is visible.- A notes file that is the keys file or the
.env—fg_is_credential_filecomparesdevice:inodeafter following links, so a symlink or hard link to it is caught too. - A token shorter than 12 characters —
fg_secret_hitmatches the whole value rather than windows, so short secrets still scrub.
fg_token_resolvewalks the requested names: environment → keys file →.env, calling_fg_try_valuefor each._fg_try_valuereturns 0 (accept), 1 (skip, placeholder) or 2 (stop, invalid).- The keys file is read via
_fg_keys_value, a childbash --noprofile --norcthat doesset +x +a +e +u, moves stdout to fd 4 and/dev/nulls the rest,.-sources the file, unsets anybuiltin/printfoverride, and prints${!2-}— the value of the requested variable name only. _fg_dotenv_valueregex-matches^[[:space:]]*(export[[:space:]]+)?NAME[[:space:]]*=(.*)$line by line, strips a\r, removes matching quotes, and for unquoted values strips an inline#comment and trailing whitespace.fg_secrets_loadcollects: the resolved token; every*TOKENshell variable; every name of every hosts-map row (FG_ALL_TOKEN_VARS) and every*TOKENname the keys file defines (via_fg_keys_all); every*TOKENname in the.envin use and in the current repository's.env._fg_sec_addskips values under 8 characters and placeholders, and de-duplicates._fg_keys_allruns in a child that iteratescompgen -vand printsNAME NUL VALUE NULpairs into a pipe, so nothing touches a file.
scripts/forge/forge.sh— the main consumer (fg_token_resolve,fg_secrets_load,fg_is_credential_file).scripts/forge/lib/common.sh— providesfg_secret_hit,_fg_windowsand the arrays this file fills.scripts/forge/lib/http.sh— sends the resolved token as acurlconfig header on stdin.scripts/forge/live_probe.sh— reusesfg_secrets_loadfor the same value set in its leak check.
2026-12-04 — bash -n scripts/forge/lib/credentials.sh → exit 0. Sourcing check a reader can repeat:
bash -c '. scripts/forge/lib/common.sh && . scripts/forge/lib/credentials.sh && fg_keys_file_path && echo'Expected: the keys-file path (by default $HOME/api_keys.sh) followed by a blank line — no value is printed.