From 26bc096fa5fb43456245691a2721171c5a44bc00 Mon Sep 17 00:00:00 2001 From: Eduard Arbona Date: Fri, 6 Nov 2026 07:42:20 -0400 Subject: [PATCH 1/6] fix(cis,ad): the last two checks of #2468 no longer pass on an absent source The three High and Medium severity cases of #2468 were fixed by #2475 and #2478. These are the two that were filed as "same class, lower severity" and are still open in main. CIS.M365.1.3.6, customer lockbox. Get-MtExo returning no organization configuration left $customerLockbox empty, the Where-Object filtered to nothing, and ($result | Measure-Object).Count -eq 0 is the pass, so a tenant whose configuration was never read was reported as having the customer lockbox enabled. Guarded the same way #2478 guarded CIS.M365.3.1.1: rows without a readable CustomerLockBoxEnabled are dropped and an empty result skips with NotAuthorized instead of passing. The same file had a second defect. $resultMd was assigned only inside the failure branch while the -replace below consumed it on both paths, so a passing tenant rendered an empty table, and the expression would throw under Set-StrictMode. The table is now built for both outcomes. AD-FOR-03, tombstone lifetime. An unreadable TombstoneLifetime was replaced with the literal 180, which made the test's own condition ($null -ne $tombstoneLifetime) impossible to fail and left the author's "Unable to retrieve tombstone lifetime" branch unreachable. A forest whose tombstone lifetime could not be read was reported as retaining deleted objects for 180 days and as meeting the recommendation, on a number the test supplied itself. The substitution is removed, so the branch the author wrote for that case is the one that runs, which is what the title of the check asserts. Dropped the "Using Default" row with it: without the substitution the test has no way to know whether the value it read equals the forest's default, and the default is 60 for older forests and 180 for newer ones. The remaining row is labelled as the recommended minimum, which is what the number is. Ten unit tests cover both: the empty source, the absent property, the pass and failure paths, the table rendering on a pass, the unable-to-retrieve branch, the absence of the invented 180, a forest on the older 60-day default, and the not-connected skip. --- .../FalsePassOnEmptySource.Tests.ps1 | 126 ++++++++++++++++++ tests/ad/domain/Test.AD-FOR-03.ps1 | 19 +-- tests/cis/Test.CIS.M365.1.3.6.ps1 | 31 +++-- 3 files changed, 157 insertions(+), 19 deletions(-) create mode 100644 powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 diff --git a/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 b/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 new file mode 100644 index 0000000000..94a6cb8b5b --- /dev/null +++ b/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 @@ -0,0 +1,126 @@ +Describe 'Checks that must not pass when their data source returns nothing' { + BeforeAll { + Import-Module $PSScriptRoot/../../Maester.psd1 -Force + . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" + # Both are native tests, so neither function is exported. + Use-MtModuleFunction -Name Test-MtCisCustomerLockBox, Test-MtAdTombstoneLifetime + } + + BeforeEach { + $script:TestResult = $null + $script:SkippedBecause = $null + + Mock -ModuleName Maester Get-MtLicenseInformation { return $true } + Mock -ModuleName Maester Add-MtTestResultDetail { + param($Result, $SkippedBecause) + + $script:TestResult = $Result + $script:SkippedBecause = $SkippedBecause + } + } + + Context 'CIS.M365.1.3.6 customer lockbox' { + It 'skips instead of passing when the organization configuration comes back empty' { + # An empty collection filters to nothing, the count is zero, and zero was the pass. + Mock -ModuleName Maester Get-MtExo { @() } + + $null -eq (Test-MtCisCustomerLockBox) | Should -BeTrue + $script:SkippedBecause | Should -Be 'NotAuthorized' + } + + It 'skips instead of passing when the row carries no CustomerLockBoxEnabled property' { + Mock -ModuleName Maester Get-MtExo { [pscustomobject]@{ Id = 'contoso' } } + + $null -eq (Test-MtCisCustomerLockBox) | Should -BeTrue + $script:SkippedBecause | Should -Be 'NotAuthorized' + } + + It 'passes when the lockbox is enabled' { + Mock -ModuleName Maester Get-MtExo { [pscustomobject]@{ Id = 'contoso'; CustomerLockBoxEnabled = 'True' } } + + Test-MtCisCustomerLockBox | Should -BeTrue + $script:TestResult | Should -Match 'Well done' + } + + It 'fails when the lockbox is disabled' { + Mock -ModuleName Maester Get-MtExo { [pscustomobject]@{ Id = 'contoso'; CustomerLockBoxEnabled = 'False' } } + + Test-MtCisCustomerLockBox | Should -BeFalse + $script:TestResult | Should -Match 'does not have the customer lockbox enabled' + } + + It 'renders the result table on a pass as well as on a failure' { + # The table used to be built only on failure while the -replace consumed it either way, + # so a passing tenant rendered an empty table and the expression would throw under + # Set-StrictMode. + Mock -ModuleName Maester Get-MtExo { [pscustomobject]@{ Id = 'contoso'; CustomerLockBoxEnabled = 'True' } } + + $null = Test-MtCisCustomerLockBox + $script:TestResult | Should -Match '\| Customer Lockbox \|' + $script:TestResult | Should -Not -Match '%TestResult%' + } + } + + Context 'AD-FOR-03 tombstone lifetime' { + It 'fails with the unable-to-retrieve message when the lifetime cannot be read' { + # The value used to be replaced with 180, which made the condition impossible to fail + # and reported a forest as retaining deleted objects for 180 days on an invented number. + Mock -ModuleName Maester Get-MtADDomainState { + [pscustomobject]@{ + Configuration = [pscustomobject]@{ TombstoneLifetime = $null } + Forest = [pscustomobject]@{ Name = 'contoso.com' } + } + } + + Test-MtAdTombstoneLifetime | Should -BeFalse + $script:TestResult | Should -Match 'Unable to retrieve tombstone lifetime' + } + + It 'does not invent a lifetime of 180 days when the value is missing' { + Mock -ModuleName Maester Get-MtADDomainState { + [pscustomobject]@{ + Configuration = [pscustomobject]@{ TombstoneLifetime = $null } + Forest = [pscustomobject]@{ Name = 'contoso.com' } + } + } + + $null = Test-MtAdTombstoneLifetime + $script:TestResult | Should -Not -Match '180 days' + $script:TestResult | Should -Not -Match 'Meets recommendation' + } + + It 'passes and reports the value when the lifetime is readable' { + Mock -ModuleName Maester Get-MtADDomainState { + [pscustomobject]@{ + Configuration = [pscustomobject]@{ TombstoneLifetime = 365 } + Forest = [pscustomobject]@{ Name = 'contoso.com' } + } + } + + Test-MtAdTombstoneLifetime | Should -BeTrue + $script:TestResult | Should -Match '365 days' + $script:TestResult | Should -Match 'Meets recommendation' + } + + It 'reports below the recommendation for a forest on the older default of 60 days' { + # The substituted 180 could also claim the recommendation was met on an older forest, + # whose default is 60. + Mock -ModuleName Maester Get-MtADDomainState { + [pscustomobject]@{ + Configuration = [pscustomobject]@{ TombstoneLifetime = 60 } + Forest = [pscustomobject]@{ Name = 'contoso.com' } + } + } + + $null = Test-MtAdTombstoneLifetime + $script:TestResult | Should -Match 'Below recommendation' + } + + It 'skips when Active Directory is not reachable at all' { + Mock -ModuleName Maester Get-MtADDomainState { return $null } + + $null -eq (Test-MtAdTombstoneLifetime) | Should -BeTrue + $script:SkippedBecause | Should -Be 'NotConnectedActiveDirectory' + } + } +} diff --git a/tests/ad/domain/Test.AD-FOR-03.ps1 b/tests/ad/domain/Test.AD-FOR-03.ps1 index 45f4d98068..3a3ef3c6bf 100644 --- a/tests/ad/domain/Test.AD-FOR-03.ps1 +++ b/tests/ad/domain/Test.AD-FOR-03.ps1 @@ -51,12 +51,16 @@ # Default values: 60 days for older forests, 180 days for newer forests $defaultValue = 180 - if ($null -eq $tombstoneLifetime) { - $tombstoneLifetime = $defaultValue - $isDefault = $true - } else { - $isDefault = $false - } + + # The unreadable value used to be replaced with $defaultValue here. That made the condition + # below impossible to fail and left the "Unable to retrieve" branch unreachable, so a forest + # whose tombstone lifetime could not be read was reported as retaining deleted objects for 180 + # days and as meeting the recommendation, on a number this test supplied itself. Worse, the + # substituted 180 is only the default for newer forests; an older forest defaults to 60, so the + # invented value could also claim the recommendation was met when it was not. + # + # Without the substitution the branch the author wrote for that case is the one that runs, which + # is what the title of this check asserts: the tombstone lifetime should be retrievable. # Test passes if we successfully retrieved tombstone lifetime $testResult = $null -ne $tombstoneLifetime @@ -66,8 +70,7 @@ $result = "| Property | Value |" + "`n" $result += "| --- | --- |" + "`n" $result += "| Tombstone Lifetime | $tombstoneLifetime days |" + "`n" - $result += "| Default Value | $defaultValue days |" + "`n" - $result += "| Using Default | $isDefault |" + "`n" + $result += "| Recommended minimum | $defaultValue days |" + "`n" $result += "| Forest Name | $($adState.Forest.Name) |" + "`n" $recommendation = if ($tombstoneLifetime -ge 180) { "✅ Meets recommendation (180+ days)" } else { "⚠️ Below recommendation (180 days)" } diff --git a/tests/cis/Test.CIS.M365.1.3.6.ps1 b/tests/cis/Test.CIS.M365.1.3.6.ps1 index 0443bedd76..28018b2516 100644 --- a/tests/cis/Test.CIS.M365.1.3.6.ps1 +++ b/tests/cis/Test.CIS.M365.1.3.6.ps1 @@ -40,8 +40,17 @@ Write-Verbose 'Requesting secure scores to get the customer lockbox setting' $customerLockbox = Get-MtExo -Request OrganizationConfig | Select-Object CustomerLockBoxEnabled + $usableConfig = @($customerLockbox | Where-Object { $null -ne $_ -and $null -ne $_.CustomerLockBoxEnabled }) + if ($usableConfig.Count -eq 0) { + # An empty collection filters to nothing, so the count below is zero and that is the pass. + # Without this guard the check reports the customer lockbox as enabled for a tenant whose + # organization configuration was never read. + Add-MtTestResultDetail -SkippedBecause NotAuthorized + return $null + } + Write-Verbose 'Checking if the customer lockbox feature is enabled' - $result = $customerLockbox | Where-Object { $_.CustomerLockBoxEnabled -ne 'True' } + $result = $usableConfig | Where-Object { $_.CustomerLockBoxEnabled -ne 'True' } # Set the result to true and pass if no tenants are found with the customer lockbox feature disabled. $testResult = ($result | Measure-Object).Count -eq 0 @@ -51,17 +60,17 @@ $testResultMarkdown = "Your tenant does not have the customer lockbox enabled:`n`n%TestResult%" } - # Prepare the markdown result table if the test fails (testResult is false). - if ($testResult -eq $false) { - $resultMd = "| Customer Lockbox |`n" - $resultMd += "| --- |`n" - foreach ($item in $customerLockbox) { - $itemResult = "❌ Fail" - if ($item.id -notin $result.id) { - $itemResult = "✅ Pass" - } - $resultMd += "| $($itemResult) |`n" + # The table is built for both outcomes. It used to be built only on failure while the + # -replace below consumed it either way, so a passing tenant rendered an empty table and the + # expression would throw under Set-StrictMode. + $resultMd = "| Customer Lockbox |`n" + $resultMd += "| --- |`n" + foreach ($item in $usableConfig) { + $itemResult = "❌ Fail" + if ($item.id -notin $result.id) { + $itemResult = "✅ Pass" } + $resultMd += "| $($itemResult) |`n" } $testResultMarkdown = $testResultMarkdown -replace '%TestResult%', $resultMd From be569aa4a7a4489e677f68ee3d647df2dd9bdcf4 Mon Sep 17 00:00:00 2001 From: Eduard Arbona <175124143+earbona23@users.noreply.github.com> Date: Sat, 7 Nov 2026 11:44:20 -0400 Subject: [PATCH 2/6] fix(cis): the lockbox row stops reading Fail on a passing tenant Moving the result table out of the failure-only branch fixed the empty-table throw under Set-StrictMode, but it exposed a row-status bug that was invisible while the table only rendered on failure. The row was decided with `$item.id -notin $result.id`, and the projection above keeps only CustomerLockBoxEnabled, so `id` is dropped. Both sides are therefore $null, and `$null -notin $null` is False in PowerShell, so every row rendered as a failure. A compliant tenant read "Well done. Your tenant has the customer lockbox enabled:" directly above a red row. The single-column table does not need an identity comparison at all: the value itself is the row's status. The passing-path test asserted that the table existed, not that the row read Pass, which is why CI stayed green. Both tests now assert the row status, and the pass assertion fails against the previous source. --- .../tests/functions/FalsePassOnEmptySource.Tests.ps1 | 7 ++++++- tests/cis/Test.CIS.M365.1.3.6.ps1 | 9 +++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 b/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 index 94a6cb8b5b..2c7f21e20d 100644 --- a/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 +++ b/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 @@ -1,4 +1,4 @@ -Describe 'Checks that must not pass when their data source returns nothing' { +Describe 'Checks that must not pass when their data source returns nothing' { BeforeAll { Import-Module $PSScriptRoot/../../Maester.psd1 -Force . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" @@ -40,6 +40,9 @@ Describe 'Checks that must not pass when their data source returns nothing' { Test-MtCisCustomerLockBox | Should -BeTrue $script:TestResult | Should -Match 'Well done' + # A passing tenant must not be shown a failing row under the "Well done" heading. + $script:TestResult | Should -Match '\| ✅ Pass \|' + $script:TestResult | Should -Not -Match '❌ Fail' } It 'fails when the lockbox is disabled' { @@ -47,6 +50,8 @@ Describe 'Checks that must not pass when their data source returns nothing' { Test-MtCisCustomerLockBox | Should -BeFalse $script:TestResult | Should -Match 'does not have the customer lockbox enabled' + $script:TestResult | Should -Match '\| ❌ Fail \|' + $script:TestResult | Should -Not -Match '✅ Pass' } It 'renders the result table on a pass as well as on a failure' { diff --git a/tests/cis/Test.CIS.M365.1.3.6.ps1 b/tests/cis/Test.CIS.M365.1.3.6.ps1 index 28018b2516..1a0aad804e 100644 --- a/tests/cis/Test.CIS.M365.1.3.6.ps1 +++ b/tests/cis/Test.CIS.M365.1.3.6.ps1 @@ -66,10 +66,11 @@ $resultMd = "| Customer Lockbox |`n" $resultMd += "| --- |`n" foreach ($item in $usableConfig) { - $itemResult = "❌ Fail" - if ($item.id -notin $result.id) { - $itemResult = "✅ Pass" - } + # The projection above keeps only CustomerLockBoxEnabled, so there is no id to compare + # against. Reading the value directly is also what the single-column table means: an + # identity comparison here left both sides $null, and $null -notin $null is False, so + # every row rendered as a failure, including under the "Well done" heading. + $itemResult = if ($item.CustomerLockBoxEnabled -eq 'True') { "✅ Pass" } else { "❌ Fail" } $resultMd += "| $($itemResult) |`n" } From c60a3c90ad2896725d3373d3706d76ae90239839 Mon Sep 17 00:00:00 2001 From: Eduard Arbona <175124143+earbona23@users.noreply.github.com> Date: Sun, 8 Nov 2026 02:50:38 -0400 Subject: [PATCH 3/6] fix: drop the customer lockbox change, main already decided that case CI showed two failures in Test-MtCisCustomerLockBox.Tests.ps1, which landed on main with the fix for this check. Its table says, explicitly, that a missing or null CustomerLockBoxEnabled reports Fail with a populated table and asserts -not $SkippedBecause. Skipping there was my inference, not a gap: it contradicts a decision that is written down and tested. Test.CIS.M365.1.3.6.ps1 is back to exactly what main says, and the 1.3.6 context is removed from FalsePassOnEmptySource.Tests.ps1 because main's file already covers every shape it covered - empty, null, enabled, disabled, missing property, null property - and covers them the way main chose. What remains is the AD-FOR-03 fix and its tests, which main has not touched. --- .../FalsePassOnEmptySource.Tests.ps1 | 51 +------------------ tests/cis/Test.CIS.M365.1.3.6.ps1 | 6 +-- 2 files changed, 3 insertions(+), 54 deletions(-) diff --git a/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 b/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 index 2c7f21e20d..dd5b67e29d 100644 --- a/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 +++ b/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 @@ -2,8 +2,8 @@ BeforeAll { Import-Module $PSScriptRoot/../../Maester.psd1 -Force . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" - # Both are native tests, so neither function is exported. - Use-MtModuleFunction -Name Test-MtCisCustomerLockBox, Test-MtAdTombstoneLifetime + # A native test, so the function is not exported. + Use-MtModuleFunction -Name Test-MtAdTombstoneLifetime } BeforeEach { @@ -19,53 +19,6 @@ } } - Context 'CIS.M365.1.3.6 customer lockbox' { - It 'skips instead of passing when the organization configuration comes back empty' { - # An empty collection filters to nothing, the count is zero, and zero was the pass. - Mock -ModuleName Maester Get-MtExo { @() } - - $null -eq (Test-MtCisCustomerLockBox) | Should -BeTrue - $script:SkippedBecause | Should -Be 'NotAuthorized' - } - - It 'skips instead of passing when the row carries no CustomerLockBoxEnabled property' { - Mock -ModuleName Maester Get-MtExo { [pscustomobject]@{ Id = 'contoso' } } - - $null -eq (Test-MtCisCustomerLockBox) | Should -BeTrue - $script:SkippedBecause | Should -Be 'NotAuthorized' - } - - It 'passes when the lockbox is enabled' { - Mock -ModuleName Maester Get-MtExo { [pscustomobject]@{ Id = 'contoso'; CustomerLockBoxEnabled = 'True' } } - - Test-MtCisCustomerLockBox | Should -BeTrue - $script:TestResult | Should -Match 'Well done' - # A passing tenant must not be shown a failing row under the "Well done" heading. - $script:TestResult | Should -Match '\| ✅ Pass \|' - $script:TestResult | Should -Not -Match '❌ Fail' - } - - It 'fails when the lockbox is disabled' { - Mock -ModuleName Maester Get-MtExo { [pscustomobject]@{ Id = 'contoso'; CustomerLockBoxEnabled = 'False' } } - - Test-MtCisCustomerLockBox | Should -BeFalse - $script:TestResult | Should -Match 'does not have the customer lockbox enabled' - $script:TestResult | Should -Match '\| ❌ Fail \|' - $script:TestResult | Should -Not -Match '✅ Pass' - } - - It 'renders the result table on a pass as well as on a failure' { - # The table used to be built only on failure while the -replace consumed it either way, - # so a passing tenant rendered an empty table and the expression would throw under - # Set-StrictMode. - Mock -ModuleName Maester Get-MtExo { [pscustomobject]@{ Id = 'contoso'; CustomerLockBoxEnabled = 'True' } } - - $null = Test-MtCisCustomerLockBox - $script:TestResult | Should -Match '\| Customer Lockbox \|' - $script:TestResult | Should -Not -Match '%TestResult%' - } - } - Context 'AD-FOR-03 tombstone lifetime' { It 'fails with the unable-to-retrieve message when the lifetime cannot be read' { # The value used to be replaced with 180, which made the condition impossible to fail diff --git a/tests/cis/Test.CIS.M365.1.3.6.ps1 b/tests/cis/Test.CIS.M365.1.3.6.ps1 index 042763f065..d31a4742b3 100644 --- a/tests/cis/Test.CIS.M365.1.3.6.ps1 +++ b/tests/cis/Test.CIS.M365.1.3.6.ps1 @@ -38,11 +38,7 @@ param() Write-Verbose 'Requesting organization configuration to get the customer lockbox setting' - # A row whose CustomerLockBoxEnabled is $null means the organization configuration was not - # readable. Dropping those here makes the count below zero, so the check skips instead of - # reporting a tenant as failing on a value it never read. - $customerLockbox = @(Get-MtExo -Request OrganizationConfig | Select-Object CustomerLockBoxEnabled | - Where-Object { $null -ne $_.CustomerLockBoxEnabled }) + $customerLockbox = @(Get-MtExo -Request OrganizationConfig | Select-Object CustomerLockBoxEnabled) if ($customerLockbox.Count -eq 0) { Add-MtTestResultDetail -SkippedBecause NotAuthorized From dc67c3dbd68f29eea86d0db010a49ff9f1bf4cb3 Mon Sep 17 00:00:00 2001 From: Eduard Arbona <175124143+earbona23@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:31:31 -0400 Subject: [PATCH 4/6] fix(ad): a tombstone lifetime that is not set is 60 days, not unreadable @merill's follow-up on AD-FOR-03. A $null TombstoneLifetime was one case in the code and two in reality: the Directory Service object was not read at all, or it was read and carries no tombstoneLifetime attribute. The second is Active Directory's 'not set', where the forest uses 60 days - a known value, so calling it 'unable to retrieve' was wrong. Get-MtLdapConfigurationContainer now reports DirectoryServiceRead alongside the value, which is the only place that can tell the two apart. The check uses it: not read stays the retrieval failure; not set reports 60 days, says so in its own row, and is measured against the recommendation like any other value, so it comes out below 180 rather than silently passing. The new case is covered by a test that fails without the fix: reverting both source files leaves it as the only red of the six. --- .../Get-MtLdapConfigurationContainer.ps1 | 5 ++++ .../FalsePassOnEmptySource.Tests.ps1 | 22 ++++++++++++++- tests/ad/domain/Test.AD-FOR-03.ps1 | 27 +++++++++++++++---- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/powershell/internal/services/ad/queries/Get-MtLdapConfigurationContainer.ps1 b/powershell/internal/services/ad/queries/Get-MtLdapConfigurationContainer.ps1 index ef341ea54b..85f22b7a70 100644 --- a/powershell/internal/services/ad/queries/Get-MtLdapConfigurationContainer.ps1 +++ b/powershell/internal/services/ad/queries/Get-MtLdapConfigurationContainer.ps1 @@ -91,6 +91,11 @@ WellKnownSecurityPrincipals = @($wellKnown); SiteLinks = @($siteLinks); DhcpServers = @($dhcp); AuthNPolicyContainers = @($authN) TrustedRootCAs = @($trustedRoots); IntermediateCAs = @($intermediate); EnterpriseCAs = @($enterprise); CertificateTemplates = @($templates) EnrollmentTemplates = $enrollmentTemplates; CrlDistributionPoints = @($cdp); NtAuthCertificates = @($ntAuth); LdapQueryPolicies = @($policies) + # A $null TombstoneLifetime means two different things and callers need to tell them + # apart: the Directory Service object was not read at all, or it was read and carries + # no tombstoneLifetime attribute. The second is Active Directory's "not set", where the + # forest falls back to 60 days - a known value, not a failure to retrieve. + DirectoryServiceRead = ($null -ne $directoryService) TombstoneLifetime = if ($null -ne $directoryService) { $directoryService.tombstoneLifetime } else { $null } DsHeuristics = if ($null -ne $directoryService) { $directoryService.dSHeuristics } else { $null } SpnMappings = if ($null -ne $directoryService) { @($directoryService.sPNMappings | Where-Object { $null -ne $_ }) } else { @() } diff --git a/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 b/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 index dd5b67e29d..04511a323f 100644 --- a/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 +++ b/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 @@ -35,9 +35,11 @@ } It 'does not invent a lifetime of 180 days when the value is missing' { + # DirectoryServiceRead false: the Directory Service object was not read at all, so + # there is no effective lifetime to report. This is the genuine retrieval failure. Mock -ModuleName Maester Get-MtADDomainState { [pscustomobject]@{ - Configuration = [pscustomobject]@{ TombstoneLifetime = $null } + Configuration = [pscustomobject]@{ TombstoneLifetime = $null; DirectoryServiceRead = $false } Forest = [pscustomobject]@{ Name = 'contoso.com' } } } @@ -47,6 +49,24 @@ $script:TestResult | Should -Not -Match 'Meets recommendation' } + It 'reports the 60-day fallback when the attribute is not set but the object was read' { + # Raised by @merill on #2699: a forest that never set tombstoneLifetime is not a + # retrieval failure. Active Directory uses 60 days, which is a known value and below + # the recommendation, so the check reports it instead of saying it could not be read. + Mock -ModuleName Maester Get-MtADDomainState { + [pscustomobject]@{ + Configuration = [pscustomobject]@{ TombstoneLifetime = $null; DirectoryServiceRead = $true } + Forest = [pscustomobject]@{ Name = 'contoso.com' } + } + } + + Test-MtAdTombstoneLifetime | Should -BeTrue + $script:TestResult | Should -Match '60 days' + $script:TestResult | Should -Match 'Not set on the forest' + $script:TestResult | Should -Match 'Below recommendation' + $script:TestResult | Should -Not -Match 'Unable to retrieve' + } + It 'passes and reports the value when the lifetime is readable' { Mock -ModuleName Maester Get-MtADDomainState { [pscustomobject]@{ diff --git a/tests/ad/domain/Test.AD-FOR-03.ps1 b/tests/ad/domain/Test.AD-FOR-03.ps1 index 3a3ef3c6bf..36fe6f5c07 100644 --- a/tests/ad/domain/Test.AD-FOR-03.ps1 +++ b/tests/ad/domain/Test.AD-FOR-03.ps1 @@ -62,21 +62,38 @@ # Without the substitution the branch the author wrote for that case is the one that runs, which # is what the title of this check asserts: the tombstone lifetime should be retrievable. - # Test passes if we successfully retrieved tombstone lifetime - $testResult = $null -ne $tombstoneLifetime + # A $null lifetime is not one case but two, and only one of them is a failure to retrieve. + # When the Directory Service object was read and simply carries no tombstoneLifetime, the + # attribute is not set and Active Directory uses 60 days. That value is known, so the check + # reports it rather than claiming the lifetime could not be retrieved. Raised by @merill. + $directoryServiceRead = [bool]$adState.Configuration.DirectoryServiceRead + $notSetLifetime = 60 + $isNotSet = $directoryServiceRead -and $null -eq $tombstoneLifetime + $effectiveLifetime = if ($isNotSet) { $notSetLifetime } else { $tombstoneLifetime } + + # Test passes if the effective tombstone lifetime is known + $testResult = $null -ne $effectiveLifetime # Generate markdown results if ($testResult) { $result = "| Property | Value |" + "`n" $result += "| --- | --- |" + "`n" - $result += "| Tombstone Lifetime | $tombstoneLifetime days |" + "`n" + $result += "| Tombstone Lifetime | $effectiveLifetime days |" + "`n" + if ($isNotSet) { + $result += "| Source | Not set on the forest, so Active Directory uses $notSetLifetime days |" + "`n" + } $result += "| Recommended minimum | $defaultValue days |" + "`n" $result += "| Forest Name | $($adState.Forest.Name) |" + "`n" - $recommendation = if ($tombstoneLifetime -ge 180) { "✅ Meets recommendation (180+ days)" } else { "⚠️ Below recommendation (180 days)" } + $recommendation = if ($effectiveLifetime -ge 180) { "✅ Meets recommendation (180+ days)" } else { "⚠️ Below recommendation (180 days)" } $result += "| Recommendation | $recommendation |" + "`n" - $testResultMarkdown = "The Active Directory tombstone lifetime has been retrieved. Deleted objects are retained for $tombstoneLifetime days before permanent removal.`n`n%TestResult%" + $lead = if ($isNotSet) { + "The forest does not set a tombstone lifetime, so Active Directory uses $notSetLifetime days. Deleted objects are retained for that long before permanent removal." + } else { + "The Active Directory tombstone lifetime has been retrieved. Deleted objects are retained for $effectiveLifetime days before permanent removal." + } + $testResultMarkdown = "$lead`n`n%TestResult%" $testResultMarkdown = $testResultMarkdown -replace "%TestResult%", $result } else { $testResultMarkdown = "Unable to retrieve tombstone lifetime. Ensure you have appropriate permissions and the Active Directory module is installed." From a22b47dd3ce13f0cfbb17f5196a3c942eeb74783 Mon Sep 17 00:00:00 2001 From: Eduard Arbona <175124143+earbona23@users.noreply.github.com> Date: Sun, 8 Nov 2026 11:30:44 -0400 Subject: [PATCH 5/6] fix(ad): never infer a tombstone lifetime the check did not read @coderabbitai is right, and the defect was mine: seeing the Directory Service object is not proof that tombstoneLifetime was readable. An account that can read the object but not that attribute gets the object back with the attribute omitted, which is identical to the forest never setting one. My previous commit inferred 60 days from that and passed the check - the same false pass this PR exists to remove, reintroduced in a narrower case. LDAP does not expose the difference, so the check no longer pretends to know it. It passes only when the forest actually returned a lifetime. When the attribute is absent, the result names both causes, says Active Directory uses 60 days if nothing was set, and points at the ACL on CN=Directory Service that would settle which one it is. @merill's point still holds and is in the output: a forest that never set one is on 60 days, below the 180-day recommendation. What changed is that the check reports that without claiming it retrieved anything. --- .../Get-MtLdapConfigurationContainer.ps1 | 9 +++-- .../FalsePassOnEmptySource.Tests.ps1 | 18 +++++---- tests/ad/domain/Test.AD-FOR-03.ps1 | 38 +++++++++---------- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/powershell/internal/services/ad/queries/Get-MtLdapConfigurationContainer.ps1 b/powershell/internal/services/ad/queries/Get-MtLdapConfigurationContainer.ps1 index 85f22b7a70..acfc55750f 100644 --- a/powershell/internal/services/ad/queries/Get-MtLdapConfigurationContainer.ps1 +++ b/powershell/internal/services/ad/queries/Get-MtLdapConfigurationContainer.ps1 @@ -91,10 +91,11 @@ WellKnownSecurityPrincipals = @($wellKnown); SiteLinks = @($siteLinks); DhcpServers = @($dhcp); AuthNPolicyContainers = @($authN) TrustedRootCAs = @($trustedRoots); IntermediateCAs = @($intermediate); EnterpriseCAs = @($enterprise); CertificateTemplates = @($templates) EnrollmentTemplates = $enrollmentTemplates; CrlDistributionPoints = @($cdp); NtAuthCertificates = @($ntAuth); LdapQueryPolicies = @($policies) - # A $null TombstoneLifetime means two different things and callers need to tell them - # apart: the Directory Service object was not read at all, or it was read and carries - # no tombstoneLifetime attribute. The second is Active Directory's "not set", where the - # forest falls back to 60 days - a known value, not a failure to retrieve. + # Whether the Directory Service object itself came back. It is NOT proof that + # tombstoneLifetime was readable: if the account can see the object but has no read + # access to that attribute, Active Directory omits the attribute and returns the + # object, which looks exactly like the attribute not being set. Callers must not + # infer a value from this flag alone - see Test-MtAdTombstoneLifetime. DirectoryServiceRead = ($null -ne $directoryService) TombstoneLifetime = if ($null -ne $directoryService) { $directoryService.tombstoneLifetime } else { $null } DsHeuristics = if ($null -ne $directoryService) { $directoryService.dSHeuristics } else { $null } diff --git a/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 b/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 index 04511a323f..0804c1c0bf 100644 --- a/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 +++ b/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 @@ -49,10 +49,13 @@ $script:TestResult | Should -Not -Match 'Meets recommendation' } - It 'reports the 60-day fallback when the attribute is not set but the object was read' { - # Raised by @merill on #2699: a forest that never set tombstoneLifetime is not a - # retrieval failure. Active Directory uses 60 days, which is a known value and below - # the recommendation, so the check reports it instead of saying it could not be read. + It 'does not pass on an inferred 60 days when the attribute is absent' { + # @merill asked for the "not set" case to say Active Directory then uses 60 days, and + # it does. @coderabbitai then pointed out that seeing the object is not proof the + # attribute was readable: an account without read access to tombstoneLifetime gets the + # object back with the attribute omitted, which is identical to it not being set. So + # the 60 days is explained, both causes are named, and the check still does not pass - + # inferring a value it never read is the false pass this file exists to prevent. Mock -ModuleName Maester Get-MtADDomainState { [pscustomobject]@{ Configuration = [pscustomobject]@{ TombstoneLifetime = $null; DirectoryServiceRead = $true } @@ -60,11 +63,10 @@ } } - Test-MtAdTombstoneLifetime | Should -BeTrue + Test-MtAdTombstoneLifetime | Should -BeFalse $script:TestResult | Should -Match '60 days' - $script:TestResult | Should -Match 'Not set on the forest' - $script:TestResult | Should -Match 'Below recommendation' - $script:TestResult | Should -Not -Match 'Unable to retrieve' + $script:TestResult | Should -Match 'cannot read the attribute' + $script:TestResult | Should -Not -Match 'Meets recommendation' } It 'passes and reports the value when the lifetime is readable' { diff --git a/tests/ad/domain/Test.AD-FOR-03.ps1 b/tests/ad/domain/Test.AD-FOR-03.ps1 index 36fe6f5c07..1564bb0b3c 100644 --- a/tests/ad/domain/Test.AD-FOR-03.ps1 +++ b/tests/ad/domain/Test.AD-FOR-03.ps1 @@ -62,41 +62,39 @@ # Without the substitution the branch the author wrote for that case is the one that runs, which # is what the title of this check asserts: the tombstone lifetime should be retrievable. - # A $null lifetime is not one case but two, and only one of them is a failure to retrieve. - # When the Directory Service object was read and simply carries no tombstoneLifetime, the - # attribute is not set and Active Directory uses 60 days. That value is known, so the check - # reports it rather than claiming the lifetime could not be retrieved. Raised by @merill. + # A $null lifetime has two causes and LDAP does not let us tell them apart. @merill asked + # for the "not set" case to report the 60 days Active Directory then uses, and that is + # worth saying. But @coderabbitai is right that seeing the Directory Service object is not + # proof the attribute was readable: an account without read access to tombstoneLifetime + # gets the object back with the attribute omitted, which is byte-for-byte what "not set" + # looks like. Passing on a value inferred from that would be the same false pass this + # check exists to remove, so the reading is reported and the check still does not pass. $directoryServiceRead = [bool]$adState.Configuration.DirectoryServiceRead $notSetLifetime = 60 - $isNotSet = $directoryServiceRead -and $null -eq $tombstoneLifetime - $effectiveLifetime = if ($isNotSet) { $notSetLifetime } else { $tombstoneLifetime } + $isAbsent = $directoryServiceRead -and $null -eq $tombstoneLifetime - # Test passes if the effective tombstone lifetime is known - $testResult = $null -ne $effectiveLifetime + # Passes only when the forest actually returned a lifetime. + $testResult = $null -ne $tombstoneLifetime # Generate markdown results if ($testResult) { $result = "| Property | Value |" + "`n" $result += "| --- | --- |" + "`n" - $result += "| Tombstone Lifetime | $effectiveLifetime days |" + "`n" - if ($isNotSet) { - $result += "| Source | Not set on the forest, so Active Directory uses $notSetLifetime days |" + "`n" - } + $result += "| Tombstone Lifetime | $tombstoneLifetime days |" + "`n" $result += "| Recommended minimum | $defaultValue days |" + "`n" $result += "| Forest Name | $($adState.Forest.Name) |" + "`n" - $recommendation = if ($effectiveLifetime -ge 180) { "✅ Meets recommendation (180+ days)" } else { "⚠️ Below recommendation (180 days)" } + $recommendation = if ($tombstoneLifetime -ge 180) { "✅ Meets recommendation (180+ days)" } else { "⚠️ Below recommendation (180 days)" } $result += "| Recommendation | $recommendation |" + "`n" - $lead = if ($isNotSet) { - "The forest does not set a tombstone lifetime, so Active Directory uses $notSetLifetime days. Deleted objects are retained for that long before permanent removal." - } else { - "The Active Directory tombstone lifetime has been retrieved. Deleted objects are retained for $effectiveLifetime days before permanent removal." - } - $testResultMarkdown = "$lead`n`n%TestResult%" + $testResultMarkdown = "The Active Directory tombstone lifetime has been retrieved. Deleted objects are retained for $tombstoneLifetime days before permanent removal.`n`n%TestResult%" $testResultMarkdown = $testResultMarkdown -replace "%TestResult%", $result } else { - $testResultMarkdown = "Unable to retrieve tombstone lifetime. Ensure you have appropriate permissions and the Active Directory module is installed." + $testResultMarkdown = if ($isAbsent) { + "The Directory Service object was read, but it carries no ``tombstoneLifetime``. That is either because the forest never set one - in which case Active Directory uses $notSetLifetime days, below the 180-day recommendation - or because this account cannot read the attribute. Active Directory returns the object with the attribute omitted in both cases, so the two cannot be told apart from here, and this check does not report a lifetime it did not read. Grant read access to ``tombstoneLifetime`` on CN=Directory Service,CN=Windows NT,CN=Services to settle which it is." + } else { + "Unable to retrieve tombstone lifetime. Ensure you have appropriate permissions and the Active Directory module is installed." + } } Add-MtTestResultDetail -Result $testResultMarkdown From 4b59a15daaf16eae5f57fe3051e6668f9ac3d738 Mon Sep 17 00:00:00 2001 From: Eduard Arbona <175124143+earbona23@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:00:42 -0400 Subject: [PATCH 6/6] docs(ad): trim the comments to what the code does The comments carried the history of how the fix was reached, including review mentions. That belongs in the pull request, not in the file, where it goes stale and tells a reader nothing about the code in front of them. Each block now states the behaviour and the reason for it, and nothing else. --- .../FalsePassOnEmptySource.Tests.ps1 | 12 ++++------ tests/ad/domain/Test.AD-FOR-03.ps1 | 24 ++++++------------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 b/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 index 0804c1c0bf..b490f74a5b 100644 --- a/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 +++ b/powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1 @@ -21,8 +21,8 @@ Context 'AD-FOR-03 tombstone lifetime' { It 'fails with the unable-to-retrieve message when the lifetime cannot be read' { - # The value used to be replaced with 180, which made the condition impossible to fail - # and reported a forest as retaining deleted objects for 180 days on an invented number. + # An unreadable lifetime must not be substituted with a default: the comparison would + # then be impossible to fail and the result would report an invented number. Mock -ModuleName Maester Get-MtADDomainState { [pscustomobject]@{ Configuration = [pscustomobject]@{ TombstoneLifetime = $null } @@ -50,12 +50,8 @@ } It 'does not pass on an inferred 60 days when the attribute is absent' { - # @merill asked for the "not set" case to say Active Directory then uses 60 days, and - # it does. @coderabbitai then pointed out that seeing the object is not proof the - # attribute was readable: an account without read access to tombstoneLifetime gets the - # object back with the attribute omitted, which is identical to it not being set. So - # the 60 days is explained, both causes are named, and the check still does not pass - - # inferring a value it never read is the false pass this file exists to prevent. + # Seeing the Directory Service object is not proof the attribute was readable, so an + # absent attribute must not be read as 'not set' and passed on the 60-day default. Mock -ModuleName Maester Get-MtADDomainState { [pscustomobject]@{ Configuration = [pscustomobject]@{ TombstoneLifetime = $null; DirectoryServiceRead = $true } diff --git a/tests/ad/domain/Test.AD-FOR-03.ps1 b/tests/ad/domain/Test.AD-FOR-03.ps1 index 1564bb0b3c..4946a59d28 100644 --- a/tests/ad/domain/Test.AD-FOR-03.ps1 +++ b/tests/ad/domain/Test.AD-FOR-03.ps1 @@ -52,23 +52,13 @@ # Default values: 60 days for older forests, 180 days for newer forests $defaultValue = 180 - # The unreadable value used to be replaced with $defaultValue here. That made the condition - # below impossible to fail and left the "Unable to retrieve" branch unreachable, so a forest - # whose tombstone lifetime could not be read was reported as retaining deleted objects for 180 - # days and as meeting the recommendation, on a number this test supplied itself. Worse, the - # substituted 180 is only the default for newer forests; an older forest defaults to 60, so the - # invented value could also claim the recommendation was met when it was not. - # - # Without the substitution the branch the author wrote for that case is the one that runs, which - # is what the title of this check asserts: the tombstone lifetime should be retrievable. - - # A $null lifetime has two causes and LDAP does not let us tell them apart. @merill asked - # for the "not set" case to report the 60 days Active Directory then uses, and that is - # worth saying. But @coderabbitai is right that seeing the Directory Service object is not - # proof the attribute was readable: an account without read access to tombstoneLifetime - # gets the object back with the attribute omitted, which is byte-for-byte what "not set" - # looks like. Passing on a value inferred from that would be the same false pass this - # check exists to remove, so the reading is reported and the check still does not pass. + # The lifetime is never substituted with a default. A value this check supplies itself would + # make the comparison below impossible to fail, and 180 is only the default for newer forests. + + # A $null lifetime has two causes LDAP does not distinguish: the forest never set the + # attribute, or this account cannot read it. Both return the object with the attribute + # omitted, so the check reports what 'not set' would mean without claiming it retrieved a + # value. $directoryServiceRead = [bool]$adState.Configuration.DirectoryServiceRead $notSetLifetime = 60 $isAbsent = $directoryServiceRead -and $null -eq $tombstoneLifetime