Files
ARTEX/egress-guard/export-scope.sh
DukotahandClaude Fable 5.2 ebb37c20bb feat(egress-guard): derive the scope lists from the operator-approved task scope
egress-guard reads hand-edited lists/scope-domains.txt and scope-cidrs.txt,
while the sandbox egress proxy builds its policy from PostgreSQL. Nothing
connected the two, so the overlay could drift from the approved scope (#421).

egress-guard/export-scope.sh regenerates both lists through the new
artex -export-egress-guard-scope. The export calls taskSandboxRunInfoChecked
(the error-returning form of taskSandboxRunInfo) and sandbox.NewEgressPolicy,
the derivation and parser of the per-task proxy, so only operator-approved
task scope, frozen company grants and operator task allow rules are exported.
Agent-added, auto-added and expanded rows are not.

Fail closed: taskSandboxRunInfoChecked reports the read errors the proxy path
ignores, any error fails the whole export, and the script touches neither
list until a complete, well-formed export was read; each list is then
replaced by rename. An empty export is refused unless --allow-empty, because
empty lists with EGRESS_MODE unset make the guard fall back to monitor.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-12-02 10:27:57 -08:00

123 lines
5.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Regenerate egress-guard/lists/scope-domains.txt and scope-cidrs.txt from the
# operator-approved task scope in the ARTEX database, so the overlay enforces
# the same boundary as the per-task sandbox egress proxy instead of a list
# maintained by hand. See README.md, "Deriving the scope lists from ARTEX".
#
# Usage:
# egress-guard/export-scope.sh [--lists DIR] [--tasks IDS] [--allow-empty] [-- COMMAND...]
#
# --lists DIR directory holding the scope lists (default: lists/ next to this script)
# --tasks IDS comma-separated task ids (default: every task that is not finished)
# --allow-empty write the lists even when the export has no entry. Only safe with
# EGRESS_MODE=enforce set explicitly: with EGRESS_MODE unset, empty
# scope lists make egress-guard fall back to monitor and block nothing.
# COMMAND how to run the artex binary
# (default: docker compose exec -T artex /app/artex)
#
# Guarantees:
# 1. The lists are replaced only after the whole export was read and checked:
# the command exited 0, every line is a well-formed entry, and the trailer
# counts match. Until then neither list is touched.
# 2. Each list is replaced by an atomic rename within DIR, one after the
# other. The two renames are not one transaction: if the second fails
# (the script exits non-zero and says so), scope-domains.txt is already new.
# 3. egress-guard reads the lists at startup only. Restart it to apply them.
set -euo pipefail
umask 022
DIR="$(cd "$(dirname "$0")" && pwd)"
LISTS="$DIR/lists"
TASKS=""
ALLOW_EMPTY=0
CMD=(docker compose exec -T artex /app/artex)
usage() {
sed -n '2,25p' "$0" | sed 's/^# \{0,1\}//'
}
die() {
echo "[export-scope] ERROR: $*" >&2
echo "[export-scope] scope lists left unchanged." >&2
exit 1
}
while [ $# -gt 0 ]; do
case "$1" in
--lists) [ $# -ge 2 ] || { usage >&2; exit 2; }; LISTS="$2"; shift 2 ;;
--tasks) [ $# -ge 2 ] || { usage >&2; exit 2; }; TASKS="$2"; shift 2 ;;
--allow-empty) ALLOW_EMPTY=1; shift ;;
-h|--help) usage; exit 0 ;;
--) shift; [ $# -gt 0 ] || { usage >&2; exit 2; }; CMD=("$@"); break ;;
*) echo "export-scope.sh: unknown argument: $1" >&2; usage >&2; exit 2 ;;
esac
done
[ -d "$LISTS" ] || die "lists directory not found: $LISTS"
# mv onto a directory would move the new list inside it and report success.
for target in "$LISTS/scope-domains.txt" "$LISTS/scope-cidrs.txt"; do
if [ -d "$target" ]; then die "$target is a directory, not a list file"; fi
done
# Staged in the same directory as the targets so rename(2) is atomic.
RAW="$(mktemp "$LISTS/.scope-export.XXXXXX")"
NEW_DOMAINS="$RAW.domains"
NEW_CIDRS="$RAW.cidrs"
cleanup() {
rm -f "$RAW" "$NEW_DOMAINS" "$NEW_CIDRS"
}
trap cleanup EXIT
ARGS=(-export-egress-guard-scope)
[ -n "$TASKS" ] && ARGS+=(-export-egress-guard-tasks "$TASKS")
"${CMD[@]}" "${ARGS[@]}" > "$RAW" || die "export command failed: ${CMD[*]} ${ARGS[*]}"
HEADER="# Generated by egress-guard/export-scope.sh from the operator-approved ARTEX task scope.
# Do not edit: the next export overwrites this file. Change the scope in ARTEX (Task Scope)."
printf '%s\n' "$HEADER" > "$NEW_DOMAINS"
printf '%s\n' "$HEADER" > "$NEW_CIDRS"
domains=0
cidrs=0
trailer=""
while IFS= read -r line || [ -n "$line" ]; do
line="${line%$'\r'}"
[ -z "$trailer" ] || die "data after the export trailer"
case "$line" in
"# end "*) trailer="$line" ;;
"# tasks: "*) printf '%s\n' "$line" | tee -a "$NEW_DOMAINS" >> "$NEW_CIDRS" ;;
"# skipped "*) echo "[export-scope] WARN: not exported: ${line#\# skipped }" >&2 ;;
"#"*|"") ;;
"domain "*)
v="${line#domain }"
[[ "$v" =~ ^[a-z0-9_]([a-z0-9_.-]*[a-z0-9_])?$ ]] || die "malformed domain entry"
printf '%s\n' "$v" >> "$NEW_DOMAINS"
domains=$((domains + 1))
;;
"cidr "*)
v="${line#cidr }"
[[ "$v" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}/[0-9]{1,2}$ ]] || die "malformed cidr entry"
printf '%s\n' "$v" >> "$NEW_CIDRS"
cidrs=$((cidrs + 1))
;;
*) die "unexpected line in export output" ;;
esac
done < "$RAW"
[ "$trailer" = "# end domains=$domains cidrs=$cidrs" ] \
|| die "export output is truncated or inconsistent (read domains=$domains cidrs=$cidrs, trailer: ${trailer:-missing})"
if [ $((domains + cidrs)) -eq 0 ] && [ "$ALLOW_EMPTY" -ne 1 ]; then
die "the export has no entry. With EGRESS_MODE unset, empty scope lists make egress-guard fall back to monitor (no blocking). Set EGRESS_MODE=enforce in .env and re-run with --allow-empty to write empty lists"
fi
mv -f "$NEW_DOMAINS" "$LISTS/scope-domains.txt" \
|| die "could not replace $LISTS/scope-domains.txt"
if ! mv -f "$NEW_CIDRS" "$LISTS/scope-cidrs.txt"; then
echo "[export-scope] ERROR: could not replace $LISTS/scope-cidrs.txt; scope-domains.txt was already replaced. Fix the cause and re-run before restarting egress-guard." >&2
exit 1
fi
echo "[export-scope] wrote $domains domain suffix(es) and $cidrs CIDR(s) to $LISTS"
echo "[export-scope] restart egress-guard to apply: docker compose -f docker-compose.yml -f docker-compose.egress.yml restart egress-guard"