Skip to content

fix(cli): keep project .env keys behind the person's own credentials - #9232

Open
michaelwang11394 wants to merge 4 commits into
mainfrom
harden-project-key-precedence
Open

michaelwang11394 wants to merge 4 commits into
mainfrom
harden-project-key-precedence

Conversation

@michaelwang11394

Copy link
Copy Markdown
Contributor

What

Tightens which HeyGen, Gemini and ElevenLabs key is used when a project folder's .env sets one.

Why

A key from a project .env should only be used when the person has no credential of their own. Three paths did not follow that rule:

  • In the CLI, when HEYGEN_API_URL or HEYGEN_API_BASE names a host other than heygen.com, a project .env key was used ahead of the saved login or saved API key.
  • In the CLI and the media-use audio scripts, a received project's HeyGen key was never weighed against the person's own ElevenLabs or Gemini credential. A received Gemini key was weighed only against other Gemini credentials.
  • The media-use note that announces a project HeyGen key printed the .env path without escaping control characters.

Related work

None.

How

  • auth/resolver.ts: only a key the shell or host app set takes the gateway shortcut. On a gateway, a project .env key is used only when nothing is saved. A saved API key wins over it. When the only saved credential is a login, which is not sent to a gateway, the command stops with a new GATEWAY_NEEDS_KEY error that says to set the key in the shell.
  • utils/dotEnv.ts and skills/media-use/audio/scripts/lib/heygen.mjs: a key from a received (or unchecked) .env for HeyGen, HyperFrames, Gemini, Google or ElevenLabs gives way when the person has any speech or music credential of their own. That includes a shell key, a service account and the saved HeyGen login or key. The existing escaped stderr line names the ignored key. The person's own and cloned projects behave as before.
  • media-use: a gateway key must come from the shell, not a project .env. The CLI now passes HeyGen key origins to launched scripts as it already does for Gemini and ElevenLabs. The project-key note escapes the path with terminalText.
  • Docs: docs/guides/authentication.mdx describes the new precedence.

Test plan

  • Unit tests added/updated

  • Manual testing performed

  • Documentation updated (if applicable)

  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

  • packages/cli: vitest run src/auth/ src/utils/dotEnv.test.ts src/capture/contentExtractor.test.ts: all pass. The new gateway cases (a saved login and a saved API key, each with a project or received .env key, under both variables) fail on main. The new yield cases (received HeyGen key with an own ElevenLabs key, received Gemini/Google/ElevenLabs/HeyGen key with a saved login) also fail on main.

  • media-use: node --test on heygen.test.mjs, tts.test.mjs and audio.test.mjs: 98 of 98 pass. The new tests cover an escaped notice path, gateway handling, received HeyGen key against an own ElevenLabs key, received Gemini key against a saved login, and the music-mode credential.

  • oxlint, oxfmt, the CLI typecheck and the pre-commit hooks pass.

@mintlify

mintlify Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
hyperframes 🟢 Ready View Preview Dec 2, 2026, 1:12 AM

💡 Tip: Enable Automations to automatically generate PRs for you.

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 275 (base branch 275), smooth 59 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (0)

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve. The security claim holds. No blockers; two should-fixes.

