fix(env): hold env keys to the identifier rule env.sh already assumes - #740
Merged
Merged
Conversation
`generateEnvFile` interpolated the key raw into `export <key>=<quoted value>`. The value had `shellQuoteValue`; the key had nothing, so a key from the team repo's env/env.yaml could produce a line that is not valid shell (`export bad key='x'`) or one that runs code (`export FOO;cmd='x'`), in every member's shell — env is pushable, so any member who can push can put such a key in front of everyone else. `parseEnvFile` already refused to read back any key outside `[A-Za-z_][A-Za-z0-9_]*`, which made the asymmetry worse: the variable was written into env.sh and then invisible to the CLI, so nothing reported it as missing. That regex is now a module-level `ENV_KEY_RE` shared by both sides, so write and read agree by construction rather than by two copies drifting. The generator drops a non-matching key instead of failing: one member's bad key must not take env.sh down for everyone, and the remaining variables are still correct. `env add` rejects such a key up front with the offending name, because the local command is where the mistake is still visible — accepting it there would report success for a variable that never reaches a shell. Refs Tencent#738
|
Findings
|
9 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #738.
generateEnvFileinterpolated the key raw intoexport <key>=<quoted value>. The value went throughshellQuoteValue; the key went through nothing, so a key from the team repo'senv/env.yamlcould produce either a line that is not valid shell, or one that executes code — in every member's shell, sinceenvis inpushableTypesand any member who can push can put such a key in front of everyone else.parseEnvFilealready refused to read back any key outside[A-Za-z_][A-Za-z0-9_]*, which is what made this worse than a one-off: the variable was written into env.sh and then invisible to the CLI, so nothing reported it as undelivered. That regex is now a module-levelENV_KEY_REshared by both sides, so write and read agree by construction instead of by two copies that can drift.Change
generateEnvFiledrops a key that is not an identifier rather than emitting a broken or dangerous line. It drops rather than fails on purpose: one member's bad key must not take env.sh down for everyone, and the remaining variables still ship.env addrejects such a key up front and names it, because the local command is where the mistake is still visible. Accepting it there would have reported success for a variable that never reaches anyone's shell.Type of Change
Test Plan
npx tsc --noEmit— no errorsnpx vitest run src/__tests__/env-commands.test.ts src/__tests__/env-handler.test.ts— 68 passed, 1 skipped (pre-existing skip)npm run build— success,dist/index.js1.76 MBRED → GREEN
Two new tests were written before the fix. Both fail on the base commit:
generateEnvFile › should drop keys that are not valid shell identifiers— the base emitsexport bad key='oops'andexport FOO;touch /tmp/pwned='y'.envAdd › refuses a key that would not survive the round trip into env.sh— the base writes it into env.yaml and logs success.A companion test (
should keep keys that are valid shell identifiers, covering_PRIVATEandA1_b2) passes both before and after, so the guard does not narrow what a legitimate team repo can express.Real-CLI end-to-end verification
Built with
npm run build, then drove the produceddist/index.js:/tmp/pwned-e2ewas checked after the run and does not exist — the injected command never executed.Generator side, driven directly:
Not verified
src/__tests__/doctor-env-delivery.test.tshas 5 failures on this branch, but they are pre-existing and environmental: they fail identically on an untouched checkout ofupstream/main(cd3e0e6) on this Windows machine, and this PR touches neither the doctor path nor anything it calls. I confirmed this by running the same file in a worktree whose only change is an unrelated workflow edit. I have not attempted to fix them.vitest runsuite was not driven to completion (283 files; it exceeds the local time budget), so I scoped verification to the env-related files plustscandbuild.cd3e0e6, not the currentmaintip (8d74aa4);git fetchwas failing on this machine. I checked the three intervening commits and none touchsrc/resources/env.ts,src/env-commands.ts, or the env tests. Happy to rebase if you would rather have it on the tip.