From 01426b276337b455fab79d0657a439658634f9a4 Mon Sep 17 00:00:00 2001 From: Kev Ritchie <65573253+KevRitchie@users.noreply.github.com> Date: Thu, 28 Oct 2021 19:56:55 +0100 Subject: [PATCH] Fix `ConvertTo-Json -Depth` to allow 100 at maximum (#16197) --- .../utility/WebCmdlet/ConvertToJsonCommand.cs | 22 ++----------------- .../resources/WebCmdletStrings.resx | 3 --- .../Json.Tests.ps1 | 4 ++-- 3 files changed, 4 insertions(+), 25 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs index 36e8acdfd6..2e33a10096 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -27,15 +27,13 @@ namespace Microsoft.PowerShell.Commands private int _depth = 2; - private const int maxDepthAllowed = 100; - private readonly CancellationTokenSource _cancellationSource = new(); /// /// Gets or sets the Depth property. /// [Parameter] - [ValidateRange(0, int.MaxValue)] + [ValidateRange(0, 100)] public int Depth { get { return _depth; } @@ -99,23 +97,7 @@ namespace Microsoft.PowerShell.Commands _cancellationSource.Dispose(); } } - - /// - /// Prerequisite checks. - /// - protected override void BeginProcessing() - { - if (_depth > maxDepthAllowed) - { - string errorMessage = StringUtil.Format(WebCmdletStrings.ReachedMaximumDepthAllowed, maxDepthAllowed); - ThrowTerminatingError(new ErrorRecord( - new InvalidOperationException(errorMessage), - "ReachedMaximumDepthAllowed", - ErrorCategory.InvalidOperation, - null)); - } - } - + private readonly List _inputObjects = new(); /// diff --git a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx index e3c64bd5ca..3d66357301 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx +++ b/src/Microsoft.PowerShell.Commands.Utility/resources/WebCmdletStrings.resx @@ -237,9 +237,6 @@ Ensure 'Json.Net.psd1' and 'Newtonsoft.Json.dll' are available in a versioned subdirectory of '{0}'. - - The maximum depth allowed for serialization is {0}. - Conversion from JSON failed with error: {0} diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Json.Tests.ps1 index d3713963f9..5f33e1b6b7 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Json.Tests.ps1 @@ -1670,10 +1670,10 @@ Describe "Json Bug fixes" -Tags "Feature" { $testCases = @( @{ - Name = "ConvertTo-Json -Depth 101 throws MaximumAllowedDepthReached when the user specifies a depth greater than 100." + Name = "ConvertTo-Json -Depth 101 throws ParameterArgumentValidationError when the user specifies a depth greater than 100." NumberOfElements = 10 MaxDepth = 101 - FullyQualifiedErrorId = "ReachedMaximumDepthAllowed,Microsoft.PowerShell.Commands.ConvertToJsonCommand" + FullyQualifiedErrorId = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ConvertToJsonCommand" ShouldThrow = $true } @{