Skip to content

Commit dcbffbe

Browse files
committed
Scope archive credential checks to prebuilt and pin its consumer
Preserve main's credential handling for every other workflow job. Update the deployment-environment inventory and reusable isolated-tree helper fixtures. Validate the delegated packed-archive consumer against its reviewed source identity, with genuine, missing and changed-source fixture controls.
1 parent 669de8d commit dcbffbe

2 files changed

Lines changed: 128 additions & 41 deletions

File tree

‎scripts/ci_preflight.py‎

Lines changed: 68 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,12 +2265,15 @@ def _private_key_inputs(text: str) -> bool:
22652265
return False
22662266

22672267

2268-
def _holds_credentials(workflow: str, body: str, *, effective_permissions: bool = False) -> bool:
2268+
def _holds_credentials(workflow: str, body: str, *, effective_permissions: bool = False,
2269+
strict: bool = False) -> bool:
22692270
"""L6: non-GITHUB_TOKEN secrets, OIDC writes or a deployment environment.
22702271
22712272
Existing VM checks conservatively include workflow permissions. GKE checks
22722273
use the job's effective scopes. Bracket/dynamic secret access and inherited
2273-
reusable-workflow secrets must not bypass either rule.
2274+
reusable-workflow secrets must not bypass either rule. ``strict`` is
2275+
reserved for the token-only prebuilt artifact job; other callers retain
2276+
the main workflow's comment-stripped credential scan.
22742277
"""
22752278
if (_job_values(body, 'environment')
22762279
or _job_values(body, 'secrets') == ['inherit']):
@@ -2282,38 +2285,42 @@ def _holds_credentials(workflow: str, body: str, *, effective_permissions: bool
22822285
for value in permissions):
22832286
return True
22842287

