diff --git a/.github/workflows/dsh-plugin-publish.yml b/.github/workflows/dsh-plugin-publish.yml index 6029397d9d..5bdf46e144 100644 --- a/.github/workflows/dsh-plugin-publish.yml +++ b/.github/workflows/dsh-plugin-publish.yml @@ -37,6 +37,8 @@ jobs: - uses: actions/setup-node@v6 with: node-version: "24" + registry-url: https://registry.npmjs.org + package-manager-cache: false - name: Require a merged, published release with matching package identity run: | git fetch origin main @@ -85,11 +87,25 @@ jobs: - name: Publish the same bytes with npm trusted publishing if: env.DSH_ALREADY_PUBLISHED != 'true' run: | + if [ -z "${ACTIONS_ID_TOKEN_REQUEST_URL:-}" ] || [ -z "${ACTIONS_ID_TOKEN_REQUEST_TOKEN:-}" ]; then + echo "::error title=GitHub OIDC unavailable::The hosted runner did not provide a GitHub OIDC request. Check the publish job's id-token permission." + exit 1 + fi + publish_log="$(mktemp)" + trap 'rm -f "$publish_log"' EXIT set +e - npm publish "$DSH_ARTIFACT" --access public --tag latest --ignore-scripts --provenance + npm publish "$DSH_ARTIFACT" --access public --tag latest --ignore-scripts --provenance --loglevel=verbose > "$publish_log" 2>&1 rc=$? set -e if [ "$rc" -ne 0 ]; then + # Describe the stage without printing the trace, which can include an OIDC request URL. + if grep -Fq 'Successfully retrieved and set token' "$publish_log"; then + echo "::notice title=npm publish stage::npm accepted the workflow identity. Publishing the immutable archive failed afterward." + elif grep -Fq 'Failed token exchange request' "$publish_log"; then + echo "::notice title=npm publish stage::GitHub supplied an identity but npm rejected its token exchange." + elif grep -Fq 'Failed to fetch id_token from GitHub' "$publish_log"; then + echo "::notice title=npm publish stage::GitHub did not return an OIDC identity to npm." + fi if [ "${DSH_NPM_PACKAGE_MISSING:-false}" = "true" ]; then echo "::error title=npm package bootstrap required::dsh-loopx-plugin returns HTTP 404 on registry.npmjs.org. npm OIDC Trusted Publishing cannot create a brand-new package: enable 2FA (auth-and-writes), run one-time web-auth 'npm publish --access public --tag latest --ignore-scripts', run 'npm logout', then bind Trusted Publisher (loopx-project / loopx / dsh-plugin-publish.yml / blank Environment name / Allow npm publish checked)." elif [ "${DSH_NPM_STAGING_PLACEHOLDER:-false}" = "true" ]; then @@ -99,6 +115,7 @@ jobs: fi exit "$rc" fi + echo "Published the immutable GitHub archive to npm." - name: Read back npm bytes, latest, and marketplace repository discovery run: | if git cat-file -e origin/main:packages/dsh-loopx-plugin/scripts/verify-distribution.mjs 2>/dev/null; then diff --git a/tests/test_dsh_plugin_publish_workflow.py b/tests/test_dsh_plugin_publish_workflow.py index 24e9cdb09c..c810665a06 100644 --- a/tests/test_dsh_plugin_publish_workflow.py +++ b/tests/test_dsh_plugin_publish_workflow.py @@ -1,8 +1,12 @@ from __future__ import annotations +import os import re +import shutil +import subprocess from pathlib import Path +import pytest import yaml REPO_ROOT = Path(__file__).resolve().parents[1] @@ -29,6 +33,9 @@ def test_dsh_plugin_publish_workflow_allows_tag_or_main_dispatch_with_relative_a checkout = next(step for step in steps if str(step.get("uses", "")).startswith("actions/checkout@")) assert checkout["with"]["ref"] == "refs/tags/${{ inputs.release_tag }}" assert checkout["with"]["fetch-depth"] == 0 + setup = next(step for step in steps if str(step.get("uses", "")).startswith("actions/setup-node@")) + assert setup["with"]["registry-url"] == "https://registry.npmjs.org" + assert setup["with"]["package-manager-cache"] is False identity_run = next( step["run"] @@ -63,3 +70,29 @@ def test_dsh_plugin_publish_workflow_allows_tag_or_main_dispatch_with_relative_a assert "on the existing published plugin tag or on `main`" in normalized_readme assert "refs/tags/" in normalized_readme assert "Allow npm publish" in normalized_readme + + +@pytest.mark.skipif(os.name != "posix" or shutil.which("bash") is None, reason="The hosted step uses Bash on a POSIX runner") +def test_dsh_publish_reports_the_authentication_stage_without_disclosing_trace(tmp_path, monkeypatch) -> None: + steps = yaml.safe_load(WORKFLOW_PATH.read_text(encoding="utf-8"))["jobs"]["publish"]["steps"] + publish = next(step["run"] for step in steps if step.get("name") == "Publish the same bytes with npm trusted publishing") + fake_npm = tmp_path / "npm" + fake_npm.write_text( + "#!/bin/sh\nprintf '%s\\n' 'npm verbose oidc Failed token exchange request with body message: synthetic-private-trace'\nexit 17\n", + encoding="utf-8", + ) + fake_npm.chmod(0o700) + monkeypatch.setenv("PATH", f"{tmp_path}:/usr/bin:/bin") + monkeypatch.setenv("DSH_ARTIFACT", "./output/immutable.tgz") + monkeypatch.delenv("ACTIONS_ID_TOKEN_REQUEST_URL", raising=False) + monkeypatch.delenv("ACTIONS_ID_TOKEN_REQUEST_TOKEN", raising=False) + missing = subprocess.run(["bash", "-e", "-c", publish], check=False, capture_output=True, text=True) + assert missing.returncode == 1 + assert "GitHub OIDC unavailable" in missing.stdout + + monkeypatch.setenv("ACTIONS_ID_TOKEN_REQUEST_URL", "https://example.invalid/oidc") + monkeypatch.setenv("ACTIONS_ID_TOKEN_REQUEST_TOKEN", "synthetic-token") + rejected = subprocess.run(["bash", "-e", "-c", publish], check=False, capture_output=True, text=True) + assert rejected.returncode == 17 + assert "npm rejected its token exchange" in rejected.stdout + assert "synthetic-private-trace" not in rejected.stdout + rejected.stderr