Browse Source

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 <ai@example.com>
pull/105/head
Raimund Andree 7 days ago
parent
commit
4dc832f658
  1. 3
      CHANGELOG.md
  2. 8
      NTFSSecurity/BaseCmdlets.cs
  3. 9
      Tests/Access.Tests.ps1

3
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

8
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));
}
}
}

9
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

Loading…
Cancel
Save