Fix ConvertTo-Json -Depth to allow 100 at maximum (#16197)

This commit is contained in:
Kev Ritchie 2021-10-28 19:56:55 +01:00 committed by GitHub
parent f41b6b415e
commit 01426b2763
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 25 deletions

View file

@ -27,15 +27,13 @@ namespace Microsoft.PowerShell.Commands
private int _depth = 2;
private const int maxDepthAllowed = 100;
private readonly CancellationTokenSource _cancellationSource = new();
/// <summary>
/// Gets or sets the Depth property.
/// </summary>
[Parameter]
[ValidateRange(0, int.MaxValue)]
[ValidateRange(0, 100)]
public int Depth
{
get { return _depth; }
@ -99,23 +97,7 @@ namespace Microsoft.PowerShell.Commands
_cancellationSource.Dispose();
}
}
/// <summary>
/// Prerequisite checks.
/// </summary>
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<object> _inputObjects = new();
/// <summary>

View file

@ -237,9 +237,6 @@
<data name="JsonNetModuleFilesRequired" xml:space="preserve">
<value>Ensure 'Json.Net.psd1' and 'Newtonsoft.Json.dll' are available in a versioned subdirectory of '{0}'.</value>
</data>
<data name="ReachedMaximumDepthAllowed" xml:space="preserve">
<value>The maximum depth allowed for serialization is {0}.</value>
</data>
<data name="JsonDeserializationFailed" xml:space="preserve">
<value>Conversion from JSON failed with error: {0}</value>
</data>

View file

@ -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
}
@{