What I checked

  • The host can't be redirected from a project .env. HEYGEN_API_URL, HEYGEN_API_BASE, HEYGEN_ACCESS_TOKEN, HYPERFRAMES_CONFIG_DIR and the OAuth token URL are on neither project-key list: PROJECT_ENV_KEYS in cli/src/utils/dotEnv.ts:6, and the one in media-use heygen.mjs. Only the person's shell picks the host.
  • The person's own credential wins. On a gateway, only a shell key takes the shortcut. A project key gives way to a saved API key, or returns GATEWAY_NEEDS_KEY when only a login is saved. On HeyGen's own host the order is unchanged. In media-use, hostGatewayKey now skips a key whose origin is a project .env.
  • Received keys give way. A received HeyGen, HyperFrames, Gemini, Google or ElevenLabs key yields to any credential the person has, including a saved HeyGen login: competitorsOf + savedHeygenCredential in the CLI, ownCompetingCredential in media-use. If the store can't be read, the person is treated as having a credential, so this fails closed.
  • Other readers of these keys: audio/providers.ts, bgm.mjs, tts.mjs, lyria-recipe.py, locate.mjs, capture/* and audio/analysis/providers/shared.ts. They all read after the CLI drops received keys at startup (cli.ts:79-80), or after loadEnvFromDir in media-use. No site was missed.

Should-fix 1: media-use treats the key the CLI chose on a gateway as a project key, and then refuses it.

  • How it happens: withMediaAuthEnvironment (auth/media-environment.ts:63) puts the CLI's chosen key into the child's HEYGEN_API_KEY. But HYPERFRAMES_PROJECT_ENV_ORIGINS still names the project folder for that variable, because the CLI's own env holds the .env value. inheritProjectKeyOrigins (heygen.mjs:118-129) then records val: process.env[name], which is whatever the CLI passed down.
  • Scenario:
    1. HEYGEN_API_BASE points at a gateway, with no shell key.
    2. A saved API key exists.
    3. The person's own project .env sets HEYGEN_API_KEY.
    4. The CLI resolver correctly picks the saved key.
    5. The script labels it a project key, hostGatewayKey returns null, storedCredential() is truthy, and the command fails with "no HyperFrames credentials".
  • Reproduced: at this head the script returns null; main returns {"X-Api-Key":"file-key"}. This fails closed, so it is a regression, not a leak.
  • Fix: pass origins only for values the CLI passes through unchanged, or compare the inherited value against the original.
  • Untested: the origins tests cover received keys with a competing credential of the person's own, but not this gateway-substitution case.

Should-fix 2: ErrGatewayNeedsKey claims something that isn't always true (cli/src/auth/errors.ts:91-96, resolver.ts:75,83).

  • The message reads "Your saved HeyGen login is not sent to a custom API host". That only holds when a project key is present.
  • Counter-case: HEYGEN_API_URL=http://127.0.0.1:4100, a saved OAuth login, and no project key. resolveCredential() returns the OAuth credential and AuthClient sends it as a Bearer token to the custom host. I probed this at this head.
  • So whether the login is sent depends on whether a .env key exists.
  • This predates the PR, and the host comes from the shell, so it isn't blocking. Either refuse OAuth whenever gateway is true (as media-environment.ts:43-50 already does for media), or reword the message and the "must never receive an OAuth token" comment.

Nits

  • Follows from should-fix 1: with the person's HYPERFRAMES_API_KEY in the shell and their .env setting HEYGEN_API_KEY, the script prints "Using HEYGEN_API_KEY from the project .env … goes to that key's HeyGen account". It is actually using the person's key, so the account it names is wrong.
  • Worth a sentence in the PR body:
    • A saved HeyGen login now also displaces a received GEMINI_API_KEY for capture's image descriptions, because it sits in both provider groups. HeyGen doesn't provide that service.
    • On a gateway with nothing saved, a received project's key is still sent to the gateway the shell named.

Tests

  • CLI: resolver.test.ts 57/57, dotEnv.test.ts 32/32, contentExtractor.test.ts 55/55, media-environment.test.ts + media-use.test.ts 29/29.
  • media-use: heygen.test.mjs + tts.test.mjs 78/78, audio.test.mjs 20/20.
  • Mutants, 4/4 caught:
    • main's gateway shortcut: 8 fail
    • no saved-HeyGen competitor: 4 fail
    • no project-key check in hostGatewayKey: 1 fail
    • no storedCredential() gate: 1 fail

Need ✅ · Size ✅ · Reuse ✅ · Simpler ✅ · Structure ✅
Risk: low. Keys are strictly tighter. One fail-closed regression on gateway + saved key + own .env.

Review by Rames

@michaelwang11394
michaelwang11394 added this pull request to the merge queue Dec 2, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Dec 2, 2026

This branch was successfully deployed

1 active deployment
staging - docs — 399d76e5 Deployed Dec 2, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants