Skip to content

Commit 10fb290

Browse files
ci: keep runner selection on hosted pools
A reusable workflow cannot tell a hosted runner label from a private one: release-desktop.yml's `runner` and `verify_runner` inputs go straight into `runs-on`, so a future caller could point release jobs, which hold signing secrets, at self-hosted hardware. Validate both inputs in a GitHub-hosted job that build and verify need, rejecting `self-hosted`, `*-runner` labels, and anything outside blacksmith-*, ubuntu-*, macos-*, and windows-*. Add .github/scripts/check-runner-labels.py, which fails when any workflow references `self-hosted` or a private-runner label, with unit tests, and run it in the cheapest PR job so a pull request that introduces one fails before any other job starts. Pin expo/expo-github-action/continuous-deploy-fingerprint in the fork PR preview to the commit v9.0.1 tags instead of `main`: that job runs fork-authored code with EXPO_PREVIEW_TOKEN, so a moving ref would let a new upstream commit run there unreviewed.
1 parent 4522c82 commit 10fb290

6 files changed

Lines changed: 322 additions & 2 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
"""Keep workflow runner selection on hosted pools.
2+
3+
This repository has no self-hosted runners. Pull requests from forks can propose
4+
workflow changes, and a workflow that selects a private runner would put
5+
untrusted code on hardware that holds credentials and reaches the internal
6+
network. Every `runs-on` value must therefore name a Blacksmith pool
7+
(`blacksmith-*`) or a GitHub-hosted image (`ubuntu-*`, `macos-*`, `windows-*`).
8+
9+
Two modes:
10+
11+
check-runner-labels.py
12+
Scan `.github/workflows/*.yml` and fail on `self-hosted` or a
13+
private-runner label (`linux-runner`, `windows-runner`,
14+
`macos-runner`, `bench-*`). Comments are ignored.
15+
16+
check-runner-labels.py --runner LABEL [--verify-runner LABEL]
17+
Validate labels passed to a reusable workflow, where the static scan
18+
cannot see through `${{ inputs.runner }}`.
19+
"""
20+
21+
import argparse
22+
import os
23+
import re
24+
import sys
25+
from pathlib import Path
26+
27+
WORKFLOWS_DIR = Path(__file__).resolve().parent.parent / "workflows"
28+
29+
# A label a hosted pool provides: Blacksmith's per-capacity labels and GitHub's
30+
# own images. `[a-z0-9.-]*` covers suffixes such as `ubuntu-24.04-arm`.
31+
HOSTED_LABEL = re.compile(r"^(?:blacksmith|ubuntu|macos|windows)-[a-z0-9][a-z0-9.-]*$")
32+
33+
# Runner labels that name hardware this account owns. `-runner` is a stronger
34+
# signal than `-hosted`, so a label such as `linux-runner` is private by
35+
# construction; `self-hosted` is GitHub's own selector.
36+
PRIVATE_LABEL_HINT = ("self-hosted", "-runner")
37+
38+
# What the static scan looks for in workflow text: GitHub's self-hosted
39+
# selector, the private labels this repository used to have, and the `bench-*`
40+
# capacity pool. `android-emulator-runner` and other action names do not match.
41+
FORBIDDEN_REFERENCE = re.compile(
42+
r"self-hosted|\b(?:linux|windows|macos)-runner\b|\bbench-[a-z0-9][a-z0-9.-]*"
43+
)
44+
45+
46+
def strip_comment(line):
47+
"""Drop a trailing YAML comment without touching quoted `#` characters."""
48+
quote = None
49+
for index, char in enumerate(line):
50+
if quote:
51+
if char == quote:
52+
quote = None
53+
elif char in ("'", '"'):
54+
quote = char
55+
elif char == "#" and (index == 0 or line[index - 1] in " \t"):
56+
return line[:index]
57+
return line
58+
59+
60+
def scan_workflows(workflows_dir=WORKFLOWS_DIR):
61+
"""Return one message per workflow line that references a private runner."""
62+
problems = []
63+
paths = sorted(workflows_dir.glob("*.yml")) + sorted(workflows_dir.glob("*.yaml"))
64+
for path in paths:
65+
for number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1):
66+
code = strip_comment(line)
67+
match = FORBIDDEN_REFERENCE.search(code)
68+
if match:
69+
problems.append(
70+
f"{path.name}:{number}: references private runner {match.group(0)!r}: "
71+
f"{code.strip()}"
72+
)
73+
return problems
74+
75+
76+
def check_label(name, label, required):
77+
"""Return a message when `label` is empty-but-required or not a hosted pool."""
78+
if not label:
79+
if required:
80+
return f"inputs.{name} is empty; this workflow needs a hosted runner label"
81+
return None
82+
if any(hint in label for hint in PRIVATE_LABEL_HINT) or not HOSTED_LABEL.match(label):
83+
return (
84+
f"inputs.{name}={label!r} is not a hosted runner label; "
85+
"use blacksmith-*, ubuntu-*, macos-* or windows-*"
86+
)
87+
return None
88+
89+
90+
def check_labels(runner, verify_runner):
91+
"""Validate the runner inputs of a reusable workflow."""
92+
problems = [
93+
message
94+
for message in (
95+
check_label("runner", runner, required=True),
96+
check_label("verify_runner", verify_runner, required=False),
97+
)
98+
if message
99+
]
100+
return problems
101+
102+
103+
def report(message):
104+
prefix = "::error::" if os.environ.get("GITHUB_ACTIONS") else "error: "
105+
print(f"{prefix}{message}", file=sys.stderr)
106+
107+
108+
def main(argv=None):
109+
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
110+
parser.add_argument(
111+
"--runner",
112+
help="runner input of a reusable workflow; validates labels instead of scanning",
113+
)
114+
parser.add_argument(
115+
"--verify-runner",
116+
help="optional verify_runner input; empty means the workflow has no verify job",
117+
)
118+
parser.add_argument(
119+
"--workflows-dir",
120+
type=Path,
121+
default=WORKFLOWS_DIR,
122+
help=f"directory to scan (default: {WORKFLOWS_DIR})",
123+
)
124+
args = parser.parse_args(argv)
125+
126+
if args.runner is None and args.verify_runner is None:
127+
problems = scan_workflows(args.workflows_dir)
128+
checked = "workflow runner references"
129+
else:
130+
# A caller may pass neither flag through the shell, so both are checked.
131+
problems = check_labels(args.runner or "", args.verify_runner or "")
132+
checked = "runner inputs"
133+
134+
for problem in problems:
135+
report(problem)
136+
if problems:
137+
return 1
138+
print(f"All {checked} name hosted runner pools.")
139+
return 0
140+
141+
142+
if __name__ == "__main__":
143+
sys.exit(main())
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import importlib.util
2+
import tempfile
3+
import unittest
4+
from pathlib import Path
5+
6+
spec = importlib.util.spec_from_file_location(
7+
"check_runner_labels", Path(__file__).with_name("check-runner-labels.py")
8+
)
9+
labels = importlib.util.module_from_spec(spec)
10+
spec.loader.exec_module(labels)
11+
12+
13+
class ScanWorkflowsTests(unittest.TestCase):
14+
def setUp(self):
15+
self.temp = tempfile.TemporaryDirectory()
16+
self.addCleanup(self.temp.cleanup)
17+
self.workflows = Path(self.temp.name)
18+
19+
def scan(self, filename, text):
20+
(self.workflows / filename).write_text(text, encoding="utf-8")
21+
return labels.scan_workflows(self.workflows)
22+
23+
def test_accepts_hosted_pools(self):
24+
self.assertEqual(self.scan("build.yml", """
25+
jobs:
26+
build:
27+
runs-on: blacksmith-8vcpu-ubuntu-2404
28+
verify:
29+
runs-on: windows-11-arm
30+
lint:
31+
runs-on: ubuntu-24.04-arm
32+
mac:
33+
runs-on: macos-26
34+
"""), [])
35+
36+
def test_rejects_self_hosted(self):
37+
problems = self.scan("build.yml", " runs-on: [self-hosted, linux]\n")
38+
self.assertEqual(len(problems), 1)
39+
self.assertIn("build.yml:1", problems[0])
40+
self.assertIn("self-hosted", problems[0])
41+
42+
def test_rejects_private_runner_labels(self):
43+
for label in ("linux-runner", "windows-runner", "macos-runner", "bench-32vcpu"):
44+
with self.subTest(label=label):
45+
self.workflows.joinpath("build.yml").unlink(missing_ok=True)
46+
problems = self.scan("build.yml", f" runs-on: {label}\n")
47+
self.assertEqual(len(problems), 1, problems)
48+
49+
def test_rejects_matrix_values(self):
50+
problems = self.scan("release.yml", """
51+
strategy:
52+
matrix:
53+
runner: [blacksmith-8vcpu-ubuntu-2404, bench-arm64]
54+
runs-on: ${{ matrix.runner }}
55+
""")
56+
self.assertEqual(len(problems), 1)
57+
self.assertIn("bench-arm64", problems[0])
58+
59+
def test_ignores_comments_and_action_names(self):
60+
self.assertEqual(self.scan("notes.yml", """
61+
# A private runner would be self-hosted or linux-runner.
62+
jobs:
63+
emulator:
64+
runs-on: blacksmith-4vcpu-ubuntu-2404
65+
steps:
66+
- uses: reactivecircus/android-emulator-runner@v2
67+
"""), [])
68+
69+
def test_ignores_trailing_comments(self):
70+
self.assertEqual(self.scan("build.yml", "runs-on: ubuntu-24.04 # not self-hosted\n"), [])
71+
72+
def test_strip_comment_keeps_hash_inside_quotes(self):
73+
self.assertEqual(
74+
labels.strip_comment('run: echo "a # b" # mentions linux-runner'),
75+
'run: echo "a # b" ',
76+
)
77+
78+
79+
class CheckLabelsTests(unittest.TestCase):
80+
def test_accepts_hosted_labels(self):
81+
self.assertEqual(
82+
labels.check_labels("blacksmith-16vcpu-ubuntu-2404", "windows-2025"), []
83+
)
84+
self.assertEqual(labels.check_labels("ubuntu-24.04-arm", ""), [])
85+
86+
def test_rejects_private_labels(self):
87+
for label in ("self-hosted", "linux-runner", "bench-32vcpu", "macos-26-runner"):
88+
with self.subTest(label=label):
89+
problems = labels.check_labels(label, "")
90+
self.assertEqual(len(problems), 1, problems)
91+
self.assertIn("inputs.runner", problems[0])
92+
93+
def test_rejects_verify_runner_on_a_private_pool(self):
94+
problems = labels.check_labels("ubuntu-24.04", "linux-runner")
95+
self.assertEqual(len(problems), 1)
96+
self.assertIn("inputs.verify_runner", problems[0])
97+
98+
def test_requires_runner_but_not_verify_runner(self):
99+
self.assertEqual(len(labels.check_labels("", "")), 1)
100+
self.assertIn("inputs.runner is empty", labels.check_labels("", "")[0])
101+
102+
103+
class MainTests(unittest.TestCase):
104+
def test_scan_mode_passes_on_the_repository_workflows(self):
105+
self.assertEqual(labels.scan_workflows(), [])
106+
107+
108+
if __name__ == "__main__":
109+
unittest.main()

‎.github/workflows/ci.yml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,23 @@ jobs:
4545
/pnpm-workspace.yaml
4646
/.github/scripts/ci-changes.cjs
4747
/.github/actions/ci-changes/
48+
/.github/workflows/
49+
/.github/scripts/check-runner-labels.py
50+
/.github/scripts/check-runner-labels.test.py
4851
/*/package.json
4952
/*/*/package.json
5053
!/.repos/
5154
sparse-checkout-cone-mode: false
5255

56+
# The repository has no self-hosted runners, so a workflow that selects
57+
# one is a security bug rather than a configuration choice. This is the
58+
# cheapest job and it runs before everything else, so a pull request that
59+
# introduces a private runner label fails before any other job starts.
60+
- name: Check workflow runner labels
61+
run: |
62+
python3 -B .github/scripts/check-runner-labels.test.py
63+
python3 -B .github/scripts/check-runner-labels.py
64+
5365
- name: Detect affected jobs
5466
id: detect
5567
uses: ./.github/actions/ci-changes

‎.github/workflows/fork-ci.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ jobs:
3939
git fetch --no-tags --depth=2 origin "$GITHUB_SHA"
4040
git sparse-checkout set --no-cone '/*' '!/.repos/' '!/apps/mobile/modules/t3-terminal/Vendor/**/*.a'
4141
git checkout --detach FETCH_HEAD
42+
43+
# Mirrors ci.yml: the repository has no self-hosted runners, so a workflow
44+
# that selects one fails here, before any other job starts.
45+
- name: Check workflow runner labels
46+
run: |
47+
python3 -B .github/scripts/check-runner-labels.test.py
48+
python3 -B .github/scripts/check-runner-labels.py
49+
4250
- name: Detect affected jobs
4351
id: detect
4452
uses: ./.github/actions/ci-changes

