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
14 changes: 9 additions & 5 deletions src/System.Management.Automation/engine/parser/SafeValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ public static bool IsAstSafe(Ast ast, GetSafeValueVisitor.SafeValueContext safeV
internal IsSafeValueVisitor(GetSafeValueVisitor.SafeValueContext safeValueContext)
{
_safeValueContext = safeValueContext;

bool skipSizeCheck = safeValueContext is GetSafeValueVisitor.SafeValueContext.SkipHashtableSizeCheck;
_maxVisitCount = skipSizeCheck ? uint.MaxValue : 5000;
_maxHashtableKeyCount = skipSizeCheck ? int.MaxValue : 500;
Comment thread
TravisEz13 marked this conversation as resolved.
}

internal bool IsAstSafe(Ast ast)
{
if ((bool)ast.Accept(this) && _visitCount < MaxVisitCount)
if ((bool)ast.Accept(this) && _visitCount < _maxVisitCount)
{
return true;
}
Expand All @@ -65,8 +69,8 @@ internal bool IsAstSafe(Ast ast)
// This is a check of the number of visits
private uint _visitCount = 0;

private const uint MaxVisitCount = 5000;
private const int MaxHashtableKeyCount = 500;
private readonly uint _maxVisitCount;
private readonly int _maxHashtableKeyCount;

// Used to determine if we are being called within a GetPowerShell() context,
// which does some additional security verification outside of the scope of
Expand Down Expand Up @@ -330,7 +334,7 @@ public object VisitArrayLiteral(ArrayLiteralAst arrayLiteralAst)

public object VisitHashtable(HashtableAst hashtableAst)
{
if (hashtableAst.KeyValuePairs.Count > MaxHashtableKeyCount)
if (hashtableAst.KeyValuePairs.Count > _maxHashtableKeyCount)
{
return false;
}
Expand Down Expand Up @@ -373,7 +377,7 @@ public static object GetSafeValue(Ast ast, ExecutionContext context, SafeValueCo
{
t_context = context;

if (safeValueContext == SafeValueContext.SkipHashtableSizeCheck || IsSafeValueVisitor.IsAstSafe(ast, safeValueContext))
if (IsSafeValueVisitor.IsAstSafe(ast, safeValueContext))
{
return ast.Accept(new GetSafeValueVisitor());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ Describe "Tests for the Import-PowerShellDataFile cmdlet" -Tags "CI" {
$result = Import-PowerShellDataFile $largePsd1Path -SkipLimitCheck
$result.Keys.Count | Should -Be 501
}

It 'Fails if psd1 file is insecure while -SkipLimitCheck is used' {
$path = Setup -f insecure2.psd1 -Content '@{ Foo = [object] (calc.exe) }' -pass
{ Import-PowerShellDataFile $path -SkipLimitCheck -ErrorAction Stop } |
Should -Throw -ErrorId "System.InvalidOperationException,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand"
}
}
Loading