From ce48d18bba5696361c33ff003c6d8214003e1aeb Mon Sep 17 00:00:00 2001 From: Eduard Arbona <175124143+earbona23@users.noreply.github.com> Date: Tue, 10 Nov 2026 08:44:16 -0400 Subject: [PATCH] fix: the recorded command no longer renders an object through its own ToString() Invoke-Maester records the command that produced a run, and that string is stored in the result file and shown in the report. Every bound parameter that was not a switch, array or string went through string interpolation, which calls the object's own ToString(). A type supplied by the caller decides what that returns, so -Config with an object whose ToString() reports its contents put those contents in the recorded command: class Leaky { [string] $Secret = 'p@ssw0rd-123'; [string] ToString() { "Config(ApiKey=$($this.Secret))" } } Invoke-Maester -Config Config(ApiKey=p@ssw0rd-123) The array branch had the same exposure: -join calls ToString() on every element. Rendering now lives in ConvertTo-MtCommandParameterValue, one function per file next to ConvertFrom-MtJsonArray. Strings stay quoted and value types are written as they are, so nothing readable is lost; any other object is recorded as its type name in angle brackets. Invoke-Maester's loop drops from four branches to two. Raised by @SamErde while reviewing #2635, where -Config made it reachable. The defect is older than that PR and is on main, which is why this is separate. Verified with the repo's runner on its pinned Pester: 8 of 8 for the new tests, and the full suite at 8,516 passing. Removing the protection fails exactly the two cases that assert the type name instead of the content, so the tests discriminate. --- .../ConvertTo-MtCommandParameterValue.ps1 | 48 +++++++++++++++++ powershell/public/run/Invoke-Maester.ps1 | 12 ++--- ...onvertTo-MtCommandParameterValue.Tests.ps1 | 52 +++++++++++++++++++ 3 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 powershell/internal/utility/ConvertTo-MtCommandParameterValue.ps1 create mode 100644 powershell/tests/functions/ConvertTo-MtCommandParameterValue.Tests.ps1 diff --git a/powershell/internal/utility/ConvertTo-MtCommandParameterValue.ps1 b/powershell/internal/utility/ConvertTo-MtCommandParameterValue.ps1 new file mode 100644 index 0000000000..394d9da647 --- /dev/null +++ b/powershell/internal/utility/ConvertTo-MtCommandParameterValue.ps1 @@ -0,0 +1,48 @@ +function ConvertTo-MtCommandParameterValue { + <# + .SYNOPSIS + Renders one bound parameter value for the recorded Invoke-Maester command string. + + .DESCRIPTION + Strings are quoted and value types are written as they are. Anything else is rendered as + its type name in angle brackets, never its content: interpolating an arbitrary object calls + that object's own ToString(), and a type supplied by the caller can use that to place its + contents - a connection string, an API key, a token - into a command string that is stored + in the result file and shown in the report. + + A $null element inside an array renders as an empty string, matching what the previous + inline join produced. + + .EXAMPLE + ConvertTo-MtCommandParameterValue -Value 'Entra' + + Returns 'Entra' in quotes. + + .EXAMPLE + ConvertTo-MtCommandParameterValue -Value $config + + Returns rather than whatever $config says about itself. + #> + [CmdletBinding()] + [OutputType([string])] + param( + [Parameter()] + [AllowNull()] + [object] $Value + ) + + if ($null -eq $Value) { return '' } + if ($Value -is [string]) { return "'$Value'" } + if ($Value -is [ValueType]) { return "$Value" } + + if ($Value -is [array]) { + $items = @($Value | ForEach-Object { + if ($null -eq $_) { '' } + elseif ($_ -is [string] -or $_ -is [ValueType]) { "$_" } + else { "<$($_.GetType().Name)>" } + }) + return "@('$($items -join "', '")')" + } + + return "<$($Value.GetType().Name)>" +} diff --git a/powershell/public/run/Invoke-Maester.ps1 b/powershell/public/run/Invoke-Maester.ps1 index fa65675ee3..16c4f239df 100644 --- a/powershell/public/run/Invoke-Maester.ps1 +++ b/powershell/public/run/Invoke-Maester.ps1 @@ -1017,7 +1017,11 @@ Set-MtConsolePhase 'Results' Write-MtProgress -Activity 'Processing test results' -Status "$(@($pesterResults.Tests).Count + $nativeRows.Count) test(s)" -Force - # Build the Invoke-Maester command string from bound parameters + # Build the Invoke-Maester command string from bound parameters. + # Only strings and value types are written out. Any other object is recorded as its + # type name, because interpolating it calls its own ToString(), and a caller-supplied + # type can use that to place its contents - a connection string, a token - into a + # command string that is then stored in the result and the report. $invokeMaesterCommand = "Invoke-Maester" foreach ($param in $PSBoundParameters.GetEnumerator()) { $paramName = $param.Key @@ -1026,12 +1030,8 @@ if ($paramValue.IsPresent) { $invokeMaesterCommand += " -$paramName" } - } elseif ($paramValue -is [array]) { - $invokeMaesterCommand += " -$paramName @('$($paramValue -join "', '")')" - } elseif ($paramValue -is [string]) { - $invokeMaesterCommand += " -$paramName '$paramValue'" } elseif ($null -ne $paramValue) { - $invokeMaesterCommand += " -$paramName $paramValue" + $invokeMaesterCommand += " -$paramName $(ConvertTo-MtCommandParameterValue -Value $paramValue)" } } diff --git a/powershell/tests/functions/ConvertTo-MtCommandParameterValue.Tests.ps1 b/powershell/tests/functions/ConvertTo-MtCommandParameterValue.Tests.ps1 new file mode 100644 index 0000000000..35a51b0f9e --- /dev/null +++ b/powershell/tests/functions/ConvertTo-MtCommandParameterValue.Tests.ps1 @@ -0,0 +1,52 @@ +BeforeAll { + Import-Module $PSScriptRoot/../../Maester.psd1 -Force + . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" + Use-MtModuleFunction -Name ConvertTo-MtCommandParameterValue + + class MtLeakyParameter { + [string] $ConnectionString = 'Server=db;Password=hunter2' + [string] ToString() { return "Config($($this.ConnectionString))" } + } +} + +Describe 'ConvertTo-MtCommandParameterValue' { + It 'Quotes a string' { + ConvertTo-MtCommandParameterValue -Value 'Entra' | Should -BeExactly "'Entra'" + } + + It 'Writes a as it is' -ForEach @( + @{ Kind = 'number'; Value = 42; Expected = '42' } + @{ Kind = 'boolean'; Value = $true; Expected = 'True' } + ) { + ConvertTo-MtCommandParameterValue -Value $Value | Should -BeExactly $Expected + } + + It 'Renders an arbitrary object as its type name, never its ToString()' { + # The recorded command is written to the result file and shown in the report. Interpolating + # an object calls its own ToString(), so a caller-supplied type could place a secret there. + $result = ConvertTo-MtCommandParameterValue -Value ([MtLeakyParameter]::new()) + + $result | Should -BeExactly '' + $result | Should -Not -Match 'hunter2' + $result | Should -Not -Match 'ConnectionString' + } + + It 'Keeps the array form for a string array' { + ConvertTo-MtCommandParameterValue -Value @('Entra', 'Teams') | Should -BeExactly "@('Entra', 'Teams')" + } + + It 'Renders an object inside an array as its type name too' { + $result = ConvertTo-MtCommandParameterValue -Value @('Entra', [MtLeakyParameter]::new()) + + $result | Should -BeExactly "@('Entra', '')" + $result | Should -Not -Match 'hunter2' + } + + It 'Returns an empty string for null' { + ConvertTo-MtCommandParameterValue -Value $null | Should -BeExactly '' + } + + It 'Renders a null element inside an array as an empty string' { + ConvertTo-MtCommandParameterValue -Value @('Entra', $null) | Should -BeExactly "@('Entra', '')" + } +}