Repository navigation
fix(cis): 2.1.4 and 2.1.7 pass rules whose user condition limits the scope #2850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
julien-ly
wants to merge
5
commits into
maester365:main
Choose a base branch
from
julien-ly:fix/cis-2.1.x-recipient-scope
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b579d9f
fix(cis): 2.1.4 and 2.1.7 pass rules whose user condition limits the …
julien-ly 2bfd8a4
fix(cis): state when 2.1.4 and 2.1.7 report Investigate
julien-ly 67af02a
test(cis): keep a failure when the scope is not the only failed check
julien-ly 2bfdcc1
test(cis): end the new test files with a newline
julien-ly 92aeeb7
fix(cis): use the plural when several anti-phishing policies need inv…
julien-ly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
powershell/tests/functions/Test-MtCisSafeAntiPhishingPolicy.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| 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 <Expected> for a rule scoped to <Scope>' -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 '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) | ||
| 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.') | ||
| } | ||
| } | ||
|
|
||
| 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") | ||
| } | ||
| } | ||
| } |
59 changes: 59 additions & 0 deletions
59
powershell/tests/functions/Test-MtCisSafeAttachment.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| 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 <Expected> for a custom rule scoped to <Scope>' -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') | ||
| } | ||
| } | ||
|
|
||
| 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' | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.