Repository navigation
426 lines (411 loc) · 19.1 KB
/
Copy pathsecure-host.yml
File metadata and controls
426 lines (411 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# secure-host against its pinned image: the two-job contract of its
# README, its inputs, and the world-write policy at run time. Each job
# runs code as root before the action, on a fresh VM, with a read-only
# token and no secrets. Host checks are Justfile recipes; `before` ones
# are positive controls for the `after` ones.
name: secure-host
on:
pull_request:
push:
branches: [main]
permissions: {}
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
# A scripted dispatch through the wrappers' agent job, cgwalters-agent.yml,
# on a fresh VM: prepare, agentic-job's runner-user admission, config
# --host --prepared, sandbox prepare and the pinned host action, then
# the setup script and run's own
# proof that the host is secured, all as the runner's user; then the
# pinned scripted agent (agentic-job's `fake`, with a local setup fixture) through
# run with no GitHub token, and its handback through the pinned check.
# Deterministic: no model, inference, tailnet credentials or secrets,
# and no apply, so nothing is published. The wrappers keep refusing
# `fake` from callers; this is the repository's own base configuration.
wrapper-policy:
permissions:
actions: read
contents: read
uses: cgwalters-forge/agentic-job/.github/workflows/policy.yml@2b937aaac81da06d8bff5f317e58df6ad7063e1e # v0.6.16
with:
id: wrapper-smoke
runner: '"ubuntu-26.04"'
repo: cgwalters-forge/actions
allow: workflow/review.toml
kind: analysis
outputs: add_comment,noop
max-outputs: '1'
# Any issue would do: check consults none, and no apply job posts.
comment-target: '103'
notify: none
agent: fake
# A built-in fake-profile is incompatible with setup. The setup
# script installs both the test tools and the scripted session.
setup: workflow/wrapper-smoke-setup.sh
max-requests: '0'
budget: '1'
task: Prove the agent job's host is secured for the runner's user and hand back a checked comment.
config: .agentic-job-source/secure-host/hosted.toml
artifact-prefix: wrapper-smoke-
wrapper-runner:
needs: wrapper-policy
runs-on: ubuntu-26.04
timeout-minutes: 20
permissions:
contents: read
actions: read
outputs:
exit: ${{ steps.run.outputs.exit }}
safe-outputs-artifact-id: ${{ steps.run.outputs.safe-outputs-artifact-id }}
safe-outputs-artifact-digest: ${{ steps.run.outputs.safe-outputs-artifact-digest }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: cgwalters-forge/agentic-job
ref: 2b937aaac81da06d8bff5f317e58df6ad7063e1e # v0.6.16
path: .agentic-job-source
persist-credentials: false
- uses: ./.agentic-job-source/prepare
with:
binary-artifact-id: ${{ needs.wrapper-policy.outputs.binary-artifact-id }}
policy-artifact-id: ${{ needs.wrapper-policy.outputs.policy-artifact-id }}
- name: Admit the runner-user configuration
env:
CALLER_PRIVATE: ${{ toJSON(github.event.repository.private) }}
AGENT_USER: runner
run: python3 .agentic-job-source/workflow/runner-setup.py admit
- name: Check inference proxy reachability
run: agentic-job secure-host inference .agentic-job/config.toml
- name: The host's configuration
id: host-config
run: |
install -D -m 0755 .agentic-job/agentic-job "$RUNNER_TEMP/agentic-job-bin/agentic-job"
agentic-job config --host --prepared --from .agentic-job/config.toml --string "host.env.PATH=$RUNNER_TEMP/agentic-job-bin:/usr/local/bin:/usr/bin:/bin:$RUNNER_TEMP/agent-home/.local/bin" > "$RUNNER_TEMP/agentic-job-host.toml"
python3 -c 'import sys, tomllib; c = tomllib.load(open(sys.argv[1], "rb")); print("egress=" + ("write-policy" if c.get("egress", {}).get("write-policy", False) else "off"))' "$RUNNER_TEMP/agentic-job-host.toml" >> "$GITHUB_OUTPUT"
- name: Prepare the agents and trusted helper
run: sudo /usr/local/bin/agentic-job sandbox prepare --config "$RUNNER_TEMP/agentic-job-host.toml"
- uses: cgwalters-forge/actions/secure-host@456a5f663b73ac367c331aaf8ed2c888e481a98c # from main
with:
config: ${{ runner.temp }}/agentic-job-host.toml
egress: ${{ steps.host-config.outputs.egress }}
- name: The setup script
env:
AGENT_HOME: ${{ runner.temp }}/agent-home
run: python3 .agentic-job-source/workflow/runner-setup.py setup
# What run/action.yml checks before it starts the agent; then that
# the runner's user has lost sudo, and that the agentic-job its
# commands find on the agent's PATH, the installed configuration's,
# is the policy binary run starts, not the image's older copy or
# the name collision installed by the setup fixture.
- name: The runner's user runs the policy binary on a secured host
run: |
python3 -c 'import sys, tomllib; c = tomllib.load(open("/etc/agentic-job/config.toml", "rb")); sys.exit(0 if c.get("host", c.get("sandbox", {})).get("lock-runner", True) is True else "run requires a locked host")'
agentic-job sandbox check --unless-passed
if sudo -n true 2> /dev/null; then echo "::error::the runner's user still has sudo"; exit 1; fi
chmod 0755 .agentic-job/agentic-job
path=$(python3 -c 'import tomllib; c = tomllib.load(open("/etc/agentic-job/config.toml", "rb")); print(c.get("host", c.get("sandbox", {}))["env"]["PATH"])')
found=$(env -i PATH="$path" sh -c 'command -v agentic-job')
echo "policy binary: $(.agentic-job/agentic-job --version); on the agent's PATH: $found, $("$found" --version)"
cmp "$found" .agentic-job/agentic-job || { echo "::error::the agent's PATH finds $found, not the policy binary"; exit 1; }
test "$(env -i PATH="$path" wrapper-setup-probe)" = setup-tool
if command -v wrapper-setup-probe; then echo '::error::setup tools leaked onto the job PATH'; exit 1; fi
# The agent job's run step, with the empty GitHub token a review
# gets and no run token: the scripted agent reaches no proxy.
- id: run
timeout-minutes: 10
uses: ./.agentic-job-source/run
with:
runner-home: ${{ runner.temp }}/agent-home
github-token: ''
# The wrappers' check, at their pin, on the scripted handback. GitHub
# validates detect's `id-token` request even though it uses it only
# with AGENTIC_JOB_DETECT_MODEL set, which this repository leaves unset
# (setting it would run a model here); nothing of the agent's runs there.
wrapper-check:
needs: [wrapper-policy, wrapper-runner]
if: ${{ needs.wrapper-runner.outputs.safe-outputs-artifact-id != '' }}
permissions:
contents: read
actions: read
id-token: write
uses: cgwalters-forge/agentic-job/.github/workflows/check.yml@2b937aaac81da06d8bff5f317e58df6ad7063e1e # v0.6.16
with:
policy: ${{ toJSON(needs.wrapper-policy.outputs) }}
safe-outputs-artifact-id: ${{ needs.wrapper-runner.outputs.safe-outputs-artifact-id }}
safe-outputs-artifact-digest: ${{ needs.wrapper-runner.outputs.safe-outputs-artifact-digest }}
# The scripted dispatch passes only if the agent ended well and check
# accepted its one comment, with no patch.
wrapper-scripted-dispatch:
needs: [wrapper-runner, wrapper-check]
if: ${{ !cancelled() }}
runs-on: ubuntu-26.04
timeout-minutes: 5
permissions: {}
steps:
- name: The scripted handback was checked
env:
EXIT: ${{ needs.wrapper-runner.outputs.exit }}
CHECK: ${{ needs.wrapper-check.result }}
CHECKED: ${{ needs.wrapper-check.outputs.artifact-id }}
REFUSAL: ${{ needs.wrapper-check.outputs.refusal }}
PATCH: ${{ needs.wrapper-check.outputs.has-patch }}
run: |
echo "agent exit $EXIT, check $CHECK, checked upload ${CHECKED:-none}, patch ${PATCH:-none}"
[[ "$EXIT" == 0 && "$CHECK" == success && "$CHECKED" =~ ^[1-9][0-9]*$ && -z "$REFUSAL" && "$PATCH" != true ]] ||
{ echo "::error::the scripted dispatch did not hand back a checked comment; check's log has any refusal"; exit 1; }
# The README's example: an untrusted job with nothing worth stealing,
# whose output a second job takes by artifact ID and digest, as data.
build:
runs-on: ubuntu-26.04
timeout-minutes: 20
permissions:
contents: read
outputs:
id: ${{ steps.up.outputs.artifact-id }}
digest: ${{ steps.up.outputs.artifact-digest }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: taiki-e/install-action@75a7c0f87bd666af3cdb75c5b9e9d03ab6044e6f # v2.90.6
with:
tool: just@1.58.0
- run: just secure-host-root before
- run: just secure-host-metadata before
# A detached process of the runner's user holding a canary, bounded
# even on failed jobs; it keeps RUNNER_TRACKING_ID for runner cleanup.
- name: Another process's environment is readable before the action
env:
SECURE_HOST_READ_PROBE: not-a-secret
run: node secure-host/read-probe.mjs
- uses: ./secure-host
with:
egress: write-policy
# What would be `make test`: steps of the runner's user, without root.
- run: just secure-host-root after
- run: just secure-host-metadata after
# Setup's hidepid=invisible decides with the same check as the read, so
# a refused process may be missing from /proc altogether. Alive around
# the read, since any other failure proves nothing; left alive, as the
# job's normal cleanup must work with the rule on.
- name: Another process's environment is refused after the action
run: |
export LC_ALL=C
pid=$(< "$RUNNER_TEMP/secure-host-read-probe.pid")
if ! [[ "$pid" =~ ^[0-9]+$ ]] || [ "$pid" -le 1 ]; then echo "::error::invalid probe PID"; exit 1; fi
kill -0 "$pid"
if err=$(cat "/proc/$pid/environ" 2>&1 > /dev/null); then
echo "::error::another runner process environment is still readable"; exit 1
fi
case "$err" in
*": Permission denied" | *": No such file or directory") ;;
*) echo "::error::unexpected: $err"; exit 1 ;;
esac
kill -0 "$pid"
- run: just secure-host-egress
- run: mkdir out && git rev-parse HEAD > out/commit
- id: up
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: out
path: out/
if-no-files-found: error
publish:
needs: build
runs-on: ubuntu-26.04
timeout-minutes: 10
permissions:
contents: read
actions: read
steps:
# The artifact the build job named is the one it uploaded, by its
# digest; download-artifact then refuses bytes that do not match it.
- name: The artifact has the build job's digest
env:
GH_TOKEN: ${{ github.token }}
ID: ${{ needs.build.outputs.id }}
DIGEST: ${{ needs.build.outputs.digest }}
run: |
[[ "$ID" =~ ^[0-9]+$ && "$DIGEST" =~ ^[0-9a-f]{64}$ ]]
test "$(gh api "repos/$GITHUB_REPOSITORY/actions/artifacts/$ID" --jq .digest)" = "sha256:$DIGEST"
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
artifact-ids: ${{ needs.build.outputs.id }}
path: out
# Validated as data, never run.
- name: Validate
run: grep -qxE '[0-9a-f]{40}' out/commit
# The inputs: a configuration of the caller's (here one that keeps the
# runner's root, which shows it was taken) and no check.
options:
runs-on: ubuntu-26.04
timeout-minutes: 20
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: taiki-e/install-action@75a7c0f87bd666af3cdb75c5b9e9d03ab6044e6f # v2.90.6
with:
tool: just@1.58.0
- run: just secure-host-options before
- uses: ./secure-host
with:
config: ${{ runner.temp }}/unlocked.toml
check: "false"
- run: just secure-host-options after
# The `egress-policy` input: the fixture's probe rule allows its POST and
# no other, its git fetch rule keeps the action's check passing, and the
# runner's own write endpoints still take an artifact.
egress-policy:
runs-on: ubuntu-26.04
timeout-minutes: 20
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: taiki-e/install-action@75a7c0f87bd666af3cdb75c5b9e9d03ab6044e6f # v2.90.6
with:
tool: just@1.58.0
- uses: ./secure-host
with:
egress: write-policy
egress-policy: secure-host/ci-egress-policy.toml
- run: just secure-host-egress /secure-host-ci
- run: mkdir out && git rev-parse HEAD > out/commit
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: egress-policy
path: out/
if-no-files-found: error
# Failed setup can leave partial host changes: never reuse this VM for
# another action invocation or untrusted work.
invalid-egress-policy:
runs-on: ubuntu-26.04
timeout-minutes: 20
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: taiki-e/install-action@75a7c0f87bd666af3cdb75c5b9e9d03ab6044e6f # v2.90.6
with:
tool: just@1.58.0
- uses: ./secure-host
id: invalid
continue-on-error: true
with:
egress: write-policy
egress-policy: secure-host/ci-invalid-egress-policy.toml
check: "false"
- name: Invalid policy fails setup without sandbox check
if: ${{ !cancelled() }}
env:
OUTCOME: ${{ steps.invalid.outcome }}
run: just secure-host-invalid-policy
# The world-write policy from the caller's side, after the action exited.
world-write:
runs-on: ubuntu-26.04
timeout-minutes: 20
permissions:
contents: read
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: taiki-e/install-action@75a7c0f87bd666af3cdb75c5b9e9d03ab6044e6f # v2.90.6
with:
tool: just@1.58.0
- run: just secure-host-world-write before
- uses: ./secure-host
- run: just secure-host-world-write after
# agentic-job's `confined` job against the pinned image instead of a
# source build: a `run:` step and a JavaScript action's main and post
# steps try sudo, docker, a root-owned world-writable file, the metadata
# service, an earlier step's process's environment, their parent's
# environment, memory and ptrace, a forced core dump of a child of their
# own, the Worker's environment and dump and its diagnostic sockets, and
# each must fail; before the action each must work. The control is the
# release binary of the image's source, by digest: bump it with the pin.
confined:
name: Confined run step and JavaScript action main and post steps
runs-on: ubuntu-26.04
timeout-minutes: 30
permissions:
contents: read
outputs:
orphan: ${{ steps.orphan.outputs.pid }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# Every probe is a later step than this; the runner's cleanup must
# still end it (confined-cleanup).
- name: Leave a process running
id: orphan
run: |
sleep infinity > /dev/null 2>&1 &
echo "$!" > "$RUNNER_TEMP/confine-orphan.pid"
echo "pid=$!" >> "$GITHUB_OUTPUT"
# Outside setup's walk: only the access-time policy refuses it later.
- name: Leave a world-writable root file
run: sudo install -m 0666 -o root -g root /dev/null /var/tmp/agentic-job-confine-root-file
# At Yama scope 0 the control's ptrace of its parent works, so the
# later refusal is the action's, which sets 1.
- name: Let a process attach to any of its user's
run: sudo sysctl -q -w kernel.yama.ptrace_scope=0
- name: Before the lock each route works (control)
env:
URL: https://github.com/cgwalters-forge/agentic-job/releases/download/v0.6.14/secure-host-x86_64-unknown-linux-musl
SHA256: 4234907373903754ccacc8b6090c414b43d46329d4162f380626c17fe5322381
run: |
bin=$RUNNER_TEMP/secure-host-bin
curl -fsSL --retry 3 -o "$bin" "$URL"
echo "$SHA256 $bin" | sha256sum -c --quiet -
chmod 0755 "$bin"
"$bin" probe control before
- uses: ./secure-host
- name: A run step is confined
run: secure-host probe confined run-step
# Its post step runs after the job's last step and fails the job if
# a route is open.
- name: The JavaScript action main and post steps are confined
uses: cgwalters-forge/agentic-job/.github/actions/confine-probe@2b937aaac81da06d8bff5f317e58df6ad7063e1e # v0.6.16
# The runner's Worker, exempt from the runner-read rule, still ends the
# process confined left running, and logs so; read as data.
confined-cleanup:
needs: confined
runs-on: ubuntu-26.04
timeout-minutes: 5
permissions:
actions: read
steps:
- name: The runner ended the orphan
env:
GH_TOKEN: ${{ github.token }}
PID: ${{ needs.confined.outputs.orphan }}
REPO: ${{ github.repository }}
RUN: ${{ github.run_id }}
ATTEMPT: ${{ github.run_attempt }}
NAME: Confined run step and JavaScript action main and post steps
run: |
[[ "$PID" =~ ^[1-9][0-9]*$ ]] || { echo "::error::no orphan pid from confined"; exit 1; }
job=$(gh api --paginate "repos/$REPO/actions/runs/$RUN/attempts/$ATTEMPT/jobs" --jq ".jobs[] | select(.name == env.NAME) | .id")
[[ "$job" =~ ^[0-9]+$ ]] || { echo "::error::no single confined job in this attempt"; exit 1; }
# A completed job's log can take a moment to be served.
for try in 1 2 3 4 5 6; do
if gh api --allow-escape-sequences "repos/$REPO/actions/jobs/$job/logs" > "$RUNNER_TEMP/confined.log"; then
grep -qF "Terminate orphan process: pid ($PID) (sleep)" "$RUNNER_TEMP/confined.log" && exit 0
grep -qF 'Cleaning up orphan processes' "$RUNNER_TEMP/confined.log" && break
fi
[ "$try" = 6 ] || sleep 10
done
echo "::error::the runner's cleanup did not end orphan pid $PID"; exit 1