Skip to content

Commit

Permalink
fix: verify-token allow empty base_ref (#2475)
Browse files Browse the repository at this point in the history
#2471 added
the base_ref in verify-token but this value may be empty.

This PR allows the base_ref to be empty string.

Signed-off-by: laurentsimon <laurentsimon@google.com>
  • Loading branch information
laurentsimon committed Jul 26, 2023
1 parent 389ab52 commit bb63553
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .github/actions/verify-token/__tests__/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ describe("validateField", () => {
validateField("foo", "foo", "foo");
});

it("validates equal empty values", () => {
validateField("foo", "", "", true);
});

expect(() => {
validateField("foo", "", "");
}).toThrow();

it("does not validate unequal values", () => {
expect(() => {
validateField("foo", "foo", "bar");
Expand Down
7 changes: 4 additions & 3 deletions .github/actions/verify-token/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ function validateGitHubFields(gho) {
// event_name.
validateField("github.event_name", gho.event_name, process.env.GITHUB_EVENT_NAME);
// base_ref.
validateField("github.base_ref", gho.base_ref, process.env.GITHUB_BASE_REF);
validateField("github.base_ref", gho.base_ref, process.env.GITHUB_BASE_REF, true);
// Validate the event. Only events in
// https://github.com/slsa-framework/github-actions-buildtypes/tree/main/workflow/v1
// are supported.
Expand Down Expand Up @@ -1027,13 +1027,14 @@ exports.validateFieldAnyOf = validateFieldAnyOf;
* @param name - the name of the value
* @param actual - the actual value of the field
* @param expected - the expected value of the field
* @param allow_empty - whether the value may be empty
* @throws Error - if actual and expected don't match or are empty.
*/
function validateField(name, actual, expected) {
function validateField(name, actual, expected, allow_empty = false) {
if (actual !== expected) {
throw new Error(`mismatch ${name}: got '${actual}', expected '${expected}'.`);
}
if (!actual) {
if (!allow_empty && !actual) {
throw new Error(`empty ${name}, expected non-empty value.`);
}
}
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/verify-token/dist/index.js.map

Large diffs are not rendered by default.

17 changes: 14 additions & 3 deletions .github/actions/verify-token/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ export function validateGitHubFields(gho: githubObj): void {
);

// base_ref.
validateField("github.base_ref", gho.base_ref, process.env.GITHUB_BASE_REF);
validateField(
"github.base_ref",
gho.base_ref,
process.env.GITHUB_BASE_REF,
true
);

// Validate the event. Only events in
// https://github.com/slsa-framework/github-actions-buildtypes/tree/main/workflow/v1
Expand Down Expand Up @@ -175,15 +180,21 @@ export function validateFieldAnyOf<T>(
* @param name - the name of the value
* @param actual - the actual value of the field
* @param expected - the expected value of the field
* @param allow_empty - whether the value may be empty
* @throws Error - if actual and expected don't match or are empty.
*/
export function validateField<T>(name: string, actual: T, expected: T): void {
export function validateField<T>(
name: string,
actual: T,
expected: T,
allow_empty = false
): void {
if (actual !== expected) {
throw new Error(
`mismatch ${name}: got '${actual}', expected '${expected}'.`
);
}
if (!actual) {
if (!allow_empty && !actual) {
throw new Error(`empty ${name}, expected non-empty value.`);
}
}
Expand Down

0 comments on commit bb63553

Please sign in to comment.