fix(config): stop a trailing space in .env becoming part of the value - #73
Merged
ralyodio merged 1 commit intoJul 30, 2026
Conversation
The .env loader's value pattern is `(.*)\s*$`. The greedy `.*` consumes the trailing whitespace before `\s*` ever runs, so the trim the regex was written to do never happens. `PUBLIC_ORIGIN=https://app.moshcode.sh ` (one stray space, easy to leave behind when editing a .env) exports the space too, and every device verification link becomes `https://app.moshcode.sh /device`. A padded `RESEND_API_KEY` or `TELEGRAM_BOT_TOKEN` goes out to the provider with the space still on it and just fails to authenticate. It also breaks the quote stripping: `KEY="value" ` no longer ends with a quote, so the value keeps its literal quotes. Making the group lazy lets the trailing `\s*` do its job. loadEnv now takes an optional path (defaulting to the same apps/pwa/.env) so it can be tested against a throwaway file instead of the repo's own .env. Verified on unmodified main: importing src/config.mjs with a .env holding `PUBLIC_ORIGIN=https://app.moshcode.sh ` yields config.origin with the trailing space.
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.
The bug
apps/pwa/src/config.mjsparses each .env line with:/^\s*([A-Z0-9_]+)\s*=\s*(.*)\s*$/iThe trailing
\s*is there to trim the value, but the greedy(.*)has alreadyeaten that whitespace by the time it runs, so nothing is ever trimmed.
One stray space at the end of a line, which is very easy to leave behind when
editing a .env by hand, ends up inside the value:
PUBLIC_ORIGIN=https://app.moshcode.sh"https://app.moshcode.sh "TELEGRAM_BOT_TOKEN=123:AAbb+ tab"123:AAbb\t"RESEND_API_KEY="re_live_key""\"re_live_key\" "Downstream that is silent and confusing:
config.originkeeps the space, so every device link becomeshttps://app.moshcode.sh /device(/cli/device/codebuildsverification_uriandverification_uri_completestraight off it).RESEND_API_KEY/TELEGRAM_BOT_TOKEN/DATABASE_AUTH_TOKENissent to the provider with the whitespace attached and just fails to
authenticate, with nothing in the config to point at.
KEY="value"no longerends with a quote, so the value keeps its literal quotes.
Reproduced on unmodified
main(dfcb8c1)Writing
apps/pwa/.envwithPUBLIC_ORIGIN=https://app.moshcode.shandimporting the real
src/config.mjs:No fault injection: the real module, the real loader, a normal .env file.
The fix
Make the value group lazy (
(.*?)) so the trailing\s*can do the trim itwas written for. That is the whole behaviour change.
loadEnvalso gains an optional file argument (defaulting to the sameapps/pwa/.envas before) and is exported, so the loader can be tested againsta throwaway file rather than the repo's own .env. Nothing else about the load
order changes: the environment still wins over the file.
Deliberately left alone: inline
#comments are still part of the value, sameas before. That is a behaviour change, not a fix, so it is not in this PR.
Tests
New
apps/pwa/test/config-env.test.mjs, 9 tests. It needs no PWA dependencies(config.mjs is node builtins only), so it runs on a bare clone rather than
skipping.
Verified fail-before / pass-after by reverting only the regex character and
keeping the export, so the difference is the fix and not the import:
with trailing space, and the mixed-lines case)
Full root suite
npm teston this branch: 206 tests, 0 fail, 25 skipped(PWA integration tests skip without
apps/pwadeps installed). Baseline onmainis 197; the 9 added are the ones here.