Skip to content

Commit 8a23864

Browse files
ci-cxclaude
andcommitted
ci: scan the release prebuilt job for secret expressions in raw text and pin its guards
The prebuilt secret scan ran after comment lines were dropped, so a commented `${{ secrets.NAME }}` inside a run block passed while GitHub still expands the expression into the step script before the shell discards the comment. The credential check and the runner policy now also read the raw job text for `${{ ... }}` expressions. New rows put commented secret expressions (dotted, indexed, dynamic index, toJSON and a format string containing a closing brace pair) into the real prebuilt job and expect the finding, keep plain comment prose and the GITHUB_TOKEN reference clean, change the job permissions in two more ways than an added id-token, and give the Build step an `if:`, a continue-on-error and a duplicate step name. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 2f50233 commit 8a23864

2 files changed

Lines changed: 96 additions & 8 deletions

File tree

‎scripts/ci_preflight.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,26 @@ def _effective_permissions(workflow: str, body: str) -> list[str]:
20272027
return _job_values(body, 'permissions') or _job_values(workflow, 'permissions')
20282028

20292029

2030+
def _github_expressions(text: str) -> list[str]:
2031+
"""Read raw expressions, including comments, without retaining plain prose."""
2032+
expressions, index = [], 0
2033+
while (start := text.find('${{', index)) != -1:
2034+
end, in_string = start + 3, False
2035+
while end < len(text):
2036+
if text[end] == "'":
2037+
if in_string and text.startswith("''", end):
2038+
end += 2
2039+
continue
2040+
in_string = not in_string
2041+
elif not in_string and text.startswith('}}', end):
2042+
end += 2
2043+
break
2044+
end += 1
2045+
expressions.append(text[start:end])
2046+
index = end
2047+
return expressions
2048+
2049+
20302050
def _holds_credentials(workflow: str, body: str, *, effective_permissions: bool = False) -> bool:
20312051
"""L6: non-GITHUB_TOKEN secrets, OIDC writes or a deployment environment.
20322052
@@ -2044,6 +2064,8 @@ def _holds_credentials(workflow: str, body: str, *, effective_permissions: bool
20442064
return True
20452065
text = '\n'.join(re.sub(r'\s+#.*$', '', line) for line in _code(body))
20462066
text += '\n' + '\n'.join(_job_values(workflow, 'env'))
2067+
# GitHub interpolates expressions before the shell discards comments.
2068+
text += '\n' + '\n'.join(_github_expressions(body))
20472069
for match in re.finditer(r'\bsecrets\s*(?:\.\s*([A-Za-z_]\w*)|\[([^\]]+)\])',
20482070
text, re.IGNORECASE):
20492071
if match.group(1) is not None:
@@ -2717,7 +2739,8 @@ def runner_findings(workflows: dict[str, str]) -> list[str]:
27172739
if _job_values(prebuilt, 'permissions') != ['contents: write']:
27182740
findings.append('release.yml prebuilt must grant only contents: write '
27192741
'without id-token')
2720-
credential_text = '\n'.join(_code(prebuilt) + _job_block(workflows['release.yml'], 'env'))
2742+
credential_text = '\n'.join(_code(prebuilt) + _github_expressions(prebuilt)
2743+
+ _job_block(workflows['release.yml'], 'env'))
27212744
if re.search(r'\bsecrets\b', re.sub(r'\bsecrets\.GITHUB_TOKEN\b', '', credential_text),
27222745
re.IGNORECASE):
27232746
findings.append('release.yml prebuilt may reference only secrets.GITHUB_TOKEN')

‎scripts/test_ci_preflight.py‎

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,6 +2325,14 @@ def test_real_release_arm_route_and_environment_are_pinned(self):
23252325
def test_protected_arm_with_release_environment_is_accepted(self):
23262326
workflow = self.protected_workflow()
23272327
self.assertEqual(ci_preflight.runner_findings({'release.yml': workflow}), [])
2328+
for comment in ('# Ordinary build comment',
2329+
'# secrets.PINEFORGE_APP_PRIVATE_KEY is not used here',
2330+
'# ${{ secrets.GITHUB_TOKEN }}'):
2331+
with self.subTest(comment=comment):
2332+
changed = in_step(workflow, 'prebuilt', 'Configure', ' run: |\n',
2333+
' run: |\n ' + comment + '\n')
2334+
self.assertNotEqual(changed, workflow)
2335+
self.assertEqual(ci_preflight.runner_findings({'release.yml': changed}), [])
23282336

23292337
def test_hosted_arm_is_refused_on_the_real_release_matrix(self):
23302338
workflow = self.protected_workflow()
@@ -2347,13 +2355,17 @@ def test_protected_arm_without_release_environment_is_refused(self):
23472355

23482356
def test_protected_arm_refuses_oidc_and_other_secret_access(self):
23492357
workflow = self.protected_workflow()
2350-
changed = in_job(workflow, 'prebuilt',
2351-
' contents: write # gh release upload\n',
2352-
' contents: write # gh release upload\n'
2353-
' id-token: write\n')
2354-
self.assertNotEqual(changed, workflow)
2355-
self.assertIn(self.PERMISSIONS_FINDING,
2356-
ci_preflight.runner_findings({'release.yml': changed}))
2358+
for permissions in (' contents: write # gh release upload\n'
2359+
' id-token: write\n',
2360+
' contents: read # gh release upload\n',
2361+
' contents: write # gh release upload\n'
2362+
' packages: write\n'):
2363+
with self.subTest(permissions=permissions):
2364+
changed = in_job(workflow, 'prebuilt',
2365+
' contents: write # gh release upload\n', permissions)
2366+
self.assertNotEqual(changed, workflow)
2367+
self.assertIn(self.PERMISSIONS_FINDING,
2368+
ci_preflight.runner_findings({'release.yml': changed}))
23572369
for reference in ('secrets.DEPLOY_KEY', "secrets['GITHUB_TOKEN']",
23582370
'secrets.github_token', 'toJSON(secrets)'):
23592371
for job, before, after in (
@@ -2368,6 +2380,47 @@ def test_protected_arm_refuses_oidc_and_other_secret_access(self):
23682380
self.assertIn(self.SECRETS_FINDING,
23692381
ci_preflight.runner_findings({'release.yml': changed}))
23702382

2383+
def test_commented_secret_expressions_are_refused_on_the_real_release(self):
2384+
workflow = self.workflow()
2385+
self.assertIn(' GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}\n',
2386+
_jobs(workflow)['prebuilt'])
2387+
self.assertEqual(ci_preflight.runner_findings({'release.yml': workflow}), [])
2388+
for reference in ('secrets.PINEFORGE_APP_PRIVATE_KEY',
2389+
"secrets['PINEFORGE_APP_PRIVATE_KEY']",
2390+
'secrets[inputs.key]', 'toJSON(secrets)',
2391+
"format('}} {0}', secrets.PINEFORGE_APP_PRIVATE_KEY)"):
2392+
with self.subTest(reference=reference):
2393+
changed = in_step(workflow, 'prebuilt', 'Configure', ' run: |\n',
2394+
' run: |\n # ${{ ' + reference + ' }}\n')
2395+
self.assertNotEqual(changed, workflow)
2396+
self.assertEqual(ci_preflight.runner_findings({'release.yml': changed}),
2397+
[self.SECRETS_FINDING])
2398+
2399+
def test_credential_detection_reads_commented_expressions_on_the_real_release(self):
2400+
# Remove the environment so it cannot mask a missed secret reference.
2401+
workflow = in_job(self.workflow(), 'prebuilt', ' environment: release\n', '')
2402+
self.assertFalse(ci_preflight._holds_credentials(workflow, _jobs(workflow)['prebuilt']))
2403+
for comment in ('# secrets.PINEFORGE_APP_PRIVATE_KEY is not used here',
2404+
'# ${{ secrets.GITHUB_TOKEN }}',
2405+
"# ${{ secrets['GITHUB_TOKEN'] }}"):
2406+
with self.subTest(allowed_comment=comment):
2407+
changed = in_step(workflow, 'prebuilt', 'Configure', ' run: |\n',
2408+
' run: |\n ' + comment + '\n')
2409+
self.assertNotEqual(changed, workflow)
2410+
self.assertFalse(ci_preflight._holds_credentials(
2411+
changed, _jobs(changed)['prebuilt']))
2412+
for reference in ('secrets.PINEFORGE_APP_PRIVATE_KEY',
2413+
"secrets['PINEFORGE_APP_PRIVATE_KEY']", 'secrets[inputs.key]',
2414+
"format('}} {0}', secrets.PINEFORGE_APP_PRIVATE_KEY)"):
2415+
for prefix in ('# ', ': # '):
2416+
with self.subTest(reference=reference, prefix=prefix):
2417+
changed = in_step(workflow, 'prebuilt', 'Configure', ' run: |\n',
2418+
' run: |\n ' + prefix
2419+
+ '${{ ' + reference + ' }}\n')
2420+
self.assertNotEqual(changed, workflow)
2421+
self.assertTrue(ci_preflight._holds_credentials(
2422+
changed, _jobs(changed)['prebuilt']))
2423+
23712424
def test_release_build_requires_runner_core_count(self):
23722425
workflow = self.workflow()
23732426
self.assertIn(f' run: cmake --build build -j {CORES}\n',
@@ -2380,6 +2433,18 @@ def test_release_build_requires_runner_core_count(self):
23802433
self.assertNotEqual(changed, workflow)
23812434
self.assertIn(self.BUILD_FINDING,
23822435
ci_preflight.runner_findings({'release.yml': changed}))
2436+
for guard in (' if: ${{ false }}\n', ' continue-on-error: true\n'):
2437+
with self.subTest(guard=guard):
2438+
changed = in_step(workflow, 'prebuilt', 'Build', ' - name: Build\n',
2439+
' - name: Build\n' + guard)
2440+
self.assertNotEqual(changed, workflow)
2441+
self.assertIn(self.BUILD_FINDING,
2442+
ci_preflight.runner_findings({'release.yml': changed}))
2443+
build = f' - name: Build\n run: cmake --build build -j {CORES}\n'
2444+
duplicate = in_job(workflow, 'prebuilt', build, build + '\n' + build)
2445+
self.assertEqual(_jobs(duplicate)['prebuilt'].count(' - name: Build\n'), 2)
2446+
self.assertIn(self.BUILD_FINDING,
2447+
ci_preflight.runner_findings({'release.yml': duplicate}))
23832448

23842449
def test_protected_release_label_is_declared_once(self):
23852450
config = (ROOT / '.github/actionlint.yaml').read_text()

0 commit comments

Comments
 (0)