Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public string[] LiteralPath
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>
Expand All @@ -61,7 +68,7 @@ protected override void ProcessRecord()
var data = ast.Find(a => a is HashtableAst, false);
if (data != null)
{
WriteObject(data.SafeGetValue());
WriteObject(data.SafeGetValue(SkipLimitCheck));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ internal enum SafeValueContext
{
Default,
GetPowerShell,
ModuleAnalysis
ModuleAnalysis,
SkipHashtableSizeCheck,
}

// future proofing
Expand All @@ -371,7 +372,8 @@ private GetSafeValueVisitor() { }
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());
}
Expand Down
15 changes: 14 additions & 1 deletion src/System.Management.Automation/engine/parser/ast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ public override string ToString()
/// 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
{
Expand All @@ -204,7 +217,7 @@ public object SafeGetValue()
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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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 } |
Expand Down Expand Up @@ -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
}
}