-
Notifications
You must be signed in to change notification settings - Fork 237
Fix PowerShell path escaping #765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
b737a89
4785078
3123832
4dbf2f8
08c8ae3
e07b5cd
2d6a37c
ce20e65
063b782
7fab6d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ namespace Microsoft.PowerShell.EditorServices | |
using System.Management.Automation.Runspaces; | ||
using Microsoft.PowerShell.EditorServices.Session.Capabilities; | ||
using System.IO; | ||
using System.ComponentModel; | ||
|
||
/// <summary> | ||
/// Manages the lifetime and usage of a PowerShell session. | ||
|
@@ -796,7 +797,7 @@ public async Task ExecuteScriptWithArgs(string script, string arguments = null, | |
if (File.Exists(script) || File.Exists(scriptAbsPath)) | ||
{ | ||
// Dot-source the launched script path | ||
script = ". " + EscapePath(script, escapeSpaces: true); | ||
script = ". " + QuoteEscapeString(script); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this continue to work if someone has a debug launch config like this:
I "think" if we single quote a path, we have to evaluate backticks as well and perhaps strip some out. For instance, with the above path the user tried to be helpful and escape the wildcard chars for us (even though unnecessary, this works today). If the backticks are left in, the path will fail with this change - I think. However take a file named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll give it a test. I don't think we can do both though. Even if it means a breaking change, personally my preference is to enable all paths to work rather than having some paths work two ways and others not work at all. It feels like we should treat the config setting as a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth noting here too that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rkeithhill I just tested with two files in the same directory ( Unfortunately the same happens in 1.9.1, so looks like I still need to do some work in this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I think I'm hitting a powershell issue here. I'll look into a workaround and open an issue there There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've found something interesting. PowerShell behaves strangely with backtick escaping: PowerShell/PowerShell#7999. It looks like we've always been receiving the message in the first place with the backtick escaped (or maybe the
This miraculously worked because PowerShell escapes So I'm currently trying to track down why the message we get doubles the backticks. |
||
} | ||
|
||
launchedScript = script + " " + arguments; | ||
|
@@ -1113,30 +1114,144 @@ public async Task SetWorkingDirectory(string path, bool isPathAlreadyEscaped) | |
{ | ||
if (!isPathAlreadyEscaped) | ||
{ | ||
path = EscapePath(path, false); | ||
path = GlobEscapePath(path); | ||
} | ||
|
||
runspaceHandle.Runspace.SessionStateProxy.Path.SetLocation(path); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Fully escape a given path for use in PowerShell script. | ||
/// Note: this will not work with PowerShell.AddParameter() | ||
/// </summary> | ||
/// <param name="path">The path to escape.</param> | ||
/// <returns>An escaped version of the path that can be embedded in PowerShell script.</returns> | ||
internal static string FullyPowerShellEscapePath(string path) | ||
{ | ||
string globEscapedPath = GlobEscapePath(path); | ||
rjmholt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return QuoteEscapeString(globEscapedPath); | ||
} | ||
|
||
/// <summary> | ||
/// Wrap a string in quotes to make it safe to use in scripts. | ||
/// </summary> | ||
/// <param name="escapedPath">The glob-escaped path to wrap in quotes.</param> | ||
/// <returns>The given path wrapped in quotes appropriately.</returns> | ||
internal static string QuoteEscapeString(string escapedPath) | ||
{ | ||
var sb = new StringBuilder(escapedPath.Length + 2); // Length of string plus two quotes | ||
sb.Append('\''); | ||
if (!escapedPath.Contains('\'')) | ||
{ | ||
sb.Append(escapedPath); | ||
} | ||
else | ||
{ | ||
foreach (char c in escapedPath) | ||
{ | ||
if (c == '\'') | ||
{ | ||
sb.Append("''"); | ||
continue; | ||
} | ||
|
||
sb.Append(c); | ||
} | ||
} | ||
sb.Append('\''); | ||
return sb.ToString(); | ||
rjmholt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// <summary> | ||
/// Return the given path with all PowerShell globbing characters escaped, | ||
/// plus optionally the whitespace. | ||
/// </summary> | ||
/// <param name="path">The path to process.</param> | ||
/// <param name="escapeSpaces">Specify True to escape spaces in the path, otherwise False.</param> | ||
/// <returns>The path with [ and ] escaped.</returns> | ||
internal static string GlobEscapePath(string path, bool escapeSpaces = false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nit but does PowerShell really do globbing? Maybe this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah good point. Discussing with @JamesWTruher we were calling it "globbing" but the help topic is about_Wildcards. I will rename. |
||
{ | ||
var sb = new StringBuilder(); | ||
for (int i = 0; i < path.Length; i++) | ||
{ | ||
char curr = path[i]; | ||
switch (curr) | ||
{ | ||
// Escape '[', ']', '?' and '*' with '`' | ||
case '[': | ||
case ']': | ||
case '*': | ||
case '?': | ||
sb.Append('`').Append(curr); | ||
break; | ||
|
||
default: | ||
// Escape whitespace if required | ||
if (escapeSpaces && char.IsWhiteSpace(curr)) | ||
{ | ||
sb.Append('`').Append(curr); | ||
break; | ||
} | ||
sb.Append(curr); | ||
break; | ||
} | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the passed in path with the [ and ] characters escaped. Escaping spaces is optional. | ||
/// </summary> | ||
/// <param name="path">The path to process.</param> | ||
/// <param name="escapeSpaces">Specify True to escape spaces in the path, otherwise False.</param> | ||
/// <returns>The path with [ and ] escaped.</returns> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
[Obsolete("This API is not meant for public usage and should not be used.")] | ||
rjmholt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public static string EscapePath(string path, bool escapeSpaces) | ||
{ | ||
string escapedPath = Regex.Replace(path, @"(?<!`)\[", "`["); | ||
escapedPath = Regex.Replace(escapedPath, @"(?<!`)\]", "`]"); | ||
return GlobEscapePath(path, escapeSpaces); | ||
} | ||
|
||
internal static string UnescapeGlobEscapedPath(string globEscapedPath) | ||
{ | ||
// Prevent relying on my implementation if we can help it | ||
if (!globEscapedPath.Contains('`')) | ||
{ | ||
return globEscapedPath; | ||
} | ||
|
||
if (escapeSpaces) | ||
var sb = new StringBuilder(globEscapedPath.Length); | ||
for (int i = 0; i < globEscapedPath.Length; i++) | ||
{ | ||
escapedPath = Regex.Replace(escapedPath, @"(?<!`) ", "` "); | ||
// If we see a backtick perform a lookahead | ||
char curr = globEscapedPath[i]; | ||
if (curr == '`' && i + 1 < globEscapedPath.Length) | ||
{ | ||
// If the next char is an escapable one, don't add this backtick to the new string | ||
char next = globEscapedPath[i + 1]; | ||
switch (next) | ||
{ | ||
case '[': | ||
case ']': | ||
case '?': | ||
case '*': | ||
continue; | ||
|
||
default: | ||
if (char.IsWhiteSpace(next)) | ||
{ | ||
continue; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
sb.Append(curr); | ||
} | ||
|
||
return escapedPath; | ||
return sb.ToString(); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -1145,14 +1260,11 @@ public static string EscapePath(string path, bool escapeSpaces) | |
/// </summary> | ||
/// <param name="path">The path to unescape.</param> | ||
/// <returns>The path with the ` character before [, ] and spaces removed.</returns> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
[Obsolete("This API is not meant for public usage and should not be used.")] | ||
public static string UnescapePath(string path) | ||
{ | ||
if (!path.Contains("`")) | ||
{ | ||
return path; | ||
} | ||
|
||
return Regex.Replace(path, @"`(?=[ \[\]])", ""); | ||
return UnescapeGlobEscapedPath(path); | ||
} | ||
|
||
#endregion | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
function Hello | ||
{ | ||
"Bye" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Write-Output "Windows won't let me put * or ? in the name of this file..." |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,7 +105,7 @@ public async Task DebuggerAcceptsScriptArgs(string[] args) | |
// it should not escape already escaped chars. | ||
ScriptFile debugWithParamsFile = | ||
this.workspace.GetFile( | ||
@"..\..\..\..\PowerShellEditorServices.Test.Shared\Debugging\Debug` With Params `[Test].ps1"); | ||
@"..\..\..\..\PowerShellEditorServices.Test.Shared\Debugging\Debug` W&ith Params `[Test].ps1"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was the deal with the & char? It isn't a wildcard char. Ah, wait a tic. I see. On WinPS it's a reserved char and on PS Core it is used to create a (background) job. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it's a "token separator", so it will break a bareword string |
||
|
||
await this.debugService.SetLineBreakpoints( | ||
debugWithParamsFile, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using System; | ||
using Xunit; | ||
using Microsoft.PowerShell.EditorServices; | ||
using System.IO; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Test.Session | ||
{ | ||
public class PathEscapingTests | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +100 ! |
||
{ | ||
private const string ScriptAssetPath = @"..\..\..\..\PowerShellEditorServices.Test.Shared\scriptassets"; | ||
|
||
[Theory] | ||
[InlineData("DebugTest.ps1", "DebugTest.ps1")] | ||
[InlineData("../../DebugTest.ps1", "../../DebugTest.ps1")] | ||
[InlineData("C:\\Users\\me\\Documents\\DebugTest.ps1", "C:\\Users\\me\\Documents\\DebugTest.ps1")] | ||
[InlineData("/home/me/Documents/weird&folder/script.ps1", "/home/me/Documents/weird&folder/script.ps1")] | ||
[InlineData("./path/with some/spaces", "./path/with some/spaces")] | ||
[InlineData("C:\\path\\with[some]brackets\\file.ps1", "C:\\path\\with`[some`]brackets\\file.ps1")] | ||
[InlineData("C:\\look\\an*\\here.ps1", "C:\\look\\an`*\\here.ps1")] | ||
[InlineData("/Users/me/Documents/?here.ps1", "/Users/me/Documents/`?here.ps1")] | ||
[InlineData("/Brackets [and s]paces/path.ps1", "/Brackets `[and s`]paces/path.ps1")] | ||
[InlineData("/CJK.chars/脚本/hello.ps1", "/CJK.chars/脚本/hello.ps1")] | ||
[InlineData("/CJK.chars/脚本/[hello].ps1", "/CJK.chars/脚本/`[hello`].ps1")] | ||
[InlineData("C:\\Animals\\утка\\quack.ps1", "C:\\Animals\\утка\\quack.ps1")] | ||
[InlineData("C:\\&nimals\\утка\\qu*ck?.ps1", "C:\\&nimals\\утка\\qu`*ck`?.ps1")] | ||
public void CorrectlyGlobEscapesPaths_NoSpaces(string unescapedPath, string escapedPath) | ||
{ | ||
string extensionEscapedPath = PowerShellContext.GlobEscapePath(unescapedPath); | ||
Assert.Equal(escapedPath, extensionEscapedPath); | ||
} | ||
|
||
[Theory] | ||
[InlineData("DebugTest.ps1", "DebugTest.ps1")] | ||
[InlineData("../../DebugTest.ps1", "../../DebugTest.ps1")] | ||
[InlineData("C:\\Users\\me\\Documents\\DebugTest.ps1", "C:\\Users\\me\\Documents\\DebugTest.ps1")] | ||
[InlineData("/home/me/Documents/weird&folder/script.ps1", "/home/me/Documents/weird&folder/script.ps1")] | ||
[InlineData("./path/with some/spaces", "./path/with` some/spaces")] | ||
[InlineData("C:\\path\\with[some]brackets\\file.ps1", "C:\\path\\with`[some`]brackets\\file.ps1")] | ||
[InlineData("C:\\look\\an*\\here.ps1", "C:\\look\\an`*\\here.ps1")] | ||
[InlineData("/Users/me/Documents/?here.ps1", "/Users/me/Documents/`?here.ps1")] | ||
[InlineData("/Brackets [and s]paces/path.ps1", "/Brackets` `[and` s`]paces/path.ps1")] | ||
[InlineData("/CJK chars/脚本/hello.ps1", "/CJK` chars/脚本/hello.ps1")] | ||
[InlineData("/CJK chars/脚本/[hello].ps1", "/CJK` chars/脚本/`[hello`].ps1")] | ||
[InlineData("C:\\Animal s\\утка\\quack.ps1", "C:\\Animal` s\\утка\\quack.ps1")] | ||
[InlineData("C:\\&nimals\\утка\\qu*ck?.ps1", "C:\\&nimals\\утка\\qu`*ck`?.ps1")] | ||
public void CorrectlyGlobEscapesPaths_Spaces(string unescapedPath, string escapedPath) | ||
{ | ||
string extensionEscapedPath = PowerShellContext.GlobEscapePath(unescapedPath, escapeSpaces: true); | ||
Assert.Equal(escapedPath, extensionEscapedPath); | ||
} | ||
|
||
[Theory] | ||
[InlineData("DebugTest.ps1", "'DebugTest.ps1'")] | ||
[InlineData("../../DebugTest.ps1", "'../../DebugTest.ps1'")] | ||
[InlineData("C:\\Users\\me\\Documents\\DebugTest.ps1", "'C:\\Users\\me\\Documents\\DebugTest.ps1'")] | ||
[InlineData("/home/me/Documents/weird&folder/script.ps1", "'/home/me/Documents/weird&folder/script.ps1'")] | ||
[InlineData("./path/with some/spaces", "'./path/with some/spaces'")] | ||
[InlineData("C:\\path\\with[some]brackets\\file.ps1", "'C:\\path\\with[some]brackets\\file.ps1'")] | ||
[InlineData("C:\\look\\an*\\here.ps1", "'C:\\look\\an*\\here.ps1'")] | ||
[InlineData("/Users/me/Documents/?here.ps1", "'/Users/me/Documents/?here.ps1'")] | ||
[InlineData("/Brackets [and s]paces/path.ps1", "'/Brackets [and s]paces/path.ps1'")] | ||
[InlineData("/file path/that isn't/normal/", "'/file path/that isn''t/normal/'")] | ||
[InlineData("/CJK.chars/脚本/hello.ps1", "'/CJK.chars/脚本/hello.ps1'")] | ||
[InlineData("/CJK chars/脚本/[hello].ps1", "'/CJK chars/脚本/[hello].ps1'")] | ||
[InlineData("C:\\Animal s\\утка\\quack.ps1", "'C:\\Animal s\\утка\\quack.ps1'")] | ||
[InlineData("C:\\&nimals\\утка\\qu*ck?.ps1", "'C:\\&nimals\\утка\\qu*ck?.ps1'")] | ||
public void CorrectlyQuoteEscapesPaths(string unquotedPath, string expectedQuotedPath) | ||
{ | ||
string extensionQuotedPath = PowerShellContext.QuoteEscapeString(unquotedPath); | ||
Assert.Equal(expectedQuotedPath, extensionQuotedPath); | ||
} | ||
|
||
[Theory] | ||
[InlineData("DebugTest.ps1", "'DebugTest.ps1'")] | ||
[InlineData("../../DebugTest.ps1", "'../../DebugTest.ps1'")] | ||
[InlineData("C:\\Users\\me\\Documents\\DebugTest.ps1", "'C:\\Users\\me\\Documents\\DebugTest.ps1'")] | ||
[InlineData("/home/me/Documents/weird&folder/script.ps1", "'/home/me/Documents/weird&folder/script.ps1'")] | ||
[InlineData("./path/with some/spaces", "'./path/with some/spaces'")] | ||
[InlineData("C:\\path\\with[some]brackets\\file.ps1", "'C:\\path\\with`[some`]brackets\\file.ps1'")] | ||
[InlineData("C:\\look\\an*\\here.ps1", "'C:\\look\\an`*\\here.ps1'")] | ||
[InlineData("/Users/me/Documents/?here.ps1", "'/Users/me/Documents/`?here.ps1'")] | ||
[InlineData("/Brackets [and s]paces/path.ps1", "'/Brackets `[and s`]paces/path.ps1'")] | ||
[InlineData("/file path/that isn't/normal/", "'/file path/that isn''t/normal/'")] | ||
[InlineData("/CJK.chars/脚本/hello.ps1", "'/CJK.chars/脚本/hello.ps1'")] | ||
[InlineData("/CJK chars/脚本/[hello].ps1", "'/CJK chars/脚本/`[hello`].ps1'")] | ||
[InlineData("C:\\Animal s\\утка\\quack.ps1", "'C:\\Animal s\\утка\\quack.ps1'")] | ||
[InlineData("C:\\&nimals\\утка\\qu*ck?.ps1", "'C:\\&nimals\\утка\\qu`*ck`?.ps1'")] | ||
public void CorrectlyFullyEscapesPaths(string unescapedPath, string escapedPath) | ||
{ | ||
string extensionEscapedPath = PowerShellContext.FullyPowerShellEscapePath(unescapedPath); | ||
Assert.Equal(escapedPath, extensionEscapedPath); | ||
} | ||
|
||
[Theory] | ||
[InlineData("DebugTest.ps1", "DebugTest.ps1")] | ||
[InlineData("../../DebugTest.ps1", "../../DebugTest.ps1")] | ||
[InlineData("C:\\Users\\me\\Documents\\DebugTest.ps1", "C:\\Users\\me\\Documents\\DebugTest.ps1")] | ||
[InlineData("/home/me/Documents/weird&folder/script.ps1", "/home/me/Documents/weird&folder/script.ps1")] | ||
[InlineData("./path/with` some/spaces", "./path/with some/spaces")] | ||
[InlineData("C:\\path\\with`[some`]brackets\\file.ps1", "C:\\path\\with[some]brackets\\file.ps1")] | ||
[InlineData("C:\\look\\an`*\\here.ps1", "C:\\look\\an*\\here.ps1")] | ||
[InlineData("/Users/me/Documents/`?here.ps1", "/Users/me/Documents/?here.ps1")] | ||
[InlineData("/Brackets` `[and` s`]paces/path.ps1", "/Brackets [and s]paces/path.ps1")] | ||
[InlineData("/CJK` chars/脚本/hello.ps1", "/CJK chars/脚本/hello.ps1")] | ||
[InlineData("/CJK` chars/脚本/`[hello`].ps1", "/CJK chars/脚本/[hello].ps1")] | ||
[InlineData("C:\\Animal` s\\утка\\quack.ps1", "C:\\Animal s\\утка\\quack.ps1")] | ||
[InlineData("C:\\&nimals\\утка\\qu`*ck`?.ps1", "C:\\&nimals\\утка\\qu*ck?.ps1")] | ||
public void CorrectlyUnescapesPaths(string escapedPath, string expectedUnescapedPath) | ||
{ | ||
string extensionUnescapedPath = PowerShellContext.UnescapeGlobEscapedPath(escapedPath); | ||
Assert.Equal(expectedUnescapedPath, extensionUnescapedPath); | ||
} | ||
|
||
[Theory] | ||
[InlineData("NormalScript.ps1")] | ||
[InlineData("Bad&name4script.ps1")] | ||
[InlineData("[Truly] b&d Name_4_script.ps1")] | ||
public void CanDotSourcePath(string rawFileName) | ||
{ | ||
string fullPath = Path.Combine(ScriptAssetPath, rawFileName); | ||
string quotedPath = PowerShellContext.QuoteEscapeString(fullPath); | ||
|
||
var psCommand = new System.Management.Automation.PSCommand().AddScript($". {quotedPath}"); | ||
|
||
using (var pwsh = System.Management.Automation.PowerShell.Create()) | ||
{ | ||
pwsh.Commands = psCommand; | ||
pwsh.Invoke(); | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.