Skip to content

feat(egress-guard): derive the scope lists from the operator-approved task scope - #859

Merged
BaaRai merged 1 commit into
BaaRai:mainfrom
Dukotah:feat/egress-guard-scope-export
Dec 2, 2026
Merged

BaaRai merged 1 commit into
BaaRai:mainfrom
Dukotah:feat/egress-guard-scope-export

Conversation

@Dukotah

@Dukotah Dukotah commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Refs #421 (closed with "a focused egress-guard exporter PR is still welcome").

Problem

The per-task sandbox egress proxy builds its policy from PostgreSQL (taskSandboxRunInfo: operator-approved task_scope rows, the company grants frozen in task_scope_egress, enabled task allow rules). The optional egress-guard overlay still reads egress-guard/lists/scope-domains.txt and scope-cidrs.txt, edited by hand per engagement. Nothing connects the two, so for ARTEX_SANDBOX=off and for tools that bypass a proxy, the overlay can allow a scope the operator revoked or miss one the operator approved.

Change

  • egress-guard/export-scope.sh regenerates the two scope lists from the database. It runs artex -export-egress-guard-scope (new flag, same pattern as -rotate-secrets), checks the output, and replaces the lists by rename.
  • server.ExportEgressGuardScope does not derive scope itself. It calls taskSandboxRunInfoChecked (see below) and parses the result with sandbox.NewEgressPolicy, the same two steps the proxy path uses, and takes the union over the exported tasks. Agent-added, auto-added and expanded rows are therefore excluded for the same reason they are excluded from the proxy.
  • Default task set: every task that is not in a terminal state (db.IsTerminal). --tasks 12,15 names tasks explicitly; an unknown id is an error.
  • taskSandboxRunInfo is split into a wrapper and taskSandboxRunInfoChecked, which returns the same scope plus the read errors the function used to drop. The proxy path keeps ignoring them (a narrower scope is a denial there); the export needs them to refuse a partial result. No other behaviour change in that function.

Nothing changes for installs that do not run the script. entrypoint.sh, docker-compose.egress.yml and base-domains.txt are untouched, and the guard still reads the lists at startup, so it is restarted to apply an export.

Fail closed

  • Any database, scan or parse error fails the whole export; the Go command prints nothing to stdout in that case.
  • The script touches neither list until the command exited 0, every line is a well-formed domain/cidr entry, and the trailer counts match what was read. On any of those failures both files stay as they were and no staged file is left behind.
  • Each list is then replaced by its own rename, domains first. The two renames are not one transaction: if the second fails, scope-domains.txt is already new, and the script says so and exits non-zero. A list path that is a directory is refused before the export runs (mv would otherwise move the new list inside it and report success).
  • An export with no entry is refused unless --allow-empty. With EGRESS_MODE unset, empty scope lists make entrypoint.sh fall back to monitor, so writing them would open the overlay instead of closing it. The consequence: after the last approved scope is revoked, the previous lists stay in force until the operator sets EGRESS_MODE=enforce and passes --allow-empty. The script prints this.
  • A scope pattern that is not a hostname suffix is skipped and reported, in Go and again in the script, because the guard writes each suffix into a dnsmasq ipset=/<suffix>/artex_allow line.
  • IPv6 entries are skipped and reported: the guard's ipsets are IPv4-only.

Design choices a reviewer may want changed

  • Host-side script instead of a server-side writer. Mounting egress-guard/lists writable into the artex container would let agent commands edit their own allowlist in ARTEX_SANDBOX=off, which is the mode the overlay is for. The cost is that the export is an operator step, not automatic on scope change.
  • Union over tasks. The overlay has one allowlist for the whole ARTEX network, so it is coarser than the per-task proxy. Documented in the README.
  • Non-terminal tasks by default. A finished task that is re-run needs a new export. The alternative is to require --tasks always.
  • Empty export refused by default rather than changing the monitor fallback in entrypoint.sh, which I left alone to keep this PR to the exporter.
  • The command uses db.Open, like -rotate-secrets, so it takes the schema migration lock and runs the idempotent schema and migration step while the server is up. The README says that a binary passed after -- must therefore be the same version as the running server.
  • Source is the proxy derivation, not db.TaskEgressGrants. security: satisfy the target gate from operator-approved egress grants #838 added TaskEgressGrants for the target gate, which leaves out task allow rules. The export follows taskSandboxRunInfo because the overlay is meant to match what the proxy allows, allow rules included.
  • A domain is allowed by the overlay for whatever it resolves to, including a private address the proxy would refuse. Documented, not changed.
  • export-scope.sh is not added to the shellcheck list in ci.yml. Its behaviour is covered by a Go test that runs it with bash, as rotate_secrets_test.go does for rotate-secrets.sh. I can add it to the list if wanted.

Tests

server/egress_guard_export_test.go (PostgreSQL):

  • TestEgressGuardExportMatchesSandboxPolicy: operator rows and an enabled allow rule are exported; agent-added, auto-added, deny and disabled entries are not; the export equals the IPv4 content of the two tasks' proxy policies; a revoked row disappears from the next export.
  • TestEgressGuardExportDefaultsToNonTerminalTasks
  • TestEgressGuardExportFailsClosed: unknown task, nil database, closed database, an entry NewEgressPolicy cannot parse, and a pattern carrying dnsmasq syntax.

cmd/artex/egress_guard_export_test.go (unix, bash):

  • TestExportScopeScriptReplacesLists, TestExportScopeScriptAllowEmpty
  • TestExportScopeScriptLeavesListsOnFailure: failing command, missing trailer, count mismatch, no output, data after the trailer, malformed domain, non-IPv4 cidr, unknown line, empty export.
  • TestExportScopeScriptRefusesDirectoryTarget: a list path that is a directory is refused, the export command is not run, the other list is unchanged.
  • TestExportEgressGuardScopeToLists: real -export-egress-guard-scope output from PostgreSQL through the script into the lists.

What I ran

On this branch rebased onto main at 048e8a8 (after #775, #779, #789, #838), go1.26.11 linux/amd64, PostgreSQL 18 with password auth (CI uses 16):

gofmt -l $(git ls-files '*.go')              -> empty
go build ./...                               -> ok
GOOS=darwin (arm64, amd64), GOOS=windows (amd64) go build ./...  -> ok
go vet ./...                                 -> ok
go test -count=1 -p 1 ./...                  -> 23 packages ok, 2 fail (below)
go test -race -count=1 -p 1 ./cmd/artex ./server -> ok
shellcheck -S warning egress-guard/export-scope.sh -> clean
bash egress-guard/test_entrypoint_shim.sh    -> PASS
bash scripts/test-extract-changelog-section.sh -> PASS

The two failures are ghidra TestComposeOverlayConfigHasInternalNetworkAndHardening and sandbox TestComposeSandboxConfigBoundary, both docker compose config failed because this machine has no compose plugin. They are in packages this PR does not touch.

Manual, on the branch before the last rebase (the exporter code did not change in the rebase): a locally built binary driven through the script against a copy of the lists, with an empty database (refused, lists unchanged), an unknown task id (exit 1), a wrong database password (exit 1), and a seeded task with operator domain/CIDR/IP/IPv6 rows, an agent-added row and allow rules (operator and allow-rule entries written, agent row absent, IPv6 reported as skipped).

What I did not run

  • The default invocation, docker compose exec -T artex /app/artex -export-egress-guard-scope, in a real container. I had no Docker daemon; the script was driven by a stub and a locally built binary.
  • A real egress-guard restart with the generated lists (iptables, ipset, dnsmasq). The list format was checked against a Go copy of read_list only.
  • A failing second rename. That path is a message and an exit code and has no test.
  • PostgreSQL 16, govulncheck, the web job, and tests on macOS or Windows (cross-compiled only).

Related

🤖 Generated with Claude Code

… 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 (BaaRai#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>
@BaaRai
BaaRai merged commit d1b371b into BaaRai:main Dec 2, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants