From b579d9fb13d0fa83e39d5101a44586a2a2b9ca9b Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Sat, 21 Nov 2026 19:41:32 +0100 Subject: [PATCH 1/5] fix(cis): 2.1.4 and 2.1.7 pass rules whose user condition limits the scope Users, groups and domains are different condition types, and existing rules must match all of them. A rule that lists every accepted domain plus one user applies to that user only, yet both scope checks passed it, as they did for a group plus users. Rules created or edited after the MC1546447 rollout (December 2026) match any condition instead, and the rule does not expose which logic applies. Such a rule is now reported as Investigate when the scope is its only failing check. Users without a domain covering the tenant or a group still fail. Adds unit tests for both checks. --- ...Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 | 82 +++++++++++++++++++ .../Test-MtCisSafeAttachment.Tests.ps1 | 41 ++++++++++ tests/cis/Test.CIS.M365.2.1.4.md | 2 +- tests/cis/Test.CIS.M365.2.1.4.ps1 | 17 +++- tests/cis/Test.CIS.M365.2.1.7.md | 2 +- tests/cis/Test.CIS.M365.2.1.7.ps1 | 17 +++- 6 files changed, 153 insertions(+), 8 deletions(-) create mode 100644 powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 create mode 100644 powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 diff --git a/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 b/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 new file mode 100644 index 0000000000..cfe6954539 --- /dev/null +++ b/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 @@ -0,0 +1,82 @@ +Describe 'Test-MtCisSafeAntiPhishingPolicy' { + BeforeAll { + Import-Module "$PSScriptRoot\..\..\Maester.psd1" -Force + . "$PSScriptRoot\..\helpers\Use-MtModuleFunction.ps1" + Use-MtModuleFunction -Name Test-MtCisSafeAntiPhishingPolicy + + # Every CIS setting is met, so the rule scope decides the result. + $script:compliantSettings = @{ + Enabled = $true + PhishThresholdLevel = 3 + EnableTargetedUserProtection = $true + TargetedUsersToProtect = @('Executive;executive@contoso.com') + EnableOrganizationDomainsProtection = $true + EnableMailboxIntelligence = $true + EnableMailboxIntelligenceProtection = $true + EnableSpoofIntelligence = $true + TargetedUserProtectionAction = 'Quarantine' + TargetedDomainProtectionAction = 'Quarantine' + MailboxIntelligenceProtectionAction = 'Quarantine' + EnableFirstContactSafetyTips = $true + EnableSimilarUsersSafetyTips = $true + EnableSimilarDomainsSafetyTips = $true + EnableUnusualCharactersSafetyTips = $true + HonorDmarcPolicy = $true + } + } + + BeforeEach { + Mock Add-MtTestResultDetail -ModuleName Maester { } + } + + It 'Reports for a rule scoped to ' -ForEach @( + @{ Scope = 'all recipients'; Domains = @(); Groups = @(); Users = @(); Expected = 'Pass' } + @{ Scope = 'every accepted domain'; Domains = @('contoso.com', 'fabrikam.com'); Groups = @(); Users = @(); Expected = 'Pass' } + @{ Scope = 'a group'; Domains = @(); Groups = @('All staff'); Users = @(); Expected = 'Pass' } + @{ Scope = 'some accepted domains'; Domains = @('contoso.com'); Groups = @(); Users = @(); Expected = 'Fail' } + @{ Scope = 'users'; Domains = @(); Groups = @(); Users = @('adele@contoso.com'); Expected = 'Fail' } + @{ Scope = 'some accepted domains and users'; Domains = @('contoso.com'); Groups = @(); Users = @('adele@contoso.com'); Expected = 'Fail' } + @{ Scope = 'every accepted domain and users'; Domains = @('contoso.com', 'fabrikam.com'); Groups = @(); Users = @('adele@contoso.com'); Expected = 'Investigate' } + @{ Scope = 'a group and users'; Domains = @(); Groups = @('All staff'); Users = @('adele@contoso.com'); Expected = 'Investigate' } + ) { + $policy = [pscustomobject](@{ Name = 'Contoso'; Identity = 'Contoso' } + $script:compliantSettings) + $rule = [pscustomobject]@{ AntiPhishPolicy = 'Contoso'; State = 'Enabled'; Priority = 0; RecipientDomainIs = $Domains; SentToMemberOf = $Groups; SentTo = $Users } + Mock Get-MtExo -ModuleName Maester { + switch ($Request) { + 'AntiPhishPolicy' { $policy } + 'AntiPhishRule' { $rule } + 'AcceptedDomain' { [pscustomobject]@{ Name = 'contoso.com' }; [pscustomobject]@{ Name = 'fabrikam.com' } } + } + } + + Test-MtCisSafeAntiPhishingPolicy | Should -Be ($Expected -eq 'Pass') + + $scopeResult = if ($Expected -eq 'Pass') { 'Pass' } else { 'Fail' } + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + [bool]$Investigate -eq ($Expected -eq 'Investigate') -and + $Result -match "\| Contoso \| Enabled matching rule and scope \| [^\r\n]* $scopeResult \|" -and + ($Expected -ne 'Investigate' -or $Result -match 'MC1546447') + } + } + + It 'Passes without investigation when another policy meets every check' { + $executivePolicy = [pscustomobject](@{ Name = 'Executives'; Identity = 'Executives' } + $script:compliantSettings) + $tenantPolicy = [pscustomobject](@{ Name = 'Contoso'; Identity = 'Contoso' } + $script:compliantSettings) + Mock Get-MtExo -ModuleName Maester { + switch ($Request) { + 'AntiPhishPolicy' { $executivePolicy; $tenantPolicy } + 'AntiPhishRule' { + [pscustomobject]@{ AntiPhishPolicy = 'Executives'; State = 'Enabled'; Priority = 0; RecipientDomainIs = @('contoso.com', 'fabrikam.com'); SentTo = @('adele@contoso.com') } + [pscustomobject]@{ AntiPhishPolicy = 'Contoso'; State = 'Enabled'; Priority = 1; RecipientDomainIs = @('contoso.com', 'fabrikam.com') } + } + 'AcceptedDomain' { [pscustomobject]@{ Name = 'contoso.com' }; [pscustomobject]@{ Name = 'fabrikam.com' } } + } + } + + Test-MtCisSafeAntiPhishingPolicy | Should -BeTrue + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + -not [bool]$Investigate -and $Result.StartsWith('Well done.') + } + } +} \ No newline at end of file diff --git a/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 b/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 new file mode 100644 index 0000000000..2ccb07d239 --- /dev/null +++ b/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 @@ -0,0 +1,41 @@ +Describe 'Test-MtCisSafeAttachment' { + BeforeAll { + Import-Module "$PSScriptRoot\..\..\Maester.psd1" -Force + . "$PSScriptRoot\..\helpers\Use-MtModuleFunction.ps1" + Use-MtModuleFunction -Name Test-MtCisSafeAttachment + } + + BeforeEach { + Mock Add-MtTestResultDetail -ModuleName Maester { } + } + + It 'Reports for a custom rule scoped to ' -ForEach @( + @{ Scope = 'all recipients'; Domains = @(); Groups = @(); Users = @(); Expected = 'Pass' } + @{ Scope = 'every accepted domain'; Domains = @('contoso.com', 'fabrikam.com'); Groups = @(); Users = @(); Expected = 'Pass' } + @{ Scope = 'a group'; Domains = @(); Groups = @('All staff'); Users = @(); Expected = 'Pass' } + @{ Scope = 'some accepted domains'; Domains = @('contoso.com'); Groups = @(); Users = @(); Expected = 'Fail' } + @{ Scope = 'users'; Domains = @(); Groups = @(); Users = @('adele@contoso.com'); Expected = 'Fail' } + @{ Scope = 'some accepted domains and users'; Domains = @('contoso.com'); Groups = @(); Users = @('adele@contoso.com'); Expected = 'Fail' } + @{ Scope = 'every accepted domain and users'; Domains = @('contoso.com', 'fabrikam.com'); Groups = @(); Users = @('adele@contoso.com'); Expected = 'Investigate' } + @{ Scope = 'a group and users'; Domains = @(); Groups = @('All staff'); Users = @('adele@contoso.com'); Expected = 'Investigate' } + ) { + $policy = [pscustomobject]@{ Name = 'Contoso'; Identity = 'Contoso'; Enable = $true; Action = 'Block'; QuarantineTag = 'AdminOnlyAccessPolicy' } + $rule = [pscustomobject]@{ Identity = 'Contoso'; SafeAttachmentPolicy = 'Contoso'; State = 'Enabled'; Priority = 0; RecipientDomainIs = $Domains; SentToMemberOf = $Groups; SentTo = $Users } + Mock Get-MtExo -ModuleName Maester { + switch ($Request) { + 'SafeAttachmentPolicy' { $policy } + 'SafeAttachmentRule' { $rule } + 'AcceptedDomain' { [pscustomobject]@{ Name = 'contoso.com' }; [pscustomobject]@{ Name = 'fabrikam.com' } } + } + } + + Test-MtCisSafeAttachment | Should -Be ($Expected -eq 'Pass') + + $scopeResult = if ($Expected -eq 'Pass') { 'Pass' } else { 'Fail' } + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + [bool]$Investigate -eq ($Expected -eq 'Investigate') -and + $Result -match "\| Rule scope \| [^\r\n]* $scopeResult \|" -and + ($Expected -ne 'Investigate' -or $Result -match 'MC1546447') + } + } +} \ No newline at end of file diff --git a/tests/cis/Test.CIS.M365.2.1.4.md b/tests/cis/Test.CIS.M365.2.1.4.md index 99bb765af3..ee20321d4e 100644 --- a/tests/cis/Test.CIS.M365.2.1.4.md +++ b/tests/cis/Test.CIS.M365.2.1.4.md @@ -2,7 +2,7 @@ The Safe Attachments policy helps protect users from malware in email attachments by scanning attachments for viruses, malware, and other malicious content. When an email attachment is received by a user, Safe Attachments will scan the attachment in a secure environment and provide a verdict on whether the attachment is safe or not. -This check evaluates the highest-priority enabled policy in this order: Strict preset, Standard preset, the highest-priority enabled custom rule, then built-in protection. It verifies the associated rule is enabled and reports its recipient scope. Domain-only rules are checked against accepted domains; group membership size is not evaluated and should be confirmed where group targets are used. +This check evaluates the highest-priority enabled policy in this order: Strict preset, Standard preset, the highest-priority enabled custom rule, then built-in protection. It verifies the associated rule is enabled and reports its recipient scope. Domain-only rules are checked against accepted domains; group membership size is not evaluated and should be confirmed where group targets are used. When a rule adds users to domain or group targets, the result is Investigate: a rule that matches all conditions applies only to those users, rules created or edited after the MC1546447 rollout match any condition, and the rule does not show which logic it uses. #### Rationale diff --git a/tests/cis/Test.CIS.M365.2.1.4.ps1 b/tests/cis/Test.CIS.M365.2.1.4.ps1 index 9ed3cc967d..955c537854 100644 --- a/tests/cis/Test.CIS.M365.2.1.4.ps1 +++ b/tests/cis/Test.CIS.M365.2.1.4.ps1 @@ -102,8 +102,15 @@ $acceptedDomains = @(Get-MtExo -Request AcceptedDomain | ForEach-Object { $_.Name }) } $domainsCoverTenant = $acceptedDomains.Count -gt 0 -and @($acceptedDomains | Where-Object { $_ -notin $domainTargets }).Count -eq 0 - $scopePass = $recipientConditions.Count -eq 0 -or $domainsCoverTenant -or $groupTargets.Count -gt 0 + # Users, groups and domains are different condition types. A rule that matches all of them (AND, the logic of existing + # rules) applies only to the listed users. Rules created or edited after the MC1546447 rollout match any of them (OR), + # and the rule does not expose which logic applies, so users added to domain or group targets need investigation. + $scopeDependsOnMatchLogic = $userTargets.Count -gt 0 -and ($domainsCoverTenant -or $groupTargets.Count -gt 0) + $scopePass = $userTargets.Count -eq 0 -and ($recipientConditions.Count -eq 0 -or $domainsCoverTenant -or $groupTargets.Count -gt 0) $scopeDescription = if ($recipientConditions.Count -eq 0) { 'All recipients' } else { $recipientConditions -join ', ' } + if ($scopeDependsOnMatchLogic) { + $scopeDescription += ' (only these users if the rule matches all conditions)' + } if ($recipientExceptions.Count -gt 0) { $scopeDescription += "; exclusions: $($recipientExceptions -join ', ')" } @@ -115,11 +122,15 @@ [pscustomobject]@{ CheckName = 'Rule state'; Value = $selectedRule.State; Pass = $selectedRule.State -eq 'Enabled' } [pscustomobject]@{ CheckName = 'Rule scope'; Value = $scopeDescription; Pass = $scopePass } ) - $testResult = @($safeAttachmentCheckList | Where-Object { -not $_.Pass }).Count -eq 0 + $failedChecks = @($safeAttachmentCheckList | Where-Object { -not $_.Pass }) + $testResult = $failedChecks.Count -eq 0 + $investigate = $scopeDependsOnMatchLogic -and $failedChecks.Count -eq 1 $portalLink = 'https://security.microsoft.com/safeattachmentv2' if ($testResult) { $testResultMarkdown = "Well done. The highest-priority Safe Attachments policy '$($policy.Name)' and its rule match CIS recommendations ($portalLink).`n`n%TestResult%" + } elseif ($investigate) { + $testResultMarkdown = "The highest-priority Safe Attachments policy '$($policy.Name)' meets the other CIS recommendations, but its rule adds users to domain or group targets. If the rule matches all conditions, as rules created before the MC1546447 rollout do until they are edited, it applies only to those users. If it matches any condition, it also covers the domains or groups. The rule does not show which logic applies, so move the users to a separate policy to make the scope explicit ($portalLink).`n`n%TestResult%" } else { $testResultMarkdown = "The highest-priority Safe Attachments policy '$($policy.Name)' or its rule does not match CIS recommendations ($portalLink).`n`n%TestResult%" } @@ -133,6 +144,6 @@ } $testResultMarkdown = $testResultMarkdown -replace '%TestResult%', $resultMd - Add-MtTestResultDetail -Result $testResultMarkdown + Add-MtTestResultDetail -Result $testResultMarkdown -Investigate:$investigate return $testResult } diff --git a/tests/cis/Test.CIS.M365.2.1.7.md b/tests/cis/Test.CIS.M365.2.1.7.md index f23343412c..0cc6879cc9 100644 --- a/tests/cis/Test.CIS.M365.2.1.7.md +++ b/tests/cis/Test.CIS.M365.2.1.7.md @@ -2,7 +2,7 @@ By default, Office 365 includes built-in features that help protect users from phishing attacks. Set up anti-phishing polices to increase this protection, for example by refining settings to better detect and prevent impersonation and spoofing attacks. The default policy applies to all users within the organization and is a single view to fine-tune antiphishing protection. Custom policies can be created and configured for specific users, groups or domains within the organization and will take precedence over the default policy for the scoped users. -This check evaluates enabled AntiPhish policies with a matching enabled rule. It checks the policy settings listed below and reports the rule's recipient scope. Group membership size is not evaluated and should be confirmed where group targets are used. +This check evaluates enabled AntiPhish policies with a matching enabled rule. It checks the policy settings listed below and reports the rule's recipient scope. Group membership size is not evaluated and should be confirmed where group targets are used. When a rule adds users to domain or group targets, the result is Investigate: a rule that matches all conditions applies only to those users, rules created or edited after the MC1546447 rollout match any condition, and the rule does not show which logic it uses. #### Rationale diff --git a/tests/cis/Test.CIS.M365.2.1.7.ps1 b/tests/cis/Test.CIS.M365.2.1.7.ps1 index ce2f410712..a97988e57b 100644 --- a/tests/cis/Test.CIS.M365.2.1.7.ps1 +++ b/tests/cis/Test.CIS.M365.2.1.7.ps1 @@ -10,7 +10,7 @@ .EXAMPLE Test-MtCisSafeAntiPhishingPolicy - Returns true if at least one anti-phishing policy and its highest-priority enabled matching rule meet all CIS checks. Recipient scope passes with no targets, coverage of every accepted domain, or at least one group target; group membership and exclusions are not validated. + Returns true if at least one anti-phishing policy and its highest-priority enabled matching rule meet all CIS checks. Recipient scope passes with no targets, coverage of every accepted domain, or at least one group target, when the rule names no users; group membership and exclusions are not validated. When users are added to domain or group targets, the scope depends on whether the rule matches all or any of its conditions, which the rule does not show (MC1546447), so the result is Investigate. .LINK https://maester.dev/docs/commands/Test-MtCisSafeAntiPhishingPolicy @@ -57,7 +57,11 @@ $userTargets = @($rule.SentTo | Where-Object { -not [string]::IsNullOrWhiteSpace([string]$_) }) $recipientConditions = @($domainTargets + $groupTargets + $userTargets) $domainScopeCoversTenant = $domainTargets.Count -gt 0 -and $acceptedDomains.Count -gt 0 -and @($acceptedDomains | Where-Object { $_ -notin $domainTargets }).Count -eq 0 - $scopePass = $null -ne $rule -and ($recipientConditions.Count -eq 0 -or $domainScopeCoversTenant -or $groupTargets.Count -gt 0) + # Users, groups and domains are different condition types. A rule that matches all of them (AND, the logic of existing + # rules) applies only to the listed users. Rules created or edited after the MC1546447 rollout match any of them (OR), + # and the rule does not expose which logic applies, so users added to domain or group targets need investigation. + $scopeDependsOnMatchLogic = $null -ne $rule -and $userTargets.Count -gt 0 -and ($domainScopeCoversTenant -or $groupTargets.Count -gt 0) + $scopePass = $null -ne $rule -and $userTargets.Count -eq 0 -and ($recipientConditions.Count -eq 0 -or $domainScopeCoversTenant -or $groupTargets.Count -gt 0) $scopeDescription = if ($null -eq $rule) { 'No enabled matching rule' } elseif ($recipientConditions.Count -eq 0) { @@ -65,6 +69,9 @@ } else { $recipientConditions -join ', ' } + if ($scopeDependsOnMatchLogic) { + $scopeDescription += ' (only these users if the rule matches all conditions)' + } $recipientExceptions = @( $rule.ExceptIfRecipientDomainIs $rule.ExceptIfSentToMemberOf @@ -101,13 +108,17 @@ Name = $policy.Name Checks = $checks Pass = @($checks | Where-Object { -not $_.Pass }).Count -eq 0 + Investigate = $scopeDependsOnMatchLogic -and @($checks | Where-Object { -not $_.Pass }).Count -eq 1 } } $testResult = @($policyReports | Where-Object { $_.Pass }).Count -gt 0 + $investigatePolicies = @(if (-not $testResult) { $policyReports | Where-Object { $_.Investigate } }) $portalLink = 'https://security.microsoft.com/antiphishing' if ($testResult) { $testResultMarkdown = "Well done. An enabled anti-phishing policy and matching rule meet CIS recommendations ($portalLink).`n`n%TestResult%" + } elseif ($investigatePolicies.Count -gt 0) { + $testResultMarkdown = "Anti-phishing policy '$($investigatePolicies.Name -join "', '")' meets the other CIS recommendations, but its rule adds users to domain or group targets. If the rule matches all conditions, as rules created before the MC1546447 rollout do until they are edited, it applies only to those users. If it matches any condition, it also covers the domains or groups. The rule does not show which logic applies, so move the users to a separate policy to make the scope explicit ($portalLink).`n`n%TestResult%" } else { $testResultMarkdown = "No enabled anti-phishing policy and matching rule meet all CIS recommendations ($portalLink).`n`n%TestResult%" } @@ -123,6 +134,6 @@ } $testResultMarkdown = $testResultMarkdown -replace '%TestResult%', $resultMd - Add-MtTestResultDetail -Result $testResultMarkdown + Add-MtTestResultDetail -Result $testResultMarkdown -Investigate:($investigatePolicies.Count -gt 0) return $testResult } From 2bfd8a47b393c6c7912da9038c70e8fdbab4d8eb Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Sat, 21 Nov 2026 20:09:13 +0100 Subject: [PATCH 2/5] fix(cis): state when 2.1.4 and 2.1.7 report Investigate --- tests/cis/Test.CIS.M365.2.1.4.md | 2 +- tests/cis/Test.CIS.M365.2.1.7.md | 2 +- tests/cis/Test.CIS.M365.2.1.7.ps1 | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/cis/Test.CIS.M365.2.1.4.md b/tests/cis/Test.CIS.M365.2.1.4.md index ee20321d4e..f8e9574b61 100644 --- a/tests/cis/Test.CIS.M365.2.1.4.md +++ b/tests/cis/Test.CIS.M365.2.1.4.md @@ -2,7 +2,7 @@ The Safe Attachments policy helps protect users from malware in email attachments by scanning attachments for viruses, malware, and other malicious content. When an email attachment is received by a user, Safe Attachments will scan the attachment in a secure environment and provide a verdict on whether the attachment is safe or not. -This check evaluates the highest-priority enabled policy in this order: Strict preset, Standard preset, the highest-priority enabled custom rule, then built-in protection. It verifies the associated rule is enabled and reports its recipient scope. Domain-only rules are checked against accepted domains; group membership size is not evaluated and should be confirmed where group targets are used. When a rule adds users to domain or group targets, the result is Investigate: a rule that matches all conditions applies only to those users, rules created or edited after the MC1546447 rollout match any condition, and the rule does not show which logic it uses. +This check evaluates the highest-priority enabled policy in this order: Strict preset, Standard preset, the highest-priority enabled custom rule, then built-in protection. It verifies the associated rule is enabled and reports its recipient scope. Domain-only rules are checked against accepted domains; group membership size is not evaluated and should be confirmed where group targets are used. When the selected rule adds users to every accepted domain or to a group and that scope is its only failed check, the result is Investigate: a rule that matches all conditions applies only to those users, rules created or edited after the MC1546447 rollout match any condition, and the rule does not show which logic it uses. Users alone, or users with only some accepted domains, still fail. #### Rationale diff --git a/tests/cis/Test.CIS.M365.2.1.7.md b/tests/cis/Test.CIS.M365.2.1.7.md index 0cc6879cc9..88eeb953f3 100644 --- a/tests/cis/Test.CIS.M365.2.1.7.md +++ b/tests/cis/Test.CIS.M365.2.1.7.md @@ -2,7 +2,7 @@ By default, Office 365 includes built-in features that help protect users from phishing attacks. Set up anti-phishing polices to increase this protection, for example by refining settings to better detect and prevent impersonation and spoofing attacks. The default policy applies to all users within the organization and is a single view to fine-tune antiphishing protection. Custom policies can be created and configured for specific users, groups or domains within the organization and will take precedence over the default policy for the scoped users. -This check evaluates enabled AntiPhish policies with a matching enabled rule. It checks the policy settings listed below and reports the rule's recipient scope. Group membership size is not evaluated and should be confirmed where group targets are used. When a rule adds users to domain or group targets, the result is Investigate: a rule that matches all conditions applies only to those users, rules created or edited after the MC1546447 rollout match any condition, and the rule does not show which logic it uses. +This check evaluates enabled AntiPhish policies with a matching enabled rule. It checks the policy settings listed below and reports the rule's recipient scope. Group membership size is not evaluated and should be confirmed where group targets are used. When no policy passes and a policy fails only on a rule that adds users to every accepted domain or to a group, the result is Investigate: a rule that matches all conditions applies only to those users, rules created or edited after the MC1546447 rollout match any condition, and the rule does not show which logic it uses. Users alone, or users with only some accepted domains, still fail. #### Rationale diff --git a/tests/cis/Test.CIS.M365.2.1.7.ps1 b/tests/cis/Test.CIS.M365.2.1.7.ps1 index a97988e57b..52157a101f 100644 --- a/tests/cis/Test.CIS.M365.2.1.7.ps1 +++ b/tests/cis/Test.CIS.M365.2.1.7.ps1 @@ -10,7 +10,7 @@ .EXAMPLE Test-MtCisSafeAntiPhishingPolicy - Returns true if at least one anti-phishing policy and its highest-priority enabled matching rule meet all CIS checks. Recipient scope passes with no targets, coverage of every accepted domain, or at least one group target, when the rule names no users; group membership and exclusions are not validated. When users are added to domain or group targets, the scope depends on whether the rule matches all or any of its conditions, which the rule does not show (MC1546447), so the result is Investigate. + Returns true if at least one anti-phishing policy and its highest-priority enabled matching rule meet all CIS checks. Recipient scope passes with no targets, coverage of every accepted domain, or at least one group target, when the rule names no users; group membership and exclusions are not validated. When no policy passes and a policy fails only on a rule that adds users to every accepted domain or to a group, the result is Investigate: the scope depends on whether the rule matches all or any of its conditions, which the rule does not show (MC1546447). Users alone, or users with only some accepted domains, fail. .LINK https://maester.dev/docs/commands/Test-MtCisSafeAntiPhishingPolicy From 67af02a10f1f75c5c5863a7c79ca71b5a3aa11c3 Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Sat, 21 Nov 2026 20:12:09 +0100 Subject: [PATCH 3/5] test(cis): keep a failure when the scope is not the only failed check --- ...Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 | 19 +++++++++++++++++++ .../Test-MtCisSafeAttachment.Tests.ps1 | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 b/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 index cfe6954539..2a1d4e818e 100644 --- a/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 +++ b/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 @@ -59,6 +59,25 @@ } } + It 'Fails without investigation when another check also fails' { + $policy = [pscustomobject](@{ Name = 'Contoso'; Identity = 'Contoso' } + $script:compliantSettings) + $policy.PhishThresholdLevel = 1 + $rule = [pscustomobject]@{ AntiPhishPolicy = 'Contoso'; State = 'Enabled'; Priority = 0; RecipientDomainIs = @('contoso.com', 'fabrikam.com'); SentTo = @('adele@contoso.com') } + Mock Get-MtExo -ModuleName Maester { + switch ($Request) { + 'AntiPhishPolicy' { $policy } + 'AntiPhishRule' { $rule } + 'AcceptedDomain' { [pscustomobject]@{ Name = 'contoso.com' }; [pscustomobject]@{ Name = 'fabrikam.com' } } + } + } + + Test-MtCisSafeAntiPhishingPolicy | Should -BeFalse + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + -not [bool]$Investigate -and $Result.StartsWith('No enabled anti-phishing policy') + } + } + It 'Passes without investigation when another policy meets every check' { $executivePolicy = [pscustomobject](@{ Name = 'Executives'; Identity = 'Executives' } + $script:compliantSettings) $tenantPolicy = [pscustomobject](@{ Name = 'Contoso'; Identity = 'Contoso' } + $script:compliantSettings) diff --git a/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 b/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 index 2ccb07d239..83e6f74981 100644 --- a/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 +++ b/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 @@ -38,4 +38,22 @@ ($Expected -ne 'Investigate' -or $Result -match 'MC1546447') } } + + It 'Fails without investigation when another check also fails' { + $policy = [pscustomobject]@{ Name = 'Contoso'; Identity = 'Contoso'; Enable = $false; Action = 'Block'; QuarantineTag = 'AdminOnlyAccessPolicy' } + $rule = [pscustomobject]@{ Identity = 'Contoso'; SafeAttachmentPolicy = 'Contoso'; State = 'Enabled'; Priority = 0; RecipientDomainIs = @('contoso.com', 'fabrikam.com'); SentTo = @('adele@contoso.com') } + Mock Get-MtExo -ModuleName Maester { + switch ($Request) { + 'SafeAttachmentPolicy' { $policy } + 'SafeAttachmentRule' { $rule } + 'AcceptedDomain' { [pscustomobject]@{ Name = 'contoso.com' }; [pscustomobject]@{ Name = 'fabrikam.com' } } + } + } + + Test-MtCisSafeAttachment | Should -BeFalse + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + -not [bool]$Investigate -and $Result -match 'does not match CIS recommendations' + } + } } \ No newline at end of file From 2bfdcc15fdc6217ecb92051b0de0552c5b8c668e Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Sat, 21 Nov 2026 20:58:29 +0100 Subject: [PATCH 4/5] test(cis): end the new test files with a newline --- .../tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 | 2 +- powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 b/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 index 2a1d4e818e..e3636ed5df 100644 --- a/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 +++ b/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 @@ -98,4 +98,4 @@ -not [bool]$Investigate -and $Result.StartsWith('Well done.') } } -} \ No newline at end of file +} diff --git a/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 b/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 index 83e6f74981..6348348d35 100644 --- a/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 +++ b/powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1 @@ -56,4 +56,4 @@ -not [bool]$Investigate -and $Result -match 'does not match CIS recommendations' } } -} \ No newline at end of file +} From 92aeeb74f3929293c80815174bf8324206c9f626 Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Sat, 21 Nov 2026 23:23:45 +0100 Subject: [PATCH 5/5] fix(cis): use the plural when several anti-phishing policies need investigation The Investigate message read "Anti-phishing policy 'A', 'B' meets ... its rule" when several policies were listed. Use the plural form in that case and cover it with a unit test. --- ...Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 | 21 +++++++++++++++++++ tests/cis/Test.CIS.M365.2.1.7.ps1 | 7 ++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 b/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 index e3636ed5df..ae481b7368 100644 --- a/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 +++ b/powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1 @@ -98,4 +98,25 @@ -not [bool]$Investigate -and $Result.StartsWith('Well done.') } } + + It 'Names every policy that needs investigation, in the plural when there are several' { + $executivePolicy = [pscustomobject](@{ Name = 'Executives'; Identity = 'Executives' } + $script:compliantSettings) + $financePolicy = [pscustomobject](@{ Name = 'Finance'; Identity = 'Finance' } + $script:compliantSettings) + Mock Get-MtExo -ModuleName Maester { + switch ($Request) { + 'AntiPhishPolicy' { $executivePolicy; $financePolicy } + 'AntiPhishRule' { + [pscustomobject]@{ AntiPhishPolicy = 'Executives'; State = 'Enabled'; Priority = 0; RecipientDomainIs = @('contoso.com', 'fabrikam.com'); SentTo = @('adele@contoso.com') } + [pscustomobject]@{ AntiPhishPolicy = 'Finance'; State = 'Enabled'; Priority = 1; SentToMemberOf = @('Finance'); SentTo = @('megan@contoso.com') } + } + 'AcceptedDomain' { [pscustomobject]@{ Name = 'contoso.com' }; [pscustomobject]@{ Name = 'fabrikam.com' } } + } + } + + Test-MtCisSafeAntiPhishingPolicy | Should -BeFalse + + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Times 1 -Exactly -ParameterFilter { + [bool]$Investigate -and $Result.StartsWith("Anti-phishing policies 'Executives', 'Finance' meet the other CIS recommendations, but their rules") + } + } } diff --git a/tests/cis/Test.CIS.M365.2.1.7.ps1 b/tests/cis/Test.CIS.M365.2.1.7.ps1 index 52157a101f..4e5ab2e21b 100644 --- a/tests/cis/Test.CIS.M365.2.1.7.ps1 +++ b/tests/cis/Test.CIS.M365.2.1.7.ps1 @@ -118,7 +118,12 @@ if ($testResult) { $testResultMarkdown = "Well done. An enabled anti-phishing policy and matching rule meet CIS recommendations ($portalLink).`n`n%TestResult%" } elseif ($investigatePolicies.Count -gt 0) { - $testResultMarkdown = "Anti-phishing policy '$($investigatePolicies.Name -join "', '")' meets the other CIS recommendations, but its rule adds users to domain or group targets. If the rule matches all conditions, as rules created before the MC1546447 rollout do until they are edited, it applies only to those users. If it matches any condition, it also covers the domains or groups. The rule does not show which logic applies, so move the users to a separate policy to make the scope explicit ($portalLink).`n`n%TestResult%" + $policyList = "'$($investigatePolicies.Name -join "', '")'" + $testResultMarkdown = if ($investigatePolicies.Count -eq 1) { + "Anti-phishing policy $policyList meets the other CIS recommendations, but its rule adds users to domain or group targets. If the rule matches all conditions, as rules created before the MC1546447 rollout do until they are edited, it applies only to those users. If it matches any condition, it also covers the domains or groups. The rule does not show which logic applies, so move the users to a separate policy to make the scope explicit ($portalLink).`n`n%TestResult%" + } else { + "Anti-phishing policies $policyList meet the other CIS recommendations, but their rules add users to domain or group targets. If a rule matches all conditions, as rules created before the MC1546447 rollout do until they are edited, it applies only to those users. If it matches any condition, it also covers the domains or groups. The rules do not show which logic applies, so move the users to separate policies to make the scope explicit ($portalLink).`n`n%TestResult%" + } } else { $testResultMarkdown = "No enabled anti-phishing policy and matching rule meet all CIS recommendations ($portalLink).`n`n%TestResult%" }