Skip to content

Commit 1b1815a

Browse files
authored
Merge pull request microsoft#68435 from geirha/61902-shell-injection
Use hard quotes rather than soft quotes microsoft#61902
2 parents 767715b + 868260f commit 1b1815a

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/vs/workbench/contrib/debug/node/terminals.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ export function prepareCommand(args: DebugProtocol.RunInTerminalRequestArguments
320320
}
321321

322322
let quote: (s: string) => string;
323+
let hardQuote: (s: string) => string;
323324
let command = '';
324325

325326
switch (shellType) {
@@ -391,17 +392,21 @@ export function prepareCommand(args: DebugProtocol.RunInTerminalRequestArguments
391392
return (s.indexOf(' ') >= 0 || s.indexOf('\\') >= 0) ? `"${s}"` : s;
392393
};
393394

395+
hardQuote = (s: string) => {
396+
return /[^\w@%\/+=,.:^-]/.test(s) ? `'${s.replace(/'/g, '\'\\\'\'')}'` : s;
397+
};
398+
394399
if (args.cwd) {
395-
command += `cd ${quote(args.cwd)} ; `;
400+
command += `cd ${hardQuote(args.cwd)} && `;
396401
}
397402
if (args.env) {
398403
command += 'env';
399404
for (let key in args.env) {
400405
const value = args.env[key];
401406
if (value === null) {
402-
command += ` -u "${key}"`;
407+
command += ` -u ${hardQuote(key)}`;
403408
} else {
404-
command += ` "${key}=${value}"`;
409+
command += ` ${hardQuote(`${key}=${value}`)}`;
405410
}
406411
}
407412
command += ' ';

0 commit comments

Comments
 (0)