2285-
# Expressions expand even in script comments; retain every raw byte.
2286-
# Other jobs' App credentials are not inherited by the artifact job.
2287-
header = _JOBS_KEY.split(workflow, maxsplit=1)[0]
2288-
inherited_env = []
2289-
in_env = False
2290-
for line in workflow.split('\n'):
2291-
if line.strip() and _indent(line) == 0 and not line.startswith('#'):
2292-
in_env = _key_value(line, 'env') is not None
2293-
if in_env:
2294-
inherited_env.append(line)
2295-
text = header + '\n' + body + '\n' + '\n'.join(inherited_env)
2296-
if (_uses_app_token(workflow, body)
2297-
or _private_key_inputs(body + '\n' + '\n'.join(inherited_env))):
2298-
return True
2299-
# A whole/dynamic secrets context cannot prove GITHUB_TOKEN-only use.
2300-
# Prose may mention secrets; inspect expressions without cutting hashes
2301-
# or treating a quoted }} (including doubled quotes) as their end.
2302-
for expression in re.finditer(r'\$\{\{', text):
2303-
end, in_string = expression.end(), False
2304-
while end < len(text):
2305-
if text[end] == "'":
2306-
if in_string and text.startswith("''", end):
2307-
end += 2
2308-
continue
2309-
in_string = not in_string
2310-
elif not in_string and text.startswith('}}', end):
2311-
break
2312-
end += 1
2313-
value = text[expression.end():end]
2314-
if any(_SECRET_REFERENCE.match(value, context.start()) is None
2315-
for context in re.finditer(r'\bsecrets\b', value, re.IGNORECASE)):
2288+
if strict:
2289+
# Expressions expand even in script comments; retain every raw byte.
2290+
# Other jobs' App credentials are not inherited by the artifact job.
2291+
header = _JOBS_KEY.split(workflow, maxsplit=1)[0]
2292+
inherited_env = []
2293+
in_env = False
2294+
for line in workflow.split('\n'):
2295+
if line.strip() and _indent(line) == 0 and not line.startswith('#'):
2296+
in_env = _key_value(line, 'env') is not None
2297+
if in_env:
2298+
inherited_env.append(line)
2299+
text = header + '\n' + body + '\n' + '\n'.join(inherited_env)
2300+
if (_uses_app_token(workflow, body)
2301+
or _private_key_inputs(body + '\n' + '\n'.join(inherited_env))):
23162302
return True
2303+
# A whole/dynamic secrets context cannot prove GITHUB_TOKEN-only use.
2304+
# Prose may mention secrets; inspect expressions without cutting hashes
2305+
# or treating a quoted }} (including doubled quotes) as their end.
2306+
for expression in re.finditer(r'\$\{\{', text):
2307+
end, in_string = expression.end(), False
2308+
while end < len(text):
2309+
if text[end] == "'":
2310+
if in_string and text.startswith("''", end):
2311+
end += 2
2312+
continue
2313+
in_string = not in_string
2314+
elif not in_string and text.startswith('}}', end):
2315+
break
2316+
end += 1
2317+
value = text[expression.end():end]
2318+
if any(_SECRET_REFERENCE.match(value, context.start()) is None
2319+
for context in re.finditer(r'\bsecrets\b', value, re.IGNORECASE)):
2320+
return True
2321+
else:
2322+
text = '\n'.join(re.sub(r'\s+#.*$', '', line) for line in _code(body))
2323+
text += '\n' + '\n'.join(_job_values(workflow, 'env'))
23172324
for match in _SECRET_REFERENCE.finditer(text):
23182325
if match.group(1) is not None:
23192326
if match.group(1).upper() != 'GITHUB_TOKEN':
@@ -2836,6 +2843,7 @@ def runner_findings(workflows: dict[str, str]) -> list[str]:
28362843
if job not in jobs]
28372844
for job, body in jobs.items():
28382845
runs_on, uses = _job_values(body, 'runs-on'), _job_values(body, 'uses')
2846+
strict_prebuilt = (name, job) == ('release.yml', 'prebuilt')
28392847
gke_pin = gke_pins.get(job)
28402848
resolved_runners, _ = _resolved_runner_values(workflow, body)
28412849
has_gke = bool(_GKE_ROUTE.search(' '.join(runs_on + resolved_runners)))
@@ -2876,7 +2884,8 @@ def runner_findings(workflows: dict[str, str]) -> list[str]:
28762884
findings.append(f'{name} job {job} must not declare job services on GKE')
28772885
if has_gke:
28782886
permissions = _effective_permissions(workflow, body)
2879-
if (_holds_credentials(workflow, body, effective_permissions=True)
2887+
if (_holds_credentials(workflow, body, effective_permissions=True,
2888+
strict=strict_prebuilt)
28802889
or any(re.search(r'\bid-token\b', value) for value in permissions)):
28812890
findings.append(f'{name} job {job} must not expose credentials on GKE')
28822891
runner_text = ' '.join(runs_on + _job_block(body, 'strategy'))
@@ -2892,7 +2901,8 @@ def runner_findings(workflows: dict[str, str]) -> list[str]:
28922901
# Other jobs retain the conservative workflow-plus-job credential check.
28932902
exact_gke_runner = bool(gke_pin and job in fixed_hosted and raw_runners == [gke_pin])
28942903
credentials = _holds_credentials(
2895-
workflow, body, effective_permissions=exact_gke_runner)
2904+
workflow, body, effective_permissions=exact_gke_runner,
2905+
strict=strict_prebuilt)
28962906
if job not in pinned:
28972907
if pinned and runs_on:
28982908
findings.append(f'{name} job {job} needs a pinned runner and time limit')
@@ -3365,6 +3375,9 @@ def _compiler_cache_restore_with(step: str) -> list[str]:
33653375
# A changed helper requires reviewing its source and controls before repinning.
33663376
# This is a source contract, never an engine/build/glibc verdict.
33673377
RELEASE_BOOKWORM_HELPER_BLOB = 'daaf2fe0541b5d0f8aab0746b35dcf069862afa3'
3378+
# The helper delegates the shipped-archive execution to this exact consumer.
3379+
# This is a source contract, never a native-runtime verdict.
3380+
RELEASE_BOOKWORM_CONSUMER_BLOB = 'd066968913c0d4ff7358e6fd4dde2a794e1be600'
33683381

33693382

33703383
def release_prebuilt_helper_findings(source: str) -> list[str]:
@@ -3376,6 +3389,15 @@ def release_prebuilt_helper_findings(source: str) -> list[str]:
33763389
return []
33773390

33783391

