Skip to content
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

fix(publish): split github workflow ref #6978

Merged
merged 3 commits into from
Nov 13, 2023
Merged

Conversation

sxzz
Copy link
Contributor

@sxzz sxzz commented Nov 8, 2023

Fix split GITHUB_WORKFLOW_REF environment variable.

After npm version 9.8.0 and onwards, the Vite publish script is malfunctioning. Upon investigation, it was discovered that the problem stems from splitting the env.GITHUB_WORKFLOW_REF at the @ symbol. This becomes problematic when a @ symbol is present in the git tag, such as in [email protected].

If the value of GITHUB_WORKFLOW_REF is octocat/hello-world/.github/workflows/my-workflow.yml@refs/tags/[email protected], it results in an incomplete workflowRef.

https://github.com/npm/cli/blob/0f7008851f1c250405e8dc326f15d535e8fc1eae/workspaces/libnpmpublish/lib/provenance.js#L22C1-L24

References

@sxzz sxzz requested a review from a team as a code owner November 8, 2023 20:21
@sxzz sxzz force-pushed the fix/github-ref branch 2 times, most recently from b7d31df to df990c2 Compare November 8, 2023 20:32
Copy link
Contributor

@bdehamer bdehamer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on a fix for this!

I think we need to rework it a bit to properly capture the full workflow ref. Also, can you add a test to exercise this case.

workspaces/libnpmpublish/lib/provenance.js Outdated Show resolved Hide resolved
sxzz added a commit to vitejs/vite-plugin-vue that referenced this pull request Nov 8, 2023
@sxzz sxzz marked this pull request as draft November 8, 2023 20:46
@sxzz sxzz marked this pull request as ready for review November 8, 2023 20:56
@sxzz
Copy link
Contributor Author

sxzz commented Nov 8, 2023

Sorry, I forgot to mark the PR as a draft before testing. It should work now, and I just changed a existing test - by adding a @ symbol to ref.

@bdehamer
Copy link
Contributor

bdehamer commented Nov 9, 2023

@sxzz I've got a few more requests . . .

Let's add an explicit test to check that we're getting the expected value for the workflow reference:

diff --git a/workspaces/libnpmpublish/test/publish.js b/workspaces/libnpmpublish/test/publish.js
index 05ca0a9ad..584508d34 100644
--- a/workspaces/libnpmpublish/test/publish.js
+++ b/workspaces/libnpmpublish/test/publish.js
@@ -529,6 +529,9 @@ t.test('publish existing package with provenance in gha', async t => {
     t.hasStrict(provenance.predicate.buildDefinition.buildType,
       'https://slsa-framework.github.io/github-actions-buildtypes/workflow/v1',
       'buildType matches expectations')
+    t.hasStrict(provenance.predicate.buildDefinition.externalParameters.workflow.ref,
+      'refs/tags/[email protected]',
+      'workflowRef matches expectations')
     t.hasStrict(provenance.predicate.runDetails.builder.id,
       `https://github.com/actions/runner/${runnerEnv}`,
       'builder id matches expectations')

Also, I'm concerned that the split logic is getting overly-clever. I think something like this makes the intent more clear:

diff --git a/workspaces/libnpmpublish/lib/provenance.js b/workspaces/libnpmpublish/lib/provenance.js
index 8788c6514..8eb8880ad 100644
--- a/workspaces/libnpmpublish/lib/provenance.js
+++ b/workspaces/libnpmpublish/lib/provenance.js
@@ -19,10 +19,9 @@ const generateProvenance = async (subject, opts) => {
   let payload
   if (ci.GITHUB_ACTIONS) {
     /* istanbul ignore next - not covering missing env var case */
-    const [workflowPath, ...rest] = (env.GITHUB_WORKFLOW_REF || '')
-      .replace(env.GITHUB_REPOSITORY + '/', '')
-      .split('@')
-    const workflowRef = rest.join('@')
+    const relativeRef = (env.GITHUB_WORKFLOW_REF || '').replace(env.GITHUB_REPOSITORY + '/', '')
+    const workflowPath = relativeRef.slice(0, relativeRef.indexOf('@'))
+    const workflowRef = relativeRef.slice(relativeRef.indexOf('@') + 1)
     payload = {
       _type: INTOTO_STATEMENT_V1_TYPE,
       subject,

@sxzz
Copy link
Contributor Author

sxzz commented Nov 10, 2023

@bdehamer Thanks for your code. Updated.

Copy link
Contributor

@bdehamer bdehamer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

workspaces/libnpmpublish/lib/provenance.js Show resolved Hide resolved
@wraithgar wraithgar merged commit fff8698 into npm:latest Nov 13, 2023
20 checks passed
@github-actions github-actions bot mentioned this pull request Nov 13, 2023
@sxzz sxzz deleted the fix/github-ref branch November 13, 2023 16:32
@wraithgar
Copy link
Member

If you want this in npm 9 you can cherry-pick commit the commit from latest into a new PR against release/v9

bdehamer pushed a commit that referenced this pull request Nov 13, 2023
Properly splits the github workflow ref on only the first `@`, ignoring any potential extras in the tag field.
wraithgar pushed a commit that referenced this pull request Nov 13, 2023
fix(publish): split github workflow ref (#6978)

Properly splits the github workflow ref on only the first `@`, ignoring any potential extras in the tag field.

Co-authored-by: 三咲智子 Kevin Deng <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants