From 16c18ffd03e6f2239b40006c20df7c9b4189609c Mon Sep 17 00:00:00 2001 From: Houman Azimi-Nejadi Date: Thu, 19 Nov 2026 20:57:16 -0700 Subject: [PATCH] release: publish to npm through trusted publishing; no npm token The Release workflow published dunstan with NODE_AUTH_TOKEN from the NPM_TOKEN secret. It now publishes through npm trusted publishing, as @cejel/cejel's publish-npm.yml does: npm exchanges the job's OIDC token (id-token: write, already granted for the MCP registry login) for a short-lived credential, so no long-lived npm token is needed. - Require npm 11.5.1 before the publish: trusted publishing needs 11.5.1 or later, and Node 22 bundles npm 10. - Remove setup-node's _authToken placeholder before the publish, and assert it is gone: left in place, npm uses it instead of the OIDC exchange. - The publish step keeps its name, its NPM_PUBLISHED guard and --provenance --access public. - release-versions.test.ts pins the shape: no NPM_TOKEN or NODE_AUTH_TOKEN, id-token: write, environment release, and the npm and placeholder steps before the publish. It fails against the previous workflow. Needs, on npmjs.com, a trusted publisher on the dunstan package for BargLabs/dunstan, workflow release.yml, environment release, before the next v* tag. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/release.yml | 34 ++++++++++++++++++++++---- src/__tests__/release-versions.test.ts | 23 +++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 017ad64..05c4969 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,9 +9,11 @@ name: Release # the tag or input matches the build. It can be re-run: a version npm already serves with this exact # tarball skips the npm publish, and an entry the registry already holds skips the registry publish. # -# Secrets: NPM_TOKEN, an npm automation or granular access token that can publish `dunstan`. The MCP -# registry needs no secret: `mcp-publisher login github-oidc` proves the io.github.BargLabs namespace -# with this workflow's OIDC token. +# Secrets: none. npm publishes through trusted publishing: the `dunstan` package trusts this workflow +# (BargLabs/dunstan, release.yml, environment `release`), and npm exchanges this job's OIDC token for a +# short-lived publish credential, so no long-lived npm token exists to leak or to publish another +# package. The MCP registry likewise needs no secret: `mcp-publisher login github-oidc` proves the +# io.github.BargLabs namespace with the same OIDC token. on: push: @@ -81,10 +83,32 @@ jobs: echo "VERSION=$version" >> "$GITHUB_ENV" echo "INTEGRITY=$local_integrity" >> "$GITHUB_ENV" + # Trusted publishing needs npm 11.5.1 or later; Node 22 bundles npm 10. + - name: Require npm CLI with trusted-publishing support + if: env.NPM_PUBLISHED != '1' + run: | + npm install --global npm@11.5.1 + test "$(npm --version)" = "11.5.1" + + # setup-node's registry-url writes an `_authToken=${NODE_AUTH_TOKEN}` placeholder. Left in place, + # npm uses it instead of exchanging the OIDC token, and the publish fails unauthenticated. + - name: Remove setup-node auth placeholder for npm trusted publishing + if: env.NPM_PUBLISHED != '1' + run: | + npmrc="${NPM_CONFIG_USERCONFIG:-$HOME/.npmrc}" + for config in "$npmrc" .npmrc; do + if [ -f "$config" ]; then + sed -i '/_authToken/d' "$config" + fi + done + if grep -rl '_authToken' "$npmrc" .npmrc 2>/dev/null | grep -q .; then + echo "_authToken present after strip; OIDC exchange would be skipped" >&2 + exit 1 + fi + echo "_authToken absence assertion passed for user and project npmrc" + - name: Publish to npm with provenance if: env.NPM_PUBLISHED != '1' - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} run: npm publish --provenance --access public # npm processes a publish before serving it: dunstan@0.1.6, the first, took about 95 seconds, diff --git a/src/__tests__/release-versions.test.ts b/src/__tests__/release-versions.test.ts index 4aef48e..72c4602 100644 --- a/src/__tests__/release-versions.test.ts +++ b/src/__tests__/release-versions.test.ts @@ -192,6 +192,29 @@ describe('the release workflow', () => { expect(release).toContain('test "$published" = "$local_integrity"'); expect(release).toMatch(/versions\/\$VERSION"\n[\s\S]*already in the MCP registry/); }); + + // dunstan publishes through npm trusted publishing: the job's OIDC token, not a long-lived token + // that could publish any package on the account. setup-node's token placeholder must be gone + // before the publish, and npm must be new enough to make the exchange. + it('publishes to npm through trusted publishing, with no npm token', () => { + expect(release).not.toMatch(/NPM_TOKEN|NODE_AUTH_TOKEN:/); + expect(release).toMatch(/permissions:\n\s+contents: read\n\s+id-token: write/); + expect(release).toContain('environment: release'); + const steps = release.slice( + release.indexOf(' publish:'), + release.indexOf(' github-release:'), + ); + const strip = steps.indexOf( + '- name: Remove setup-node auth placeholder for npm trusted publishing', + ); + const npm = steps.indexOf('npm install --global npm@11.5.1'); + const publish = steps.indexOf('- name: Publish to npm with provenance'); + expect(strip).toBeGreaterThan(-1); + expect(npm).toBeGreaterThan(-1); + expect(publish).toBeGreaterThan(Math.max(strip, npm)); + expect(steps).toContain("sed -i '/_authToken/d'"); + expect(steps).toContain('npm publish --provenance --access public'); + }); }); // No example hands the server a credential the user's machine already holds: the checklist holds a