3392+
def release_prebuilt_consumer_findings(source: str) -> list[str]:
3393+
payload = source.encode('utf-8')
3394+
actual = hashlib.sha1(b'blob ' + str(len(payload)).encode('ascii') + b'\0' + payload).hexdigest()
3395+
if actual != RELEASE_BOOKWORM_CONSUMER_BLOB:
3396+
return ['release prebuilt archive consumer differs from the reviewed full-library '
3397+
'native consumer source contract']
3398+
return []
3399+
3400+
33793401
def release_prebuilt_workflow_findings(workflow: str) -> list[str]:
33803402
"""Pin artifact structure and its token-only admission surface."""
33813403
prebuilt = _jobs(workflow).get('prebuilt', '')
@@ -4890,6 +4912,17 @@ def main() -> int:
48904912
findings.append('release prebuilt Bookworm helper must be a regular source file')
48914913
else:
48924914
checked += release_prebuilt_helper_findings(read_ci_text(helper_path, findings))
4915+
consumer_path = ROOT / 'scripts/check_shipped_archive_runtime.sh'
4916+
if (consumer_path.parent.is_symlink() or consumer_path.is_symlink()
4917+
or not consumer_path.is_file()):
4918+
findings.append('release prebuilt archive consumer must be a regular source file')
4919+
else:
4920+
try:
4921+
consumer = read_ci_text(consumer_path, findings, ROOT)
4922+
except OSError as error:
4923+
findings.append(f'release prebuilt archive consumer cannot be read: {error}')
4924+
else:
4925+
checked += release_prebuilt_consumer_findings(consumer)
48934926
findings = list(dict.fromkeys(findings + checked))
48944927
for finding in findings:
48954928
print(finding)

‎scripts/test_ci_preflight.py‎

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,6 @@ def test_disabling_or_loosening_the_guard_breaks_the_regression_assertion(self):
734734
def test_real_minting_jobs_have_exactly_the_inventoried_environments(self):
735735
expected = {
736736
('release.yml', 'release'): ['release'],
737-
('release.yml', 'prebuilt'): ['release'],
738737
('release.yml', 'notify-hub'): ['release'],
739738
('release-ruleset-probe.yml', 'probe'): ['release'],
740739
('promote-baseline.yml', 'promote'): ['release-bot'],
@@ -789,13 +788,35 @@ def test_plain_private_key_documentation_keeps_effective_token_scopes(self):
789788
self.assertEqual(ci_preflight.runner_findings(
790789
{**workflows, name: changed}), [])
791790
for reference in ('secrets.DEPLOY_TOKEN', "secrets['DEPLOY_TOKEN']",
792-
'secrets[inputs.secret_name]', 'toJSON(secrets)'):
791+
'secrets[inputs.secret_name]'):
793792
with self.subTest(workflow=name, reference=reference):
794-
changed = '# ${{ ' + reference + ' }}\n' + workflow
793+
changed = in_job(
794+
workflow, job, ' steps:\n',
795+
' env:\n TOKEN: ${{ ' + reference + ' }}\n'
796+
' steps:\n')
795797
self.assertNotEqual(changed, workflow)
798+
changed_body = _jobs(changed)[job]
799+
self.assertTrue(ci_preflight._holds_credentials(
800+
changed, changed_body, effective_permissions=True))
796801
self.assertIn(f'{name} job {job} must not expose credentials on GKE',
797802
ci_preflight.runner_findings({**workflows, name: changed}))
798803

804+
for reference in ('secrets.DEPLOY_TOKEN', "secrets['DEPLOY_TOKEN']",
805+
'secrets[inputs.secret_name]', 'toJSON(secrets)'):
806+
with self.subTest(workflow='release.yml', reference=reference):
807+
changed = '# ${{ ' + reference + ' }}\n' + workflows['release.yml']
808+
self.assertNotEqual(changed, workflows['release.yml'])
809+
changed_body = _jobs(changed)['prebuilt']
810+
self.assertFalse(ci_preflight._holds_credentials(
811+
changed, changed_body, effective_permissions=True))
812+
self.assertTrue(ci_preflight._holds_credentials(
813+
changed, changed_body, effective_permissions=True, strict=True))
814+
self.assertIn(
815+
'release.yml job prebuilt must keep production credentials '
816+
'on a hosted standard runner',
817+
ci_preflight.runner_findings(
818+
{**workflows, 'release.yml': changed}))
819+
799820
def test_gkp_needs_group15_mirror(self):
800821
workflows = self.workflows()
801822
mirror_file = '.github/runner-group-15-allowlist.txt'
@@ -3742,6 +3763,7 @@ class CiFileLoading(unittest.TestCase):
37423763
CONFIG = '.github/actionlint.yaml'
37433764
CMAKE = 'tests/CMakeLists.txt'
37443765
HELPER = 'scripts/build_prebuilt_bookworm.sh'
3766+
CONSUMER = 'scripts/check_shipped_archive_runtime.sh'
37453767

