Skip to content

fix: the recorded command no longer renders an object through its own ToString() - #2780

Merged
merill merged 1 commit into
maester365:mainfrom
earbona23:fix/invokecommand-tostring
Nov 11, 2026
Merged

merill merged 1 commit into
maester365:mainfrom
earbona23:fix/invokecommand-tostring

Conversation

@earbona23

@earbona23 earbona23 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Separate PR for the ToString() exposure @SamErde raised while reviewing #2635, as offered there. The defect is on main and predates that PR; -Config just made it reachable with an object the caller controls.

What happens today

Invoke-Maester records the command that produced a run. That string is stored in the result file and shown in the report. Every bound parameter that is not a switch, array or string goes through string interpolation, which calls the object's own ToString():

} elseif ($null -ne $paramValue) {
    $invokeMaesterCommand += " -$paramName $paramValue"
}

The object decides what that returns. Reproduced:

class Leaky { [string] $Secret = 'p@ssw0rd-123'; [string] ToString() { return "Config(ApiKey=$($this.Secret))" } }

Invoke-Maester -Config Config(ApiKey=p@ssw0rd-123)

The array branch had the same exposure for a different reason — -join calls ToString() on every element.

What this changes

Rendering moves to ConvertTo-MtCommandParameterValue, one function per file next to ConvertFrom-MtJsonArray:

Value Recorded as
'Entra' 'Entra'
42, $true (value types) 42, True
@('Entra','Teams') @('Entra', 'Teams')
any other object <TypeName>
$null empty, as before

Nothing readable is lost: strings and value types are every parameter type that currently produces useful output. Invoke-Maester's loop drops from four branches to two.

Verification

Run through the repository's own runner on its pinned Pester, not a direct Invoke-Pester:

./powershell/tests/pester.ps1 -Include 'ConvertTo-MtCommandParameterValue*','FileEncoding*'
  ConvertTo-MtCommandParameterValue.Tests.ps1   8 passed
  FileEncoding.Tests.ps1                        5 passed

./powershell/tests/pester.ps1          (full suite)
  All 8516 tests executed without a single failure!

The tests discriminate rather than passing for free — with the protection removed, exactly the two cases that assert the type name instead of the content fail:

proteccion quitada  ->  Tests Passed: 6, Failed: 2
restaurada          ->  Tests Passed: 8, Failed: 0

@SamErde — this is the one I said I would send. Happy to fold it into #2635 instead if you would rather have it there.

Summary by CodeRabbit

  • Bug Fixes
    • Improved command logging for parameter values, including arrays and nulls.
    • Prevented object contents, including secret-bearing values, from appearing in logged commands.
  • Tests
    • Added coverage for how common values and arbitrary objects are represented in command logs.

… 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 maester365#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.
@earbona23
earbona23 requested a review from a team as a code owner November 10, 2026 12:44
@github-actions

Copy link
Copy Markdown
Contributor

This pull request changes files that a website explainer describes:

  • What happens in a run (website/public/explainers/run/) describes powershell/public/run/Invoke-Maester.ps1
  • How Maester works (website/public/explainers/how-it-works/) describes powershell/public/run/Invoke-Maester.ps1

If the change alters what the explainer shows (a gate, a reason code, a command, a count), update its composition and its chapters in website/src/data/explainers.json. If it doesn't, no action is needed.

@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

A new helper formats values for recorded command strings. Invoke-Maester now uses it for non-null, non-switch bound parameters. Tests cover scalar, array, null, and object rendering, including omission of sensitive fixture details.

Changes: Command parameter rendering

Layer / File(s) Summary
Parameter formatting and command-string integration
powershell/internal/utility/ConvertTo-MtCommandParameterValue.ps1, powershell/public/run/Invoke-Maester.ps1, powershell/tests/functions/ConvertTo-MtCommandParameterValue.Tests.ps1
The helper renders strings in quotes, value types as strings, arrays with @(...) syntax, and other objects by type name. Invoke-Maester uses the helper for non-null, non-switch bound parameters. Tests cover these formats and verify that secret-bearing object details do not appear in the output.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested labels: area: engine

Suggested reviewers: merill

Merge Risk: Minimal · up to ce48d

The recorded command is intended to avoid exposing object contents, and no actionable issue is established by the supplied evidence. The change appears mergeable after normal checks.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title clearly identifies the main change: preventing recorded commands from rendering objects through their own ToString() method.
Description check Passed The description explains the defect, the change, and the test results. It does not fill in the template’s issue-closure field or checkboxes, but the description is otherwise complete.
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 0 files. (3 skipped: 3 unsupported.)

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 80f8cb5 and ce48d18.

Files selected for processing: 3

  • powershell/internal/utility/ConvertTo-MtCommandParameterValue.ps1
  • powershell/public/run/Invoke-Maester.ps1
  • powershell/tests/functions/ConvertTo-MtCommandParameterValue.Tests.ps1

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@merill merill left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @earbona23, nice clean split from #2635. Approving.

Optional nit, which predates this PR: a single quote in a string value isn't escaped, so -Path "C:\it's" records as 'C:\it's' and won't paste back. Doubling it ($Value -replace "'", "''") in both the scalar and array branches would fix that. Fine to leave for another time.

@merill
merill merged commit 7cbc156 into maester365:main Nov 11, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants