Skip to content

Latest commit

 

History

History
155 lines (122 loc) · 8.64 KB

http.md

File metadata and controls

155 lines (122 loc) · 8.64 KB

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

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 to curl's standard input (curl -K -), and printf is 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 curl starts (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: curl runs as a background job that the shell waits 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.

Prerequisites

  • curl, timeout (coreutils), base64, sha256sum, jq.
  • Sourced after lib/common.sh (needs fg_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's prov_init), and optionally FG_HDRS[].
  • FG_TMP must be initialised via fg_tmp_init before the first request.

Usage examples

. "$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=1

Exit codes

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

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

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.

Edge cases

  • 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 http is protocol unless it is loopback and FORGE_TEST_ALLOW_HTTP_LOOPBACK=1.
  • A malformed URL (userinfo, whitespace, backslash, stray percent outside the path) — fg_origin returns 1, FG_NET=protocol.
  • A hung curl is killed by timeout -k 5 (tmo+10) without --foreground, so its whole process group dies with it. 124/137/28 map to timeout.
  • A signal mid-request ends with REASON: interrupted and RESULT: 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 with fg_secret_hit), so a hostile or echoing server cannot leak a credential back through the evidence lines.
  • Bad JSON makes fg_json print nothing (status of jq is 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 EXIT trap, and the cleanup is guarded: it only acts on a path matching */forge.??????, removes the known file names only, and uses rmdir (never rm -rf).

Internal behaviour

  1. fg_http METHOD PATH [PAYLOAD_FILE] simply calls fg_http_url with $FG_API_BASE$PATH.
  2. fg_http_url canonicalises the URL through fg_origin, compares it with FG_API_ORIGIN (the pin), picks --proto =https (or =http for an allowed loopback), accepts only GET|POST|PATCH|PUT, and clamps the timeout.
  3. 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.
  4. The token becomes header = "<FG_AUTH_FMT>" built with printf -v (a builtin), piped into curl on stdin. The charset check in credentials.sh rules out quotes, backslashes and newlines, so the line cannot be broken out of.
  5. curl runs in the background; FG_CHILD=$! is recorded so a trap can kill exactly that child; the shell waits on it.
  6. A non-zero exit maps to FG_NET (124|137|28→timeout, 6→dns, 7→refused, the TLS set→tls, 1|3|47→protocol, otherwise other) and returns 1.
  7. On success it reads FG_CODE and the peer fields from $FG_TMP/code, validates them against safe character sets (any mismatch → unsafe), derives the leaf certificate digest from curl's %{certs} PEM, and picks a request id from the headers.

Related scripts

  • scripts/forge/forge.sh — the main consumer; supplies the pin and calls fg_net_end on failure.
  • scripts/forge/lib/common.sh — scrubbing and exit helpers used on every printed line.
  • scripts/forge/lib/credentials.sh — produces FG_TOKEN and the secret set used to detect reflection.
  • scripts/forge/providers/*.sh — set FG_AUTH_FMT, FG_HDRS, and call fg_http / fg_http_url.
  • scripts/forge/live_probe.sh — sets FORGE_TRACE_PEER=1 so every answer prints a PEER: line.

Last verified

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.