37463768
@staticmethod
37473769
def action_sources():
@@ -3764,9 +3786,11 @@ def make_tree(self, *, include_helper=True):
37643786
shutil.copyfile(ROOT / self.CONFIG, tree / self.CONFIG)
37653787
(tree / 'tests').mkdir()
37663788
shutil.copyfile(ROOT / self.CMAKE, tree / self.CMAKE)
3789+
(tree / 'scripts').mkdir(exist_ok=True)
3790+
shutil.copyfile(ROOT / CiFileLoading.CONSUMER, tree / CiFileLoading.CONSUMER)
37673791
if include_helper:
3768-
(tree / 'scripts').mkdir()
3769-
shutil.copyfile(ROOT / self.HELPER, tree / self.HELPER)
3792+
helper = CiFileLoading.HELPER
3793+
shutil.copyfile(ROOT / helper, tree / helper)
37703794
return tree
37713795

37723796
def crlf_tree(self, names):
@@ -3880,6 +3904,14 @@ def test_required_bookworm_helper_is_copied_from_the_genuine_source(self):
38803904
code, output = self.run_stage(tree, '--check-ci-workflow')
38813905
self.assertEqual(code, 0, output)
38823906

3907+
def test_required_bookworm_consumer_is_copied_from_the_genuine_source(self):
3908+
consumer = CiFileLoading.CONSUMER
3909+
tree = self.make_tree()
3910+
self.assertEqual((tree / consumer).read_bytes(), (ROOT / consumer).read_bytes())
3911+
self.assertFalse((tree / consumer).is_symlink())
3912+
code, output = self.run_stage(tree, '--check-ci-workflow')
3913+
self.assertEqual(code, 0, output)
3914+
38833915
def test_missing_or_symlink_bookworm_helper_is_refused(self):
38843916
finding = 'release prebuilt Bookworm helper must be a regular source file'
38853917
for kind in ('missing', 'file-symlink', 'parent-symlink'):
@@ -3890,14 +3922,36 @@ def test_missing_or_symlink_bookworm_helper_is_refused(self):
38903922
genuine.mkdir()
38913923
shutil.copyfile(ROOT / self.HELPER, genuine / 'build_prebuilt_bookworm.sh')
38923924
if kind == 'parent-symlink':
3925+
shutil.rmtree(tree / 'scripts')
38933926
(tree / 'scripts').symlink_to(genuine, target_is_directory=True)
38943927
else:
3895-
(tree / 'scripts').mkdir()
3928+
(tree / 'scripts').mkdir(exist_ok=True)
38963929
(tree / self.HELPER).symlink_to(genuine / 'build_prebuilt_bookworm.sh')
38973930
code, output = self.run_stage(tree, '--check-ci-workflow')
38983931
self.assertEqual(code, 1, output)
38993932
self.assertIn(finding, output)
39003933

3934+
def test_missing_bookworm_consumer_is_refused_by_its_source_pin(self):
3935+
consumer = CiFileLoading.CONSUMER
3936+
tree = self.make_tree()
3937+
(tree / consumer).unlink()
3938+
code, output = self.run_stage(tree, '--check-ci-workflow')
3939+
finding = 'release prebuilt archive consumer must be a regular source file'
3940+
self.assertEqual(code, 1, output)
3941+
self.assertEqual(output.splitlines(), [finding], output)
3942+
3943+
def test_changed_bookworm_consumer_is_refused_by_its_source_pin(self):
3944+
consumer = CiFileLoading.CONSUMER
3945+
original = (ROOT / consumer).read_bytes()
3946+
self.assertTrue(original.endswith(b'\n') and b'\r' not in original)
3947+
tree = self.make_tree()
3948+
(tree / consumer).write_bytes(original + b'# changed source\n')
3949+
code, output = self.run_stage(tree, '--check-ci-workflow')
3950+
finding = ('release prebuilt archive consumer differs from the reviewed full-library '
3951+
'native consumer source contract')
3952+
self.assertEqual(code, 1, output)
3953+
self.assertEqual(output.splitlines(), [finding], output)
3954+
39013955
def test_changed_bookworm_helper_and_line_endings_are_refused_by_the_source_pin(self):
39023956
original = (ROOT / self.HELPER).read_bytes()
39033957
self.assertTrue(original.endswith(b'\n') and b'\r' not in original)

0 commit comments

Comments
 (0)