Skip to content

fix(envvar): expand only whole-value placeholders in secret config fields#367

Draft
NikolayS wants to merge 2 commits into
masterfrom
claude/determined-edison-3Z8Ld
Draft

fix(envvar): expand only whole-value placeholders in secret config fields#367
NikolayS wants to merge 2 commits into
masterfrom
claude/determined-edison-3Z8Ld

Conversation

@NikolayS
Copy link
Copy Markdown
Contributor

What

Make envvar.ExpandStrict expand environment placeholders only when a config field's value is exactly a single ${VAR} or $VAR. Any value that merely contains a $ (a password, a regex backreference, an unmatched brace) is now returned verbatim.

Why

Two confirmed bugs in the env-token placeholder feature (commit 362c7b0), both affecting secret fields (verificationToken, accessToken, webhook secret, source token, CLI token):

  • A literal $ in a secret was reinterpreted by os.Expand as a variable reference: silently corrupted when the embedded name matched a set env var, or a hard startup failure otherwise. No escape existed.
  • Malformed placeholders (${UNCLOSED, ${}) were silently truncated to a partial string or empty value, contradicting the function's "fail loudly" contract.

How

ExpandStrict now matches the value against ^\$\{NAME\}$ | ^\$NAME$. On a match it resolves the variable (unset → loud error, preserving documented startup-safety); otherwise it returns the input unchanged. All intended whole-value placeholders in the example configs and existing tests are unaffected.

Tests (red/green TDD)

  • Red commit adds cases asserting verbatim preservation of ***$1, pa$$w0rd, tok$en (with a colliding env var set), ${UNCLOSED, ${}, $, and embedded $DBLAB_TOKEN-suffix — these fail against the old os.Expand implementation.
  • Green commit implements whole-value-only expansion; all cases pass.
  • go vet and gofmt clean.

Canonical review happens on the GitLab MR (postgres-ai/database-lab!1158), which closes the two filed work items. This mirror PR exists to run CI and collect review feedback.


Generated by Claude Code

claude added 2 commits May 29, 2026 17:04
Encode whole-value-only expansion semantics: values that merely contain a
$ (passwords, regex backreferences, unmatched braces) must be preserved
verbatim, and only a value that is exactly ${VAR} or $VAR is expanded.
These tests fail against the current os.Expand-based implementation, which
corrupts embedded references and silently truncates malformed placeholders.
Resolve env references only when a field's value is exactly ${VAR} or
$VAR. A value that merely contains a $ (a password, a regex
backreference, an unmatched brace) is now returned verbatim instead of
being passed through os.Expand.

This fixes two issues in secret fields (verificationToken, accessToken,
webhook secret, source token, CLI token):

- literal $ in a secret was reinterpreted as a variable reference,
  silently corrupting the secret when the embedded name matched a set
  env var, or failing startup when it did not.
- malformed placeholders (${UNCLOSED, ${}) were silently truncated to a
  partial string or an empty value instead of being preserved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants