Skip to content

Commit a924576

Browse files
committed
fix(ci): pin script checkout to event base SHA; treat duplicate checklist markers as malformed
1 parent a62bc19 commit a924576

5 files changed

Lines changed: 36 additions & 11 deletions

File tree

‎.github/scripts/enforce-pr-target.test.cjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("enforce-pr-target workflow", () => {
5050

5151
it("checks out trusted base-branch scripts only (never PR head)", () => {
5252
assert.match(workflow, /actions\/checkout@[0-9a-f]{40}/);
53-
assert.match(workflow, /ref:\s*\$\{\{\s*github\.event\.pull_request\.base\.ref\s*\}\}/);
53+
assert.match(workflow, /ref:\s*\$\{\{\s*github\.event\.pull_request\.base\.sha\s*\}\}/);
5454
// The readiness ping reads MAINTAINERS.md from the same trusted checkout.
5555
assert.match(workflow, /sparse-checkout:\s*\|\s*\n\s*\.github\/scripts\n\s*MAINTAINERS\.md/);
5656
assert.match(workflow, /persist-credentials:\s*false/);

‎.github/scripts/pr-quality.cjs‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,19 @@ function extractReviewReadiness(body) {
249249
}
250250
const start = body.indexOf(REVIEW_READINESS_START);
251251
const end = body.indexOf(REVIEW_READINESS_END);
252+
const startCount = body.split(REVIEW_READINESS_START).length - 1;
253+
const endCount = body.split(REVIEW_READINESS_END).length - 1;
252254
// Any marker presence counts as present: an author-edited section that is
253255
// inverted or partial must never trigger another append, or every `edited`
254-
// event would stack a second checklist (and a second body write).
255-
if (start === -1 || end === -1 || end <= start) {
256+
// event would stack a second checklist (and a second body write). Exactly
257+
// one marker pair is required: duplicates are malformed, not complete.
258+
if (
259+
start === -1 ||
260+
end === -1 ||
261+
end <= start ||
262+
startCount !== 1 ||
263+
endCount !== 1
264+
) {
256265
return {
257266
present: start !== -1 || end !== -1,
258267
complete: false,
@@ -298,6 +307,14 @@ function stripReviewReadinessSection(body) {
298307
const start = body.indexOf(REVIEW_READINESS_START);
299308
const end = body.indexOf(REVIEW_READINESS_END);
300309
if (start === -1 || end === -1 || end <= start) return body;
310+
// Malformed marker sets (duplicates, extra pairs) stay untouched: removing
311+
// only one section would leave the body half-cleaned and still marked.
312+
if (
313+
body.split(REVIEW_READINESS_START).length - 1 !== 1 ||
314+
body.split(REVIEW_READINESS_END).length - 1 !== 1
315+
) {
316+
return body;
317+
}
301318
const stripped =
302319
body.slice(0, start) + body.slice(end + REVIEW_READINESS_END.length);
303320
return stripped.replace(/\n{3,}/g, "\n\n").trimEnd();

‎.github/scripts/pr-quality.test.cjs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,14 @@ describe("review readiness checklist", () => {
352352
assert.equal(appendReviewReadinessSection(orphanEnd), orphanEnd);
353353

354354
const duplicate = SECTION + SECTION;
355-
assert.equal(extractReviewReadiness(duplicate).present, true);
355+
const duplicated = extractReviewReadiness(duplicate);
356+
assert.equal(duplicated.present, true);
357+
// A second marker pair is malformed, never complete — even when the first
358+
// section's boxes would parse as checked (CodeRabbit round 3).
359+
assert.equal(duplicated.complete, false);
360+
assert.equal(duplicated.total, 0);
356361
assert.equal(appendReviewReadinessSection(duplicate), duplicate);
362+
assert.equal(stripReviewReadinessSection(duplicate), duplicate);
357363
});
358364

359365
it("appends once and is idempotent", () => {

‎.github/workflows/enforce-pr-target.yml‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ jobs:
2929
- name: Checkout trusted PR-quality scripts
3030
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
3131
with:
32-
# The PR's base branch, not the repository default: pull_request_target
32+
# The event's base commit, not the repository default: pull_request_target
3333
# runs this workflow from the base revision, and the scripts must come
3434
# from the same revision or a merged gate would run against the
35-
# pre-promotion scripts on `main`.
36-
ref: ${{ github.event.pull_request.base.ref }}
35+
# pre-promotion scripts on `main`. The immutable SHA pins the checkout
36+
# to the exact base commit the event was built against.
37+
ref: ${{ github.event.pull_request.base.sha }}
3738
persist-credentials: false
3839
sparse-checkout: |
3940
.github/scripts

‎tests/ci-workflows.test.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -840,10 +840,11 @@ describe("GitHub Actions hardening", () => {
840840
"sparse-checkout",
841841
]);
842842
expect(checkout.with).toEqual({
843-
// The base branch, not the repository default: pull_request_target runs
844-
// this workflow from the base revision, and the scripts must match it —
845-
// a merged gate would otherwise run against pre-promotion `main` scripts.
846-
ref: "${{ github.event.pull_request.base.ref }}",
843+
// The event's base commit, not the repository default: pull_request_target
844+
// runs this workflow from the base revision, and the scripts must match
845+
// it — a merged gate would otherwise run against pre-promotion `main`
846+
// scripts. The immutable SHA pins the checkout to the event's base commit.
847+
ref: "${{ github.event.pull_request.base.sha }}",
847848
"persist-credentials": false,
848849
// MAINTAINERS.md rides along so the completion ping reads the canonical
849850
// maintainer list from the same trusted base revision as the scripts.

0 commit comments

Comments
 (0)