-
Notifications
You must be signed in to change notification settings - Fork 784
chore(release): require audited dispatch SHA #1559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5e5d3fb
Make expected-sha input mandatory
Wibias 3e2733e
fix(release): add trusted dispatch guard
Wibias 4722f87
test(release): cover trusted dispatch guard
Wibias d4bfae5
fix(release): gate publish behind trusted dispatch validation
Wibias d373694
test(release): pin scoped dispatch permissions
Wibias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| "use strict"; | ||
|
|
||
| const ALLOWED_RELEASE_REFS = new Set([ | ||
| "refs/heads/main", | ||
| "refs/heads/preview", | ||
| ]); | ||
|
|
||
| function validateReleaseDispatch({ | ||
| eventName, | ||
| ref, | ||
| expectedSha, | ||
| actualSha, | ||
| }) { | ||
| if (eventName !== "workflow_dispatch") { | ||
| return `Release must be triggered by workflow_dispatch; got ${eventName || "(empty)"}.`; | ||
| } | ||
|
|
||
| if (!ALLOWED_RELEASE_REFS.has(ref)) { | ||
| return `Release must run from main or preview; got ${ref || "(empty)"}.`; | ||
| } | ||
|
|
||
| if (!expectedSha) { | ||
| return "expected-sha is required; refusing to publish without an audited commit."; | ||
| } | ||
|
|
||
| if (!/^[0-9a-f]{40}$/.test(expectedSha)) { | ||
| return `expected-sha must be a full 40-character commit SHA; got ${expectedSha}.`; | ||
| } | ||
|
|
||
| if (actualSha !== expectedSha) { | ||
| return ( | ||
| `branch moved after the release audit ` + | ||
| `(expected ${expectedSha}, got ${actualSha || "(empty)"}) — ` + | ||
| "refusing to publish an unaudited commit." | ||
| ); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| module.exports = { | ||
| ALLOWED_RELEASE_REFS, | ||
| validateReleaseDispatch, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| "use strict"; | ||
|
|
||
| const { describe, it } = require("node:test"); | ||
| const assert = require("node:assert/strict"); | ||
| const { validateReleaseDispatch } = require("./release-dispatch-guard.cjs"); | ||
|
|
||
| const SHA = "0123456789abcdef0123456789abcdef01234567"; | ||
| const OTHER_SHA = "89abcdef0123456789abcdef0123456789abcdef"; | ||
|
|
||
| function validate(overrides = {}) { | ||
| return validateReleaseDispatch({ | ||
| eventName: "workflow_dispatch", | ||
| ref: "refs/heads/main", | ||
| expectedSha: SHA, | ||
| actualSha: SHA, | ||
| ...overrides, | ||
| }); | ||
| } | ||
|
|
||
| describe("release dispatch guard", () => { | ||
| it("accepts an exact audited SHA on main", () => { | ||
| assert.equal(validate(), null); | ||
| }); | ||
|
|
||
| it("accepts an exact audited SHA on preview", () => { | ||
| assert.equal( | ||
| validate({ ref: "refs/heads/preview" }), | ||
| null, | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects non-workflow_dispatch events", () => { | ||
| assert.match( | ||
| validate({ eventName: "push" }), | ||
| /must be triggered by workflow_dispatch/, | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects release dispatches from unapproved refs", () => { | ||
| assert.match( | ||
| validate({ ref: "refs/heads/dev" }), | ||
| /must run from main or preview/, | ||
| ); | ||
| }); | ||
|
|
||
| it("requires expected-sha", () => { | ||
| assert.match( | ||
| validate({ expectedSha: "" }), | ||
| /expected-sha is required/, | ||
| ); | ||
| }); | ||
|
|
||
| it("requires a full 40-character commit SHA", () => { | ||
| assert.match( | ||
| validate({ expectedSha: "0123456" }), | ||
| /full 40-character commit SHA/, | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects when the selected ref moved after audit", () => { | ||
| assert.match( | ||
| validate({ actualSha: OTHER_SHA }), | ||
| /branch moved after the release audit/, | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.