‎.github/workflows/mobile-eas-preview-fork.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ jobs:
129129
130130
- name: Deploy with fingerprint check
131131
if: steps.expo-token.outputs.present == 'true'
132-
uses: expo/expo-github-action/continuous-deploy-fingerprint@main
132+
# Pinned to the commit v9.0.1 tags rather than `main`: this job runs the
133+
# reviewed head commit of a fork PR with EXPO_PREVIEW_TOKEN, so a moving
134+
# branch ref would let a new upstream commit run here unreviewed.
135+
uses: expo/expo-github-action/continuous-deploy-fingerprint@9da89d3617bb88139671bad31b09c65714c2ee06 # v9.0.1
133136
env:
134137
EXPO_TOKEN: ${{ secrets.EXPO_PREVIEW_TOKEN }}
135138
RUNNER_DEBUG: "1"

‎.github/workflows/release-desktop.yml‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,53 @@ permissions:
120120
contents: read
121121

122122
jobs:
123+
# `runner` and `verify_runner` are caller-supplied strings used directly as
124+
# `runs-on`, so this workflow cannot tell a hosted pool from a private one on
125+
# its own. Fail closed before either job is scheduled: a caller that passes
126+
# `self-hosted`, a `*-runner` label, or anything outside the pools this
127+
# workflow is designed for stops here instead of running release work (with
128+
# release secrets) on hardware this repository does not control. The static
129+
# half of the same policy is .github/scripts/check-runner-labels.py.
130+
validate:
131+
name: Validate runner inputs
132+
runs-on: ubuntu-24.04
133+
timeout-minutes: 5
134+
steps:
135+
- name: Reject private runner labels
136+
shell: bash
137+
env:
138+
RUNNER_LABEL: ${{ inputs.runner }}
139+
VERIFY_RUNNER_LABEL: ${{ inputs.verify_runner }}
140+
run: |
141+
set -euo pipefail
142+
# Blacksmith's per-capacity labels and GitHub's own images only.
143+
allowed='^(blacksmith|ubuntu|macos|windows)-[a-z0-9][a-z0-9.-]*$'
144+
failed=0
145+
check_label() {
146+
local name="$1" label="$2" required="$3"
147+
if [[ -z "$label" ]]; then
148+
if [[ "$required" == "true" ]]; then
149+
echo "::error::inputs.$name is empty, but this workflow needs a hosted runner label."
150+
failed=1
151+
fi
152+
return 0
153+
fi
154+
# A private label is either a `*-runner` pool or simply outside the
155+
# hosted pools above (GitHub's own self-hosted selector included).
156+
if [[ "$label" == *-runner* ]] || [[ ! "$label" =~ $allowed ]]; then
157+
echo "::error::inputs.$name=$label is not a hosted runner label (blacksmith-*, ubuntu-*, macos-*, windows-*)."
158+
failed=1
159+
else
160+
echo "inputs.$name=$label is a hosted runner label."
161+
fi
162+
}
163+
check_label runner "$RUNNER_LABEL" true
164+
check_label verify_runner "$VERIFY_RUNNER_LABEL" false
165+
exit "$failed"
166+
123167
build:
124168
name: Build ${{ inputs.label }}
169+
needs: [validate]
125170
runs-on: ${{ inputs.runner }}
126171
# Windows waits for the Linux CLI archive inside its own budget (see
127172
# "Wait for Linux CLI archive"), so it gets that wait on top of the usual 30.
@@ -801,7 +846,7 @@ jobs:
801846

802847
verify:
803848
name: Verify
804-
needs: [build]
849+
needs: [validate, build]
805850
if: inputs.verify_runner != ''
806851
runs-on: ${{ inputs.verify_runner }}
807852
timeout-minutes: 15

0 commit comments

Comments
 (0)