Skip to content

File Explorer context-menu integration fails with directory names with embedded apostrophes (single quotes) #6598

Description

Just as the current Windows PowerShell File Explorer context-menu integration (with Shift-right-click) is broken with paths with embedded ' chars. , as described here, so is PowerShell Core's.

The following two lines must be revised in accordance with the linked instructions:

<RegistryValue Type="string" Value="[$(var.ProductDirectoryName)]pwsh.exe -NoExit -Command Set-Location -LiteralPath '%V'"/>

<RegistryValue Type="string" Value="[$(var.ProductDirectoryName)]pwsh.exe -NoExit -Command Set-Location -LiteralPath '%V'"/>

Environment data

PowerShell Core v6.1.0-preview.1

Activity

  1. changed the title [-]File Explorer context-menu integration fails with directories with embedded apostrophes (single quotes)[/-] [+]File Explorer context-menu integration fails with directory names with embedded apostrophes (single quotes)[/+] on Apr 9, 2018
  2. SteveL-MSFT commented on Apr 9, 2018

    @SteveL-MSFT
    Member

    Christoph Bergmeister (@bergmeister) do you want to take this?

  3. bergmeister commented on Apr 9, 2018

    @bergmeister
    Contributor

    Steve Lee (@SteveL-MSFT) I will try to take a look at this but cannot promise anything at the moment (I will be at psconf.eu next week and after that on holiday for 2 weeks so if I cannot fix/investigate it until psconf, then others will need to take a look at it). But it seems Michael Klement (@mklement0) has already found the required fix in the linked superuser thread to handle this special case, thanks for the initial investigation.
    I am glad people actually use it and find it generally useful :-)

  4. bergmeister commented on Apr 9, 2018

    @bergmeister
    Contributor

    Michael Klement (@mklement0) I have tried your suggestion of using \" instead of the ' and various other variations with quotes and it does not work for me. This bug exist also with the native Windows PowerShell integration in Windows as you said. The reason is because %V gets replaced with the actual path and I could not quickly find a way of making the command work because escaping a single quote inside a single quote means that it needs to appear twice. I also tried double quotes, no success but double quotes are not good from a security perspective because folder names can contain $ and therefore you would open up a hole for script injection...
    Steve Lee (@SteveL-MSFT) This would be much simpler if pwsh had a startup path option and then we could also solve the other issue of starting pwsh in the user folder instead of the installation folder.

  5. mklement0 commented on Apr 9, 2018

    @mklement0
    ContributorAuthor

    Christoph Bergmeister (@bergmeister):

    folder names can contain $ and therefore you would open up a hole for script injection...

    That's a good point.

    I suppose the only way to avoid this to use -File rather than -Command, because the arguments would then be treated as literals by PowerShell, but that would require us to ship a helper script.

  6. bergmeister commented on Apr 9, 2018

    @bergmeister
    Contributor

    Michael Klement (@mklement0) I am not sure if -File would solve it because how do you pass the literal string to the function then, I imagine you would run into the same problem. To me it seems to be rather a language problem because adding just one single quote inside a single quote make it invalid syntax. Maybe an expert in batch syntax or Windows could help out?
    What do the powershell language experts think Jason Shirk (@lzybkr) or Bruce Payette (@BrucePay) ?

  7. mklement0 commented on Apr 9, 2018

    @mklement0
    ContributorAuthor

    I imagine you would run into the same problem

    No, you wouldn't - any argument passed to -File from the outside is a literal.

    Thus, something like the following would be safe:

    powershell.exe -noexit -File  /path/to/helper-script.ps1 "%V"

    As an aside: The way that -Command parsing works is really unfortunate, but the decision was made to keep the old behavior.


    The alternative is to document the hypothetical vulnerability (I've already added a note to my superuser.com answer) and rely on users being savvy enough to recognize a malicious folder name when they see one.

  8. bergmeister commented on Apr 9, 2018

    @bergmeister
    Contributor

    Michael Klement (@mklement0) A sophisticated attack could involve launching an interactive process (a bit like a Ranorex/Selenium test) but by this time the machine is already exploited I guess.
    My vote goes for having a -Path option in pwsh because it would also help to fix #5705, which already failed to be solved once.
    P.S. I just realised that you're Austrian as well ;)

  9. mklement0 commented on Apr 9, 2018

    @mklement0
    ContributorAuthor

    Christoph Bergmeister (@bergmeister): Servas ;)

    I definitely like the idea of the pwsh CLI having a -Path parameter (though we should think about the specific name, given that Start-Process calls the analogous parameter -WorkingDirectory, for instance; perhaps -WorkingDirectory / -wd?).

    Is there an issue for this suggestion yet?

  10. SteveL-MSFT commented on Apr 9, 2018

    @SteveL-MSFT
    Member

    I'm thinking a -WorkingDirectory parameter makes sense. I'll work on a PR for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productWG-Maintainers-Buildspecific to affecting the build

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions