From 4dc832f658fa7675c6a453c81bd52b64fc5e6c54 Mon Sep 17 00:00:00 2001 From: Raimund Andree Date: Mon, 5 Oct 2026 02:42:38 +0200 Subject: [PATCH] fix: keep paths with braces out of the message format strings The message overloads of BaseCmdletWithPrivControl hide the methods of Cmdlet, so every message went through string.Format, also one without arguments. A path with braces in it, such as C:\Data\{Archive}, then stopped the cmdlet with a FormatException (#3). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: AI Assistant --- CHANGELOG.md | 3 +++ NTFSSecurity/BaseCmdlets.cs | 8 +++++--- Tests/Access.Tests.ps1 | 9 +++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f4c0b7..3400c1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -169,5 +169,8 @@ The format is based on `RIPEMD160` and `MACTripleDES`, which .NET lacks there, now stop the cmdlet with an error that names the algorithm and points to Windows PowerShell 5.1 +- Fix a `FormatException` in the cmdlets for a path with braces, such as + `C:\Data\{Archive}`: their messages formatted the path a second time + ([#3](https://github.com/raandree/NTFSSecurity/issues/3)) [Unreleased]: https://github.com/raandree/NTFSSecurity/compare/4.2.6...HEAD diff --git a/NTFSSecurity/BaseCmdlets.cs b/NTFSSecurity/BaseCmdlets.cs index 8cdb84d..2aaf5da 100644 --- a/NTFSSecurity/BaseCmdlets.cs +++ b/NTFSSecurity/BaseCmdlets.cs @@ -337,18 +337,20 @@ namespace NTFSSecurity WriteDebug("The privilige 'Security' was disabled."); } + // These overloads hide the single-argument methods of Cmdlet, so a message without arguments must not be + // formatted: a path with braces in it would make string.Format throw (#3). protected void WriteWarning(string text, params string[] args) { - base.WriteWarning(string.Format(text, args)); + base.WriteWarning(args == null || args.Length == 0 ? text : string.Format(text, args)); } protected void WriteVerbose(string text, params string[] args) { - base.WriteVerbose(string.Format(text, args)); + base.WriteVerbose(args == null || args.Length == 0 ? text : string.Format(text, args)); } protected void WriteDebug(string text, params string[] args) { - base.WriteDebug(string.Format(text, args)); + base.WriteDebug(args == null || args.Length == 0 ? text : string.Format(text, args)); } } } \ No newline at end of file diff --git a/Tests/Access.Tests.ps1 b/Tests/Access.Tests.ps1 index 5582a81..1008201 100644 --- a/Tests/Access.Tests.ps1 +++ b/Tests/Access.Tests.ps1 @@ -94,6 +94,15 @@ Describe 'Get-NTFSEffectiveAccess' { } Describe 'Get-NTFSOrphanedAccess' { + # Before 5.0.0, a path with braces stopped the cmdlet with a FormatException (#3). + It 'Should read a folder whose name contains braces' { + $braces = Join-Path -Path $sandbox -ChildPath ('{{Braces}}-{0}' -f [guid]::NewGuid().ToString('N').Substring(0, 8)) + Assert-TestSandboxPath -Sandbox $sandbox -Path $braces + [IO.Directory]::CreateDirectory($braces) | Out-Null + + { Get-NTFSOrphanedAccess -Path $braces -ErrorAction Stop } | Should -Not -Throw + } + BeforeAll { $orphanedFile = New-TestSandboxItem -Sandbox $sandbox -Name 'Orphaned' Assert-TestSandboxPath -Sandbox $sandbox -Path $orphanedFile