interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / spark / forged-public.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Trusted supervisor adapter. The seeded reader refuses links in ANY component, |
| 3 | # non-regular files and files above 1048576 bytes before emitting any bytes. |
| 4 | public_die() { echo "forged-public: $*" >&2; return 2; } |
| 5 | public_init() { |
| 6 | PUBLIC=${FORGED_PUBLIC_DIR:-$DATA/../forge-public} |
| 7 | READ_HOME=${FORGED_READ_HOME:-$HOME} |
| 8 | READER=${FORGED_PUBLIC_READER:-$(dirname "$BIN")/public-read} |
| 9 | [[ -x $READER ]] || { public_die "missing pinned public-read executable: $READER"; return 2; } |
| 10 | [[ ! -L $PUBLIC ]] || { public_die 'export directory is a symlink'; return 2; } |
| 11 | PUBLIC=$(realpath -m "$PUBLIC") || return 2 |
| 12 | local data_real |
| 13 | data_real=$(realpath -e "$DATA") || return 2 |
| 14 | case "$PUBLIC/" in "$data_real/"*) public_die 'export is inside writable data'; return 2;; esac |
| 15 | case "$data_real/" in "$PUBLIC/"*) public_die 'export contains writable data'; return 2;; esac |
| 16 | [[ $PUBLIC != / && $PUBLIC != "$HOME" && $PUBLIC != "$READ_HOME" ]] || return 2 |
| 17 | # The mirror runs this reader as the owner. Refuse one the sandboxed child could replace. |
| 18 | READER=$(realpath -e "$READER") || return 2 |
| 19 | case "$READER" in "$data_real"/*|"$PUBLIC"/*) |
| 20 | public_die "refusing public-read inside writable data or export: $READER"; return 2;; esac |
| 21 | case "$PUBLIC/" in "$READ_HOME/handoffs/"*|"$READ_HOME/.radicle/"*|"$READ_HOME/.local/"*) |
| 22 | public_die 'export overlaps source trees'; return 2;; esac |
| 23 | mkdir -m 700 -p -- "$PUBLIC" || return 2 |
| 24 | # A mistaken path must never publish an unrelated existing directory. |
| 25 | local entry |
| 26 | while IFS= read -r -d '' entry; do |
| 27 | case ${entry##*/} in RIDS.md|nodes.txt|counts.snapshot|*.verdict.txt|*.receipt.txt|.mirror.*) ;; |
| 28 | *) public_die 'export contains an unrelated entry'; return 2;; esac |
| 29 | [[ -f $entry && ! -L $entry ]] || { public_die 'unsafe export entry'; return 2; } |
| 30 | done < <(find "$PUBLIC" -mindepth 1 -maxdepth 1 -print0) |
| 31 | export FORGED_PUBLIC_DIR=$PUBLIC |
| 32 | } |
| 33 | public_sync() { |
| 34 | local src name tmp f |
| 35 | local -A wanted=() |
| 36 | local -a sources=("$READ_HOME/handoffs/radicle/RIDS.md" "$READ_HOME/.local/state/aien-factory/nodes.txt") |
| 37 | shopt -s nullglob |
| 38 | sources+=("$READ_HOME/handoffs/hive/manager/reviews/"*.verdict.txt |
| 39 | "$READ_HOME/handoffs/hive/forge/receipts/"*.receipt.txt) |
| 40 | for src in "${sources[@]}"; do |
| 41 | name=${src##*/} |
| 42 | tmp=$(mktemp "$PUBLIC/.mirror.XXXXXX") || return 2 |
| 43 | if "$READER" "$src" > "$tmp"; then |
| 44 | wanted["$name"]=1 |
| 45 | if [[ ! -L $PUBLIC/$name ]] && cmp -s -- "$tmp" "$PUBLIC/$name"; then |
| 46 | rm -f -- "$tmp" || return 2 |
| 47 | else |
| 48 | mv -Tf -- "$tmp" "$PUBLIC/$name" || return 2 |
| 49 | fi |
| 50 | else |
| 51 | rm -f -- "$tmp" || return 2 |
| 52 | # A removed, oversized or unsafe source revokes its previous public copy. |
| 53 | if [[ -e $PUBLIC/$name || -L $PUBLIC/$name ]]; then |
| 54 | echo "forged-public: removing unavailable or refused input $name" >&2 |
| 55 | fi |
| 56 | fi |
| 57 | done |
| 58 | for f in "$PUBLIC/RIDS.md" "$PUBLIC/nodes.txt" "$PUBLIC/"*.verdict.txt "$PUBLIC/"*.receipt.txt; do |
| 59 | name=${f##*/} |
| 60 | if [[ ! ${wanted[$name]+yes} ]]; then rm -f -- "$f" || return 2; fi |
| 61 | done |
| 62 | } |
| 63 | # Match the first table and field/deny limits in server/repos.om and server/radsrc.om: radsrc reads |
| 64 | # at most FAMAX (256, server/util.om) rows of the first table. Nothing is dropped silently: a row past |
| 65 | # FAMAX and a row outside the row rules each get one stderr line naming the row (its table row number |
| 66 | # and the repo cell, control bytes shown as ?). A row on the deny list is left out on purpose, without one. |
| 67 | public_rids() { |
| 68 | [[ -f $PUBLIC/RIDS.md && ! -L $PUBLIC/RIDS.md ]] || return 0 |
| 69 | awk -F '|' ' |
| 70 | function warn(why) { r = $2; gsub(/[^ -~]/, "?", r); printf "forged-public: RIDS.md row %d (%s) not bound: %s\n", rows, substr(r, 1, 120), why > "/dev/stderr" } |
| 71 | /^\| repo \| RID \| default branch/ { if (seen) exit; seen=1; getline; next } |
| 72 | seen && !/^\|/ { exit } |
| 73 | seen { rows++; for(i=2;i<=4;i++) gsub(/^ +| +$/, "", $i) |
| 74 | if (rows > 256) { warn("past FAMAX (256 rows of the first table)"); next } |
| 75 | if (!($2 ~ /^[A-Za-z0-9_.-]+$/ && length($2)<=100)) { warn("name outside the row rules"); next } |
| 76 | if (!($3 ~ /^rad:[A-Za-z0-9]+$/ && length($3)>=6 && length($3)<=64)) { warn("RID outside the row rules"); next } |
| 77 | if (!($4 ~ /^[A-Za-z0-9_.\/-]+$/ && length($4)<=60)) { warn("branch outside the row rules"); next } |
| 78 | if ($2=="omega-pearl" || $2=="aien-crypto" || $3=="rad:z2ft64NFLsKzWttCXw6NWNbRQLggQ") next |
| 79 | print substr($3,5) |
| 80 | }' "$PUBLIC/RIDS.md" | LC_ALL=C sort -u |
| 81 | } |
| 82 | # store_names ROOT: the projects of the native store at ROOT that the store-reading build serves, one |
| 83 | # name a line, in catalog order: the forged-sandbox.sh FORGED_STORE path binds only these. The rule is |
| 84 | # store/repos.om's (every public catalog line, Drake 2026-11-14), less what forged would refuse by the |
| 85 | # line alone: a line without five tab-separated fields, a name outside [A-Za-z0-9_.-] or longer than |
| 86 | # 100 bytes (or . or ..), the deny list (server/repos.om fdeny: the names omega-pearl and |
| 87 | # aien-crypto, and a records/1 alias rad:z2ft64NFLsKzWttCXw6NWNbRQLggQ), public lines past FAMAX (256). |
| 88 | # Each line left out for a rule or for FAMAX gets one stderr line naming it; private and denied lines |
| 89 | # are left out without one. A records/1 that is missing or a link is not read here (forged refuses |
| 90 | # that project itself; binding it gives the sandbox nothing the catalog does not name). |
| 91 | store_names() { |
| 92 | local root=${1:?store_names ROOT} name rec |
| 93 | [[ -f $root/catalog.tsv && ! -L $root/catalog.tsv ]] || return 0 |
| 94 | while IFS= read -r name; do |
| 95 | rec=$root/projects/$name/records/1 |
| 96 | if [[ -f $rec && ! -L $rec ]] && grep -qx 'alias rad:z2ft64NFLsKzWttCXw6NWNbRQLggQ' -- "$rec"; then continue; fi |
| 97 | printf '%s\n' "$name" |
| 98 | done < <(awk -F '\t' ' |
| 99 | function warn(why) { r = $1; gsub(/[^ -~]/, "?", r); printf "forged-public: catalog line %d (%s) not bound: %s\n", NR, substr(r, 1, 120), why > "/dev/stderr" } |
| 100 | NF != 5 { warn("not five tab-separated fields"); next } |
| 101 | $5 != "public" { next } |
| 102 | { pub++ } |
| 103 | pub > 256 { warn("past FAMAX (256 public lines)"); next } |
| 104 | $1 !~ /^[A-Za-z0-9_.-]+$/ || length($1) > 100 || $1 == "." || $1 == ".." { warn("name outside the name rule"); next } |
| 105 | $1 == "omega-pearl" || $1 == "aien-crypto" { next } |
| 106 | { print $1 }' "$root/catalog.tsv") |
| 107 | } |
| 108 | public_repos() { |
| 109 | local rid repo resolved |
| 110 | while IFS= read -r rid; do |
| 111 | repo=$READ_HOME/.radicle/storage/$rid |
| 112 | [[ -d $repo && ! -L $repo ]] || continue |
| 113 | resolved=$(realpath -e "$repo") || return 2 |
| 114 | printf '%s\t%s\t%s\n' "$rid" "$resolved" "$(stat -Lc '%d:%i' "$repo")" |
| 115 | done < <(public_rids) |
| 116 | } |
| 117 | public_digest() { public_repos | sha256sum | cut -d' ' -f1; } |