Skip to content

[release/v7.7.0-preview.1] Update MaxVisitCount and MaxHashtableKeyCount if VisitorSafeValueContext indicates SkipLimitCheck is true#27308

Merged
jshigetomi merged 1 commit intoPowerShell:release/v7.7.0-preview.1from
jshigetomi:backport/release/v7.7.0-preview.1/27306-e2784b886
Apr 20, 2026
Merged

[release/v7.7.0-preview.1] Update MaxVisitCount and MaxHashtableKeyCount if VisitorSafeValueContext indicates SkipLimitCheck is true#27308
jshigetomi merged 1 commit intoPowerShell:release/v7.7.0-preview.1from
jshigetomi:backport/release/v7.7.0-preview.1/27306-e2784b886

Conversation

@jshigetomi
Copy link
Copy Markdown
Collaborator

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

  • Required tooling change
  • Optional tooling change (include reasoning)

Customer Impact

  • Customer reported
  • Found internally

Engine fix: ensures MaxVisitCount and MaxHashtableKeyCount are updated when VisitorSafeValueContext indicates SkipLimitCheck is 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.

  • Yes
  • No

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.

  • High
  • Medium
  • Low

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.

…eContext` indicates `SkipLimitCheck` is true (PowerShell#27306)

Co-authored-by: Dongbo Wang <dongbow@microsoft.com>
@jshigetomi jshigetomi added the CL-Engine Indicates that a PR should be marked as an engine change in the Change Log label Apr 20, 2026
Copilot AI review requested due to automatic review settings April 20, 2026 18:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 IsSafeValueVisitor to make the max visit-count and max hashtable-key-count configurable per SafeValueContext, disabling those limits for SkipHashtableSizeCheck.
  • Removes the SkipHashtableSizeCheck bypass that previously skipped all safety validation in GetSafeValue.
  • Adds a Pester test ensuring Import-PowerShellDataFile -SkipLimitCheck still 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.

Comment on lines +51 to +53
bool skipSizeCheck = safeValueContext is GetSafeValueVisitor.SafeValueContext.SkipHashtableSizeCheck;
_maxVisitCount = skipSizeCheck ? uint.MaxValue : 5000;
_maxHashtableKeyCount = skipSizeCheck ? int.MaxValue : 500;
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines 48 to 50
It 'Succeeds if -NoLimit is used and has more than 500 keys' {
$result = Import-PowerShellDataFile $largePsd1Path -SkipLimitCheck
$result.Keys.Count | Should -Be 501
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test description says "-NoLimit" but the cmdlet parameter used here is -SkipLimitCheck. Updating the It description would reduce confusion when reading test output.

Copilot uses AI. Check for mistakes.
@jshigetomi jshigetomi merged commit 5b00d0f into PowerShell:release/v7.7.0-preview.1 Apr 20, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-Engine Indicates that a PR should be marked as an engine change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants