-
Notifications
You must be signed in to change notification settings - Fork 9.1k
fix(powershell): make CMDLET_PATH_CONFIG prototype-safe #1913
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
Merged
kevincodex1
merged 2 commits into
Twigpine:main
from
0xfandom:fix/powershell-cmdlet-path-config-proto
Jul 10, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { describe, expect, test } from 'bun:test' | ||
| import type { ToolPermissionContext } from '../../types/permissions.js' | ||
| import type { ParsedPowerShellCommand } from '../../utils/powershell/parser.js' | ||
| import { checkPathConstraints } from './pathValidation.js' | ||
|
|
||
| function defaultContext(): ToolPermissionContext { | ||
| return { | ||
| mode: 'default', | ||
| additionalWorkingDirectories: new Map(), | ||
| alwaysAllowRules: {}, | ||
| alwaysDenyRules: {}, | ||
| alwaysAskRules: {}, | ||
| isBypassPermissionsModeAvailable: false, | ||
| } | ||
| } | ||
|
|
||
| // Build a parsed command with a single cmdlet whose name we control. The real | ||
| // PowerShell AST parser requires a pwsh binary, so the parsed structure is | ||
| // constructed directly — the defect under test is in the CMDLET_PATH_CONFIG | ||
| // lookup keyed by the cmdlet name, downstream of parsing. A PowerShell command | ||
| // like `constructor -Path C:\x` parses to a command named `constructor` on a | ||
| // machine with pwsh installed. | ||
| function parsedCommandNamed(name: string): ParsedPowerShellCommand { | ||
| const command = `${name} -Path C:\\temp\\file.txt` | ||
| return { | ||
| valid: true, | ||
| errors: [], | ||
| variables: [], | ||
| hasStopParsing: false, | ||
| originalCommand: command, | ||
| statements: [ | ||
| { | ||
| statementType: 'PipelineAst', | ||
| text: command, | ||
| redirections: [], | ||
| commands: [ | ||
| { | ||
| name, | ||
| nameType: 'cmdlet', | ||
| elementType: 'CommandAst', | ||
| args: ['-Path', 'C:\\temp\\file.txt'], | ||
| text: command, | ||
| elementTypes: ['StringConstantExpressionAst', 'CommandParameterAst'], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| } as unknown as ParsedPowerShellCommand | ||
| } | ||
|
|
||
| // CMDLET_PATH_CONFIG is keyed by the (lowercased) cmdlet name. A command whose | ||
| // name collides with an inherited Object.prototype member must resolve to no | ||
| // config (unknown-cmdlet passthrough), not the inherited prototype value. Before | ||
| // the null-proto fix the lookup returned a truthy inherited member, so | ||
| // `[...config.knownSwitches]` threw (spread of undefined) and crashed | ||
| // path-constraint validation. `constructor` and `__proto__` below are | ||
| // representative all-lowercase collisions reachable through the lowercased key; | ||
| // the null-prototype container neutralizes every inherited-key lookup, not just | ||
| // these two. | ||
| describe('checkPathConstraints with prototype-polluting cmdlet names', () => { | ||
| test.each(['constructor', '__proto__'])( | ||
| 'treats %s as an unknown cmdlet instead of crashing', | ||
| (name) => { | ||
| const parsed = parsedCommandNamed(name) | ||
| expect(() => | ||
| checkPathConstraints( | ||
| { command: parsed.originalCommand }, | ||
| parsed, | ||
| defaultContext(), | ||
| ), | ||
| ).not.toThrow() | ||
| const result = checkPathConstraints( | ||
| { command: parsed.originalCommand }, | ||
| parsed, | ||
| defaultContext(), | ||
| ) | ||
| expect(result.behavior).toBe('passthrough') | ||
| }, | ||
| ) | ||
|
|
||
| test('a real path cmdlet is still classified (control)', () => { | ||
| const parsed = parsedCommandNamed('set-content') | ||
| // set-content is a known write cmdlet, so its config is found and the | ||
| // lookup does not fall through to the unknown-cmdlet branch — proves the | ||
| // null-proto container still holds and resolves its own entries. Its | ||
| // parameterized `-Path` cannot be statically validated, so the classified | ||
| // decision is `ask` rather than the `passthrough` returned for the | ||
| // prototype-key names above. | ||
| const result = checkPathConstraints( | ||
| { command: parsed.originalCommand }, | ||
| parsed, | ||
| defaultContext(), | ||
| ) | ||
| expect(result.behavior).toBe('ask') | ||
| }) | ||
| }) | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.