Repository navigation
Fix variable interpolation in .env files loaded through envFile #14828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
214642c
c5946b7
e12e15a
1f8a192
d15097c
5ab402b
c8b399f
bcbef43
336c457
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,8 +57,42 @@ export class ParsedEnvironmentFile { | |
| value = value.replace(/\\n/gm, "\n"); | ||
| } | ||
|
|
||
| // A single-quoted value is literal, so a reference inside it must not be | ||
| // expanded. The quotes come off on the next line, so this has to be read here. | ||
| // Whitespace outside the closing quote is not part of the value, and leaving it | ||
| // in hid the quote from the check below. | ||
| const trimmed: string = value.replace(/\s+$/, ""); | ||
| const singleQuoted: boolean = (trimmed.length > 1) && (trimmed.charAt(0) === "'") && (trimmed.charAt(trimmed.length - 1) === "'"); | ||
| if (singleQuoted) { | ||
| value = trimmed; | ||
| } | ||
|
|
||
| value = value.replace(/(^['"]|['"]$)/g, ""); | ||
|
|
||
| // Resolve ${VAR} references against the variables defined so far in the file, | ||
| // then the process environment. A name we don't know is left as-is rather than | ||
| // dropped, so ${workspaceFolder} and the other launch variables still resolve | ||
| // from the debug configuration. | ||
| // | ||
| // A reference that has to stay literal is written back as ${dollar}{VAR}. This | ||
| // parser's output goes on to expandAllStrings (configurationProvider.expand), | ||
| // which substitutes every remaining ${...} it knows and warns about the rest, so | ||
| // a bare ${VAR} would be replaced or reported instead of reaching the debuggee. | ||
| // ${dollar} is the one form that expansion leaves alone and collapses to a plain | ||
| // $ once it is done, which is what makes the literal survive the trip. | ||
| // | ||
| // The dotenv-style names are not the only ones that expansion touches: it also | ||
| // rewrites ${env:...} and runs ${command:...}, both matched on anything up to the | ||
| // closing brace. So the protection cannot be limited to [\w\.\-]+ -- every ${ in | ||
| // a single-quoted value, and every escaped ${ anywhere, is written back, whatever | ||
| // follows it. Unescaped dotenv-style names are the only ones resolved here; an | ||
| // unescaped ${env:...} is left for the launch expansion, as before. | ||
| if (singleQuoted) { | ||
| value = value.replace(/\$\{/g, `\${dollar}{`); | ||
| } else { | ||
| value = ParsedEnvironmentFile.resolveReferences(value, (name: string) => ParsedEnvironmentFile.lookup(name, env)); | ||
| } | ||
|
|
||
| env.set(key, value); | ||
| } else { | ||
| // Blank lines and lines starting with # are no parse errors | ||
|
|
@@ -86,4 +120,90 @@ export class ParsedEnvironmentFile { | |
|
|
||
| return new ParsedEnvironmentFile(arrayEnv, warning); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the references the file can actually answer, and leave the others untouched so | ||
| * the launch expansion still sees them. | ||
| */ | ||
| private static resolveReferences(value: string, known: (name: string) => string | undefined): string { | ||
| // A backslash in front of a reference escapes it, unless it separates path segments. The | ||
| // value as a whole cannot decide: prose can carry another backslash (\${USER} regex\w) and | ||
| // a path can be built out of references (${workspaceFolder}\${env:USERNAME}). Each | ||
| // occurrence is judged by its neighbours, and a drive or UNC value keeps them all. | ||
| const pathValue: boolean = /^[A-Za-z]:[\\/]/.test(value) || /^\\\\/.test(value); | ||
| let result: string = ""; | ||
| let i: number = 0; | ||
| while (i < value.length) { | ||
| const char: string = value.charAt(i); | ||
|
|
||
| // An escaped reference is literal. ${dollar} is the form the launch expansion leaves | ||
| // alone and collapses back to a plain $ once it is done, so the debuggee is the only | ||
| // place that sees the literal. The whole opener has to be there: \$USER} is not a | ||
| // reference and must not be rewritten. | ||
| if (char === "\\" && value.charAt(i + 1) === "$" && value.charAt(i + 2) === "{") { | ||
| const end: number = value.indexOf("}", i + 3); | ||
| if (end !== -1 && !pathValue && !ParsedEnvironmentFile.separatesSegments(value, i, end)) { | ||
| result += `\${dollar}{${value.substring(i + 3, end)}}`; | ||
| i = end + 1; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| if (char === "$" && value.charAt(i + 1) === "{") { | ||
| const end: number = value.indexOf("}", i + 2); | ||
| if (end !== -1) { | ||
| const name: string = value.substring(i + 2, end); | ||
| const resolved: string | undefined = /^[\w\.\-]+$/.test(name) ? known(name) : undefined; | ||
| result += resolved !== undefined ? resolved : value.substring(i, end + 1); | ||
| i = end + 1; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| result += char; | ||
| i += 1; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Whether the backslash at index separates path segments instead of escaping the reference that | ||
| * ends at end. It continues a path when it follows a reference, a dot or a separator, and it | ||
| * starts one when the value goes on with a separator: tools\${env:USERNAME}\bin. In prose | ||
| * neither holds, so the backslash escapes. | ||
| */ | ||
| private static separatesSegments(value: string, index: number, end: number): boolean { | ||
| const before: string = index > 0 ? value.charAt(index - 1) : ""; | ||
| const after: string = value.charAt(end + 1); | ||
| return (before !== "" && "}.\\/:".includes(before)) || after === "\\" || after === "/"; | ||
| } | ||
|
|
||
| /** | ||
| * Environment variable names are case-insensitive on Windows, and the launch configuration | ||
| * may have spelled the name differently than the reference does, so Path and PATH have to | ||
| * find each other there. | ||
| */ | ||
| private static lookup(name: string, env: Map<string, any>): string | undefined { | ||
| if (env.has(name)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨Copilot (devbox3-wsl/agent179): [Moderate] Case-folding only the lookup still misses an env-file override with different casing. On Windows, if the initial environment contains |
||
| return env.get(name); | ||
| } | ||
|
|
||
| if (process.platform === "win32") { | ||
| const folded: string = name.toLowerCase(); | ||
| for (const key of env.keys()) { | ||
| if (key.toLowerCase() === folded) { | ||
| return env.get(key); | ||
| } | ||
| } | ||
| for (const key of Object.keys(process.env)) { | ||
| if (key.toLowerCase() === folded) { | ||
| return process.env[key]; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| return process.env[name]; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨Copilot (devbox7-wsl/agent60): [Minor] Please keep
${dollar}reserved for the downstream expander before looking up env-file names. Withdollar=USDfollowed byLITERAL=${dollar}{env:HOME}, this now producesUSD{env:HOME}instead of the previous literal${env:HOME}. This breaks escaping launch references when an environment variable is nameddollar. A parser-to-expander test would cover the interaction.