|
1 | 1 | // Copyright (c) Microsoft Corporation.
|
2 | 2 | // Licensed under the MIT License.
|
3 | 3 |
|
4 |
| -using System.Text; |
5 | 4 | using System.Management.Automation.Language;
|
| 5 | +using System.Text; |
6 | 6 |
|
7 | 7 | namespace Microsoft.PowerShell.EditorServices.Utility
|
8 | 8 | {
|
9 | 9 | internal static class ArgumentEscaping
|
10 | 10 | {
|
11 | 11 | /// <summary>
|
12 | 12 | /// 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> |
17 | 18 | /// </summary>
|
18 | 19 | public static string Escape(string Arg)
|
19 | 20 | {
|
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. |
21 | 28 | if (Arg.StartsWith("{") && Arg.EndsWith("}"))
|
22 | 29 | {
|
23 | 30 | return Arg;
|
24 | 31 | }
|
25 | 32 |
|
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. |
27 | 34 | if (Arg.Contains(" "))
|
28 | 35 | {
|
29 |
| - if (Arg.StartsWith("\"") && Arg.EndsWith("\"") || Arg.StartsWith("'") && Arg.EndsWith("'")) |
| 36 | + if ((Arg.StartsWith("\"") && Arg.EndsWith("\"")) || (Arg.StartsWith("'") && Arg.EndsWith("'"))) |
30 | 37 | {
|
31 | 38 | return Arg;
|
32 | 39 | }
|
|
0 commit comments