-
Notifications
You must be signed in to change notification settings - Fork 8.1k
CommandSearcher: treat invalid wildcard path as literal #7399
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 all commits
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 |
|---|---|---|
|
|
@@ -314,6 +314,70 @@ public static bool ContainsWildcardCharacters(string pattern) | |
| return result; | ||
| } | ||
|
|
||
| /// FIXME | ||
| /// For invalid wildcard pattern, it currently only check if there is | ||
| /// unclosed bracket. Need to find a better solution and rewrite this. | ||
| /// | ||
| /// <summary> | ||
| /// Checks to see if the given string might contains wildcard pattern. | ||
| /// </summary> | ||
| /// <param name="pattern"> | ||
| /// String which needs to be checked | ||
| /// </param> | ||
| /// <returns> | ||
| /// true if the string does not contain invalid wildcard pattern, | ||
| /// false otherwise. | ||
| /// </returns> | ||
| /// <remarks> | ||
| /// Currently string contains { '*', '?' } or both { '[', ']' } is | ||
| /// considered to be properly a wildcard pattern and | ||
| /// '`' is the escape character. | ||
| /// </remarks> | ||
| internal static bool ContainsValidWildcardPattern(string pattern) | ||
| { | ||
| if (string.IsNullOrEmpty(pattern)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| bool hasWildcard = false; | ||
| bool openBracket = false; | ||
|
|
||
| for (int index = 0; index < pattern.Length; ++index) | ||
| { | ||
| switch (pattern[index]) | ||
|
||
| { | ||
| case '*': | ||
| case '?': | ||
| hasWildcard = true; | ||
| break; | ||
|
|
||
| case '[': | ||
| hasWildcard = true; | ||
|
||
| openBracket = true; | ||
| break; | ||
|
|
||
| case ']': | ||
| // ']' is wildcard only if '[' exists | ||
| // so we do not set hasWildcard here | ||
| openBracket = false; | ||
| break; | ||
kwkam marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // If it is an escape character then advance past | ||
| // the next character | ||
| case escapeChar: | ||
| ++index; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (openBracket) { | ||
| return false; | ||
| } | ||
|
|
||
| return hasWildcard; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Unescapes any escaped characters in the input string. | ||
| /// </summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,13 @@ Describe "Tests Get-Command with relative paths and wildcards" -Tag "CI" { | |
| # Create temporary EXE command files | ||
| $file1 = Setup -f WildCardCommandA.exe -pass | ||
| $file2 = Setup -f WildCardCommand[B].exe -pass | ||
| $file3 = Setup -f [.exe -pass | ||
|
||
| #$null = New-Item -ItemType File -Path (Join-Path $TestDrive WildCardCommandA.exe) -ErrorAction Ignore | ||
| #$null = New-Item -ItemType File -Path (Join-Path $TestDRive WildCardCommand[B].exe) -ErrorAction Ignore | ||
| if ( $IsLinux -or $IsMacOS ) { | ||
| /bin/chmod a+rw "$file1" | ||
| /bin/chmod a+rw "$file2" | ||
| /bin/chmod a+rw "$file3" | ||
| } | ||
| $commandInfo = Get-Command Get-Date -ShowCommandInfo | ||
| } | ||
|
|
@@ -54,6 +56,11 @@ Describe "Tests Get-Command with relative paths and wildcards" -Tag "CI" { | |
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -Be WildCardCommand[B].exe | ||
|
|
||
| # This should find the file [.exe | ||
| $result = Get-Command -Name .\[.exe -Type Application | ||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeExactly [.exe | ||
|
|
||
| Pop-Location | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of extra cases this method should reject:
"[a-mt]ook""[ab-z]ook""[a?b]""[a*]x""goose]"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth putting these into test cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused now. All patterns suggested in the list are valid, would you please explain why should
ContainsValidWildcardPatternreject them?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"goose]"is a not a pattern, even though it's valid (not throw when callingWildCardPatter.IsMatch)