diff --git a/Extension/src/Debugger/ParsedEnvironmentFile.ts b/Extension/src/Debugger/ParsedEnvironmentFile.ts index 9018a540b..031ce94c6 100644 --- a/Extension/src/Debugger/ParsedEnvironmentFile.ts +++ b/Extension/src/Debugger/ParsedEnvironmentFile.ts @@ -57,8 +57,49 @@ 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. + const singleQuoted: boolean = (value.length > 1) && (value.charAt(0) === "'") && (value.charAt(value.length - 1) === "'"); + 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 = value.replace(/(\\)?\$\{([\w\.\-]+)\}|\\\$\{/g, (full: string, escape: string | undefined, name: string | undefined): string => { + if (escape) { + return `\${dollar}{${name}}`; + } + if (name === undefined) { + // Escaped opener that is not a dotenv-style name, e.g. \${env:HOME}. + return `\${dollar}{`; + } + if (env.has(name)) { + return env.get(name); + } + const processValue: string | undefined = process.env[name]; + return processValue !== undefined ? processValue : full; + }); + } + env.set(key, value); } else { // Blank lines and lines starting with # are no parse errors diff --git a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts index a107971ce..9f1725f94 100644 --- a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts +++ b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts @@ -100,4 +100,104 @@ MyName2=Value2 assertEnvironmentEqual(result.Env, "MyName1", "Value1"); assertEnvironmentEqual(result.Env, "MyName2", "Value2"); }); + + test("Resolve a reference to a variable defined earlier in the file", () => { + const content: string = `USER=admin +EMAIL=\${USER}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "USER", "admin"); + assertEnvironmentEqual(result.Env, "EMAIL", "admin@example.org"); + }); + + test("Resolve a reference from the initial environment", () => { + const initialEnv: Environment[] = [{ name: "USER", value: "admin" }]; + const content: string = `EMAIL=\${USER}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", initialEnv); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "admin@example.org"); + }); + + test("Leave an unknown reference untouched", () => { + const content: string = `EMAIL=\${UNKNOWN_USER}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "${UNKNOWN_USER}@example.org"); + }); + + test("Resolve a reference from the process environment", () => { + const name: string = "CPPTOOLS_ENVFILE_INTERPOLATION_TEST"; + const previous: string | undefined = process.env[name]; + process.env[name] = "from-process"; + try { + const content: string = `EMAIL=\${${name}}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "from-process@example.org"); + } finally { + if (previous === undefined) { + delete process.env[name]; + } else { + process.env[name] = previous; + } + } + }); + + test("Protect a reference inside a single-quoted value from the launch expansion", () => { + const content: string = `USER=admin\nEMAIL='\${USER}@example.org'`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + // ${dollar} is the one form expandAllStrings leaves alone, and it collapses to a plain $ + // when that expansion finishes (expand.ts), so the debuggee still receives the literal. + assertEnvironmentEqual(result.Env, "EMAIL", "${dollar}{USER}@example.org"); + }); + + test("Protect an escaped reference the same way, without the backslash", () => { + const content: string = `USER=admin\nEMAIL=\\\${USER}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "${dollar}{USER}@example.org"); + }); + + test("Protect a launch reference inside a single-quoted value", () => { + // ${env:...} and ${command:...} are matched on anything up to the closing brace, so a + // dotenv-style name filter would let them through and the launch expansion would + // substitute (or run) them even though the value is single-quoted. + const content: string = `HOME_DIR='\${env:HOME}'\nCMD='\${command:workbench.action.files.newUntitledFile}'`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "HOME_DIR", "${dollar}{env:HOME}"); + assertEnvironmentEqual(result.Env, "CMD", "${dollar}{command:workbench.action.files.newUntitledFile}"); + }); + + test("Protect an escaped launch reference, without the backslash", () => { + const content: string = `HOME_DIR=\\\${env:HOME}`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "HOME_DIR", "${dollar}{env:HOME}"); + }); + + test("Leave an unescaped launch reference bare for the expansion", () => { + const content: string = `HOME_DIR=\${env:HOME}`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "HOME_DIR", "${env:HOME}"); + }); + + test("Leave a resolved reference bare, so the expansion can still see it", () => { + const content: string = `USER=admin\nEMAIL=\${USER}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "admin@example.org"); + }); });