Add -SkipLimitCheck switch to Import-PowerShellDataFile (#13672)

This commit is contained in:
Steve Lee 2020-12-09 11:07:07 -08:00 committed by GitHub
parent aecada3e67
commit 95ce0643c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 4 deletions

View file

@ -39,6 +39,13 @@ namespace Microsoft.PowerShell.Commands
set { _isLiteralPath = true; Path = value; }
}
/// <summary>
/// Gets or sets switch that determines if built-in limits are applied to the data.
/// </summary>
[Experimental("Microsoft.PowerShell.Utility.PSImportPSDataFileSkipLimitCheck", ExperimentAction.Show)]
[Parameter]
public SwitchParameter SkipLimitCheck { get; set; }
/// <summary>
/// For each path, resolve it, parse it and write all hashtables to the output stream.
/// </summary>
@ -61,7 +68,7 @@ namespace Microsoft.PowerShell.Commands
var data = ast.Find(a => a is HashtableAst, false);
if (data != null)
{
WriteObject(data.SafeGetValue());
WriteObject(data.SafeGetValue(SkipLimitCheck));
}
else
{

View file

@ -38,6 +38,10 @@ PrivateData = @{
Name = 'Microsoft.PowerShell.Utility.PSManageBreakpointsInRunspace'
Description = 'Enables -BreakAll parameter on Debug-Runspace and Debug-Job cmdlets to allow users to decide if they want PowerShell to break immediately in the current location when they attach a debugger. Enables -Runspace parameter on *-PSBreakpoint cmdlets to support management of breakpoints in another runspace.'
}
@{
Name = 'Microsoft.PowerShell.Utility.PSImportPSDataFileSkipLimitCheck'
Description = 'Enable -SkipLimitCheck switch for Import-PowerShellDataFile to not enforce built-in hashtable limits'
}
)
}
}

View file

@ -37,6 +37,10 @@ PrivateData = @{
Name = 'Microsoft.PowerShell.Utility.PSManageBreakpointsInRunspace'
Description = 'Enables -BreakAll parameter on Debug-Runspace and Debug-Job cmdlets to allow users to decide if they want PowerShell to break immediately in the current location when they attach a debugger. Enables -Runspace parameter on *-PSBreakpoint cmdlets to support management of breakpoints in another runspace.'
}
@{
Name = 'Microsoft.PowerShell.Utility.PSImportPSDataFileSkipLimitCheck'
Description = 'Enable -NoLimit switch for Import-PowerShellDataFile to not enforce built-in hashtable limits'
}
)
}
}

View file

@ -362,7 +362,8 @@ namespace System.Management.Automation.Language
{
Default,
GetPowerShell,
ModuleAnalysis
ModuleAnalysis,
SkipHashtableSizeCheck,
}
// future proofing
@ -371,7 +372,8 @@ namespace System.Management.Automation.Language
public static object GetSafeValue(Ast ast, ExecutionContext context, SafeValueContext safeValueContext)
{
t_context = context;
if (IsSafeValueVisitor.IsAstSafe(ast, safeValueContext))
if (safeValueContext == SafeValueContext.SkipHashtableSizeCheck || IsSafeValueVisitor.IsAstSafe(ast, safeValueContext))
{
return ast.Accept(new GetSafeValueVisitor());
}

View file

@ -198,6 +198,19 @@ namespace System.Management.Automation.Language
/// If <paramref name="extent"/> is deemed unsafe
/// </exception>
public object SafeGetValue()
{
return SafeGetValue(skipHashtableSizeCheck: false);
}
/// <summary>
/// Constructs the resultant object from the AST and returns it if it is safe.
/// </summary>
/// <param name="skipHashtableSizeCheck">Set to skip hashtable limit validation.</param>
/// <returns>The object represented by the AST as a safe object.</returns>
/// <exception cref="InvalidOperationException">
/// If <paramref name="extent"/> is deemed unsafe.
/// </exception>
public object SafeGetValue(bool skipHashtableSizeCheck)
{
try
{
@ -207,7 +220,7 @@ namespace System.Management.Automation.Language
context = System.Management.Automation.Runspaces.Runspace.DefaultRunspace.ExecutionContext;
}
return GetSafeValueVisitor.GetSafeValue(this, context, GetSafeValueVisitor.SafeValueContext.Default);
return GetSafeValueVisitor.GetSafeValue(this, context, skipHashtableSizeCheck ? GetSafeValueVisitor.SafeValueContext.SkipHashtableSizeCheck : GetSafeValueVisitor.SafeValueContext.Default);
}
catch
{

View file

@ -1,6 +1,19 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Tests for the Import-PowerShellDataFile cmdlet" -Tags "CI" {
BeforeAll {
$largePsd1Path = Join-Path -Path $TestDrive -ChildPath 'large.psd1'
$largePsd1Builder = [System.Text.StringBuilder]::new('@{')
1..501 | ForEach-Object {
$largePsd1Builder.Append("key$_ = $_;")
}
$largePsd1Builder.Append('}')
Set-Content -Path $largePsd1Path -Value $largePsd1Builder.ToString()
if ((Get-ExperimentalFeature Microsoft.PowerShell.Utility.PSImportPSDataFileSkipLimitCheck).Enabled -ne $true) {
$skipTest = $true
}
}
It "Validates error on a missing path" {
{ Import-PowerShellDataFile -Path /SomeMissingDirectory -ErrorAction Stop } |
@ -32,4 +45,12 @@ Describe "Tests for the Import-PowerShellDataFile cmdlet" -Tags "CI" {
$result.Hello | Should -BeExactly "World"
}
It 'Fails if psd1 file has more than 500 keys' {
{ Import-PowerShellDataFile $largePsd1Path } | Should -Throw -ErrorId 'System.InvalidOperationException,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand'
}
It 'Succeeds if -NoLimit is used and has more than 500 keys' -Skip:$skipTest {
$result = Import-PowerShellDataFile $largePsd1Path -SkipLimitCheck
$result.Keys.Count | Should -Be 501
}
}