Skip to content

Latest commit

 

History

History
155 lines (118 loc) · 8.47 KB

credentials.md

File metadata and controls

155 lines (118 loc) · 8.47 KB

credentials.sh (scripts/forge/lib)

Revision: 1 Last modified: 2026-12-04T18:15:21Z

Overview

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.

Prerequisites

  • bash 4+, timeout (coreutils), stat with -c, id.
  • Called after lib/common.sh (it uses fg_msg, fg_ok_varname, FG_SEC_NAMES / FG_SEC_VALS, FG_SEC_WIN).
  • The caller should already have FG_REPO_ROOT and FG_DIR_PHYS set — they decide whether the repository .env is trusted at all.

Usage examples

. "$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 applicable

Exit codes

The 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.

Contract — what a caller must set, what it may read

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.

Token precedence and the rules around it

  1. The process environment. fg_env_clean has already carried *TOKEN names over as unexported shell variables, so a child cannot see them but this shell can.
  2. The keys file — FORGE_KEYS_FILE, default $HOME/api_keys.sh. Sourced in a child bash bounded by timeout (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.
  3. The project .env — FORGE_ENV_FILE, default <repository root>/.env. Parsed, never executed: NAME=VALUE lines, optional export , optional matching single or double quotes, last assignment wins. The default is used only when forge.sh lies inside that repository; the .env of any other repository the command is run from is not read, with NOTE dotenv_not_trusted. An explicit FORGE_ENV_FILE is 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.

Edge cases

  • Keys file missing — _fg_file_safe returns 2 (absent), which is not a warning; the search simply continues to the .env.
  • Keys file times out — fg_secrets_load returns 1 and forge.sh refuses to publish, because the candidate set would be incomplete and the leak scan could miss a value.
  • .env outside the vendored repo — not read; dotenv_not_trusted is printed so the behaviour is visible.
  • A notes file that is the keys file or the .env — fg_is_credential_file compares device:inode after following links, so a symlink or hard link to it is caught too.
  • A token shorter than 12 characters — fg_secret_hit matches the whole value rather than windows, so short secrets still scrub.

Internal behaviour

  1. fg_token_resolve walks the requested names: environment → keys file → .env, calling _fg_try_value for each. _fg_try_value returns 0 (accept), 1 (skip, placeholder) or 2 (stop, invalid).
  2. The keys file is read via _fg_keys_value, a child bash --noprofile --norc that does set +x +a +e +u, moves stdout to fd 4 and /dev/nulls the rest, .-sources the file, unsets any builtin/printf override, and prints ${!2-} — the value of the requested variable name only.
  3. _fg_dotenv_value regex-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.
  4. fg_secrets_load collects: the resolved token; every *TOKEN shell variable; every name of every hosts-map row (FG_ALL_TOKEN_VARS) and every *TOKEN name the keys file defines (via _fg_keys_all); every *TOKEN name in the .env in use and in the current repository's .env. _fg_sec_add skips values under 8 characters and placeholders, and de-duplicates.
  5. _fg_keys_all runs in a child that iterates compgen -v and prints NAME NUL VALUE NUL pairs into a pipe, so nothing touches a file.

Related scripts

  • scripts/forge/forge.sh — the main consumer (fg_token_resolve, fg_secrets_load, fg_is_credential_file).
  • scripts/forge/lib/common.sh — provides fg_secret_hit, _fg_windows and the arrays this file fills.
  • scripts/forge/lib/http.sh — sends the resolved token as a curl config header on stdin.
  • scripts/forge/live_probe.sh — reuses fg_secrets_load for the same value set in its leak check.

Last verified

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.