Repository navigation
329 lines (310 loc) · 15 KB
/
Copy pathdocs-screenshots-capture.yml
File metadata and controls
329 lines (310 loc) · 15 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
# docs-screenshots-capture — boot the dev stack, capture installer +
# panel screenshots, and commit the diff back to the PR branch.
#
# Capture geometry (driven by docs/scripts/capture.mjs):
#
# - Viewport: 1920×1080 (Full HD), with `fullPage: true` so the
# entire scrollable surface lands in the PNG.
# - Panel routes are captured TWICE — once in light mode and once
# in dark — and emit per-theme PNGs (e.g.
# `panel-02-dashboard-light.png`, `panel-02-dashboard-dark.png`).
# The dark pass seeds `localStorage['sbpp-theme']` via Playwright's
# `addInitScript` so the inline FOUC bootloader in
# `core/header.tpl` lands `<html class="dark">` on the first
# paint (no white flash). See AGENTS.md "Anti-FOUC theme
# bootloader" for the contract.
# - Install routes are captured ONCE in the :root light default.
# The wizard's `_chrome.tpl` doesn't load `theme.js` or the FOUC
# bootloader (the wizard has no theme toggle by design; see
# AGENTS.md "Install wizard").
#
# SECURITY MODEL — read this before changing the workflow.
#
# `pull_request_target` runs with the upstream repo's GITHUB_TOKEN
# regardless of where the PR comes from. A naive checkout of the PR
# head followed by `npm ci` would let any contributor exfil that token
# through a postinstall script or a tweaked capture.mjs. The defenses
# below — split-checkout, label gate, fork rejection, label-strip on
# push — are load-bearing; do not relax them without a paired threat
# model in the PR description.
#
# 1. The trusted code surface (the capture script itself, its
# package-lock.json, and the Playwright dep tree) is checked out
# from the PR's BASE branch (effectively `main`) into `trusted/`.
# `npm ci` runs there — postinstall scripts come from main, never
# from the PR diff.
#
# 2. The PR head is checked out into a separate `pr-head/` directory.
# We use it for two and only two things: (a) `docker compose up`
# so the panel under test reflects the PR's UI changes, and (b)
# as the destination for the captured PNG diff (the auto-commit
# action pushes from there). NO node code from this checkout is
# executed on the runner.
#
# 3. The capture script reads `CAPTURE_OUT_OVERRIDE` and writes its
# PNG outputs into `pr-head/docs/src/assets/auto/`. The auto-
# commit action then pushes from that working directory. Because
# the *script* is the trusted-from-main copy, a malicious PR
# can't redirect the writes elsewhere or run a different binary.
#
# 4. The `safe-to-screenshot` label is the maintainer ack. The
# `unlabel-on-synchronize` job below removes it on every new
# push so a maintainer must re-apply after reviewing each new
# commit. Bare opens (no label) and fork PRs short-circuit out.
#
# 5. Fork PRs are rejected via the
# `head.repo.full_name == github.repository` check. Auto-commit
# back into a fork PR via `pull_request_target` is the canonical
# "compromise the upstream maintainer's token" shape; if the
# contribution genuinely needs screenshot regeneration, push the
# branch into the upstream repo first and the workflow will run.
#
# Required repo configuration BEFORE this workflow can succeed:
#
# - The `safe-to-screenshot` label must exist (one-time setup).
# Maintainers apply it after reviewing the PR diff; the
# `unlabel-on-synchronize` job auto-removes it on every push so
# re-application is explicit per commit set.
#
# Pair with: .github/workflows/docs-screenshots-build.yml (the
# untrusted-side parse / install verification that runs on every PR).
name: docs-screenshots-capture
on:
pull_request_target:
types: [labeled, synchronize, reopened]
workflow_dispatch:
# Serialize per-PR so two pushes in quick succession don't race on the
# same set of PNGs.
concurrency:
group: docs-screenshots-${{ github.event.pull_request.number || github.run_id }}
cancel-in-progress: true
jobs:
# Strip the `safe-to-screenshot` label whenever a new commit lands so
# a maintainer must re-apply after reviewing the new code. Without
# this, a contributor could land a benign-looking PR, get the label
# applied, and then push a malicious follow-up commit that runs with
# the originally-granted ack.
unlabel-on-synchronize:
name: Strip safe-to-screenshot on push
if: github.event_name == 'pull_request_target' && github.event.action == 'synchronize'
runs-on: ubuntu-24.04
permissions:
pull-requests: write
steps:
- name: Remove safe-to-screenshot label
env:
GH_TOKEN: ${{ github.token }}
run: |
gh api -X DELETE \
"repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels/safe-to-screenshot" \
|| echo "label was not present; nothing to remove"
capture:
name: Capture installer + panel screenshots
# Three gates, all required:
# 1. The PR carries `safe-to-screenshot` (maintainer ack), OR
# the workflow was triggered manually via workflow_dispatch.
# 2. The PR is from the same repo (no forks — see security note
# above).
# 3. (Implicit) The unlabel-on-synchronize job ran first when
# this is a `synchronize` event; the label gate then catches
# the now-stripped state.
if: |
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'pull_request_target' &&
contains(github.event.pull_request.labels.*.name, 'safe-to-screenshot') &&
github.event.pull_request.head.repo.full_name == github.repository)
runs-on: ubuntu-24.04
timeout-minutes: 30
permissions:
contents: write
pull-requests: read
steps:
# TRUSTED checkout — the capture script + lockfile come from the
# PR's base branch (effectively `main`). All `npm ci` postinstall
# hooks run against this tree, NEVER against the PR head.
- name: Checkout trusted code from base branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.sha || github.sha }}
path: trusted
# We run scripts from this tree but never push from it; no
# need to persist the checkout token.
persist-credentials: false
# PR HEAD checkout — we need this for `docker compose up` (the
# panel under test) and as the destination for captured PNGs
# (the auto-commit action pushes from here). No JS/PHP code from
# this tree is executed on the runner; the PHP runs inside the
# docker `web` container.
#
# Canonical fork-PR checkout shape (per the auto-commit action's
# docs): explicit `repository` + `ref: head_ref` + persist creds.
# The `if` gate above already rejects fork PRs, so in practice
# `head.repo.full_name == github.repository`, but keeping the
# canonical shape means a future change to the gate (e.g. opt-in
# fork support) doesn't silently break the push.
- name: Checkout PR head (docker stack + screenshot output dir)
uses: actions/checkout@v4
with:
repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
ref: ${{ github.head_ref || github.ref_name }}
path: pr-head
persist-credentials: true
- name: Setup Node
uses: actions/setup-node@v4
with:
# Astro 6 requires Node >=22.12; mirrors the bump in
# docs-build.yml that landed with the docs astro+starlight
# group bump (#1346).
node-version: '22'
cache: 'npm'
cache-dependency-path: trusted/docs/package-lock.json
# Install deps + the chromium browser from the TRUSTED tree.
# Postinstall scripts in any package come from the trusted
# lockfile — no PR-side override possible.
- name: Install docs deps + Playwright chromium (trusted tree)
working-directory: trusted/docs
run: |
npm ci --no-audit --no-fund
npx playwright install --with-deps chromium
# Boot the stack from the PR head — docker-compose / Dockerfile
# changes need to flow through here. The runner's privileged
# token is NOT mounted into the container; the PHP code runs
# sandboxed by docker.
- name: Bring up the dev stack (PR-head docker-compose)
working-directory: pr-head
run: docker compose up -d --wait
- name: Wait for the panel
run: |
for i in {1..30}; do
code=$(curl -sS -o /dev/null -w '%{http_code}' http://localhost:8080/index.php?p=login || true)
if [[ "$code" == "200" ]]; then
echo "panel ready after attempt $i"
exit 0
fi
echo "attempt $i: HTTP $code, retrying"
sleep 3
done
echo "panel never came up" >&2
(cd pr-head && docker compose logs --tail=200 web db)
exit 1
# Populate the dev DB with realistic synthetic data so the
# post-install panel routes (Servers, Banlist, Dashboard)
# screenshot the populated state, not first-run empty
# placeholders. The seeder runs INSIDE the docker `web`
# container (sbpp.sh shells in via `docker compose exec`),
# so it's sandboxed from the runner's filesystem.
- name: Seed dev DB
working-directory: pr-head
run: ./sbpp.sh db-seed
# Run the TRUSTED capture script. Outputs land in the PR head's
# docs/src/assets/auto/ via CAPTURE_OUT_OVERRIDE so the auto-
# commit step picks them up below.
#
# Two passes — one per route group — because the install wizard
# and the post-install panel need opposite states of
# `web/config.php`:
#
# - The panel needs `config.php` to bootstrap (without it
# `web/init.php` redirects every panel URL to `/install/`).
# - The install wizard's #1335 C2 guard refuses to start over
# a panel where `config.php` exists (it renders a static
# 409 "already installed" page instead).
#
# The dev stack's entrypoint (`docker/php/web-entrypoint.sh`)
# creates `config.php` on first boot, so we land in the
# "panel works, wizard refuses" state by default. The dance
# below moves the file aside on the host (the bind-mounted
# `./web` means the container sees the rename immediately) for
# the duration of the install captures, then restores it.
#
# The panel pass loops both `light` and `dark` themes inside
# `capture.mjs` (one browser context per theme; localStorage
# seeded before navigation so the FOUC bootloader picks dark
# up on the first paint). See the workflow header comment
# for the per-theme output naming.
- name: Capture panel screenshots (light + dark, 1920×1080 full-page)
working-directory: trusted/docs
env:
STEAM_API_KEY: '00000000000000000000000000000000'
PANEL_URL: 'http://localhost:8080'
CAPTURE_OUT_OVERRIDE: ${{ github.workspace }}/pr-head/docs/src/assets/auto
CAPTURE_ROUTES: panel
run: npm run capture
# Stash to a sibling path inside `pr-head/` (alongside `web/`),
# NOT to `/tmp`. The dev container's entrypoint runs as root, so
# `web/config.php` lands on the bind-mounted host filesystem
# owned `root:root`. On GHA ubuntu-24.04 runners `/tmp` is on
# the same filesystem as the workspace, so `mv web/config.php
# /tmp/...` is a same-fs atomic `rename(2)` that PRESERVES
# ownership — and `/tmp` carries the sticky bit, so the runner
# (UID 1001) cannot move the still-root-owned file back later
# (per `rename(2)`: "EPERM: the directory containing oldpath
# has the sticky bit set and the process's effective UID is
# neither the UID of the file to be deleted nor that of the
# directory containing it"). PR 1430 was the first run after
# #1427 to actually exercise the stash/restore pair and hit
# this trap (`mv: cannot move '/tmp/sbpp-config-stash.php' to
# 'web/config.php': Operation not permitted`).
#
# `pr-head/.sbpp-config-stash.php` is a runner-owned, sticky-
# bit-free directory on the same filesystem as `pr-head/web/`,
# so the rename is atomic both ways regardless of which UID
# the docker container created the file as. The auto-commit
# step further down constrains the commit to `docs/src/assets/auto/**`,
# so a stale stash file (if the restore somehow no-ops) never
# leaks into the PR diff.
- name: Stash config.php to expose the install wizard
working-directory: pr-head
run: |
if [[ -f web/config.php ]]; then
mv web/config.php .sbpp-config-stash.php
echo "stashed web/config.php → pr-head/.sbpp-config-stash.php"
else
echo "web/config.php was already absent; nothing to stash"
fi
# Install pass is light-only — the wizard's `_chrome.tpl`
# doesn't load `theme.js` or the FOUC bootloader, so a "dark
# install" capture would just be the same light render with a
# different filename (see the workflow header comment).
- name: Capture install screenshots (light only, 1920×1080 full-page)
working-directory: trusted/docs
env:
STEAM_API_KEY: '00000000000000000000000000000000'
PANEL_URL: 'http://localhost:8080'
CAPTURE_OUT_OVERRIDE: ${{ github.workspace }}/pr-head/docs/src/assets/auto
CAPTURE_ROUTES: install
run: npm run capture
# Restore is `if: always()` so a failed install capture (timeout,
# unexpected wizard render, etc.) still leaves the panel in a
# bootable state for the tear-down step. The matching stash
# above only renames when the source file exists, so the pair
# is idempotent.
- name: Restore config.php
if: always()
working-directory: pr-head
run: |
if [[ -f .sbpp-config-stash.php ]]; then
mv .sbpp-config-stash.php web/config.php
echo "restored pr-head/.sbpp-config-stash.php → web/config.php"
else
echo "no stash to restore; nothing to do"
fi
- name: Tear down stack
if: always()
working-directory: pr-head
run: docker compose down -v
# Commit the captured PNG diff back to the PR branch from the
# PR-head working directory. The action no-ops when nothing
# changed under the file_pattern.
- name: Commit captured screenshots back to PR branch
if: github.event_name != 'workflow_dispatch'
uses: stefanzweifel/git-auto-commit-action@v5
with:
repository: pr-head
commit_message: |
docs: refresh auto-captured screenshots (#1333)
Generated by .github/workflows/docs-screenshots-capture.yml
after a maintainer applied the `safe-to-screenshot` label.
file_pattern: 'docs/src/assets/auto/**'
commit_user_name: 'sbpp-docs-bot'
commit_user_email: 'sbpp-docs-bot@users.noreply.github.com'