Repository navigation
fix(cli): keep project .env keys behind the person's own credentials - #9232
michaelwang11394 wants to merge 4 commits into
Conversation
…person's own credentials
|
Preview deployment for your docs. Learn more about Mintlify Previews.
💡 Tip: Enable Automations to automatically generate PRs for you. |
Edit accuracy: accurate 275 (base branch 275), smooth 59 of thoseThe gate passes. Quarantined, measured but not gated (0) |
jrusso1020
left a comment
There was a problem hiding this comment.
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_DIRand the OAuth token URL are on neither project-key list:PROJECT_ENV_KEYSincli/src/utils/dotEnv.ts:6, and the one in media-useheygen.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_KEYwhen only a login is saved. On HeyGen's own host the order is unchanged. In media-use,hostGatewayKeynow 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+savedHeygenCredentialin the CLI,ownCompetingCredentialin 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/*andaudio/analysis/providers/shared.ts. They all read after the CLI drops received keys at startup (cli.ts:79-80), or afterloadEnvFromDirin 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'sHEYGEN_API_KEY. ButHYPERFRAMES_PROJECT_ENV_ORIGINSstill names the project folder for that variable, because the CLI's own env holds the.envvalue.inheritProjectKeyOrigins(heygen.mjs:118-129) then recordsval: process.env[name], which is whatever the CLI passed down. - Scenario:
HEYGEN_API_BASEpoints at a gateway, with no shell key.- A saved API key exists.
- The person's own project
.envsetsHEYGEN_API_KEY. - The CLI resolver correctly picks the saved key.
- The script labels it a project key,
hostGatewayKeyreturns 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 andAuthClientsends 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
.envkey exists. - This predates the PR, and the host comes from the shell, so it isn't blocking. Either refuse OAuth whenever
gatewayis true (asmedia-environment.ts:43-50already 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_KEYin the shell and their.envsettingHEYGEN_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_KEYfor 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.
- A saved HeyGen login now also displaces a received
Tests
- CLI:
resolver.test.ts57/57,dotEnv.test.ts32/32,contentExtractor.test.ts55/55,media-environment.test.ts+media-use.test.ts29/29. - media-use:
heygen.test.mjs+tts.test.mjs78/78,audio.test.mjs20/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
What
Tightens which HeyGen, Gemini and ElevenLabs key is used when a project folder's
.envsets one.Why
A key from a project
.envshould only be used when the person has no credential of their own. Three paths did not follow that rule:HEYGEN_API_URLorHEYGEN_API_BASEnames a host other than heygen.com, a project.envkey was used ahead of the saved login or saved API key..envpath 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.envkey 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 newGATEWAY_NEEDS_KEYerror that says to set the key in the shell.utils/dotEnv.tsandskills/media-use/audio/scripts/lib/heygen.mjs: a key from a received (or unchecked).envfor 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..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 withterminalText.docs/guides/authentication.mdxdescribes 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.envkey, under both variables) fail onmain. The new yield cases (received HeyGen key with an own ElevenLabs key, received Gemini/Google/ElevenLabs/HeyGen key with a saved login) also fail onmain.media-use:
node --testonheygen.test.mjs,tts.test.mjsandaudio.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.