[release/v7.7.0-preview.1] Update MaxVisitCount and MaxHashtableKeyCount if VisitorSafeValueContext indicates SkipLimitCheck is true#27308
Conversation
…eContext` indicates `SkipLimitCheck` is true (PowerShell#27306) Co-authored-by: Dongbo Wang <dongbow@microsoft.com>
There was a problem hiding this comment.
Pull request overview
Backports an engine fix to ensure safe-value evaluation correctly honors -SkipLimitCheck by lifting the visit/key-count limits while still enforcing security checks.
Changes:
- Updates
IsSafeValueVisitorto make the max visit-count and max hashtable-key-count configurable perSafeValueContext, disabling those limits forSkipHashtableSizeCheck. - Removes the
SkipHashtableSizeCheckbypass that previously skipped all safety validation inGetSafeValue. - Adds a Pester test ensuring
Import-PowerShellDataFile -SkipLimitCheckstill rejects insecure PSD1 content.
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 |
Applies context-sensitive limits and ensures SkipHashtableSizeCheck only skips limit checks, not security validation. |
test/powershell/Modules/Microsoft.PowerShell.Utility/PowerShellData.tests.ps1 |
Adds regression coverage to confirm insecure PSD1 fails even with -SkipLimitCheck. |
| bool skipSizeCheck = safeValueContext is GetSafeValueVisitor.SafeValueContext.SkipHashtableSizeCheck; | ||
| _maxVisitCount = skipSizeCheck ? uint.MaxValue : 5000; | ||
| _maxHashtableKeyCount = skipSizeCheck ? int.MaxValue : 500; |
There was a problem hiding this comment.
The default limits (5000 visits / 500 hashtable keys) are now hard-coded literals in the constructor. Consider keeping these as named constants (e.g., DefaultMaxVisitCount / DefaultMaxHashtableKeyCount) to avoid magic numbers and to prevent future divergence if the limits change again.
| It 'Succeeds if -NoLimit is used and has more than 500 keys' { | ||
| $result = Import-PowerShellDataFile $largePsd1Path -SkipLimitCheck | ||
| $result.Keys.Count | Should -Be 501 |
There was a problem hiding this comment.
Test description says "-NoLimit" but the cmdlet parameter used here is -SkipLimitCheck. Updating the It description would reduce confusion when reading test output.
5b00d0f
into
PowerShell:release/v7.7.0-preview.1
Backport of #27306 to release/v7.7.0-preview.1
Triggered by @jshigetomi on behalf of @SeeminglyScience
Original CL Label: CL-Engine
/cc @PowerShell/powershell-maintainers
Impact
REQUIRED: Choose either Tooling Impact or Customer Impact (or both). At least one checkbox must be selected.
Tooling Impact
Customer Impact
Engine fix: ensures
MaxVisitCountandMaxHashtableKeyCountare updated whenVisitorSafeValueContextindicatesSkipLimitCheckis true. Without this fix, expression evaluation can hit visit/key-count limits in safe-value contexts that should bypass them. Already merged to release/v7.4.x, v7.5.x, and v7.6.x.Regression
REQUIRED: Check exactly one box.
This is not a regression.
Testing
Original PR was verified by tests added with the change and has been successfully backported to release/v7.4.x, v7.5.x, and v7.6.x branches (all marked Backport-*-Done). Cherry-pick to release/v7.7.0-preview.1 applied cleanly with no conflicts. CI on the backport branch will validate the change in the preview branch context.
Risk
REQUIRED: Check exactly one box.
Low risk: small, well-scoped engine change to limit-check handling in
VisitorSafeValueContext. The same change has already shipped successfully in v7.4, v7.5, and v7.6 release lines without issues. Cherry-pick was clean (no conflicts), so the code applied is identical to the already-validated versions.