From 9aa816689146ee6cd86d8262112677c38651c6bd Mon Sep 17 00:00:00 2001 From: undergroundwires Date: Sun, 17 Oct 2021 15:37:06 +0100 Subject: [PATCH] Change PowerShell double quotes escape It changes the way privacy.sexy escape double quotes inside batch command when running PowerShell scripts as an argument to PowerShell.exe. It uses more robust and stable way offering support for wider use-cases. --- .../PipeDefinitions/EscapeDoubleQuotes.ts | 21 ++++++++++++++++--- .../EscapeDoubleQuotes.spec.ts | 4 ++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes.ts b/src/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes.ts index 45fa4c6a..46df3745 100644 --- a/src/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes.ts +++ b/src/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes.ts @@ -3,10 +3,25 @@ import { IPipe } from '../IPipe'; export class EscapeDoubleQuotes implements IPipe { public readonly name: string = 'escapeDoubleQuotes'; public apply(raw: string): string { - return raw?.replaceAll('"', '\\"'); + return raw?.replaceAll('"', '"^""'); /* - In batch, it also works with 4 double quotes but looks bloated - An easy test: PowerShell -Command "Write-Host '\"Hello World\"'" + "^"" is the most robust and stable choice. + Other options: + "" + Breaks, because it is fundamentally unsupported + """" + Does not work with consecutive double quotes. + E.g. PowerShell -Command "$name='aq'; Write-Host """"Disabled `""""$name`"""""""";" + Works when using: PowerShell -Command "$name='aq'; Write-Host "^""Disabled `"^""$name`"^"" "^"";" + \" + May break as they are interpreted by cmd.exe as metacharacters breaking the command + E.g. PowerShell -Command "Write-Host 'Hello \"w&orld\"'" does not work due to unescaped "&" + Works when using: PowerShell -Command "Write-Host 'Hello "^""w&orld"^""'" + \"" + Normalizes interior whitespace + E.g. PowerShell -Command "\""a& c\"".length", outputs 4 and discards one of two whitespaces + Works when using "^"": PowerShell -Command ""^""a& c"^"".length" + A good explanation: https://stackoverflow.com/a/31413730 */ } } diff --git a/tests/unit/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes.spec.ts b/tests/unit/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes.spec.ts index 70519e43..f8e76530 100644 --- a/tests/unit/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes.spec.ts +++ b/tests/unit/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes.spec.ts @@ -10,7 +10,7 @@ describe('EscapeDoubleQuotes', () => { { name: 'using "', input: 'hello "world"', - expectedOutput: 'hello \\"world\\"', + expectedOutput: 'hello "^""world"^""', }, { name: 'not using any double quotes', @@ -20,7 +20,7 @@ describe('EscapeDoubleQuotes', () => { { name: 'consecutive double quotes', input: '""hello world""', - expectedOutput: '\\"\\"hello world\\"\\"', + expectedOutput: '"^"""^""hello world"^"""^""', }, { name: 'returns undefined when if input is undefined',