Title: Some scripts / files with wildcard metacharacter [ in the file name or path cannot be invoked or redirected to · Issue #4726 · PowerShell/PowerShell · GitHub
Open Graph Title: Some scripts / files with wildcard metacharacter [ in the file name or path cannot be invoked or redirected to · Issue #4726 · PowerShell/PowerShell
X Title: Some scripts / files with wildcard metacharacter [ in the file name or path cannot be invoked or redirected to · Issue #4726 · PowerShell/PowerShell
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 aspec...
Open Graph 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...
X 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...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/4726
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Some scripts / files with wildcard metacharacter [ in the file name or path cannot be invoked or redirected to","articleBody":"\u003csup\u003eEdited for the current status as of PowerShell Core 7.3.0-preview.4\u003c/sup\u003e\r\n\r\nNote: \r\n\r\n* Originally, this issue was also about the inability to _execute_ files whose literal names happen to be syntactically invalid wildcard patterns. \r\n\r\n* This execution aspect was _partially_ fixed in #9202 (but wasn't back-ported to the currently stable version, v6.2.3)\r\n * Invocation by wildcard pattern still works as a fallback, but paths with `[` and `]` that exist _literally_ now take precedence.\r\n * 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.ps1` is present in the current dir. and `.\\[ab].ps1` is invoked, with no file _literally_ named `[ab].ps1` present, `.\\a.ps1` is executed instead.)\r\n\r\nSeemingly, all external-executable invocation methods ( `.`, `\u0026`, `Start-Process`, `powershell -File`) as well as redirections `\u003e` / `\u003e\u003e` 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.\r\n\r\n* 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 invalid `b[b` does not.\r\n\r\n* But even with a syntactically valid range expression - and some invalid ones - the behavior is unexpected:\r\n\r\n * `'foo' \u003e 'file[1].txt'` attempts wildcard matching and complains, `Cannot perform operation because the wildcard path file[1].txt did not resolve to a file.`\r\n * Attempting _escaping_ then inappropriately retains the escape characters - see #9475\r\n\r\n* Attempting to invoke (open) such a file fails quietly, even with escaping (run `New-Item file[1].txt` first):\r\n * `./file[1].txt` ... quiet no-op\r\n * ``./file`[1`].txt`` ... quiet no-op\r\n * If you created a `.ps1` file named, say, `file[1].ps1` (`'\"hi\"' | Set-Content -LiteralPath file[1].ps1`):\r\n * Invocation with an explicit path unexpectedly only works if `.ps1` is also specified.\r\n * Invocation via $env:PATH doesn't work at all - neither with or without `.ps1`\r\n\r\n**Fundamentally, external-executable / redirection output paths should be treated as _literals_ in these situations.**\r\nSee [this comment](https://github.com/PowerShell/PowerShell/issues/17106#issuecomment-1088755835) for a discussion and an overview of related issues.\r\n\r\nOriginal tests below.\r\n\r\nSteps to reproduce\r\n------------------\r\n\r\nRun the following Pester tests.\r\n\r\n```powershell\r\nDescribe \"Redirection with and invocation of literal file paths that are invalid as wildcard patterns\" {\r\n\r\n BeforeAll {\r\n Push-Location testdrive:\\\r\n $f1 = './t[1.ps1'\r\n $f2 = './t[1.txt'\r\n New-Item -Type File $f1 -Value \"'hi'\"\r\n }\r\n\r\n # Passes now, due to fix in #9202\r\n It \"Script file can be dot-sourced.\" {\r\n . $f1 | Should -Be 'hi'\r\n }\r\n\r\n # Passes now, due to fix in #9202\r\n It \"Script file can be called.\" {\r\n \u0026 $f1 | Should -Be 'hi'\r\n }\r\n\r\n # Passes now, due to fix in #9202\r\n It \"Script file can be called via CLI with -File\" {\r\n pwsh -noprofile -file $f1 | Should -Be 'hi'\r\n }\r\n\r\n It \"Redirection works with new file (\u003e)\" {\r\n \u0026 { 'hi' \u003e $f2; Get-Content -LiteralPath $f2 } | Should -Be 'hi'\r\n }\r\n\r\n It \"Redirection works with existing file (\u003e)\" {\r\n New-Item -Force -Type File $f2\r\n \u0026 { 'hi' \u003e $f2; Get-Content -LiteralPath $f2 } | Should -Be 'hi'\r\n }\r\n\r\n It \"File path is passed correctly to external programs\" {\r\n if ($IsWindows) {\r\n cmd /c type ($f1 -replace '/', '\\') | Should -Be \"'hi'\"\r\n } else {\r\n /bin/cat $f1 | Should -Be \"'hi'\"\r\n }\r\n }\r\n\r\n AfterAll {\r\n Pop-Location\r\n }\r\n}\r\n```\r\n\r\nExpected behavior\r\n-----------------\r\n\r\nAll tests should pass.\r\n\r\nActual behavior\r\n---------------\r\n\r\nThe `\u003e` tests fail on all platforms.\r\n\r\nIn v6.2.3, the invocation tests fail too.\r\n\r\nEnvironment data\r\n----------------\r\n\r\n```powershell\r\nPowerShell Core 7.0.0-preview.5\r\n```\r\n\r\nOriginally reported for PowerShell Core v6.0.0-beta.7.\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2017-08-31T16:34:25.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":27},"url":"https://github.com/4726/PowerShell/issues/4726"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:cae5521d-9158-165f-f3e5-088303427de6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8260:223D7:9A49BE:D18F0B:6A581AE7 |
| html-safe-nonce | 5d10f4eea422b7d3d63c91a3ddc28e14eb6b1405474ec8debf836f7cd3f5ff13 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MjYwOjIyM0Q3OjlBNDlCRTpEMThGMEI6NkE1ODFBRTciLCJ2aXNpdG9yX2lkIjoiMjM4NjQ5ODE3MTc4NDY2NTgzMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 17dc9be3a574a4c45897a0a4127167e0492908c6cec593a000a365d8d71b2a39 |
| hovercard-subject-tag | issue:254396076 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/4726/issue_layout |
| twitter:image | https://opengraph.githubassets.com/66176b0a86c1639743d7c0f9efa443ea7b15cdb4ca70c82f6b236c6efdbe1f38/PowerShell/PowerShell/issues/4726 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/66176b0a86c1639743d7c0f9efa443ea7b15cdb4ca70c82f6b236c6efdbe1f38/PowerShell/PowerShell/issues/4726 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 49c8c15fabcbf356d607a90ca115c13b273e42ff8b74155de050fd229a9b0121 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 3fb1f684e7a833eb1b2d01d39875a2b52cb4fe9b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width