mirror of https://github.com/raandree/NTFSSecurity
Browse Source
Defect 9, both parts: - Get-NTFSOwner wrote each owner inside a try block whose catch-all turned every exception into a ReadSecurityError. When a command such as Select-Object -First 1 stopped the pipeline, the cmdlet wrote "The pipeline has been stopped" as an error for every path, and the stop escaped into the caller's script. The owner is now written after the try block. - After access was denied, the retry called the same failing GetOwner again before taking ownership, so it always failed and reported a WriteError. Taking ownership would also replace the owner that the cmdlet reports. The cmdlet now writes a ReadSecurityError with the category PermissionDenied and continues. Tests/Owner.Tests.ps1 (new): 2 tests; the access-denied test skips where the Backup privilege may bypass the deny entry (CI). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: AI Assistant <ai@example.com>pull/100/head
5 changed files with 75 additions and 20 deletions
@ -0,0 +1,54 @@ |
|||||
|
<# |
||||
|
Tests the owner cmdlets of the module built in NTFSSecurity\bin\Release on files in a sandbox folder. |
||||
|
#> |
||||
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute( |
||||
|
'PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Pester shares variables between blocks.' |
||||
|
)] |
||||
|
param () |
||||
|
|
||||
|
BeforeDiscovery { |
||||
|
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'TestHelpers.psm1') -Force |
||||
|
# With the Backup privilege, Windows may grant reading the owner despite a deny entry. |
||||
|
$canBypassDeny = Test-PrivilegeHeld -Name 'SeBackupPrivilege' |
||||
|
} |
||||
|
|
||||
|
BeforeAll { |
||||
|
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'TestHelpers.psm1') -Force |
||||
|
$modulePath = Join-Path -Path $PSScriptRoot -ChildPath '..\NTFSSecurity\bin\Release\NTFSSecurity.psd1' |
||||
|
Import-Module -Name $modulePath -Force -ErrorAction Stop |
||||
|
$sandbox = New-TestSandbox -Name 'Owner' |
||||
|
Push-Location -LiteralPath $sandbox |
||||
|
} |
||||
|
|
||||
|
AfterAll { |
||||
|
Pop-Location |
||||
|
Remove-TestSandbox -Sandbox $sandbox |
||||
|
Remove-Module -Name NTFSSecurity -Force -ErrorAction SilentlyContinue |
||||
|
} |
||||
|
|
||||
|
Describe 'Get-NTFSOwner' { |
||||
|
Context 'When a downstream command stops the pipeline' { |
||||
|
It 'Should stop without writing errors' { |
||||
|
$files = 1..3 | ForEach-Object -Process { New-TestSandboxItem -Sandbox $sandbox -Name "Owner$_" } |
||||
|
|
||||
|
$result = @(Get-NTFSOwner -Path $files -ErrorVariable ownerErrors -ErrorAction SilentlyContinue | Select-Object -First 1) |
||||
|
|
||||
|
$ownerErrors | Should -BeNullOrEmpty |
||||
|
$result | Should -HaveCount 1 |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
Context 'When the owner cannot be read' { |
||||
|
It 'Should write one permission error and keep the owner' -Skip:$canBypassDeny { |
||||
|
$file = New-TestSandboxItem -Sandbox $sandbox -Name 'Denied' |
||||
|
Block-TestReadPermission -Sandbox $sandbox -Path $file |
||||
|
|
||||
|
$result = @(Get-NTFSOwner -Path $file -ErrorVariable ownerErrors -ErrorAction SilentlyContinue) |
||||
|
|
||||
|
$result | Should -BeNullOrEmpty |
||||
|
$ownerErrors | Should -HaveCount 1 |
||||
|
$ownerErrors[0].FullyQualifiedErrorId | Should -BeLike 'ReadSecurityError,*' |
||||
|
$ownerErrors[0].CategoryInfo.Category | Should -Be 'PermissionDenied' |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue