Revision: 1 Last modified: 2026-12-04T18:15:21Z
This is a library. It is sourced, never executed. No shebang, mode rw-rw-r--.
scripts/forge/lib/http.sh is the single HTTP client of scripts/forge/forge.sh. Every request the tool
makes goes through it, which is what makes the guarantees checkable in one place:
- The token is never in an
argv, a URL, a child environment or a file. It is written as a config line tocurl's standard input (curl -K -), andprintfis a builtin, so it never becomes a process argument. - The token is sent only to the origin of the pinned API base. Any other URL is refused before
curlstarts (FG_NET=pin), so a redirect can never carry the credential elsewhere — and redirects are not followed anyway (--max-redirs 0). curl's own error text is never read or printed, because it can contain a URL with a credential. Only its exit status is mapped onto a closed set of network classes.- A signal interrupts the request immediately rather than waiting for the timeout:
curlruns as a background job that the shellwaits on, so a trap can stop that one child at once.
It also records peer identity (§11.4.289(D)) for every answer — resolved IP and port, HTTP version,
ssl_verify_result, the leaf certificate's sha256 and subject, and the provider's request id when it sends
one — so a caller can prove what actually answered rather than trusting a status code.
curl,timeout(coreutils),base64,sha256sum,jq.- Sourced after
lib/common.sh(needsfg_msg,fg_line,fg_scrub,fg_secret_hit,fg_ok_port,fg_api_err,fg_end). - The caller must have set
FG_API_BASE(the pinned base) and, to authenticate,FG_TOKEN,FG_AUTH_FMT(set by the provider'sprov_init), and optionallyFG_HDRS[]. FG_TMPmust be initialised viafg_tmp_initbefore the first request.
. "$FG_DIR/lib/http.sh" || fg_end USAGE 2 install_broken
fg_tmp_init || fg_end GAP 3 tmp_unavailable # private temp dir + EXIT/HUP/INT/TERM traps
# a request relative to the pinned API base
if fg_http GET "/repos/owner/repo"; then
fg_line "status=$FG_CODE" # a three-digit code; any code counts as an answer
jq -r '.full_name' <"$FG_TMP/resp"
else
fg_net_end # maps FG_NET to REASON/RESULT and exits
fi
# a request to an absolute URL — must share the origin of FG_API_BASE
fg_http_url GET "$url"
# a write with a JSON body from a file
fg_http POST "/repos/owner/repo/releases" "$FG_TMP/payload.json"
# endpoint identity of the last answer
fg_peer_fields # peer=IP:PORT http=V tls_verify=N cert_sha256=H subject=S request_id=NAME=VALUE
fg_peer_trace "GET /repos/owner/repo" # prints a PEER: line only when FORGE_TRACE_PEER=1fg_http / fg_http_url return a status; they do not exit the process (except on a signal, via
fg_interrupted).
| Function | Returns | Meaning |
|---|---|---|
fg_http / fg_http_url |
0 |
an HTTP answer arrived — any status code, including 4xx/5xx |
fg_http / fg_http_url |
1 |
no answer arrived; FG_NET names the class |
fg_tmp_init |
0 / 1 |
private temp dir created and traps installed / mktemp or chmod failed |
fg_origin URL |
0 + prints |
a clean scheme://host[:port] / 1 when the URL is not clean |
fg_origin_is_loopback URL |
0 / 1 |
http://127.0.0.1|localhost|[::1]:port / otherwise |
fg_peer_fields |
prints | the identity words for the last answer |
fg_json FILTER |
prints | jq -r over the last body; empty on bad JSON |
fg_keys_summary |
prints | object keys=a,b / array items=N item_keys=… / type=… |
fg_net_end |
exits | 4 (RESULT: API) with network_<class> or redirect_refused |
fg_interrupted RC |
exits | 129 / 130 / 143 — REASON: interrupted, RESULT: INTERRUPTED |
FG_NET is one word of the closed set: timeout, dns, refused, tls, protocol, other, pin.
Must set: FG_API_BASE (pinned base URL), FG_API_ORIGIN (its origin, from fg_origin), FG_TMP
(after fg_tmp_init), and — for authenticated requests — FG_TOKEN plus the provider's FG_AUTH_FMT.
May read after a call: FG_CODE (three digits), the body in $FG_TMP/resp, headers in $FG_TMP/hdrs,
FG_NET, and FG_PEER_IP / FG_PEER_PORT / FG_PEER_HTTP / FG_PEER_TLS / FG_PEER_CERT /
FG_PEER_SUBJECT / FG_PEER_REQID.
Env vars: FORGE_HTTP_TIMEOUT (default 60 s; a value that is not 1–4 digits falls back to 60),
FORGE_TRACE_PEER (1 enables PEER: lines), FORGE_TEST_ALLOW_HTTP_LOOPBACK (1 permits plain http
only for a loopback API base — the test seam; production stays https), TMPDIR.
- A URL outside the pinned origin —
FG_NET=pin, status 1, and the request never starts. A 3xx is never followed (--max-redirs 0), and a provider that needs one hop (GitHub renames) must do it itself on the same origin. - Plain
httpisprotocolunless it is loopback andFORGE_TEST_ALLOW_HTTP_LOOPBACK=1. - A malformed URL (userinfo, whitespace, backslash, stray percent outside the path) —
fg_originreturns 1,FG_NET=protocol. - A hung
curlis killed bytimeout -k 5 (tmo+10)without--foreground, so its whole process group dies with it.124/137/28map totimeout. - A signal mid-request ends with
REASON: interruptedandRESULT: INTERRUPTED. The message is explicit that a write may or may not have reached the forge and the caller should read the release back — never a silent truncation. - A server-supplied field that reflects a token value is replaced with
unsafe(checked withfg_secret_hit), so a hostile or echoing server cannot leak a credential back through the evidence lines. - Bad JSON makes
fg_jsonprint nothing (status ofjqis dropped), so callers must treat empty as "unknown", not as "absent". - A response body with more than 2000 characters of key summary is truncated by
fg_keys_summary; it lists key names and counts, never values. - The temp dir is removed by an
EXITtrap, and the cleanup is guarded: it only acts on a path matching*/forge.??????, removes the known file names only, and usesrmdir(neverrm -rf).
fg_http METHOD PATH [PAYLOAD_FILE]simply callsfg_http_urlwith$FG_API_BASE$PATH.fg_http_urlcanonicalises the URL throughfg_origin, compares it withFG_API_ORIGIN(the pin), picks--proto =https(or=httpfor an allowed loopback), accepts onlyGET|POST|PATCH|PUT, and clamps the timeout.- It builds
curl -q -sS --globoff --max-redirs 0 --proto … --connect-timeout 15 -K - -X METHOD -o resp -D hdrs -w '%{http_code} %{remote_ip} …'plus the provider headers and, for a body,-H 'Content-Type: application/json' --data-binary @file. - The token becomes
header = "<FG_AUTH_FMT>"built withprintf -v(a builtin), piped intocurlon stdin. The charset check incredentials.shrules out quotes, backslashes and newlines, so the line cannot be broken out of. curlruns in the background;FG_CHILD=$!is recorded so a trap cankillexactly that child; the shellwaits on it.- A non-zero exit maps to
FG_NET(124|137|28→timeout,6→dns,7→refused, the TLS set→tls,1|3|47→protocol, otherwiseother) and returns 1. - On success it reads
FG_CODEand the peer fields from$FG_TMP/code, validates them against safe character sets (any mismatch →unsafe), derives the leaf certificate digest fromcurl's%{certs}PEM, and picks a request id from the headers.
scripts/forge/forge.sh— the main consumer; supplies the pin and callsfg_net_endon failure.scripts/forge/lib/common.sh— scrubbing and exit helpers used on every printed line.scripts/forge/lib/credentials.sh— producesFG_TOKENand the secret set used to detect reflection.scripts/forge/providers/*.sh— setFG_AUTH_FMT,FG_HDRS, and callfg_http/fg_http_url.scripts/forge/live_probe.sh— setsFORGE_TRACE_PEER=1so every answer prints aPEER:line.
2026-12-04 — bash -n scripts/forge/lib/http.sh → exit 0. Sourcing/behaviour check a reader can repeat:
bash -c '. scripts/forge/lib/common.sh && . scripts/forge/lib/http.sh \
&& o=$(fg_origin https://api.github.com/x) && printf "%s\n" "$o" \
&& fg_origin_is_loopback http://127.0.0.1:8080 && echo loopback-ok'Expected: https://api.github.com then loopback-ok.