From aab7d8f59ee99918cf69a37e078c2fb5746bd978 Mon Sep 17 00:00:00 2001 From: Sven Frank <82213858+FiraSenax@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:43:34 +0100 Subject: [PATCH 1/5] fix(report): preserve literal tenant names in security check results --- .../Test-MtLiteralCisPolicyNames.Tests.ps1 | 90 +++++++++++++++++++ .../Test-MtSecurityCheckMarkdown.Tests.ps1 | 59 ++++++++++++ tests/cis/Test.CIS.M365.1.3.2.ps1 | 2 +- tests/cis/Test.CIS.M365.2.1.14.ps1 | 4 +- tests/cis/Test.CIS.M365.5.2.2.1.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.10.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.11.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.12.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.13.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.14.ps1 | 4 +- tests/cis/Test.CIS.M365.5.2.2.15.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.16.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.17.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.2.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.3.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.4.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.5.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.6.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.7.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.8.ps1 | 2 +- tests/cis/Test.CIS.M365.5.2.2.9.ps1 | 2 +- tests/maester/entra/Test.MT.1224.ps1 | 3 +- tests/maester/entra/Test.MT.1259.ps1 | 2 +- 23 files changed, 172 insertions(+), 24 deletions(-) create mode 100644 powershell/tests/functions/Test-MtLiteralCisPolicyNames.Tests.ps1 create mode 100644 powershell/tests/functions/Test-MtSecurityCheckMarkdown.Tests.ps1 diff --git a/powershell/tests/functions/Test-MtLiteralCisPolicyNames.Tests.ps1 b/powershell/tests/functions/Test-MtLiteralCisPolicyNames.Tests.ps1 new file mode 100644 index 0000000000..1067e0ed8b --- /dev/null +++ b/powershell/tests/functions/Test-MtLiteralCisPolicyNames.Tests.ps1 @@ -0,0 +1,90 @@ +BeforeDiscovery { + $cases = foreach ($id in @('1.3.2') + @(1..13 + 15..17 | ForEach-Object { "5.2.2.$_" })) { + foreach ($state in @('enabled', 'disabled', 'enabledForReportingButNotEnforced')) { + @{ Id = "CIS.M365.$id"; State = $state; Expected = $state -eq 'enabled' } + } + } +} +BeforeAll { + $script:root = (Resolve-Path "$PSScriptRoot/../../..").Path + Import-Module "$script:root/powershell/Maester.psd1" -Force + $script:adminRoles = @(& (Get-Module Maester) { Get-MtCisAdministratorRole | ForEach-Object Id }) +} +Describe 'Literal tenant names in CIS Conditional Access results' { + BeforeEach { + $script:policy = $null + Mock Get-MtConditionalAccessPolicy -ModuleName Maester { $script:policy } + Mock Test-MtConnection -ModuleName Maester { $true } + Mock Get-MgContext -ModuleName Maester { $null } + Mock Invoke-MtGraphRequest -ModuleName Maester { + switch ($RelativeUri) { + 'identity/conditionalAccess/namedLocations' { @() } + 'organization' { [pscustomobject]@{ displayName = 'Report test tenant' } } + 'policies/activityBasedTimeoutPolicies' { [pscustomobject]@{ definition=@('{"ActivityBasedTimeoutPolicy":{"ApplicationPolicies":[{"WebSessionIdleTimeout":"03:00:00"}]}}') } } + default { throw "Unexpected endpoint: $RelativeUri" } + } + } + } + It ' preserves replacement tokens and the verdict' -ForEach $cases { + $name = 'Policy $& $$ $1 ${name}' + $scope = @{ + users = @{ includeUsers=@('All'); excludeUsers=@(); includeGroups=@(); excludeGroups=@(); includeRoles=$script:adminRoles; excludeRoles=@() } + applications = @{ includeApplications=@('All'); excludeApplications=@(); includeUserActions=@(); includeAuthenticationContextClassReferences=@(); applicationFilter=$null } + clientAppTypes = @('all') + userRiskLevels = @() + signInRiskLevels = @() + } + $controls = @{ grantControls=@{ operator='OR'; builtInControls=@('mfa'); customAuthenticationFactors=@(); termsOfUse=@(); authenticationStrength=$null }; sessionControls=$null } + switch ($Id) { + 'CIS.M365.1.3.2' { + $scope.applications.includeApplications=@('Office365');$scope.clientAppTypes=@('browser') + $controls.sessionControls=@{applicationEnforcedRestrictions=@{isEnabled=$true}} + } + 'CIS.M365.5.2.2.3' { $scope.clientAppTypes=@('exchangeActiveSync','other');$controls.grantControls.builtInControls=@('block') } + 'CIS.M365.5.2.2.4' { $controls.sessionControls=@{persistentBrowser=@{isEnabled=$true;mode='never'};signInFrequency=@{isEnabled=$true;frequencyInterval='everyTime'}} } + 'CIS.M365.5.2.2.5' { $controls.grantControls.builtInControls=@();$controls.grantControls.authenticationStrength=@{id='strength';allowedCombinations=@('fido2')} } + 'CIS.M365.5.2.2.6' { + $scope.userRiskLevels=@('high');$controls.grantControls.operator='AND';$controls.grantControls.builtInControls=@('mfa','passwordChange') + $controls.sessionControls=@{signInFrequency=@{isEnabled=$true;frequencyInterval='everyTime'}} + } + 'CIS.M365.5.2.2.7' { $scope.signInRiskLevels=@('medium','high');$controls.grantControls.builtInControls=@('block') } + 'CIS.M365.5.2.2.8' { $scope.signInRiskLevels=@('medium','high');$controls.grantControls.builtInControls=@('block') } + 'CIS.M365.5.2.2.9' { $controls.grantControls.builtInControls=@('compliantDevice') } + 'CIS.M365.5.2.2.10' { + $scope.applications.includeApplications=@();$scope.applications.includeUserActions=@('urn:user:registersecurityinfo') + $controls.grantControls.builtInControls=@('compliantDevice') + } + 'CIS.M365.5.2.2.11' { + $scope.applications.includeApplications=@('d4ebce55-015a-49b5-a083-c84d1797ae8c') + $controls.sessionControls=@{signInFrequency=@{isEnabled=$true;frequencyInterval='everyTime'}} + } + 'CIS.M365.5.2.2.12' { $scope.authenticationFlows=@{transferMethods=@('deviceCodeFlow')};$controls.grantControls.builtInControls=@('block') } + 'CIS.M365.5.2.2.13' { $controls.sessionControls=@{signInFrequency=@{isEnabled=$true;frequencyInterval='everyTime'}} } + 'CIS.M365.5.2.2.15' { $scope.locations=@{includeLocations=@('All');excludeLocations=@('AllTrusted')};$controls.grantControls.builtInControls=@('block') } + 'CIS.M365.5.2.2.16' { + $scope.applications.includeApplications=@('00000002-0000-0ff1-ce00-000000000000','00000003-0000-0ff1-ce00-000000000000','cc15fd57-2c6c-4117-a88c-83b1d56b4bbe') + $scope.platforms=@{includePlatforms=@('windows');excludePlatforms=@()};$scope.clientAppTypes=@('mobileAppsAndDesktopClients') + $controls.sessionControls=@{secureSignInSession=@{isEnabled=$true}} + } + 'CIS.M365.5.2.2.17' { $scope.authenticationFlows=@{transferMethods=@('authenticationTransfer')};$controls.grantControls.builtInControls=@('block') } + } + $script:policy = [pscustomobject]@{ + id = 'standalone-policy' + displayName = $name + state = $State + conditions = $scope + grantControls = $controls.grantControls + sessionControls = $controls.sessionControls + } + $config = [pscustomobject]@{ Environment = [pscustomobject]@{ Licenses=@('AAD_PREMIUM','AAD_PREMIUM_P2'); Services=[pscustomobject]@{Graph=$true} } } + $row = Invoke-MtTest -Id $Id -Config $config + $row.Result | Should -Be $(if ($Expected) { 'Passed' } else { 'Failed' }) -Because ($row.ReasonDetail | Out-String) + $candidate = @($row.ResultDetail.TestResult -split "`n" | Where-Object { $_.StartsWith('| Policy') -and $_.Contains('$&') }) + $candidate | Should -HaveCount 1 + $candidate[0] | Should -Match ([regex]::Escape($name)) + $candidate[0] | Should -Match ([regex]::Escape($State)) + $cells = $candidate[0].Split('|') + $cells[-2].Trim() | Should -Be $(if ($Expected) { '✅ Pass' } else { '❌ Fail' }) + $script:policy.displayName | Should -BeExactly $name + } +} diff --git a/powershell/tests/functions/Test-MtSecurityCheckMarkdown.Tests.ps1 b/powershell/tests/functions/Test-MtSecurityCheckMarkdown.Tests.ps1 new file mode 100644 index 0000000000..c687e82eee --- /dev/null +++ b/powershell/tests/functions/Test-MtSecurityCheckMarkdown.Tests.ps1 @@ -0,0 +1,59 @@ +BeforeAll { + Import-Module "$PSScriptRoot/../../Maester.psd1" -Force +} +Describe 'Tenant-controlled names in security check reports' { + BeforeEach { + $script:captured=$null + Mock Add-MtTestResultDetail -ModuleName Maester { $script:captured=$Result } + } + It 'preserves literal replacement tokens in a CIS policy name' { + $script:policy=[pscustomobject]@{ + id='policy';displayName='Policy $& $$';state='enabled' + conditions=@{users=@{includeUsers=@('All')};applications=@{includeApplications=@('All');excludeApplications=@()};clientAppTypes=@('all');userRiskLevels=@();signInRiskLevels=@()} + grantControls=@{operator='OR';builtInControls=@('mfa');authenticationStrength=$null;termsOfUse=@();customAuthenticationFactors=@()} + } + Mock Get-MtConditionalAccessPolicy -ModuleName Maester {$script:policy} + & (Get-Module Maester) { Test-MtCisMfaForEveryUser }|Should -BeTrue + $script:captured|Should -Match ([regex]::Escape('Policy $& $$')) + } + It 'keeps a named location containing a pipe inside one cell' { + Mock Invoke-MtGraphRequest -ModuleName Maester { + [pscustomobject]@{'@odata.type'='#microsoft.graph.ipNamedLocation';displayName='HQ | Berlin $& $$';isTrusted=$true;ipRanges=@(@{cidrAddress='10.0.0.0/24'})} + } + & (Get-Module Maester) { Test-MtCisTrustedNamedLocation }|Should -BeTrue + $script:captured | Should -Match ([regex]::Escape('HQ \| Berlin $& $$')) + $lines=@($script:captured -split "`n"|Where-Object {$_.StartsWith('|')}) + ([regex]::Split($lines[2],'(? Date: Thu, 12 Nov 2026 01:46:16 +0100 Subject: [PATCH 2/5] fix(tests): restore UTF-8 BOM in private pack request tests --- powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 b/powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 index edba6469da..fc0f702b60 100644 --- a/powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 +++ b/powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 @@ -1,4 +1,4 @@ -BeforeAll { +BeforeAll { Import-Module "$PSScriptRoot/../../Maester.psd1" -Force $script:savedGhToken = $env:GH_TOKEN From 2a20df8399a5c62a098ef68735c926faea6fe0d2 Mon Sep 17 00:00:00 2001 From: Sven Frank <82213858+FiraSenax@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:02:53 +0100 Subject: [PATCH 3/5] fix(report): recover escaped affected-object names consistently --- .../Get-MtAffectedObjectFromMarkdown.ps1 | 7 +++--- .../Test-MtSecurityCheckMarkdown.Tests.ps1 | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/powershell/internal/report/affected-objects/Get-MtAffectedObjectFromMarkdown.ps1 b/powershell/internal/report/affected-objects/Get-MtAffectedObjectFromMarkdown.ps1 index 86eddea483..7f8724ae99 100644 --- a/powershell/internal/report/affected-objects/Get-MtAffectedObjectFromMarkdown.ps1 +++ b/powershell/internal/report/affected-objects/Get-MtAffectedObjectFromMarkdown.ps1 @@ -64,9 +64,10 @@ # different occurrence. .NET allows the repeated 'url' group name across the alternation. # https only: display names are not escaped in result markdown, so a crafted name can smuggle # a javascript: url into a link that would otherwise match a portal pattern. - # The name may contain one level of brackets ("[RaviK] - Policy") and the url may be followed - # by a link title (`[name](url "title")`). - $linkPattern = '\[(?(?:[^\[\]]|\[[^\[\]]*\])+)\]\((?https://[^)\s]+)(?:\s+"[^"]*")?\)|(?https://[^\s)\]]+)' + # Consume backslash escapes before interpreting brackets. Generated names can contain escaped + # unmatched or nested brackets; legacy unescaped names may contain one bracketed group. + # The url may be followed by a link title (`[name](url "title")`). + $linkPattern = '\[(?(?:\\.|[^\[\]\\]|\[(?:\\.|[^\[\]\\])*\])+)\]\((?https://[^)\s]+)(?:\s+"(?[^"]*)")?\)|(?<url>https://[^\s)\]]+)' # Markdown backslash escapes (\( \_ \* ...) that result markdown adds around display names. # Must stay a superset of what Get-MtSafeMarkdown escapes, or a name keeps a literal # backslash on the affected-objects page. `:` and `@` were added there for GFM autolink diff --git a/powershell/tests/functions/Test-MtSecurityCheckMarkdown.Tests.ps1 b/powershell/tests/functions/Test-MtSecurityCheckMarkdown.Tests.ps1 index c687e82eee..42f7c379bb 100644 --- a/powershell/tests/functions/Test-MtSecurityCheckMarkdown.Tests.ps1 +++ b/powershell/tests/functions/Test-MtSecurityCheckMarkdown.Tests.ps1 @@ -57,3 +57,25 @@ Describe 'Tenant-controlled names in security check reports' { ([regex]::Split($lines[2],'(?<!\\)\|')).Count|Should -Be ([regex]::Split($lines[0],'(?<!\\)\|')).Count } } + +Describe 'Affected-object matching with escaped display names' { + It 'preserves IDs and duplicate names for <Name>' -ForEach @( + @{ Name = 'Same name' } + @{ Name = 'Rule ] review' } + @{ Name = '[outer [inner] tail]' } + @{ Name = 'Back\slash [review] ]' } + @{ Name = 'Policy $& $$' } + ) { + $ids = @('11111111-1111-4111-8111-111111111111', '22222222-2222-4222-8222-222222222222') + $label = Get-MtSafeMarkdown $Name + $markdown = ($ids | ForEach-Object { "[$label](https://entra.microsoft.com/#view/Microsoft_AAD_ConditionalAccess/PolicyBlade/policyId/$_)" }) -join "`n" + $records = @(& (Get-Module Maester) { param($md) Get-MtAffectedObjectFromMarkdown -Markdown $md -TestId 'PROBE.1' } $markdown) + $records | Should -HaveCount 2 + @($records.Id | Sort-Object) | Should -Be $ids + foreach ($record in $records) { + $record.Type | Should -Be 'ConditionalAccessPolicy' + $record.DisplayName | Should -BeExactly $Name + $record.TestId | Should -Be 'PROBE.1' + } + } +} From 729fbcdcad06a8eb8e18999f63bdbe9dd0cae6e7 Mon Sep 17 00:00:00 2001 From: Sven Frank <82213858+FiraSenax@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:35:11 +0100 Subject: [PATCH 4/5] fix(tests): scope literal-name discovery cases explicitly --- .../tests/functions/Test-MtLiteralCisPolicyNames.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/powershell/tests/functions/Test-MtLiteralCisPolicyNames.Tests.ps1 b/powershell/tests/functions/Test-MtLiteralCisPolicyNames.Tests.ps1 index 1067e0ed8b..662fd29de0 100644 --- a/powershell/tests/functions/Test-MtLiteralCisPolicyNames.Tests.ps1 +++ b/powershell/tests/functions/Test-MtLiteralCisPolicyNames.Tests.ps1 @@ -1,5 +1,5 @@ BeforeDiscovery { - $cases = foreach ($id in @('1.3.2') + @(1..13 + 15..17 | ForEach-Object { "5.2.2.$_" })) { + $script:CisLiteralNameCases = foreach ($id in @('1.3.2') + @(1..13 + 15..17 | ForEach-Object { "5.2.2.$_" })) { foreach ($state in @('enabled', 'disabled', 'enabledForReportingButNotEnforced')) { @{ Id = "CIS.M365.$id"; State = $state; Expected = $state -eq 'enabled' } } @@ -25,7 +25,7 @@ Describe 'Literal tenant names in CIS Conditional Access results' { } } } - It '<Id> preserves replacement tokens and the <State> verdict' -ForEach $cases { + It '<Id> preserves replacement tokens and the <State> verdict' -ForEach $script:CisLiteralNameCases { $name = 'Policy $& $$ $1 ${name}' $scope = @{ users = @{ includeUsers=@('All'); excludeUsers=@(); includeGroups=@(); excludeGroups=@(); includeRoles=$script:adminRoles; excludeRoles=@() } From 53a044273ae3bab08700c572c98c68ac7d8f4777 Mon Sep 17 00:00:00 2001 From: Sven Frank <82213858+FiraSenax@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:01:09 +0100 Subject: [PATCH 5/5] chore(tests): defer unrelated BOM correction to #2795 --- powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 b/powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 index fc0f702b60..edba6469da 100644 --- a/powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 +++ b/powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 @@ -1,4 +1,4 @@ -BeforeAll { +BeforeAll { Import-Module "$PSScriptRoot/../../Maester.psd1" -Force $script:savedGhToken = $env:GH_TOKEN