diff --git a/.github/workflows/catalog-publish.yml b/.github/workflows/catalog-publish.yml index 25730ac324..953cd8cd5b 100644 --- a/.github/workflows/catalog-publish.yml +++ b/.github/workflows/catalog-publish.yml @@ -19,6 +19,7 @@ env: jobs: publish: + if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest timeout-minutes: 30 steps: diff --git a/.github/workflows/publish-mcp-registry.yml b/.github/workflows/publish-mcp-registry.yml index a248551385..513bc70754 100644 --- a/.github/workflows/publish-mcp-registry.yml +++ b/.github/workflows/publish-mcp-registry.yml @@ -15,6 +15,8 @@ on: jobs: publish: + # The registry trusts any OIDC token from this repo, so only main may request one. + if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest timeout-minutes: 10 permissions: @@ -24,6 +26,9 @@ jobs: MCP_PUBLISHER_VERSION: v1.8.9 steps: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + ref: ${{ github.sha }} + persist-credentials: false - name: Install cosign uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2 diff --git a/.github/workflows/sync-skills-to-clawhub.yml b/.github/workflows/sync-skills-to-clawhub.yml index 7c6b76292a..96087fa793 100644 --- a/.github/workflows/sync-skills-to-clawhub.yml +++ b/.github/workflows/sync-skills-to-clawhub.yml @@ -32,12 +32,17 @@ permissions: jobs: sync: name: Publish changed skills + # A dispatch runs this file from the chosen branch, so only main may reach the token. + if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest timeout-minutes: 10 env: CLAWHUB_DISABLE_TELEMETRY: "1" steps: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + ref: ${{ github.sha }} + persist-credentials: false - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 with: diff --git a/package.json b/package.json index 7fee78c495..1ae9d0e6f5 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "player:perf": "bun run --filter @hyperframes/player perf", "format:check": "oxfmt --check .", "knip": "knip", - "test:scripts": "node --import tsx --test scripts/animejs-v4-guidance.test.mjs scripts/check-tracked-artifacts.test.mjs scripts/check-committed-keys.test.mjs scripts/check-symlinks.test.mjs scripts/upload-docs-images.test.mjs scripts/check-registry-set-delta.test.mjs scripts/check-no-main-deletions.test.mjs scripts/check-pr-captures.test.mjs scripts/check-comment-citations.test.mjs scripts/comments-workflow.test.mjs scripts/comment-ratchet.test.mjs scripts/check-docs-snippet-motion.test.mjs scripts/registry-target-paths.test.mjs scripts/check-workspace-contracts.test.mjs scripts/check-media-use-copy-parity.test.mjs scripts/check-svg-sanitize-parity.test.mjs scripts/check-media-use-svg-sanitize-generated.test.mjs scripts/check-package-cycles.test.mjs scripts/check-cli-process-ownership.test.mjs scripts/check-cli-lazy-deps.test.mjs scripts/file-url-paths.test.mjs scripts/check-studio-copy-dashes.test.mjs scripts/check-large-files.test.mjs scripts/check-registry-innerhtml.test.mjs scripts/package-subpaths.test.mjs scripts/validate-release-channel.test.mjs scripts/clawhub-sync.test.mjs scripts/clawhub-unlist-retired.test.mjs scripts/publish-workflow.test.mjs scripts/pr-edit-concurrency.test.mjs scripts/merge-queue-workflows.test.mjs scripts/ci-cache-workflows.test.mjs scripts/install-workspace-dependencies.test.mjs scripts/check-edit-accuracy-filter.test.mjs scripts/edit-accuracy-retry.test.mjs scripts/draft-changelog.test.ts scripts/set-version.test.ts scripts/release-prepare.test.ts scripts/cli-options.test.ts scripts/changelog-weekly.test.ts scripts/claude-plugin-compression.test.ts scripts/catalog-payload-assets.test.ts scripts/host-registry-assets.test.ts scripts/catalog-preview-temp.test.ts scripts/backfill-block-previews.test.ts scripts/catalog-hosted-files.test.ts scripts/player-cdn-pin.test.ts scripts/registry-cdn-pins.test.ts scripts/studio-runtime-smoke.test.mjs scripts/verify-packed-manifests.test.mjs scripts/rewrite-relative-import-extensions.test.mjs scripts/lint-skills.test.mjs scripts/check-core-skills.test.mjs scripts/creator-editing-recipes.test.mjs packages/gcp-cloud-run/check-dockerfile-workspaces.test.mjs packages/core/scripts/writeGeneratedFile.test.ts scripts/catalog-publication.test.mjs scripts/ci/resolve-workflow-pr.test.mjs scripts/lefthook-order.test.mjs scripts/inline-gsap.test.mjs scripts/ci/flake-gate.test.mjs scripts/ci/motion-blur-nightly-report.test.mjs scripts/ci/base-file.test.mjs scripts/ci/release-only-diff.test.mjs scripts/check-catalog-source-pr.test.mjs scripts/generate-registry-items.test.ts scripts/catalog-drift.test.ts scripts/catalog-fetch-mirror.test.ts scripts/catalog-script-inlining.test.ts scripts/catalog-detail.test.ts scripts/generate-catalog-pages.test.ts scripts/verify-catalog-payloads.test.ts scripts/registry-theme-fallbacks.test.ts scripts/registry-skill-files.test.ts scripts/creator-editing-capabilities.test.mjs scripts/generate-catalog-previews.test.ts scripts/registry-primitive-payloads.test.ts registry/components/pan-stations/pan-stations.test.mjs registry/components/filmed-screen/filmed-screen.test.mjs registry/components/echo-smear/echo-smear.test.mjs registry/examples/path-traced-3d/source/path-traced-3d.test.mjs && vitest run scripts/catalog/ scripts/contrastRatchet.test.ts scripts/generate-catalog-payloads.test.ts", + "test:scripts": "node --import tsx --test scripts/animejs-v4-guidance.test.mjs scripts/check-tracked-artifacts.test.mjs scripts/check-committed-keys.test.mjs scripts/check-symlinks.test.mjs scripts/upload-docs-images.test.mjs scripts/check-registry-set-delta.test.mjs scripts/check-no-main-deletions.test.mjs scripts/check-pr-captures.test.mjs scripts/check-comment-citations.test.mjs scripts/comments-workflow.test.mjs scripts/comment-ratchet.test.mjs scripts/check-docs-snippet-motion.test.mjs scripts/registry-target-paths.test.mjs scripts/check-workspace-contracts.test.mjs scripts/check-media-use-copy-parity.test.mjs scripts/check-svg-sanitize-parity.test.mjs scripts/check-media-use-svg-sanitize-generated.test.mjs scripts/check-package-cycles.test.mjs scripts/check-cli-process-ownership.test.mjs scripts/check-cli-lazy-deps.test.mjs scripts/file-url-paths.test.mjs scripts/check-studio-copy-dashes.test.mjs scripts/check-large-files.test.mjs scripts/check-registry-innerhtml.test.mjs scripts/package-subpaths.test.mjs scripts/validate-release-channel.test.mjs scripts/clawhub-sync.test.mjs scripts/clawhub-unlist-retired.test.mjs scripts/publish-workflow.test.mjs scripts/publish-main-only-workflows.test.mjs scripts/pr-edit-concurrency.test.mjs scripts/merge-queue-workflows.test.mjs scripts/ci-cache-workflows.test.mjs scripts/install-workspace-dependencies.test.mjs scripts/check-edit-accuracy-filter.test.mjs scripts/edit-accuracy-retry.test.mjs scripts/draft-changelog.test.ts scripts/set-version.test.ts scripts/release-prepare.test.ts scripts/cli-options.test.ts scripts/changelog-weekly.test.ts scripts/claude-plugin-compression.test.ts scripts/catalog-payload-assets.test.ts scripts/host-registry-assets.test.ts scripts/catalog-preview-temp.test.ts scripts/backfill-block-previews.test.ts scripts/catalog-hosted-files.test.ts scripts/player-cdn-pin.test.ts scripts/registry-cdn-pins.test.ts scripts/studio-runtime-smoke.test.mjs scripts/verify-packed-manifests.test.mjs scripts/rewrite-relative-import-extensions.test.mjs scripts/lint-skills.test.mjs scripts/check-core-skills.test.mjs scripts/creator-editing-recipes.test.mjs packages/gcp-cloud-run/check-dockerfile-workspaces.test.mjs packages/core/scripts/writeGeneratedFile.test.ts scripts/catalog-publication.test.mjs scripts/ci/resolve-workflow-pr.test.mjs scripts/lefthook-order.test.mjs scripts/inline-gsap.test.mjs scripts/ci/flake-gate.test.mjs scripts/ci/motion-blur-nightly-report.test.mjs scripts/ci/base-file.test.mjs scripts/ci/release-only-diff.test.mjs scripts/check-catalog-source-pr.test.mjs scripts/generate-registry-items.test.ts scripts/catalog-drift.test.ts scripts/catalog-fetch-mirror.test.ts scripts/catalog-script-inlining.test.ts scripts/catalog-detail.test.ts scripts/generate-catalog-pages.test.ts scripts/verify-catalog-payloads.test.ts scripts/registry-theme-fallbacks.test.ts scripts/registry-skill-files.test.ts scripts/creator-editing-capabilities.test.mjs scripts/generate-catalog-previews.test.ts scripts/registry-primitive-payloads.test.ts registry/components/pan-stations/pan-stations.test.mjs registry/components/filmed-screen/filmed-screen.test.mjs registry/components/echo-smear/echo-smear.test.mjs registry/examples/path-traced-3d/source/path-traced-3d.test.mjs && vitest run scripts/catalog/ scripts/contrastRatchet.test.ts scripts/generate-catalog-payloads.test.ts", "typecheck:scripts": "tsc --noEmit -p scripts/tsconfig.json", "test:skills": "HYPERFRAMES_MEDIA_HOME_REQUIRED=1 node --import ./packages/cli/scripts/test-home.mjs --test 'skills/**/*.test.mjs' 'packages/cli/src/media-use/**/*.test.mjs'", "generate:previews": "tsx scripts/generate-template-previews.ts", diff --git a/scripts/publish-main-only-workflows.test.mjs b/scripts/publish-main-only-workflows.test.mjs new file mode 100644 index 0000000000..d289506ff0 --- /dev/null +++ b/scripts/publish-main-only-workflows.test.mjs @@ -0,0 +1,154 @@ +import assert from "node:assert/strict"; +import { readdirSync, readFileSync } from "node:fs"; +import { join } from "node:path"; +import test from "node:test"; +import { parse } from "yaml"; + +const workflowsDir = join(import.meta.dirname, "..", ".github", "workflows"); +const workflows = readdirSync(workflowsDir) + .filter((file) => /\.ya?ml$/.test(file)) + .map((file) => { + const source = readFileSync(join(workflowsDir, file), "utf8"); + return { file, source, doc: parse(source) }; + }); + +const PUBLISH_SCOPES = ["contents", "id-token", "packages", "pages", "deployments", "attestations"]; +const MAIN_GUARD = "github.ref == 'refs/heads/main'"; +const normalize = (expression) => + String(expression ?? "") + .replace(/\s+/g, " ") + .trim(); +const usesPublishSecret = (value) => + [...JSON.stringify(value ?? {}).matchAll(/secrets\.(\w+)/g)].some( + ([, name]) => name !== "GITHUB_TOKEN", + ); + +function holdsPublishCredential(doc, job) { + const permissions = job.permissions ?? doc.permissions ?? {}; + const writes = + permissions === "write-all" || PUBLISH_SCOPES.some((scope) => permissions[scope] === "write"); + return writes || usesPublishSecret(job) || usesPublishSecret(doc.env); +} + +const publishingJobs = workflows.flatMap(({ file, doc }) => + Object.entries(doc.jobs ?? {}) + .filter(([, job]) => holdsPublishCredential(doc, job)) + .map(([name, job]) => ({ id: `${file} ${name}`, file, doc, job })), +); + +function triggers(doc) { + if (typeof doc.on === "string") return { [doc.on]: null }; + if (Array.isArray(doc.on)) return Object.fromEntries(doc.on.map((event) => [event, null])); + return doc.on ?? {}; +} +const runsOnChosenRef = (doc) => + ["workflow_dispatch", "repository_dispatch", "workflow_call"].some((event) => + Object.hasOwn(triggers(doc), event), + ); + +test("the publish detector finds every known publishing job", () => { + const ids = publishingJobs.map(({ id }) => id); + for (const id of [ + "catalog-publish.yml publish", + "publish-mcp-registry.yml publish", + "publish.yml publish", + "sync-skills-to-clawhub.yml sync", + ]) { + assert.ok(ids.includes(id), `${id} is no longer detected as publishing`); + } +}); + +function targetsBranches(push) { + if (["branches", "branches-ignore"].some((filter) => Object.hasOwn(push, filter))) return true; + // A tags-only push filter disables branch pushes; an absent filter does not. + return !["tags", "tags-ignore"].some((filter) => Object.hasOwn(push, filter)); +} + +function branchPushViolations({ id, doc }) { + const events = triggers(doc); + if (!Object.hasOwn(events, "push")) return []; + const push = events.push ?? {}; + if (!targetsBranches(push)) return []; + const branches = push.branches; + if (branches?.length === 1 && branches[0] === "main") return []; + return [`${id} publishes from a branch push`]; +} + +function dispatchGuardViolations({ id, doc, job }) { + if (!runsOnChosenRef(doc)) return []; + const guard = normalize(job.if); + if (guard === MAIN_GUARD) return []; + return [`${id} can be dispatched on any branch; its job needs if: ${MAIN_GUARD}`]; +} + +const isCheckout = (step) => step.uses?.startsWith("actions/checkout@"); +const checksOutMain = (step) => ["main", "${{ github.sha }}"].includes(step.with?.ref); + +function checkoutViolations({ id, doc, job }) { + if (!runsOnChosenRef(doc)) return []; + return (job.steps ?? []) + .filter((step) => isCheckout(step) && !checksOutMain(step)) + .map((step) => `${id} checks out ${step.with?.ref ?? "the dispatched ref"}`); +} + +test("a publishing job that can be dispatched runs only for main", () => { + const violations = publishingJobs.flatMap((entry) => [ + ...branchPushViolations(entry), + ...dispatchGuardViolations(entry), + ...checkoutViolations(entry), + ]); + assert.deepEqual(violations, []); +}); + +test("the ClawHub token reaches only the pinned CLI login step", () => { + const { source, doc } = workflows.find(({ file }) => file === "sync-skills-to-clawhub.yml"); + const sync = doc.jobs.sync; + assert.equal(source.match(/secrets\.CLAWHUB_TOKEN/g).length, 1); + assert.ok(!usesPublishSecret(doc.env)); + assert.ok(!usesPublishSecret(sync.env)); + const holders = sync.steps.filter((step) => usesPublishSecret(step)); + assert.equal(holders.length, 1); + assert.deepEqual(holders[0].env, { CLAWHUB_TOKEN: "${{ secrets.CLAWHUB_TOKEN }}" }); + assert.match(holders[0].run, /^clawhub login --token "\$CLAWHUB_TOKEN"/); + const install = sync.steps.find((step) => /npm i -g clawhub@/.test(step.run ?? "")); + assert.match(install.run, /clawhub@\d+\.\d+\.\d+$/); + assert.ok(sync.steps.indexOf(install) < sync.steps.indexOf(holders[0])); +}); + +test("unrestricted branch pushes cannot evade the publishing policy", () => { + for (const on of [ + "push", + ["push"], + { push: null }, + { push: { paths: ["skills/**"] } }, + { push: { "branches-ignore": ["archive"] } }, + ]) { + assert.equal( + branchPushViolations({ id: "fixture", doc: { on } }).length, + 1, + JSON.stringify(on), + ); + } + for (const on of [ + { workflow_dispatch: null }, + { push: { branches: ["main"] } }, + { push: { tags: ["v*-*"] } }, + ]) { + assert.deepEqual(branchPushViolations({ id: "fixture", doc: { on } }), []); + } +}); + +test("dispatch arrays and OR suffixes cannot evade the main guard", () => { + for (const on of [{ workflow_dispatch: null }, ["workflow_dispatch"], "workflow_call"]) { + for (const guard of [undefined, `${MAIN_GUARD} && false || true`]) { + assert.equal( + dispatchGuardViolations({ id: "fixture", doc: { on }, job: { if: guard } }).length, + 1, + ); + } + assert.deepEqual( + dispatchGuardViolations({ id: "fixture", doc: { on }, job: { if: MAIN_GUARD } }), + [], + ); + } +});