From 638bff0ac3cfdd5608b89cfa75973ae8d30bd66b Mon Sep 17 00:00:00 2001 From: KirtiRamchandani Date: Sun, 24 May 2026 12:49:22 +0530 Subject: [PATCH] Bound member invocation logging argument strings --- .../engine/runtime/Operations/MiscOps.cs | 21 +++++++++++++-- .../Logging/MemberInvocationLogging.Tests.ps1 | 27 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 test/powershell/engine/Logging/MemberInvocationLogging.Tests.ps1 diff --git a/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs b/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs index d584666ab62..74d434ed80f 100644 --- a/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs +++ b/src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs @@ -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), + "...".AsSpan()); + } + private static string ArgumentToString(object arg) { object baseObj = PSObject.Base(arg); @@ -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 @@ -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; diff --git a/test/powershell/engine/Logging/MemberInvocationLogging.Tests.ps1 b/test/powershell/engine/Logging/MemberInvocationLogging.Tests.ps1 new file mode 100644 index 00000000000..fe93994ce28 --- /dev/null +++ b/test/powershell/engine/Logging/MemberInvocationLogging.Tests.ps1 @@ -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 '' + } +}