-
Notifications
You must be signed in to change notification settings - Fork 104
Update PowerShell dev actions and namespace handlers #2849
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
Open
Hillary Mutisya (hillary-mutisya)
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
hillary-mutisya:actionMode2_part3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6eca9a8
Update PowerShell dev actions and namespace handlers
hillary-mutisya 6912220
Potential fix for pull request finding
hillary-mutisya d8094b8
Fix copilot review feedback
hillary-mutisya da5dffe
Update powershell tests to handle short path aliases present on CI ma…
hillary-mutisya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,44 @@ param( | |
|
|
||
| $ErrorActionPreference = 'Stop' | ||
|
|
||
| function Remove-TrailingDirectorySeparator { | ||
| param([string]$Path) | ||
|
|
||
| $root = [System.IO.Path]::GetPathRoot($Path) | ||
| if ($Path.Equals($root, [System.StringComparison]::OrdinalIgnoreCase)) { | ||
| return $root | ||
| } | ||
| return $Path.TrimEnd('\', '/') | ||
| } | ||
|
|
||
| function Get-CanonicalFileSystemPath { | ||
| param([string]$Path) | ||
|
|
||
| $fullPath = [System.IO.Path]::GetFullPath($Path) | ||
| if (Test-Path -LiteralPath $fullPath) { | ||
| $item = Get-Item -LiteralPath $fullPath -Force | ||
| return Remove-TrailingDirectorySeparator $item.FullName | ||
| } | ||
|
|
||
| $missingSegments = [System.Collections.Generic.List[string]]::new() | ||
| $existingPath = $fullPath | ||
| while (-not (Test-Path -LiteralPath $existingPath)) { | ||
| $leaf = Split-Path -Leaf $existingPath | ||
| $parent = Split-Path -Parent $existingPath | ||
| if (-not $leaf -or -not $parent -or $parent -eq $existingPath) { | ||
| throw "Unable to resolve path '$Path'." | ||
| } | ||
| $missingSegments.Insert(0, $leaf) | ||
| $existingPath = $parent | ||
| } | ||
|
|
||
| $canonicalPath = (Get-Item -LiteralPath $existingPath -Force).FullName | ||
| foreach ($segment in $missingSegments) { | ||
| $canonicalPath = Join-Path $canonicalPath $segment | ||
| } | ||
| return Remove-TrailingDirectorySeparator ([System.IO.Path]::GetFullPath($canonicalPath)) | ||
| } | ||
|
|
||
| try { | ||
| $allowedCmdlets = $AllowedCmdletsJson | ConvertFrom-Json | ||
| $params = $ParametersJson | ConvertFrom-Json | ||
|
|
@@ -50,9 +88,11 @@ try { | |
| $expandedAllowedPaths = @() | ||
| foreach ($ap in $AllowedPaths) { | ||
| try { | ||
| $expandedAllowedPaths += $ExecutionContext.InvokeCommand.ExpandString($ap) | ||
| $expandedPath = $ExecutionContext.InvokeCommand.ExpandString($ap) | ||
| $expandedAllowedPaths += Get-CanonicalFileSystemPath $expandedPath | ||
| } catch { | ||
| $expandedAllowedPaths += $ap | ||
| Write-Error "Invalid allowed path '$ap': $_" | ||
| exit 1 | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -73,26 +113,28 @@ try { | |
| continue | ||
| } | ||
|
|
||
| $isValidPath = $false | ||
| try { $isValidPath = Test-Path $val -IsValid } catch { } | ||
| if ($isValidPath) { | ||
| $resolvedPath = $null | ||
| try { $resolvedPath = (Resolve-Path $val -ErrorAction SilentlyContinue).Path } catch {} | ||
| if ($resolvedPath) { | ||
| $pathAllowed = $false | ||
| foreach ($ap in $expandedAllowedPaths) { | ||
| if ($resolvedPath -like "$ap*") { | ||
| $pathAllowed = $true | ||
| break | ||
| } | ||
| } | ||
| # ENFORCEMENT: Block execution if path not allowed | ||
| if (-not $pathAllowed) { | ||
| Write-Error "Path access denied: '$resolvedPath' is not in allowedPaths. Allowed paths: $($expandedAllowedPaths -join ', ')" | ||
| exit 1 | ||
| } | ||
| try { | ||
| $resolvedPath = Get-CanonicalFileSystemPath $val | ||
|
Contributor
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 work for URL's in data like |
||
| } catch { | ||
| Write-Error "Invalid path parameter '$($prop.Name)': $_" | ||
| exit 1 | ||
| } | ||
| $pathAllowed = $false | ||
| foreach ($ap in $expandedAllowedPaths) { | ||
| if ( | ||
| $resolvedPath.Equals($ap, [System.StringComparison]::OrdinalIgnoreCase) -or | ||
| $resolvedPath.StartsWith("$ap\", [System.StringComparison]::OrdinalIgnoreCase) -or | ||
| $resolvedPath.StartsWith("$ap/", [System.StringComparison]::OrdinalIgnoreCase) | ||
| ) { | ||
| $pathAllowed = $true | ||
| break | ||
| } | ||
| } | ||
| # ENFORCEMENT: Block execution if path not allowed | ||
| if (-not $pathAllowed) { | ||
| Write-Error "Path access denied: '$resolvedPath' is not in allowedPaths. Allowed paths: $($expandedAllowedPaths -join ', ')" | ||
| exit 1 | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Copilot highlighted a concern here that Start-Process accepts executable names and resolves them through PATH. So executables outside of allowedPaths can still be run here for example :
Path = "powershell.exe" with Arguments = "-NoProfile -Command Get-Process"It suggests explicitly declaring which action parameters represent paths. This will allow executable parameters to be resolved to canonical paths and validated, while parameters representing things like library names can properly skipped and not marked as a path.