Skip to content

Commit 2a13604

Browse files
authored
ci: snapshot fork PR Storybooks with Pixel from a trusted workflow (#1233)
Fork PRs never receive PIXEL_KEY on `pull_request`, so the Pixel job failed for them, and the old `repository_owner == 'coder'` guard is always true for forks. Split Pixel into an untrusted build and a trusted report step, following GitHub Security Lab's guidance on pwn requests. - `pixel.yaml`: PR runs build Storybook with no secrets and upload it as a one-day `storybook` artifact. Pushes to main and manual runs snapshot directly, as before. - `pixel-snapshot.yaml`: runs from the default branch via `workflow_run` and never checks out the PR. It snapshots only when exactly one open PR into main has the run's head commit, rejects symlinks and special files in the artifact, and posts a `Pixel Snapshot` commit status. Fork builds report as `fork:<owner>/<branch>`. - Add `pnpm pixel-storybook:ci`, which fails the job when the Pixel build fails and launches Chromium with its sandbox on. The Chromium action now allows the user namespaces the sandbox needs on Ubuntu 24.04. - Pin `@coder/pixel-storybook` to 0.3.0, since the wrapper imports its internals. Residual risk: the snapshot job renders a fork's Storybook in the same job as PIXEL_KEY, which is scoped to the Pixel project. The sandbox limits this.
1 parent a367f8c commit 2a13604

8 files changed

Lines changed: 250 additions & 28 deletions

File tree

‎.github/actions/playwright-chromium/action.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ runs:
2727
shell: bash
2828
run: timeout 5m pnpm exec playwright-core install chromium
2929

30+
# Ubuntu 24.04 blocks the unprivileged user namespaces that Chromium's sandbox
31+
# needs. The runner is a throwaway VM whose user already has passwordless sudo.
32+
- name: Allow Chromium's sandbox
33+
if: runner.os == 'Linux'
34+
shell: bash
35+
run: sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
36+
3037
- name: Save Chromium cache
3138
if: inputs.save-cache == 'true' && steps.restore.outputs.cache-hit != 'true'
3239
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0

‎.github/workflows/ci.yaml‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -101,32 +101,6 @@ jobs:
101101
run: ${{ runner.os == 'Linux' && 'xvfb-run -a' || '' }} pnpm test:integration --label "VS Code ${{ matrix.vscode-version }}"
102102
shell: bash
103103

104-
pixel:
105-
name: Pixel
106-
runs-on: ubuntu-24.04
107-
timeout-minutes: 15
108-
# Forks and Dependabot can't read the PIXEL_KEY secret.
109-
if: github.repository_owner == 'coder' && github.actor != 'dependabot[bot]'
110-
steps:
111-
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
112-
with:
113-
persist-credentials: false
114-
- name: Setup pnpm, Node.js, and dependencies
115-
uses: ./.github/actions/pnpm-setup
116-
117-
- name: Install Chromium
118-
uses: ./.github/actions/playwright-chromium
119-
120-
# Builds the Storybook (buildCommand in pixel.jsonc) and snapshots it.
121-
- name: Snapshot
122-
run: pnpm exec pixel-storybook
123-
env:
124-
PIXEL_KEY: ${{ secrets.PIXEL_KEY }}
125-
# On pull_request, github.sha is a synthetic merge commit, not the PR head.
126-
PIXEL_COMMIT: ${{ github.event.pull_request.head.sha || github.sha }}
127-
# Auto-approve on mainline to avoid blocking CI after squash merges.
128-
PIXEL_AUTO_REVIEW: ${{ github.ref == 'refs/heads/main' }}
129-
130104
package:
131105
name: Package
132106
runs-on: ubuntu-24.04
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
name: Pixel Snapshot
2+
3+
# Snapshots the Storybook that the Pixel workflow built for a pull request.
4+
# This runs from the default branch with PIXEL_KEY and never checks out or runs
5+
# the pull request's code: its Storybook is only served to Chromium.
6+
on:
7+
workflow_run:
8+
workflows: [Pixel]
9+
types: [completed]
10+
11+
permissions: {}
12+
13+
cache-mode: read
14+
15+
concurrency:
16+
group: ${{ github.workflow }}-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }}
17+
cancel-in-progress: true
18+
19+
jobs:
20+
# Statuses are per commit, so only snapshot when exactly one open PR has this
21+
# head commit, and it targets main. A fork can't share another PR's commit,
22+
# or pick a stale base, to post statuses on it.
23+
resolve:
24+
name: Resolve pull request
25+
runs-on: ubuntu-24.04
26+
timeout-minutes: 5
27+
if: >-
28+
github.repository_owner == 'coder' &&
29+
github.event.workflow_run.event == 'pull_request' &&
30+
github.event.workflow_run.conclusion == 'success' &&
31+
github.event.workflow_run.actor.login != 'dependabot[bot]'
32+
permissions:
33+
pull-requests: read
34+
outputs:
35+
number: ${{ steps.pr.outputs.number }}
36+
base_sha: ${{ steps.pr.outputs.base_sha }}
37+
steps:
38+
- name: Find PR
39+
id: pr
40+
run: |
41+
gh pr list --repo "$GITHUB_REPOSITORY" --state open --limit 1000 \
42+
--json number,headRefOid,headRefName,headRepositoryOwner,baseRefName,baseRefOid \
43+
--jq '[.[] | select(.headRefOid == env.HEAD_SHA)] | select(length == 1) | .[]
44+
| select(.baseRefName == "main" and .headRepositoryOwner.login == env.HEAD_OWNER and .headRefName == env.HEAD_BRANCH)
45+
| "number=\(.number)\nbase_sha=\(.baseRefOid)"' >> "$GITHUB_OUTPUT"
46+
env:
47+
GH_TOKEN: ${{ github.token }}
48+
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
49+
HEAD_OWNER: ${{ github.event.workflow_run.head_repository.owner.login }}
50+
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
51+
52+
snapshot:
53+
name: Snapshot
54+
needs: resolve
55+
if: needs.resolve.outputs.number != ''
56+
runs-on: ubuntu-24.04
57+
timeout-minutes: 15
58+
permissions:
59+
actions: read
60+
contents: read
61+
statuses: write
62+
env:
63+
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
64+
steps:
65+
- name: Set pending status
66+
run: >-
67+
gh api "repos/$GITHUB_REPOSITORY/statuses/$HEAD_SHA"
68+
-f state=pending -f context="Pixel Snapshot"
69+
-f description="Snapshotting Storybook"
70+
-f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
71+
env:
72+
GH_TOKEN: ${{ github.token }}
73+
74+
# The default branch, never the pull request.
75+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
76+
with:
77+
persist-credentials: false
78+
- name: Setup pnpm, Node.js, and dependencies
79+
uses: ./.github/actions/pnpm-setup
80+
81+
- name: Install Chromium
82+
uses: ./.github/actions/playwright-chromium
83+
84+
# Outside the checkout, so the artifact can't replace trusted files.
85+
- name: Download Storybook
86+
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
87+
with:
88+
name: storybook
89+
path: ${{ runner.temp }}/storybook-static
90+
run-id: ${{ github.event.workflow_run.id }}
91+
github-token: ${{ github.token }}
92+
93+
# serve-handler follows symlinked directories in a request path, which
94+
# would let the PR's Storybook read files such as /proc/self/environ.
95+
# Serve regular files and directories only.
96+
- name: Reject symlinks
97+
run: |
98+
if find "$RUNNER_TEMP/storybook-static" ! -type f ! -type d | grep -q .; then
99+
echo "::error::Storybook artifact contains links or special files"
100+
exit 1
101+
fi
102+
103+
# Fork branches are reported as fork:<owner>/<branch>; git branch names
104+
# can't contain ":", so they never match this repository's. GITHUB_REF is
105+
# how pixel-storybook links the build to the PR.
106+
- name: Snapshot
107+
run: GITHUB_REF="refs/pull/$PR_NUMBER/merge" pnpm pixel-storybook:ci
108+
env:
109+
PR_NUMBER: ${{ needs.resolve.outputs.number }}
110+
PIXEL_KEY: ${{ secrets.PIXEL_KEY }}
111+
PIXEL_STORYBOOK_DIR: ${{ runner.temp }}/storybook-static
112+
PIXEL_COMMIT: ${{ github.event.workflow_run.head_sha }}
113+
PIXEL_BASELINE_COMMIT: ${{ needs.resolve.outputs.base_sha }}
114+
PIXEL_BRANCH: ${{ github.event.workflow_run.head_repository.full_name == github.repository && github.event.workflow_run.head_branch || format('fork:{0}/{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch) }}
115+
PIXEL_AUTO_REVIEW: "false"
116+
117+
- name: Set final status
118+
if: ${{ !cancelled() }}
119+
run: >-
120+
gh api "repos/$GITHUB_REPOSITORY/statuses/$HEAD_SHA"
121+
-f state=${{ job.status == 'success' && 'success' || 'failure' }}
122+
-f context="Pixel Snapshot"
123+
-f description="${{ job.status == 'success' && 'Snapshot complete' || 'Snapshot failed' }}"
124+
-f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
125+
env:
126+
GH_TOKEN: ${{ github.token }}

‎.github/workflows/pixel.yaml‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: Pixel
2+
3+
# Pull requests, forks included, only build Storybook here and never see
4+
# PIXEL_KEY. When this workflow finishes, pixel-snapshot.yaml snapshots the
5+
# uploaded Storybook from the default branch.
6+
on:
7+
push:
8+
branches:
9+
- main
10+
11+
pull_request:
12+
13+
workflow_dispatch:
14+
15+
permissions:
16+
contents: read
17+
18+
cache-mode: read
19+
20+
# Cancel superseded PR runs; main pushes each get their own group.
21+
concurrency:
22+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
23+
cancel-in-progress: true
24+
25+
jobs:
26+
build:
27+
name: Build
28+
runs-on: ubuntu-24.04
29+
timeout-minutes: 15
30+
if: github.repository_owner == 'coder' && github.event_name == 'pull_request'
31+
steps:
32+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
33+
with:
34+
persist-credentials: false
35+
- name: Setup pnpm, Node.js, and dependencies
36+
uses: ./.github/actions/pnpm-setup
37+
38+
- run: pnpm storybook:ci
39+
40+
- name: Upload Storybook
41+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
42+
with:
43+
name: storybook
44+
path: storybook-static/
45+
if-no-files-found: error
46+
retention-days: 1
47+
48+
snapshot:
49+
name: Snapshot
50+
runs-on: ubuntu-24.04
51+
timeout-minutes: 15
52+
if: github.repository_owner == 'coder' && github.event_name != 'pull_request'
53+
steps:
54+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
55+
with:
56+
persist-credentials: false
57+
- name: Setup pnpm, Node.js, and dependencies
58+
uses: ./.github/actions/pnpm-setup
59+
60+
- name: Install Chromium
61+
uses: ./.github/actions/playwright-chromium
62+
63+
# Builds the Storybook (buildCommand in pixel.jsonc) and snapshots it.
64+
- name: Snapshot
65+
run: pnpm pixel-storybook:ci
66+
env:
67+
PIXEL_KEY: ${{ secrets.PIXEL_KEY }}
68+
# Auto-approve on mainline to avoid blocking CI after squash merges.
69+
PIXEL_AUTO_REVIEW: ${{ github.ref == 'refs/heads/main' }}

‎CONTRIBUTING.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ Webviews" to see webview changes.
6969
needs them.
7070
- Link related issues and PRs. Add screenshots for UI changes and numbers
7171
for performance changes.
72+
- The Pixel workflow (`.github/workflows/pixel.yaml`) builds Storybook
73+
without secrets, and `pixel-snapshot.yaml` then snapshots it from the
74+
default branch and reports a `Pixel Snapshot` status. This runs on every
75+
PR, forks included; a fork PR that GitHub holds for workflow approval runs
76+
once a maintainer approves it.
7277
- Leave out test plans, "benefits" sections, line-by-line walkthroughs and
7378
marketing language. Let GitHub wrap the prose instead of hard-wrapping it.
7479

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"prepackage": "pnpm clean && pnpm install --frozen-lockfile",
4141
"package": "pnpm build:production && vsce package --no-dependencies",
4242
"package:prerelease": "pnpm build:production && vsce package --pre-release --no-dependencies",
43+
"pixel-storybook:ci": "node scripts/pixel-storybook-ci.mjs",
4344
"storybook": "storybook dev -p 6006 --config-dir .storybook",
4445
"storybook:build": "storybook build --config-dir .storybook",
4546
"storybook:ci": "storybook build --test --config-dir .storybook",
@@ -854,7 +855,7 @@
854855
"zod": "^4.6.7"
855856
},
856857
"devDependencies": {
857-
"@coder/pixel-storybook": "^0.3.0",
858+
"@coder/pixel-storybook": "0.3.0",
858859
"@eslint-react/eslint-plugin": "^5.30.6",
859860
"@eslint/markdown": "^9.0.0",
860861
"@repo/mocks": "workspace:*",

‎pnpm-lock.yaml‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎scripts/pixel-storybook-ci.mjs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// pixel-storybook 0.3.0 exits 0 even after reporting a failed build, so mirror
2+
// the status of its final PATCH /api/build into the exit code, as coder/coder's
3+
// site/scripts/pixelStorybookCi.mjs does. The export map hides these modules.
4+
import { configure } from "../node_modules/@coder/pixel-storybook/build/config.js";
5+
import { run } from "../node_modules/@coder/pixel-storybook/build/runner.js";
6+
import { chromium } from "playwright-core";
7+
8+
// Pull request Storybooks render here next to PIXEL_KEY, so keep Chromium's
9+
// sandbox on; Playwright turns it off by default. pixel-storybook launches this
10+
// same chromium instance.
11+
const launch = chromium.launch.bind(chromium);
12+
chromium.launch = (options) => launch({ ...options, chromiumSandbox: true });
13+
14+
let buildStatus;
15+
const realFetch = globalThis.fetch;
16+
globalThis.fetch = (url, init) => {
17+
if (
18+
init?.method === "PATCH" &&
19+
typeof url === "string" &&
20+
url.endsWith("/api/build") &&
21+
typeof init.body === "string"
22+
) {
23+
buildStatus = JSON.parse(init.body).status;
24+
}
25+
return realFetch(url, init);
26+
};
27+
28+
const config = await configure();
29+
// Snapshot a Storybook built elsewhere, as pixel-snapshot.yaml does for PRs.
30+
if (process.env.PIXEL_STORYBOOK_DIR) {
31+
config.storybook.buildCommand = "true";
32+
config.storybook.outputDirectory = process.env.PIXEL_STORYBOOK_DIR;
33+
}
34+
await run();
35+
if (buildStatus !== "complete") {
36+
console.error(
37+
`pixel: build finished with status ${buildStatus ?? "unknown"}`,
38+
);
39+
process.exitCode = 1;
40+
}

0 commit comments

Comments
 (0)