Skip to content
Closed
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 @@ -3657,6 +3657,23 @@ internal static class MemberInvocationLoggingOps
);
#endif

private const int MaxLoggedArgumentStringLength = 4096;

private static string LimitLoggedArgumentString(string value)
{
if (value is null || value.Length <= MaxLoggedArgumentStringLength)
{
return value;
}

string originalLength = value.Length.ToString(CultureInfo.InvariantCulture);
return string.Concat(
value.AsSpan(0, MaxLoggedArgumentStringLength),
"...<truncated; original length: ".AsSpan(),
originalLength.AsSpan(),
">".AsSpan());
}

private static string ArgumentToString(object arg)
{
object baseObj = PSObject.Base(arg);
Expand All @@ -3669,7 +3686,7 @@ private static string ArgumentToString(object arg)
// The comparisons below are ordered by the likelihood of arguments being of those types.
if (baseObj is string str)
{
return str;
return LimitLoggedArgumentString(str);
}

// Special case some types to call 'ToString' on the object. For the rest, we return its
Expand All @@ -3683,7 +3700,7 @@ private static string ArgumentToString(object arg)
|| baseType == typeof(BigInteger)
|| baseType == typeof(decimal))
{
return baseObj.ToString();
return LimitLoggedArgumentString(baseObj.ToString());
}

return baseType.FullName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'Member invocation logging' -Tags 'CI' {
BeforeAll {
$type = [psobject].Assembly.GetType('System.Management.Automation.MemberInvocationLoggingOps')
$argumentToString = $type.GetMethod(
'ArgumentToString',
[System.Reflection.BindingFlags]'NonPublic, Static')
}

It 'Keeps short string arguments unchanged' {
$value = 'short argument'

$argumentToString.Invoke($null, [object[]]@($value)) | Should -BeExactly $value
}

It 'Limits long string arguments' {
$value = 'a' * 5000

$result = $argumentToString.Invoke($null, [object[]]@($value))

$result.Length | Should -BeLessThan $value.Length
$result.StartsWith(('a' * 4096), [System.StringComparison]::Ordinal) | Should -BeTrue
$result | Should -Match '<truncated; original length: 5000>'
}
}