From d468a12ec60f849a65f84f5b739aa26aa6a5a590 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Mon, 4 Apr 2022 14:04:47 -0700 Subject: [PATCH] Wrap file paths containg an ampersand in double quotation marks for running commands in a shell (#18855) --- news/2 Fixes/18722.md | 1 + src/client/common/extensions.ts | 4 +++- src/test/common/extensions.unit.test.ts | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 news/2 Fixes/18722.md diff --git a/news/2 Fixes/18722.md b/news/2 Fixes/18722.md new file mode 100644 index 000000000000..e185b6d0e32c --- /dev/null +++ b/news/2 Fixes/18722.md @@ -0,0 +1 @@ +Wrap file paths containg an ampersand in double quotation marks for running commands in a shell. diff --git a/src/client/common/extensions.ts b/src/client/common/extensions.ts index ea3fc81327b8..19972df44d16 100644 --- a/src/client/common/extensions.ts +++ b/src/client/common/extensions.ts @@ -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(); }; /** diff --git a/src/test/common/extensions.unit.test.ts b/src/test/common/extensions.unit.test.ts index 133382b251fa..d26c6e2428a4 100644 --- a/src/test/common/extensions.unit.test.ts +++ b/src/test/common/extensions.unit.test.ts @@ -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('');