Update MaxVisitCount and MaxHashtableKeyCount if VisitorSafeValueContext indicates SkipLimitCheck is true#27306
Merged
TravisEz13 merged 1 commit intoPowerShell:masterfrom Apr 20, 2026
Conversation
… `VisitorSafeValueContext` indicates `SkipLimitCheck` is true Update MaxVisitCount and MaxHashtableKeyCount if visitor safe value context indicates SkipLimitCheck is true Related work items: #163537 ---- #### AI description (iteration 1) #### PR Classification Enhancement: Conditionally update AST limit checks based on safe value context flags. #### PR Summary This pull request refactors the safe value visitor to initialize runtime limit values, setting them to maximum if the `SkipLimitCheck` flag is present, and adjusts the corresponding condition checks to support larger AST structures safely. - `src/System.Management.Automation/engine/parser/SafeValues.cs`: Replaced hard-coded limit constants with runtime-initialized readonly fields based on the safe value context, and updated conditional checks in `IsAstSafe` and `VisitHashtable`. - `test/powershell/Modules/Microsoft.PowerShell.Utility/PowerShellData.tests.ps1`: Added tests to validate behavior when using `-SkipLimitCheck`, ensuring insecure PSD1 files are properly rejected. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot --> Related work items: #163537
Contributor
There was a problem hiding this comment.
Pull request overview
Updates PowerShell’s “safe value” AST validation so that when Import-PowerShellDataFile -SkipLimitCheck is used, the size limits are relaxed without bypassing the security validation that prevents unsafe expressions from being evaluated.
Changes:
- Make
IsSafeValueVisitorapply dynamic max visit/key-count limits based onSafeValueContext(instead of always using constants). - Remove the
SkipHashtableSizeCheckfast-path that previously bypassedIsAstSafeentirely. - Add a regression test ensuring insecure PSD1 content still fails when
-SkipLimitCheckis used.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/System.Management.Automation/engine/parser/SafeValues.cs |
Ensures “skip limit” relaxes size limits via visitor configuration while still enforcing AST safety checks. |
test/powershell/Modules/Microsoft.PowerShell.Utility/PowerShellData.tests.ps1 |
Adds coverage for insecure PSD1 input with -SkipLimitCheck. |
TravisEz13
approved these changes
Apr 20, 2026
daxian-dbw
approved these changes
Apr 20, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR Summary
Looks like I cherry picked a different porting and it erased the correct attribution, credit to @anamnavi for the fix.
PR Context
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header