Repository navigation
Description
Edited for the current status as of PowerShell Core 7.3.0-preview.4
Note:
-
Originally, this issue was also about the inability to execute files whose literal names happen to be syntactically invalid wildcard patterns.
-
This execution aspect was partially fixed in Make command searcher not use wildcard search for execution #9202 (but wasn't back-ported to the currently stable version, v6.2.3)
- Invocation by wildcard pattern still works as a fallback, but paths with
[and]that exist literally now take precedence. - Therefore, if no literal match is present, you may still end up invoking an unrelated file by accident, which is problematic (e.g., if
a.ps1is present in the current dir. and.\[ab].ps1is invoked, with no file literally named[ab].ps1present,.\a.ps1is executed instead.)
- Invocation by wildcard pattern still works as a fallback, but paths with
Seemingly, all external-executable invocation methods ( ., &, Start-Process, powershell -File) as well as redirections > / >> mistakenly interpret script / file paths as wildcard patterns, and if a path that contains [ happens not contain a syntactically valid range expression, invocation and redirection break (in Windows PowerShell, no longer in PowerShell Core, except with Start-Process) or can end up accidentally targeting an unrelated executable.
-
In other words: The problem sometimes occurs, unless each instance of
[happens to be part of a valid range expression; e.g.,a[b](valid) vs.a[b(invalid); curiously, however, the equally invalidb[bdoes not. -
But even with a syntactically valid range expression - and some invalid ones - the behavior is unexpected:
'foo' > 'file[1].txt'attempts wildcard matching and complains,Cannot perform operation because the wildcard path file[1].txt did not resolve to a file.- Attempting escaping then inappropriately retains the escape characters - see Passing an escaped wildcard expression to -Path in file-creating cmdlets causes the escaped representation to be used as the literal filename #9475
-
Attempting to invoke (open) such a file fails quietly, even with escaping (run
New-Item file[1].txtfirst):./file[1].txt... quiet no-op./file`[1`].txt... quiet no-op- If you created a
.ps1file named, say,file[1].ps1('"hi"' | Set-Content -LiteralPath file[1].ps1):- Invocation with an explicit path unexpectedly only works if
.ps1is also specified. - Invocation via $env:PATH doesn't work at all - neither with or without
.ps1
- Invocation with an explicit path unexpectedly only works if
Fundamentally, external-executable / redirection output paths should be treated as literals in these situations.
See this comment for a discussion and an overview of related issues.
Original tests below.
Steps to reproduce
Run the following Pester tests.
Describe "Redirection with and invocation of literal file paths that are invalid as wildcard patterns" {
BeforeAll {
Push-Location testdrive:\
$f1 = './t[1.ps1'
$f2 = './t[1.txt'
New-Item -Type File $f1 -Value "'hi'"
}
# Passes now, due to fix in #9202
It "Script file can be dot-sourced." {
. $f1 | Should -Be 'hi'
}
# Passes now, due to fix in #9202
It "Script file can be called." {
& $f1 | Should -Be 'hi'
}
# Passes now, due to fix in #9202
It "Script file can be called via CLI with -File" {
pwsh -noprofile -file $f1 | Should -Be 'hi'
}
It "Redirection works with new file (>)" {
& { 'hi' > $f2; Get-Content -LiteralPath $f2 } | Should -Be 'hi'
}
It "Redirection works with existing file (>)" {
New-Item -Force -Type File $f2
& { 'hi' > $f2; Get-Content -LiteralPath $f2 } | Should -Be 'hi'
}
It "File path is passed correctly to external programs" {
if ($IsWindows) {
cmd /c type ($f1 -replace '/', '\') | Should -Be "'hi'"
} else {
/bin/cat $f1 | Should -Be "'hi'"
}
}
AfterAll {
Pop-Location
}
}Expected behavior
All tests should pass.
Actual behavior
The > tests fail on all platforms.
In v6.2.3, the invocation tests fail too.
Environment data
PowerShell Core 7.0.0-preview.5Originally reported for PowerShell Core v6.0.0-beta.7.
Activity
[-]Scripts with wildcard metacharacters [ and ] in the file name or path cannot be invoked or redirected to[/-][+]Some scripts / files with wildcard metacharacter [ in the file name or path cannot be invoked or redirected to[/+]iSazonov commentedon Sep 4, 2017
It seems we should escape (try tab completion):
mklement0 commentedon Sep 4, 2017
No, I think there is no reason to attempt wildcard pattern matching at all in all these scenarios - the paths should be taken as literals, which obviates the need for escaping.
iSazonov commentedon Sep 4, 2017
I discover this in #4699 and we can see this in https://github.com/iSazonov/PowerShell/blob/c1b300caeef8e242e484eb827667931fde714796/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1#L752
mklement0 commentedon Sep 4, 2017
When tab-completing for parameters that support wildcards, the escaping you mention makes sense:
Since there is no good reason to treat the file path in scenarios such as
. <file-path>as a wildcard pattern:tab completion should work the same way it does with
Get-Item -LiteralPath- treat it as a literal.and execution of such a path should obviously succeed.
iSazonov commentedon Sep 4, 2017
Here can be a root of the bug.
daxian-dbw commentedon Sep 16, 2017
Michael Klement (@mklement0) The file redirection is broken for this scenario, but the invocations seem working fine on
beta.7:The
$PSVersion:Am I missing something?
mklement0 commentedon Sep 18, 2017
Dongbo Wang (@daxian-dbw):
Sorry, I didn't realize that the behavior varies with similar filenames present in the same directory.
I've updated the original post with scenarios that should fail predictably with initially empty directories, on both Windows and Unix platforms.
The key is to use something like
t[.ps1- something truly invalid if interpreted as a wildcard pattern.daxian-dbw commentedon Sep 18, 2017
Michael Klement (@mklement0) Thanks for the clarification!
TravisEz13 commentedon Mar 22, 2019
Michael Klement (@mklement0) This issue is about redirection. Which occurs at execution time, but it is not about what you execute. I think this would drop it out of the Defense in Depth category and into a functional issue. If you believe this is a security issue, Please report this issue to
secure@microsoft.comper the contribution guidelines and not via GitHub. If they determine that the issue, can be disclosed publicly, feel free to post security issues here.mklement0 commentedon Mar 22, 2019
Travis Plunk (@TravisEz13), quoting from the OP (emphasis added):
Similarly, the repro steps show execution commands.
So, as stated in #9202, the latter therefore addresses part of this issue, correct?
As for the other part of this issue, redirection: no, I don't think it is security-critical.
mklement0 commentedon Mar 22, 2019
(The execution issue is framed differently here, because my angle was the inability to execute, but my understanding is that #9202 will fix that too.)
TravisEz13 commentedon Mar 22, 2019
Yes... sorry... I was reading only the parts, not address by #9202 . I'm in a bit of a rush to get that PR done and some of the suggested changes have broken the PR. I'm trying to track back which change broke the PR.
4 remaining items
mklement0 commentedon Mar 22, 2019
I appreciate the pointers, Travis Plunk (@TravisEz13).
msftrncs commentedon Jul 24, 2019
Attempting to escape the wildcard characters from a redirection target argument is still broken. I ran in to this issue testing file path completion scenario's.
arberg commentedon Oct 25, 2019
I suspect this bug I found is this issue, but since I'm not sure I think its valuable if I add the details:
Somehow create the file:
c:\temp\ivydata-[2.4,).propertiespiping to the file in powershell does not work, it gives the same error.
ps: I found the file under c:\users<myname>.ivy2
mklement0 commentedon Oct 25, 2019
Thanks, arberg, but the behavior you're seeing is by design, because positionally passing a path to
Test-Pathimplicitly binds to the-Pathparameter, which by design interprets its argument as a wildcard expression. If you useTest-Path -LiteralPath "c:\temp\ivydata-[2.4,).properties", the problem goes away.By contrast, the issue at hand is that when you use
>/>>you (a) don't get a choice about how the path is interpreted and (b) the implied behavior should be the same as with-LiteralPath, i.e. interpretation as a literal path.In the past, the same problem existed for invoking files, but that has been fixed in #9202 (though not back-ported to 6.2.3).
I've updated the OP with the current bug status and Pester tests.
msftrncs commentedon Nov 7, 2019
Here is the code for the redirection:
PowerShell/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs
Lines 1153 to 1173 in d58a82a
Of course, changing this, makes it a breaking change.
msftrncs commentedon Nov 7, 2019
Also, it really seems like its
Out-Filethat is broken. Its not processing wildcard paths correctly, when they have been escaped. Doesn't seem to be something specific to Out-File, as it calls the LocationGlobber to handle wildcards ...mklement0 commentedon Nov 7, 2019
Thanks for the pointer, Carl Morris (@msftrncs):
As for broken escaping in wildcards - see #7999
As for making
>/>>bind toOut-File's-LiteralPathparameter instead being a breaking change:Technically, yes, but the current behavior is unexpected, always fraught and mostly useless:
It is fraught, because the file getting targeted after matching the wildcard may not be the one you intended to target. (And, of course, if no file matches, you'll get an error.)
It is mostly useless in that you can't target multiple files with a wildcard pattern.
Set-Content, which I feel should switch to treating its paths as literals too - see Set-Content should not allow writing to multiple files using wildcards #6729 and Set/Add-Content doesn't work with paths with escaped wildcards characters #9588microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.