From cba41cad6bc5d63bcc4d45f4e2fc58f952a9fc0a Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Mon, 23 Nov 2026 10:19:30 +0100 Subject: [PATCH 1/4] Publish packed tarballs in the OIDC preview workflow The publish job used to overlay workspace files from the pull request build on top of a checkout of the default branch, then run `yarn install` to reconcile the two. That mixed trusted and untrusted files in the job that holds `id-token: write`. Now the build job packs the packages and uploads only the tarballs. The publish job no longer checks out the repository or runs Yarn. It validates each tarball (single `package/` directory, exactly one manifest, name in the preview scope, preview version, restricted `publishConfig`) and publishes it with `npm publish --ignore-scripts`. `prepare-preview.yml` gains a `pack` input, off by default, so the token-based `publish-preview.yml` workflow keeps working as before. --- .github/workflows/prepare-preview.yml | 49 +++++- .github/workflows/publish-preview-oidc.yml | 195 +++++++++++---------- CHANGELOG.md | 6 + 3 files changed, 151 insertions(+), 99 deletions(-) diff --git a/.github/workflows/prepare-preview.yml b/.github/workflows/prepare-preview.yml index af4e5c70..18d99398 100644 --- a/.github/workflows/prepare-preview.yml +++ b/.github/workflows/prepare-preview.yml @@ -46,6 +46,11 @@ on: description: 'Days to retain build artifacts' type: number required: true + pack: + description: 'Whether to upload packed tarballs of the packages to publish ("preview-build-tarballs"), instead of the workspace ("preview-build-artifacts").' + type: boolean + required: false + default: false outputs: is-fork: description: 'Whether the pull request is from a fork. The build is skipped when it is.' @@ -323,8 +328,48 @@ jobs: env: ${{ fromJSON(secrets.BUILD_ENV || '{}') }} run: ${{ inputs.build-command }} + - name: Pack packages + if: ${{ inputs.pack }} + env: + IS_MONOREPO: ${{ inputs.is-monorepo }} + PUBLISH_LOCATIONS: ${{ steps.select-packages.outputs.publish-locations }} + run: | + if [[ "$IS_MONOREPO" == "true" ]]; then + mapfile -t locations < <( + yarn workspaces list --no-private --json \ + | jq -r --argjson selected "${PUBLISH_LOCATIONS:-null}" ' + select($selected == null or (.location | IN($selected[]))) + | .location + ' + ) + else + locations=(.) + fi + + tarballs="${RUNNER_TEMP}/preview-build-tarballs" + mkdir -p "$tarballs" + + for location in "${locations[@]}"; do + manifest="$location/package.json" + jq 'if .scripts then .scripts |= with_entries(select(.key | test("^(pre|post)?(pack|publish|prepare)$") | not)) else . end' "$manifest" > temp.json + mv temp.json "$manifest" + + name="$(jq -r '.name' "$manifest")" + echo "::group::Packing $name" + (cd "$location" && yarn pack --out "${tarballs}/${name//\//-}.tgz") + echo "::endgroup::" + done + + - name: Upload tarballs + if: ${{ inputs.pack }} + uses: actions/upload-artifact@v7 + with: + name: preview-build-tarballs + retention-days: ${{ inputs.artifact-retention-days }} + path: ${{ runner.temp }}/preview-build-tarballs/*.tgz + - name: Upload build artifacts (monorepo) - if: ${{ inputs.is-monorepo }} + if: ${{ !inputs.pack && inputs.is-monorepo }} uses: actions/upload-artifact@v7 with: name: preview-build-artifacts @@ -340,7 +385,7 @@ jobs: !./packages/**/*.test.* - name: Upload build artifacts (polyrepo) - if: ${{ !inputs.is-monorepo }} + if: ${{ !inputs.pack && !inputs.is-monorepo }} uses: actions/upload-artifact@v7 with: name: preview-build-artifacts diff --git a/.github/workflows/publish-preview-oidc.yml b/.github/workflows/publish-preview-oidc.yml index dee2f8d7..de876c40 100644 --- a/.github/workflows/publish-preview-oidc.yml +++ b/.github/workflows/publish-preview-oidc.yml @@ -87,6 +87,7 @@ jobs: is-monorepo: ${{ inputs.is-monorepo }} rename-after-install-and-build: ${{ inputs.rename-after-install-and-build }} artifact-retention-days: ${{ inputs.artifact-retention-days }} + pack: true secrets: BUILD_ENV: ${{ secrets.BUILD_ENV }} @@ -101,106 +102,103 @@ jobs: environment: ${{ inputs.environment }} runs-on: ubuntu-latest steps: - - name: Checkout and setup environment - uses: MetaMask/action-checkout-and-setup@v3 - with: - is-high-risk-environment: true - - - name: Restore build artifacts + - name: Download tarballs + if: ${{ needs.build.outputs.publish-locations != '[]' }} uses: actions/download-artifact@v8 with: - name: preview-build-artifacts + name: preview-build-tarballs + path: ${{ runner.temp }}/preview-build-tarballs - # The artifact package.json files come from the PR branch. - # A malicious PR could inject lifecycle scripts (prepack/postpack) that - # execute during `yarn npm publish` with the NPM token in the environment - # (enableScripts: false does NOT prevent pack/publish lifecycle scripts). - # It could also override publishConfig.registry to exfiltrate the token. - # We strip dangerous lifecycle scripts (they already ran during build) - # and block unexpected registries outright. - - name: Sanitize and validate artifact manifests + - name: Validate tarballs env: - IS_MONOREPO: ${{ inputs.is-monorepo }} + NPM_SCOPE: ${{ inputs.npm-scope }} run: | - bad=0 - if [[ "$IS_MONOREPO" == "true" ]]; then - mapfile -t manifests < <(find packages -name package.json -not -path '*/node_modules/*') - else - manifests=(package.json) - fi - if [[ ${#manifests[@]} -eq 0 ]]; then - echo "::error::No package.json files found to validate" - exit 1 - fi - # Strip registry overrides from .yarnrc.yml to prevent registry - # redirects that could exfiltrate the NPM token. npmPublishRegistry - # takes precedence over npmRegistryServer for yarn npm publish, and - # npmScopes can override per-scope. YARN_NPM_REGISTRY_SERVER env var - # only overrides npmRegistryServer, not the others. - if [[ -f .yarnrc.yml ]]; then - echo "Stripping registry config from .yarnrc.yml" - yq -i 'del(.npmRegistryServer) | del(.npmPublishRegistry) | del(.npmScopes)' .yarnrc.yml - fi - for f in "${manifests[@]}"; do - # Strip lifecycle scripts that run during pack/publish - if jq -e '.scripts // {} | keys[] | select(test("^(pre|post)?(pack|publish|prepare)$"))' "$f" > /dev/null 2>&1; then - echo "Stripping lifecycle scripts from $f" - jq 'if .scripts then .scripts |= with_entries(select(.key | test("^(pre|post)?(pack|publish|prepare)$") | not)) else . end' "$f" > "${f}.tmp" - mv "${f}.tmp" "$f" + shopt -s nullglob + has_invalid_tarballs=false + + for tarball in "$RUNNER_TEMP"/preview-build-tarballs/*.tgz; do + file="$(basename "$tarball")" + + if ! entries="$(tar -tzf "$tarball")"; then + echo "::error::${file} isn't a valid tarball." + has_invalid_tarballs=true + continue fi - # Block unexpected registries - reg=$(jq -r '.publishConfig.registry // ""' "$f") - if [[ -n "$reg" && "$reg" != "https://registry.npmjs.org/" ]]; then - echo "::error::Unexpected registry in $f: $reg" - bad=1 + + if grep -qv '^package/' <<< "$entries" || grep -qE '//|(^|/)\.\.?(/|$)' <<< "$entries"; then + echo "::error::${file} contains unexpected paths." + has_invalid_tarballs=true + continue + fi + + if [[ "$(grep -cx 'package/package.json' <<< "$entries")" -ne 1 ]]; then + echo "::error::${file} must contain exactly one package/package.json." + has_invalid_tarballs=true + continue + fi + + # The name must be in the preview scope, and the version must be a + # preview version. publishConfig may only set the access and the + # npm registry, since npm applies it when publishing. + if ! tar -xzOf "$tarball" package/package.json | jq -e --arg scope "${NPM_SCOPE}/" ' + (.name | type == "string" and startswith($scope)) + and (.version | type == "string" and test("-preview-[0-9a-f]+$")) + and ((.publishConfig // {}) | type == "object" + and (keys - ["access", "registry"] | length == 0) + and ((.registry // "https://registry.npmjs.org/") == "https://registry.npmjs.org/")) + ' > /dev/null; then + echo "::error::${file} has an invalid name, version, or publishConfig." + has_invalid_tarballs=true fi done - exit "$bad" - - name: Reconcile workspace state - run: yarn install --no-immutable + if [[ "$has_invalid_tarballs" == "true" ]]; then + exit 1 + fi - name: Publish preview builds (monorepo) if: ${{ inputs.is-monorepo && !inputs.dry-run }} run: | - mapfile -t workspaces < <( - yarn workspaces list --no-private --json \ - | jq -r --argjson selected "${PUBLISH_LOCATIONS:-null}" ' - select($selected == null or (.location | IN($selected[]))) - | [.name, .location] - | @tsv - ' - ) + shopt -s nullglob - for workspace in "${workspaces[@]}"; do - IFS=$'\t' read -r name location <<< "$workspace" - version="$(jq -r '.version' "$location/package.json")" - echo "::group::Publishing $name" - if (cd "$location" && yarn npm publish --tag preview); then - echo "${name}@${version}" >> "$RUNNER_TEMP/published.txt" + for tarball in "$RUNNER_TEMP"/preview-build-tarballs/*.tgz; do + package="$(tar -xzOf "$tarball" package/package.json | jq -r '"\(.name)@\(.version)"')" + echo "::group::Publishing $package" + if npm publish "$tarball" \ + --tag preview \ + --ignore-scripts \ + --provenance=false \ + --registry https://registry.npmjs.org/; then + echo "$package" >> "$RUNNER_TEMP/published.txt" else - echo "::warning::Failed to publish $name" - echo "${name}@${version}" >> "$RUNNER_TEMP/failed.txt" + echo "::warning::Failed to publish $package" + echo "$package" >> "$RUNNER_TEMP/failed.txt" fi echo "::endgroup::" done - env: - PUBLISH_LOCATIONS: ${{ needs.build.outputs.publish-locations }} - YARN_NPM_REGISTRY_SERVER: 'https://registry.npmjs.org' - - name: Publish preview builds (polyrepo) + - name: Publish preview build (polyrepo) if: ${{ !inputs.is-monorepo && !inputs.dry-run }} - run: yarn npm publish --tag preview - env: - YARN_NPM_REGISTRY_SERVER: 'https://registry.npmjs.org' + run: | + tarballs=("$RUNNER_TEMP"/preview-build-tarballs/*.tgz) + if [[ ${#tarballs[@]} -ne 1 ]]; then + echo "::error::Expected exactly one tarball, found ${#tarballs[@]}." + exit 1 + fi + + npm publish "${tarballs[0]}" \ + --tag preview \ + --ignore-scripts \ + --provenance=false \ + --registry https://registry.npmjs.org/ - name: Dry run notice if: ${{ inputs.dry-run }} run: echo "Dry run — skipping publish" - - name: Generate preview build message + - name: Generate preview build message (monorepo) + if: ${{ inputs.is-monorepo }} env: - IS_MONOREPO: ${{ inputs.is-monorepo }} DOCS_URL: ${{ inputs.docs-url }} run: | docs_link="" @@ -208,16 +206,15 @@ jobs: docs_link="[Learn how to use preview builds in other projects](${DOCS_URL})." fi - if [[ "$IS_MONOREPO" == "true" ]]; then - published="$(cat "$RUNNER_TEMP/published.txt" 2>/dev/null || true)" - failed="$(cat "$RUNNER_TEMP/failed.txt" 2>/dev/null || true)" + published="$(cat "$RUNNER_TEMP/published.txt" 2>/dev/null || true)" + failed="$(cat "$RUNNER_TEMP/failed.txt" 2>/dev/null || true)" - if [[ -n "$published" ]]; then - echo -n "Preview builds have been published." > preview-build-message.txt - if [[ -n "$docs_link" ]]; then - echo -n " ${docs_link}" >> preview-build-message.txt - fi - cat <<-MSGEOF >> preview-build-message.txt + if [[ -n "$published" ]]; then + echo -n "Preview builds have been published." > preview-build-message.txt + if [[ -n "$docs_link" ]]; then + echo -n " ${docs_link}" >> preview-build-message.txt + fi + cat <<-MSGEOF >> preview-build-message.txt
Expand for full list of packages and versions. @@ -228,12 +225,12 @@ jobs:
MSGEOF - else - echo "No preview builds were published." > preview-build-message.txt - fi + else + echo "No preview builds were published." > preview-build-message.txt + fi - if [[ -n "$failed" ]]; then - cat <<-MSGEOF >> preview-build-message.txt + if [[ -n "$failed" ]]; then + cat <<-MSGEOF >> preview-build-message.txt The following packages failed to publish. [See the workflow run for details](https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/attempts/${GITHUB_RUN_ATTEMPT}). @@ -241,20 +238,23 @@ jobs: ${failed} \`\`\` MSGEOF - fi - else - name="$(jq -r '.name' package.json)" - version="$(jq -r '.version' package.json)" - cat <<-MSGEOF > preview-build-message.txt + fi + + - name: Generate preview build message (polyrepo) + if: ${{ !inputs.is-monorepo }} + env: + DOCS_URL: ${{ inputs.docs-url }} + run: | + package="$(tar -xzOf "$RUNNER_TEMP"/preview-build-tarballs/*.tgz package/package.json | jq -r '"\(.name)@\(.version)"')" + cat <<-MSGEOF > preview-build-message.txt The following preview build has been published: \`\`\` - ${name}@${version} + ${package} \`\`\` MSGEOF - if [[ -n "$docs_link" ]]; then - printf '\n%s\n' "$docs_link" >> preview-build-message.txt - fi + if [[ -n "$DOCS_URL" ]]; then + printf '\n%s\n' "[Learn how to use preview builds in other projects](${DOCS_URL})." >> preview-build-message.txt fi - name: Add build preview to the job summary @@ -264,5 +264,6 @@ jobs: if: ${{ inputs.pull-request }} run: gh pr comment "${PR_NUMBER}" --body-file preview-build-message.txt env: + GH_REPO: ${{ github.repository }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_NUMBER: ${{ inputs.pull-request }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 21e86de2..2eb00939 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- Publish tarballs packed in the build job in the `publish-preview-oidc` workflow, instead of the workspace + - The publish job no longer checks out the repository or runs Yarn. It publishes the tarballs with `npm publish`, after checking that each package is in the preview scope and has a preview version. + - Add a `pack` input to the `prepare-preview` workflow, to upload the tarballs instead of the workspace. + ## [1.27.0] ### Added From 36ab330f1df1d624476b1de5b1b4fc1d29d64d97 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Mon, 23 Nov 2026 10:24:25 +0100 Subject: [PATCH 2/4] Allow the npm registry without a trailing slash The tarball validation compared `publishConfig.registry` against `https://registry.npmjs.org/` exactly, so a manifest using `https://registry.npmjs.org` failed the job even though both point to the same registry. Accept both forms and nothing else, so lookalike hosts are still rejected. --- .github/workflows/publish-preview-oidc.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/publish-preview-oidc.yml b/.github/workflows/publish-preview-oidc.yml index de876c40..7362427b 100644 --- a/.github/workflows/publish-preview-oidc.yml +++ b/.github/workflows/publish-preview-oidc.yml @@ -145,7 +145,7 @@ jobs: and (.version | type == "string" and test("-preview-[0-9a-f]+$")) and ((.publishConfig // {}) | type == "object" and (keys - ["access", "registry"] | length == 0) - and ((.registry // "https://registry.npmjs.org/") == "https://registry.npmjs.org/")) + and ((.registry // "https://registry.npmjs.org/") | IN("https://registry.npmjs.org", "https://registry.npmjs.org/"))) ' > /dev/null; then echo "::error::${file} has an invalid name, version, or publishConfig." has_invalid_tarballs=true From 7f4ba9a021dbd78f64aae95441dcba8ea7cc1d56 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Mon, 23 Nov 2026 10:26:32 +0100 Subject: [PATCH 3/4] Add PR number to changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2eb00939..e6ad82eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Publish tarballs packed in the build job in the `publish-preview-oidc` workflow, instead of the workspace +- Publish tarballs packed in the build job in the `publish-preview-oidc` workflow, instead of the workspace ([#348](https://github.com/MetaMask/github-tools/pull/348)) - The publish job no longer checks out the repository or runs Yarn. It publishes the tarballs with `npm publish`, after checking that each package is in the preview scope and has a preview version. - Add a `pack` input to the `prepare-preview` workflow, to upload the tarballs instead of the workspace. From 9e2dc1e9bd82240d3da3b65a623b2224f10c7ebc Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Mon, 23 Nov 2026 12:17:03 +0100 Subject: [PATCH 4/4] Fail the preview steps when listing packages fails The steps that select and pack packages filled arrays with `mapfile` from a process substitution. GitHub runs steps with `bash -e` but without `pipefail`, so a failing `yarn workspaces list` or `jq` left the array empty and the step passed. Capture the output in a variable first, so a failure stops the step. Call `mapfile` only when there is output, because it turns empty input into an array with one empty element. --- .github/workflows/prepare-preview.yml | 34 ++++++++++++++++++--------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/.github/workflows/prepare-preview.yml b/.github/workflows/prepare-preview.yml index 18d99398..67a6c26e 100644 --- a/.github/workflows/prepare-preview.yml +++ b/.github/workflows/prepare-preview.yml @@ -190,7 +190,7 @@ jobs: locations=() if [[ -n "$PACKAGES" ]]; then - mapfile -t names < <(jq -rn --arg input "$PACKAGES" --arg scope "$SOURCE_SCOPE" ' + selected_names="$(jq -rn --arg input "$PACKAGES" --arg scope "$SOURCE_SCOPE" ' $input | split(",") | map(gsub("^\\s+|\\s+$"; "")) @@ -198,7 +198,12 @@ jobs: | map(if startswith("@") then . else $scope + . end) | unique | .[] - ') + ')" + + names=() + if [[ -n "$selected_names" ]]; then + mapfile -t names <<< "$selected_names" + fi if [[ ${#names[@]} -eq 0 ]]; then echo "::error::No package names found in \"$PACKAGES\"." @@ -233,10 +238,14 @@ jobs: git fetch --no-tags --depth=1 origin "$merge_base" changed_files="$(git diff --name-only --no-renames "$merge_base" "$head")" - mapfile -t locations < <(jq -r --arg files "$changed_files" ' + changed_locations="$(jq -r --arg files "$changed_files" ' select(.location as $location | $files | split("\n") | any(startswith($location + "/"))) | .location - ' <<< "$workspaces") + ' <<< "$workspaces")" + + if [[ -n "$changed_locations" ]]; then + mapfile -t locations <<< "$changed_locations" + fi if [[ ${#locations[@]} -eq 0 ]]; then echo "::warning::No packages changed compared to ${base}." @@ -335,13 +344,16 @@ jobs: PUBLISH_LOCATIONS: ${{ steps.select-packages.outputs.publish-locations }} run: | if [[ "$IS_MONOREPO" == "true" ]]; then - mapfile -t locations < <( - yarn workspaces list --no-private --json \ - | jq -r --argjson selected "${PUBLISH_LOCATIONS:-null}" ' - select($selected == null or (.location | IN($selected[]))) - | .location - ' - ) + workspaces="$(yarn workspaces list --no-private --json)" + selected="$(jq -r --argjson selected "${PUBLISH_LOCATIONS:-null}" ' + select($selected == null or (.location | IN($selected[]))) + | .location + ' <<< "$workspaces")" + + locations=() + if [[ -n "$selected" ]]; then + mapfile -t locations <<< "$selected" + fi else locations=(.) fi