fix: use PowerShell to parse PowerShell #7607
Open
+547
−115
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.
Previous to this PR, we used a hand-rolled PowerShell parser in
windows_safe_commands.rsto take a&strof PowerShell script see if it is equivalent to a list ofexecvp(3)invocations, and if so, we then test each usingis_safe_powershell_command()to determine if the overall command is safe:codex/codex-rs/core/src/command_safety/windows_safe_commands.rs
Lines 89 to 98 in 6e6338a
Unfortunately, our PowerShell parser did not recognize
@(...)as a special construct, so it was treated as an ordinary token. This meant that the following would erroneously be considered "safe:"The fix introduced in this PR is to do something comparable what we do for Bash/Zsh, which is to use a "proper" parser to derive the list of
execvp(3)calls. For Bash/Zsh, we rely on https://crates.io/crates/tree-sitter-bash, but there does not appear to be a crate of comparable quality for parsing PowerShell statically (https://github.com/airbus-cert/tree-sitter-powershell/ is the best thing I found).Instead, in this PR, we use a PowerShell script to parse the input PowerShell program to produce the AST.