diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a9bed42e27..0255de1a06 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -115,6 +115,8 @@ jobs: run: sh scripts/check-large-files.sh --range "$BASE_SHA" HEAD - name: Reject symbolic links outside the allowlist run: node scripts/check-symlinks.mjs + - name: Reject values shaped like a live HeyGen API key + run: node scripts/check-committed-keys.mjs - name: Find a release-only diff id: release uses: ./.github/actions/release-only-diff diff --git a/lefthook.yml b/lefthook.yml index b8e3cec0ea..eaa73fa86e 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -50,6 +50,8 @@ pre-commit: # `git ls-files` observes the staged index, so staged removals pass and # an accidental force-add fails before the commit is created. run: bun run check:tracked-artifacts + - name: committed-keys + run: bun run check:committed-keys - name: filesize # Scoped to packages/studio — the 600 LOC limit is a studio architecture # standard enforced as part of the App.tsx decomposition work. Player and diff --git a/package.json b/package.json index ddf5996d8a..7fee78c495 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "check:skill-mirror": "node scripts/check-skill-mirror.mjs", "lint:fix": "oxlint --fix .", "check:tracked-artifacts": "node scripts/check-tracked-artifacts.mjs", + "check:committed-keys": "node scripts/check-committed-keys.mjs", "check:symlinks": "node scripts/check-symlinks.mjs", "check:docs-snippet-motion": "node scripts/check-docs-snippet-motion.mjs", "check:docs-catalog": "node scripts/check-docs-catalog.mjs", @@ -56,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-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/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/packages/cli/src/auth/store.test.ts b/packages/cli/src/auth/store.test.ts index aa94a05b0c..8bd47e7de2 100644 --- a/packages/cli/src/auth/store.test.ts +++ b/packages/cli/src/auth/store.test.ts @@ -148,19 +148,19 @@ describe("auth/store", () => { await expect(readStore(path)).rejects.toSatisfy((err) => isAuthError(err)); }); + const FAKE_SK_V2_KEY = ["sk", "V2", "hgu", "0".repeat(11), "x".repeat(32)].join("_"); + it("accepts a legacy plaintext key of any HeyGen key format", async () => { // Real HeyGen keys come in multiple formats (`sk_V2_…`, `hg_…`, // partner keys, etc.). The CLI doesn't shape-check — the backend // does. Any single-line printable non-empty value is accepted as // a legacy key here; the next /v3/users/me call decides validity. - await fs.writeFile(path, "sk_V2_hgu_kVzzCxfI3cT_Yi96MxT2Ki6UamtWxyP7oOIPqsxaFHqN", { + await fs.writeFile(path, FAKE_SK_V2_KEY, { mode: 0o600, }); const result = await readStore(path); expect(result.source).toBe("file_legacy"); - expect(result.credentials.api_key).toBe( - "sk_V2_hgu_kVzzCxfI3cT_Yi96MxT2Ki6UamtWxyP7oOIPqsxaFHqN", - ); + expect(result.credentials.api_key).toBe(FAKE_SK_V2_KEY); }); it("still rejects plaintext that contains a space (not a credential shape)", async () => { diff --git a/scripts/check-committed-keys.mjs b/scripts/check-committed-keys.mjs new file mode 100644 index 0000000000..4ce76b7e82 --- /dev/null +++ b/scripts/check-committed-keys.mjs @@ -0,0 +1,61 @@ +import { readFileSync } from "node:fs"; +import { resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +import { readTrackedPaths } from "./check-tracked-artifacts.mjs"; + +const KEY_SHAPES = [ + /sk_V2_[A-Za-z0-9]+_[A-Za-z0-9]{8,}_[A-Za-z0-9]{24,}/g, + /\bhg_[A-Za-z0-9]{24,}/g, +]; +const MAX_SCANNED_BYTES = 4 * 1024 * 1024; + +function looksRandom(value) { + const tail = value.slice(value.lastIndexOf("_") + 1); + const classes = [/[a-z]/, /[A-Z]/, /[0-9]/].filter((re) => re.test(tail)).length; + return classes >= 2 && new Set(tail).size >= tail.length / 3; +} + +export function findKeyShapedValues(text) { + const found = []; + for (const shape of KEY_SHAPES) { + for (const match of text.matchAll(shape)) { + if (looksRandom(match[0])) found.push(match.index); + } + } + return found; +} + +export function lineOf(text, index) { + return text.slice(0, index).split("\n").length; +} + +export function checkCommittedKeys(cwd = process.cwd()) { + const hits = []; + for (const path of readTrackedPaths(cwd)) { + let buffer; + try { + buffer = readFileSync(resolve(cwd, path)); + } catch { + continue; + } + if (buffer.length > MAX_SCANNED_BYTES || buffer.includes(0)) continue; + const text = buffer.toString("utf8"); + for (const index of findKeyShapedValues(text)) hits.push(`${path}:${lineOf(text, index)}`); + } + return hits.sort(); +} + +function main() { + const hits = checkCommittedKeys(); + if (hits.length === 0) { + console.log("Committed key check passed."); + return; + } + console.error("Values shaped like a live HeyGen API key are tracked by Git:"); + for (const hit of hits) console.error(`- ${hit}`); + console.error('Build test keys at runtime from obvious filler, such as "x".repeat(32).'); + process.exitCode = 1; +} + +const entryPath = process.argv[1] ? resolve(process.argv[1]) : null; +if (entryPath === fileURLToPath(import.meta.url)) main(); diff --git a/scripts/check-committed-keys.test.mjs b/scripts/check-committed-keys.test.mjs new file mode 100644 index 0000000000..2a39470222 --- /dev/null +++ b/scripts/check-committed-keys.test.mjs @@ -0,0 +1,27 @@ +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; +import { findKeyShapedValues, lineOf } from "./check-committed-keys.mjs"; + +const random = (length) => + Array.from({ length }, (_, i) => "aZ3kQ9mP2xR7bN5vL8cT4wY6"[(i * 7 + 3) % 24]).join(""); +const liveShapedSk = ["sk", "V2", "hgu", random(11), random(32)].join("_"); +const liveShapedHg = ["hg", random(40)].join("_"); + +describe("committed key check", () => { + it("finds values shaped like a live sk_V2_ or hg_ key", () => { + assert.equal(findKeyShapedValues(`const k = "${liveShapedSk}";`).length, 1); + assert.equal(findKeyShapedValues(`key=${liveShapedHg}\n`).length, 1); + }); + + it("passes obvious fakes and short toy values", () => { + const fakeSk = ["sk", "V2", "hgu", "0".repeat(11), "x".repeat(32)].join("_"); + for (const text of [fakeSk, "hg_test_abc", "hg_legacy_key", "sk_V2_hgu_abcd123", "hg_x"]) { + assert.deepEqual(findKeyShapedValues(text), [], text); + } + }); + + it("reports the line a value is on", () => { + const text = `a\nb\nconst k = "${liveShapedSk}";\n`; + assert.equal(lineOf(text, findKeyShapedValues(text)[0]), 3); + }); +});