Skip to content

fix(cli): a SECRET containing '#' does not round-trip through .env #1060

Closed

Description

@chappie-daemon

Surviving finding from the CodeRabbit review of #1058 (review 5789566267). Two of that review's three findings were already fixed at the reviewed head (550ceb8) and are not filed here — see "Already fixed" below.

Problem

A SECRET that contains # is accepted from the environment, then written to .env unquoted, where Node's loader truncates it at the #. The secret the relay derives from changes between setup --start and the next start.

src/cli/commands/setup.ts at f335613:

  • resolveSecret (line ~91) returns an inherited process.env.SECRET through stripQuotes with no # check. The # validator at line ~119 guards only the interactive prompt.
  • upsertSecret (line ~140) writes SECRET=${secret} with no quoting or escaping.
  • deriveFromSecret (src/utils/secret.ts) HMACs the raw process.env.SECRET, so a different value means different derived signatures.

Measured

Node 24.22.1, the loader the CLI itself uses (node --env-file-if-exists=.env):

$ printf 'SECRET=my-secret#suffix\n' > t.env
$ node --env-file=t.env -e 'console.log(JSON.stringify(process.env.SECRET))'
"my-secret"

$ printf 'SECRET="my-secret#suffix"\n' > t2.env
$ node --env-file=t2.env -e 'console.log(JSON.stringify(process.env.SECRET))'
"my-secret#suffix"

runCommand spreads process.env into the child (src/cli/utils/process.ts:21-23), so setup --start uses the full value while every later start uses the truncated one.

Fix

Make accepted secrets round-trip. Either is sound:

  1. Reject values that cannot be written safely — apply the prompt validator's checks (#, newlines, unterminated quotes) to the inherited value in resolveSecret, and to whatever upsertSecret is handed.
  2. Serialize instead of rejecting — quote or escape on write so parseEnvValue and Node's loader both recover the exact value.

Option 2 plus a round-trip assertion in the setup tests is the safer read, since a hex secret is only the recommended shape, not an enforced one.

Acceptance

  • An inherited SECRET containing #, a newline, or an unbalanced quote either fails setup with a clear message or is written in a form both parseEnvValue and node --env-file read back unchanged.
  • A test asserts that the value written by upsertSecret reads back identically through readEnvSecret and through Node's loader.

Already fixed (do not re-file)

  • Quotes defeat the placeholder check (setup.ts:39-40) — readEnvSecret already returns parseEnvValue(raw) and needsSecretReplacement already calls stripQuotes at the reviewed head. SECRET="change_me_to_something_long_and_random" is recognised as the placeholder.
  • An inherited placeholder survives when .env holds a valid secret (setup.ts:136-137) — the early return at lines 182-188 already assigns process.env.SECRET = envFileSecret when the inherited value needs replacement. That was the point of fix(cli): reject placeholder secret inherited from env or prompt in setup (#1018) #1058.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't workingsecuritySecurity vulnerability or disclosure

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions