Repository navigation
Expand file tree
/
Copy pathtest_release_workflow.py
More file actions
1972 lines (1719 loc) · 67 KB
/
Copy pathtest_release_workflow.py
File metadata and controls
1972 lines (1719 loc) · 67 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import os
import re
import subprocess
import sys
from collections.abc import Callable
from pathlib import Path
import pytest
from scripts._checks import CANONICAL_ACTIVE_MODEL
from scripts.check_workflows import (
PYPI_PUBLISH_ACTION,
PYPI_PUBLISH_ACTION_RUNTIME,
PYPI_PUBLISH_ACTION_VERSION,
check_action_pins,
check_ci_workflow,
check_publish_workflow,
check_release_please_config,
check_release_please_workflow,
check_release_recovery_workflow,
check_workflow_inventory,
workflow_paths,
)
from tests.live.test_live_smoke import resolve_live_model
PROJECT_ROOT = Path(__file__).resolve().parents[1]
CI_WORKFLOW = PROJECT_ROOT / ".github" / "workflows" / "ci.yml"
PUBLISH_WORKFLOW = PROJECT_ROOT / ".github" / "workflows" / "publish.yml"
LIVE_SMOKE_WORKFLOW = PROJECT_ROOT / ".github" / "workflows" / "live-smoke.yml"
RELEASE_PLEASE_WORKFLOW = PUBLISH_WORKFLOW
RELEASE_RECOVERY_WORKFLOW = PUBLISH_WORKFLOW
RELEASE_PLEASE_CONFIG = PROJECT_ROOT / "release-please-config.json"
RELEASE_PLEASE_MANIFEST = PROJECT_ROOT / ".release-please-manifest.json"
TRUST_SCRIPT = PROJECT_ROOT / "scripts" / "verify_release_trust.sh"
def _reviewed_release_please_bridge_config() -> str:
stable = RELEASE_PLEASE_CONFIG.read_text(encoding="utf-8")
marker = ' "include-v-in-tag": true,\n'
if marker not in stable:
raise AssertionError("stable Release Please config lost its tag contract")
bridge = (
' "last-release-sha": "31b68904141489ca04932edbf305ccf88af09372",\n'
' "prerelease": false,\n'
' "versioning": "prerelease",\n'
)
return stable.replace(marker, marker + bridge, 1)
def _git(root: Path, *arguments: str, input_text: str | None = None) -> str:
result = subprocess.run(
["git", *arguments],
cwd=root,
input=input_text,
text=True,
check=True,
capture_output=True,
)
return result.stdout.strip()
@pytest.fixture
def release_repository(tmp_path: Path) -> tuple[Path, str]:
remote = tmp_path / "origin.git"
repository = tmp_path / "repository"
remote.mkdir()
repository.mkdir()
_git(remote, "init", "--bare")
_git(repository, "init", "--initial-branch=main")
_git(repository, "config", "user.name", "Release Contract")
_git(repository, "config", "user.email", "release-contract@example.invalid")
(repository / "tracked.txt").write_text("main\n", encoding="utf-8")
_git(repository, "add", "tracked.txt")
_git(repository, "commit", "-m", "initial")
release_commit = _git(repository, "rev-parse", "HEAD")
_git(repository, "tag", "v0.1.0-alpha.1")
_git(repository, "remote", "add", "origin", str(remote))
_git(repository, "push", "--set-upstream", "origin", "main")
_git(repository, "checkout", "--detach", "v0.1.0-alpha.1")
return repository, release_commit
def _verify_trust(
repository: Path,
tmp_path: Path,
*,
immutable: str = "true",
tag: str = "v0.1.0-alpha.1",
expected_release_sha: str | None = None,
) -> subprocess.CompletedProcess[str]:
output = tmp_path / "github-output.txt"
environment = os.environ.copy()
environment.update(
{
"DEFAULT_BRANCH": "main",
"EXPECTED_RELEASE_SHA": expected_release_sha
or _git(repository, "rev-parse", f"{tag}^{{commit}}"),
"GITHUB_OUTPUT": str(output),
"RELEASE_IMMUTABLE": immutable,
"RELEASE_TAG": tag,
}
)
return subprocess.run(
["bash", str(TRUST_SCRIPT)],
cwd=repository,
env=environment,
text=True,
check=False,
capture_output=True,
)
def _step(text: str, name: str) -> tuple[int, int]:
start = text.index(f" - name: {name}")
next_step = text.find("\n - name: ", start + 1)
return start, len(text) if next_step < 0 else next_step + 1
def _bypass_immutable_event(text: str) -> str:
return text.replace(
'RELEASE_IMMUTABLE: "true"',
'RELEASE_IMMUTABLE: "false"',
1,
)
def _remove_live_dependency(text: str) -> str:
return text.replace(" - release-live-smoke\n", "", 1)
def _bypass_verified_release_commit(text: str) -> str:
return text.replace(
"ref: ${{ needs.build.outputs.release-commit }}",
"ref: ${{ github.event.release.tag_name }}",
1,
)
def _remove_publication_serialization(text: str) -> str:
return text.replace("group: pypi-publish", "group: pypi-release-tag", 1)
def _split_live_concurrency_group(text: str) -> str:
return text.replace("group: trusted-live-smoke", "group: release-live-smoke", 1)
def _remove_registry_publish_dependency(text: str) -> str:
registry_start = text.index(" verify-registry:")
return text[:registry_start] + text[registry_start:].replace(" - publish\n", "", 1)
def _grant_build_write_permission(text: str) -> str:
build_start = text.index(" build:")
return text[:build_start] + text[build_start:].replace(
"permissions:\n contents: read",
"permissions:\n contents: read\n actions: write",
1,
)
def _broaden_live_credential_scope(text: str) -> str:
text = text.replace(
' COMETAPI_LIVE_CONCURRENCY: "1"',
' COMETAPI_KEY: ${{ secrets.COMETAPI_KEY }}\n COMETAPI_LIVE_CONCURRENCY: "1"',
1,
)
return text.replace(
" env:\n COMETAPI_KEY: ${{ secrets.COMETAPI_KEY }}\n",
"",
1,
)
def _remove_public_registry_install(text: str) -> str:
start, end = _step(text, "Install from public PyPI and run the isolated mocked-call smoke")
return text[:start] + text[end:]
def _weaken_verifier_bootstrap(text: str) -> str:
return (
text.replace(" PIP_CONFIG_FILE: /dev/null\n", "", 1)
.replace("python -m pip --isolated install", "python -m pip install", 1)
.replace(" --no-cache-dir\n", "", 1)
)
def _publish_regardless_of_failed_dependencies(text: str) -> str:
return text.replace(
" - release-live-smoke\n runs-on: ubuntu-latest",
" - release-live-smoke\n if: always()\n runs-on: ubuntu-latest",
1,
)
def _continue_after_live_failure(text: str) -> str:
return text.replace(
" - name: Run the bounded exact-release live suite\n",
" - name: Run the bounded exact-release live suite\n continue-on-error: true\n",
1,
)
def _continue_after_provenance_failure(text: str) -> str:
return text.replace(
" - name: Verify public artifact identity, digests, and provenance\n",
" - name: Verify public artifact identity, digests, and provenance\n"
" continue-on-error: true\n",
1,
)
def _continue_after_public_install_failure(text: str) -> str:
return text.replace(
" - name: Install from public PyPI and run the isolated mocked-call smoke\n",
" - name: Install from public PyPI and run the isolated mocked-call smoke\n"
" continue-on-error: true\n",
1,
)
def _grant_build_write_all(text: str) -> str:
build_start = text.index(" build:")
return text[:build_start] + text[build_start:].replace(
"permissions:\n contents: read",
"permissions: write-all",
1,
)
def _replace_protected_live_environment(text: str) -> str:
return text.replace(
" environment: live-smoke",
" environment: unprotected\n # environment: live-smoke",
1,
)
def _replace_protected_pypi_environment(text: str) -> str:
return text.replace(
" name: pypi",
" name: unprotected\n # name: pypi",
1,
)
def _swallow_trust_failure(text: str) -> str:
return text.replace(
"run: bash scripts/verify_release_trust.sh",
'run: bash scripts/verify_release_trust.sh || echo "release-commit=fallback"',
1,
)
def _swallow_live_failure(text: str) -> str:
return text.replace(
"run: uv run pytest -m live --maxfail=1 -q",
"run: uv run pytest -m live --maxfail=1 -q || true",
1,
)
def _swallow_provenance_failure(text: str) -> str:
provenance_start = text.index(
" - name: Verify public artifact identity, digests, and provenance"
)
return text[:provenance_start] + text[provenance_start:].replace(
" --retry-delay 10\n",
" --retry-delay 10 || true\n",
1,
)
def _swallow_public_install_failure(text: str) -> str:
install_start = text.index(
" - name: Install from public PyPI and run the isolated mocked-call smoke"
)
return text[:install_start] + text[install_start:].replace(
" --retry-delay 10\n",
" --retry-delay 10 || true\n",
1,
)
def _quote_publish_if_key(text: str) -> str:
return text.replace(
" - release-live-smoke\n runs-on: ubuntu-latest",
' - release-live-smoke\n "if": always()\n runs-on: ubuntu-latest',
1,
)
def _quote_build_write_permission(text: str) -> str:
build_start = text.index(" build:")
return text[:build_start] + text[build_start:].replace(
"permissions:\n contents: read",
'permissions:\n contents: read\n "actions": write',
1,
)
def _override_exact_release_live_model(text: str) -> str:
return text.replace(
f"COMETAPI_LIVE_MODEL: {CANONICAL_ACTIVE_MODEL}",
"COMETAPI_LIVE_MODEL: ${{ vars.COMETAPI_LIVE_MODEL }}",
1,
)
def _remove_live_credential_preflight(text: str) -> str:
start, end = _step(text, "Require the protected live credential")
return text[:start] + text[end:]
def _allow_publication_rerun(text: str) -> str:
build_start = text.index(" build:")
return text[:build_start] + text[build_start:].replace(
" github.run_attempt == 1 &&\n",
" github.run_attempt >= 1 &&\n",
1,
)
def _allow_cancelled_release_job(text: str) -> str:
build_start = text.index(" build:")
return text[:build_start] + text[build_start:].replace(
" !cancelled() &&\n",
"",
1,
)
def _accept_skipped_release_dependency(text: str) -> str:
build_start = text.index(" build:")
return text[:build_start] + text[build_start:].replace(
"needs.select-release.result == 'success'",
"needs.select-release.result != 'failure'",
1,
)
def _remove_release_selector_dependency(text: str) -> str:
build_start = text.index(" build:")
return text[:build_start] + text[build_start:].replace(
" needs:\n - select-release\n",
"",
1,
)
def _use_unverified_release_input(text: str) -> str:
build_start = text.index(" build:")
return text[:build_start] + text[build_start:].replace(
"needs.select-release.outputs.release-tag",
"inputs.release-tag",
1,
)
def _disable_publish_attestations(text: str) -> str:
return text.replace(" attestations: true\n", " attestations: false\n", 1)
PUBLICATION_BYPASSES: list[Callable[[str], str]] = [
_bypass_immutable_event,
_remove_live_dependency,
_bypass_verified_release_commit,
_remove_publication_serialization,
_split_live_concurrency_group,
_remove_registry_publish_dependency,
_grant_build_write_permission,
_broaden_live_credential_scope,
_remove_public_registry_install,
_weaken_verifier_bootstrap,
_publish_regardless_of_failed_dependencies,
_continue_after_live_failure,
_continue_after_provenance_failure,
_continue_after_public_install_failure,
_grant_build_write_all,
_replace_protected_live_environment,
_replace_protected_pypi_environment,
_swallow_trust_failure,
_swallow_live_failure,
_swallow_provenance_failure,
_swallow_public_install_failure,
_quote_publish_if_key,
_quote_build_write_permission,
_override_exact_release_live_model,
_remove_live_credential_preflight,
_allow_publication_rerun,
_allow_cancelled_release_job,
_accept_skipped_release_dependency,
_remove_release_selector_dependency,
_use_unverified_release_input,
_disable_publish_attestations,
]
def test_current_publish_workflow_satisfies_semantic_contract() -> None:
check_publish_workflow(
PUBLISH_WORKFLOW.read_text(encoding="utf-8"),
LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"),
)
@pytest.mark.parametrize(
"needle",
[
" --require-releasable-docs --print-version)",
" --require-releasable-docs dist/*",
],
ids=["source-tag-gate", "artifact-version-gate"],
)
def test_publish_contract_requires_releasable_document_gate(needle: str) -> None:
text = PUBLISH_WORKFLOW.read_text(encoding="utf-8")
assert needle in text
with pytest.raises(RuntimeError):
check_publish_workflow(
text.replace(needle, needle.replace(" --require-releasable-docs", ""), 1),
LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"),
)
@pytest.mark.parametrize(
"replacement",
[
"",
" - name: Verify the immutable release as a copied standalone repository\n"
' run: echo "python scripts/check_repository_independence.py"\n',
],
ids=["missing", "echo-decoy"],
)
def test_publish_contract_requires_immutable_standalone_verification(
replacement: str,
) -> None:
text = PUBLISH_WORKFLOW.read_text(encoding="utf-8")
step = (
" - name: Verify the immutable release as a copied standalone repository\n"
" run: python scripts/check_repository_independence.py\n"
)
assert text.count(step) == 1
with pytest.raises(RuntimeError):
check_publish_workflow(
text.replace(step, replacement, 1),
LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"),
)
def test_publish_contract_orders_immutable_standalone_verification() -> None:
text = PUBLISH_WORKFLOW.read_text(encoding="utf-8")
clean_install = (
" - name: Install and smoke-test each exact artifact\n"
" run: uv run python scripts/check_clean_install.py dist/*\n"
)
standalone = (
" - name: Verify the immutable release as a copied standalone repository\n"
" run: python scripts/check_repository_independence.py\n"
)
digest = (
" - name: Record immutable artifact digests\n"
" run: sha256sum dist/* > artifact-sha256.txt\n"
)
assert text.index(clean_install) < text.index(standalone) < text.index(digest)
reordered = text.replace(clean_install + standalone, standalone + clean_install, 1)
with pytest.raises(RuntimeError):
check_publish_workflow(
reordered,
LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"),
)
def test_release_please_uses_split_node24_v5_execution() -> None:
text = RELEASE_PLEASE_WORKFLOW.read_text(encoding="utf-8")
action = (
"googleapis/release-please-action@45996ed1f6d02564a971a2fa1b5860e934307cf7 # v5.0.0, node24"
)
assert text.count(action) == 3
check_release_please_workflow(text)
def test_release_please_rejects_legacy_node20_pin() -> None:
text = RELEASE_PLEASE_WORKFLOW.read_text(encoding="utf-8").replace(
"45996ed1f6d02564a971a2fa1b5860e934307cf7",
"5c625bfb5d1ff62eadeeb3772007f7f66fdcf071",
1,
)
with pytest.raises(RuntimeError, match=r"5\.0\.0.*node24"):
check_release_please_workflow(text)
def test_pypi_publisher_uses_reviewed_node24_action() -> None:
text = PUBLISH_WORKFLOW.read_text(encoding="utf-8")
action = (
f"{PYPI_PUBLISH_ACTION} # v{PYPI_PUBLISH_ACTION_VERSION}, {PYPI_PUBLISH_ACTION_RUNTIME}"
)
assert text.count(action) == 1
check_publish_workflow(text, LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"))
@pytest.mark.parametrize(
"unreviewed_sha",
[
"ba38be9e461d3875417946c167d0b5f3d385a247",
"dc37677b2e1c63e2034f94d8a5b11f265b73ba33",
"cef221092ed1bacb1cc03d23a2d87d1d172e277b",
],
ids=["prior-1.14.1", "prior-1.14.2", "unreviewed"],
)
def test_pypi_publisher_rejects_another_full_sha(unreviewed_sha: str) -> None:
text = PUBLISH_WORKFLOW.read_text(encoding="utf-8").replace(
PYPI_PUBLISH_ACTION,
f"pypa/gh-action-pypi-publish@{unreviewed_sha}",
1,
)
with pytest.raises(RuntimeError, match=re.escape(PYPI_PUBLISH_ACTION)):
check_publish_workflow(text, LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"))
@pytest.mark.parametrize(
("needle", "replacement"),
[
(
' skip-github-pull-request: "true"',
' skip-github-pull-request: "false"',
),
(
' skip-github-release: "true"',
' skip-github-release: "false"',
),
(" continue-on-error: true", " continue-on-error: false"),
(
" steps.release-pr.outcome == 'failure'",
" steps.release-pr.conclusion == 'failure'",
),
(
" id: release\n uses:",
" id: release\n continue-on-error: true\n uses:",
),
(
" id: retry-release-pr\n if:",
" id: retry-release-pr\n continue-on-error: true\n if:",
),
],
ids=[
"release-step-can-maintain-pr",
"pr-step-can-create-release",
"first-pr-attempt-cannot-fail",
"retry-does-not-use-outcome",
"release-step-retryable",
"second-pr-attempt-retryable",
],
)
def test_release_please_rejects_unsafe_retry_boundaries(needle: str, replacement: str) -> None:
text = RELEASE_PLEASE_WORKFLOW.read_text(encoding="utf-8")
assert needle in text
with pytest.raises(RuntimeError):
check_release_please_workflow(text.replace(needle, replacement, 1))
@pytest.mark.parametrize("configured", [None, ""])
def test_live_model_defaults_when_unset_or_empty(configured: str | None) -> None:
assert resolve_live_model(configured) == CANONICAL_ACTIVE_MODEL
def test_workflow_live_models_match_the_canonical_active_model() -> None:
monitoring = LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8")
publication = PUBLISH_WORKFLOW.read_text(encoding="utf-8")
assert f"COMETAPI_LIVE_MODEL: {CANONICAL_ACTIVE_MODEL}" in monitoring
assert f"COMETAPI_LIVE_MODEL: {CANONICAL_ACTIVE_MODEL}" in publication
@pytest.mark.parametrize(
("workflow", "needle", "replacement"),
[
(
"monitoring",
f"COMETAPI_LIVE_MODEL: {CANONICAL_ACTIVE_MODEL}",
"COMETAPI_LIVE_MODEL: gpt-5.6",
),
(
"publication",
f"COMETAPI_LIVE_MODEL: {CANONICAL_ACTIVE_MODEL}",
"COMETAPI_LIVE_MODEL: ${{ vars.COMETAPI_LIVE_MODEL || 'gpt-5.6-sol' }}",
),
],
ids=["monitoring-model-drift", "exact-release-model-drift"],
)
def test_semantic_contract_rejects_live_model_drift(
workflow: str, needle: str, replacement: str
) -> None:
publication = PUBLISH_WORKFLOW.read_text(encoding="utf-8")
monitoring = LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8")
if workflow == "monitoring":
assert needle in monitoring
monitoring = monitoring.replace(needle, replacement, 1)
else:
assert needle in publication
publication = publication.replace(needle, replacement, 1)
with pytest.raises(RuntimeError, match="bounded execution budget"):
check_publish_workflow(publication, monitoring)
@pytest.mark.parametrize(
("needle", "replacement", "message"),
[
(' default: "64"', ' default: "128"', "64-token default"),
(' - "256"', ' - "512"', "64/128/256"),
(
"COMETAPI_LIVE_MAX_OUTPUT_TOKENS: ${{ inputs.max_output_tokens || '64' }}",
"COMETAPI_LIVE_MAX_OUTPUT_TOKENS: ${{ inputs.max_output_tokens }}",
"bounded execution budget",
),
],
ids=["wrong-default", "unreviewed-choice", "missing-scheduled-fallback"],
)
def test_monitoring_live_output_cap_is_a_bounded_choice(
needle: str, replacement: str, message: str
) -> None:
monitoring = LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8")
assert needle in monitoring
with pytest.raises(RuntimeError, match=message):
check_publish_workflow(
PUBLISH_WORKFLOW.read_text(encoding="utf-8"),
monitoring.replace(needle, replacement, 1),
)
def test_exact_release_live_output_cap_is_fixed_at_64() -> None:
publication = PUBLISH_WORKFLOW.read_text(encoding="utf-8")
release_job_start = publication.index(" release-live-smoke:")
publish_job_start = publication.index(" publish:")
release_job = publication[release_job_start:publish_job_start]
needle = 'COMETAPI_LIVE_MAX_OUTPUT_TOKENS: "64"'
assert needle in release_job
release_job = release_job.replace(
needle,
'COMETAPI_LIVE_MAX_OUTPUT_TOKENS: "128"',
1,
)
with pytest.raises(RuntimeError, match="bounded execution budget"):
check_publish_workflow(
publication[:release_job_start] + release_job + publication[publish_job_start:],
LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"),
)
def test_workflow_contract_rejects_mutable_action_reference() -> None:
text, replacements = re.subn(
r"(actions/checkout@)[0-9a-f]{40}",
r"\g<1>v4",
CI_WORKFLOW.read_text(encoding="utf-8"),
count=1,
)
assert replacements == 1
with pytest.raises(RuntimeError, match="full commit SHA"):
check_action_pins(text, CI_WORKFLOW.name)
@pytest.mark.parametrize(
"text",
[
"jobs:\n check:\n runs-on: ubuntu-latest\n steps:\n"
' - "uses": actions/checkout@v4\n',
"jobs:\n check:\n runs-on: ubuntu-latest\n steps: [{ uses: actions/checkout@v4 }]\n",
"jobs:\n check:\n runs-on: ubuntu-latest\n steps:\n"
" - uses: >-\n actions/checkout@v4\n",
"checkout: &checkout actions/checkout@v4\njobs:\n check:\n"
" runs-on: ubuntu-latest\n steps:\n - uses: *checkout\n",
"jobs:\n check:\n runs-on: ubuntu-latest\n steps:\n"
" - ? uses\n : actions/checkout@v4\n",
"jobs:\n check:\n runs-on: ubuntu-latest\n steps:\n"
" - uses: !!str actions/checkout@v4\n",
],
ids=[
"quoted-key",
"inline-mapping",
"folded-scalar",
"alias",
"explicit-mapping",
"tagged-scalar",
],
)
def test_workflow_contract_rejects_disguised_mutable_action_reference(text: str) -> None:
with pytest.raises(RuntimeError, match="full commit SHA"):
check_action_pins(text, "adversarial workflow")
def test_workflow_contract_ignores_nonsemantic_uses_key() -> None:
text = """\
env:
uses: actions/checkout@v4
jobs:
check:
runs-on: ubuntu-latest
steps:
- run: echo checked
"""
check_action_pins(text, "nonsemantic uses workflow")
def test_workflow_contract_rejects_unpinned_docker_action() -> None:
text = """\
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: docker://alpine:latest
"""
with pytest.raises(RuntimeError, match="Docker action references are not permitted"):
check_action_pins(text, "Docker workflow")
@pytest.mark.parametrize(
"mutation",
PUBLICATION_BYPASSES,
ids=[
"immutable-event-bypass",
"live-dependency-bypass",
"release-commit-bypass",
"publication-concurrency-bypass",
"live-concurrency-bypass",
"registry-publication-bypass",
"build-write-permission-bypass",
"live-credential-scope-bypass",
"public-registry-install-bypass",
"verifier-bootstrap-bypass",
"publish-always-bypass",
"live-continue-on-error-bypass",
"provenance-continue-on-error-bypass",
"public-install-continue-on-error-bypass",
"build-write-all-bypass",
"live-environment-comment-bypass",
"pypi-environment-comment-bypass",
"trust-shell-failure-bypass",
"live-shell-failure-bypass",
"provenance-shell-failure-bypass",
"public-install-shell-failure-bypass",
"quoted-publish-if-bypass",
"quoted-build-write-bypass",
"release-model-variable-override",
"missing-live-credential-preflight",
"publication-rerun-bypass",
"cancelled-release-job-bypass",
"skipped-release-dependency-bypass",
"release-selector-dependency-bypass",
"unverified-release-input-bypass",
"disabled-publish-attestations-bypass",
],
)
def test_semantic_contract_rejects_publication_bypasses(
mutation: Callable[[str], str],
) -> None:
with pytest.raises(RuntimeError):
check_publish_workflow(
mutation(PUBLISH_WORKFLOW.read_text(encoding="utf-8")),
LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"),
)
@pytest.mark.parametrize(
("needle", "replacement"),
[
(
"run: bash scripts/verify_release_trust.sh",
'run: echo "bash scripts/verify_release_trust.sh"',
),
(
"run: uv run python scripts/check_workflows.py",
"run: uv run python scripts/check_workflows.py | true",
),
(
'run: test "$(git rev-parse HEAD)" = "$RELEASE_COMMIT"',
"run: true",
),
(
"run: uv run pytest -m live --maxfail=1 -q",
"run: uv run pytest -m live --maxfail=1 -q --collect-only",
),
(
"run: sha256sum --check artifact-sha256.txt",
'run: echo "sha256sum --check artifact-sha256.txt"',
),
(
"uses: pypa/gh-action-pypi-publish@",
"uses: attacker/example-action@",
),
(
"python -m pip --isolated install",
"echo python -m pip --isolated install",
),
(
"python scripts/check_registry_release.py",
"echo python scripts/check_registry_release.py",
),
(
"python scripts/check_clean_install.py\n"
' --expected-version "$RELEASE_VERSION"',
"echo python scripts/check_clean_install.py\n"
' --expected-version "$RELEASE_VERSION"',
),
],
ids=[
"trust-echo-decoy",
"workflow-check-pipe-bypass",
"commit-check-noop",
"live-collect-only",
"publish-digest-echo-decoy",
"arbitrary-oidc-action",
"provenance-install-echo-decoy",
"registry-verification-echo-decoy",
"registry-install-echo-decoy",
],
)
def test_semantic_contract_rejects_exact_step_decoys(needle: str, replacement: str) -> None:
text = PUBLISH_WORKFLOW.read_text(encoding="utf-8")
assert needle in text
with pytest.raises(RuntimeError):
check_publish_workflow(
text.replace(needle, replacement, 1),
LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"),
)
def test_semantic_contract_rejects_registry_ref_decoy() -> None:
text = PUBLISH_WORKFLOW.read_text(encoding="utf-8")
registry_start = text.index(" verify-registry:")
registry = text[registry_start:].replace(
"ref: ${{ needs.build.outputs.release-commit }}",
"ref: main\n"
" env:\n"
" EXPECTED_REF: ${{ needs.build.outputs.release-commit }}",
1,
)
with pytest.raises(RuntimeError):
check_publish_workflow(
text[:registry_start] + registry,
LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"),
)
def test_semantic_contract_rejects_publish_environment_test_bypass() -> None:
text = PUBLISH_WORKFLOW.read_text(encoding="utf-8").replace(
" UV_VERSION: 0.11.8",
" UV_VERSION: 0.11.8\n PYTEST_ADDOPTS: --collect-only",
1,
)
with pytest.raises(RuntimeError, match="pinned uv"):
check_publish_workflow(
text,
LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"),
)
def test_semantic_contract_rejects_extra_oidc_step() -> None:
text = PUBLISH_WORKFLOW.read_text(encoding="utf-8").replace(
" steps:\n - name: Download the verified release bundle",
" steps:\n"
" - name: Unreviewed OIDC consumer\n"
" uses: attacker/example-action@"
"0123456789abcdef0123456789abcdef01234567\n"
" - name: Download the verified release bundle",
1,
)
with pytest.raises(RuntimeError, match="reviewed sequence"):
check_publish_workflow(
text,
LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"),
)
def test_semantic_contract_rejects_split_monitoring_live_concurrency() -> None:
live_smoke = LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8").replace(
"group: trusted-live-smoke", "group: monitoring-live-smoke", 1
)
with pytest.raises(RuntimeError, match="monitoring live smokes must share"):
check_publish_workflow(
PUBLISH_WORKFLOW.read_text(encoding="utf-8"),
live_smoke,
)
@pytest.mark.parametrize(
"replacement",
[
"github.event_name == 'workflow_dispatch'",
"(github.event_name == 'workflow_dispatch' || vars.LIVE_SMOKE_ENABLED == 'true')",
"vars.LIVE_SMOKE_ENABLED != 'false'",
"vars.LIVE_SMOKE_ENABLED == 'true'\n || github.event_name == 'workflow_dispatch'",
],
ids=["manual-only", "manual-bypass", "non-exact-opt-in", "continued-manual-bypass"],
)
def test_semantic_contract_rejects_live_smoke_gate_bypasses(replacement: str) -> None:
live_smoke = LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8").replace(
"vars.LIVE_SMOKE_ENABLED == 'true'",
replacement,
1,
)
with pytest.raises(RuntimeError, match="every trigger"):
check_publish_workflow(
PUBLISH_WORKFLOW.read_text(encoding="utf-8"),
live_smoke,
)
def test_semantic_contract_checks_live_smoke_gate_on_smoke_job() -> None:
live_smoke = LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8").replace(
"vars.LIVE_SMOKE_ENABLED == 'true'",
"github.event_name == 'workflow_dispatch'",
1,
)
live_smoke += """
decoy:
if: >-
github.ref == format('refs/heads/{0}', github.event.repository.default_branch) &&
vars.LIVE_SMOKE_ENABLED == 'true'
runs-on: ubuntu-latest
"""
with pytest.raises(RuntimeError, match="every trigger"):
check_publish_workflow(
PUBLISH_WORKFLOW.read_text(encoding="utf-8"),
live_smoke,
)
@pytest.mark.parametrize(
("needle", "replacement", "message"),
[
(
"permissions:\n contents: read",
"permissions:\n contents: write",
"permissions",
),
(
"run: uv run pytest -m live --maxfail=1 -q",
"continue-on-error: true\n run: uv run pytest -m live --maxfail=1 -q",
"must not allow failure",
),
(
"on:\n schedule:",
"on:\n push:\n branches: [main]\n schedule:",
"only on schedule or manual dispatch",
),
("runs-on: ubuntu-latest", "runs-on: self-hosted", "GitHub-hosted runner"),
],
ids=["write-token", "continued-test", "extra-trigger", "self-hosted-runner"],
)
def test_semantic_contract_rejects_monitoring_live_bypasses(
needle: str, replacement: str, message: str
) -> None:
live_smoke = LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8").replace(needle, replacement, 1)
with pytest.raises(RuntimeError, match=message):
check_publish_workflow(
PUBLISH_WORKFLOW.read_text(encoding="utf-8"),
live_smoke,
)
@pytest.mark.parametrize(
("needle", "replacement"),
[
(
" run: bash scripts/verify_release_trust.sh",
" working-directory: decoy\n run: bash scripts/verify_release_trust.sh",
),
(
" run: uv run pytest -m live --maxfail=1 -q",
" working-directory: decoy\n run: uv run pytest -m live --maxfail=1 -q",
),
(
" run: >-\n python scripts/check_registry_release.py",
" working-directory: decoy\n"
" run: >-\n"
" python scripts/check_registry_release.py",
),
],
ids=["release-trust", "exact-release-live", "registry-provenance"],
)
def test_semantic_contract_rejects_release_working_directory_redirects(
needle: str, replacement: str
) -> None:
text = PUBLISH_WORKFLOW.read_text(encoding="utf-8")
assert needle in text
with pytest.raises(RuntimeError, match="repository root"):
check_publish_workflow(
text.replace(needle, replacement, 1),
LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8"),
)
def test_semantic_contract_rejects_monitoring_working_directory_redirect() -> None:
live_smoke = LIVE_SMOKE_WORKFLOW.read_text(encoding="utf-8").replace(
" run: uv run pytest -m live --maxfail=1 -q",
" working-directory: decoy\n run: uv run pytest -m live --maxfail=1 -q",
1,
)