Skip to content

Commit

Permalink
Wrap file paths containg an ampersand in double quotation marks for r…
Browse files Browse the repository at this point in the history
…unning commands in a shell (microsoft#18855)
  • Loading branch information
Kartik Raj committed Apr 6, 2022
1 parent 8a7f732 commit d468a12
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/2 Fixes/18722.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Wrap file paths containg an ampersand in double quotation marks for running commands in a shell.
4 changes: 3 additions & 1 deletion src/client/common/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ String.prototype.toCommandArgument = function (this: string): string {
if (!this) {
return this;
}
return this.indexOf(' ') >= 0 && !this.startsWith('"') && !this.endsWith('"') ? `"${this}"` : this.toString();
return (this.indexOf(' ') >= 0 || this.indexOf('&') >= 0) && !this.startsWith('"') && !this.endsWith('"')
? `"${this}"`
: this.toString();
};

/**
Expand Down
4 changes: 4 additions & 0 deletions src/test/common/extensions.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ suite('String Extensions', () => {
const argTotest = 'one two three';
expect(argTotest.toCommandArgument()).to.be.equal(`"${argTotest}"`);
});
test('Should quote command arguments containing ampersand', () => {
const argTotest = 'one&twothree';
expect(argTotest.toCommandArgument()).to.be.equal(`"${argTotest}"`);
});
test('Should return empty string for empty path', () => {
const fileToTest = '';
expect(fileToTest.fileToCommandArgument()).to.be.equal('');
Expand Down

0 comments on commit d468a12

Please sign in to comment.