Skip to content

Commit 83c078c

Browse files
committed
Add magic marker to disable argument escaping when necessary
1 parent 68434e6 commit 83c078c

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/PowerShellEditorServices/Utility/ArgumentUtils.cs

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System.Text;
54
using System.Management.Automation.Language;
5+
using System.Text;
66

77
namespace Microsoft.PowerShell.EditorServices.Utility
88
{
99
internal static class ArgumentEscaping
1010
{
1111
/// <summary>
1212
/// Escape a PowerShell argument while still making it able to be evaluated in AddScript.
13-
///
14-
/// NOTE: This does not "sanitize" parameters, e.g., a pipe in one argument might affect another argument.
15-
/// This is intentional to give flexibility to specifying arguments.
16-
/// It also does not try to fix invalid PowerShell syntax, e.g., a single quote in a string literal.
13+
/// <remarks>
14+
/// This does not "sanitize" parameters, e.g., a pipe in one argument might affect another
15+
/// argument. This is intentional to give flexibility to specifying arguments. It also does
16+
/// not try to fix invalid PowerShell syntax, e.g., a single quote in a string literal.
17+
/// </remarks>
1718
/// </summary>
1819
public static string Escape(string Arg)
1920
{
20-
// if argument is a scriptblock return as-is
21+
// If argument has our magic marker, remove it and don't escape.
22+
if (Arg.StartsWith("__psEditorServices_NoEscape_"))
23+
{
24+
return Arg.Remove(0, "__psEditorServices_NoEscape_".Length);
25+
}
26+
27+
// If argument is a script block return as-is.
2128
if (Arg.StartsWith("{") && Arg.EndsWith("}"))
2229
{
2330
return Arg;
2431
}
2532

26-
// If argument has a space enclose it in quotes unless it is already quoted
33+
// If argument has a space enclose it in quotes unless it is already quoted.
2734
if (Arg.Contains(" "))
2835
{
29-
if (Arg.StartsWith("\"") && Arg.EndsWith("\"") || Arg.StartsWith("'") && Arg.EndsWith("'"))
36+
if ((Arg.StartsWith("\"") && Arg.EndsWith("\"")) || (Arg.StartsWith("'") && Arg.EndsWith("'")))
3037
{
3138
return Arg;
3239
}

0 commit comments

Comments
 (0)