Skip to content

Commit 905394d

Browse files
author
Kartik Raj
authored
Wrap file paths containg an ampersand in double quotation marks for running commands in a shell (#18855)
1 parent 5d7be84 commit 905394d

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

news/2 Fixes/18722.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Wrap file paths containg an ampersand in double quotation marks for running commands in a shell.

src/client/common/extensions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ String.prototype.toCommandArgument = function (this: string): string {
7373
if (!this) {
7474
return this;
7575
}
76-
return this.indexOf(' ') >= 0 && !this.startsWith('"') && !this.endsWith('"') ? `"${this}"` : this.toString();
76+
return (this.indexOf(' ') >= 0 || this.indexOf('&') >= 0) && !this.startsWith('"') && !this.endsWith('"')
77+
? `"${this}"`
78+
: this.toString();
7779
};
7880

7981
/**

src/test/common/extensions.unit.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ suite('String Extensions', () => {
2020
const argTotest = 'one two three';
2121
expect(argTotest.toCommandArgument()).to.be.equal(`"${argTotest}"`);
2222
});
23+
test('Should quote command arguments containing ampersand', () => {
24+
const argTotest = 'one&twothree';
25+
expect(argTotest.toCommandArgument()).to.be.equal(`"${argTotest}"`);
26+
});
2327
test('Should return empty string for empty path', () => {
2428
const fileToTest = '';
2529
expect(fileToTest.fileToCommandArgument()).to.be.equal('');

0 commit comments

Comments
 (0)