From 59f718cc206a6625cc6975f6a72208fdee9882fb Mon Sep 17 00:00:00 2001 From: Sven Frank <82213858+FiraSenax@users.noreply.github.com> Date: Tue, 10 Nov 2026 07:02:21 +0100 Subject: [PATCH 1/2] fix(entra): escape Markdown in agent check results --- .../Test-MtEntraAgentMarkdown.Tests.ps1 | 190 ++++++++++++++++++ .../Test-MtEntraAgentSecurity.Tests.ps1 | 2 +- tests/maester/entra/Test.MT.1200.ps1 | 3 +- tests/maester/entra/Test.MT.1201.ps1 | 8 +- tests/maester/entra/Test.MT.1203.ps1 | 4 +- tests/maester/entra/Test.MT.1204.ps1 | 5 +- tests/maester/entra/Test.MT.1205.ps1 | 5 +- tests/maester/entra/Test.MT.1206.ps1 | 10 +- tests/maester/entra/Test.MT.1207.ps1 | 21 +- tests/maester/entra/Test.MT.1208.ps1 | 5 +- tests/maester/entra/Test.MT.1209.ps1 | 15 +- tests/maester/entra/Test.MT.1210.ps1 | 24 +-- tests/maester/entra/Test.MT.1211.ps1 | 5 +- tests/maester/entra/Test.MT.1212.ps1 | 3 +- tests/maester/entra/Test.MT.1213.ps1 | 7 +- tests/maester/entra/Test.MT.1223.ps1 | 5 +- 16 files changed, 230 insertions(+), 82 deletions(-) create mode 100644 powershell/tests/functions/Test-MtEntraAgentMarkdown.Tests.ps1 diff --git a/powershell/tests/functions/Test-MtEntraAgentMarkdown.Tests.ps1 b/powershell/tests/functions/Test-MtEntraAgentMarkdown.Tests.ps1 new file mode 100644 index 0000000000..45e8e008e7 --- /dev/null +++ b/powershell/tests/functions/Test-MtEntraAgentMarkdown.Tests.ps1 @@ -0,0 +1,190 @@ +param([string] $MarkdownCapturePath) + +BeforeDiscovery { + $checks = @( + @{ Id = 'MT.1200'; Command = 'Test-MtEntraAgentIdentityOrphaned' } + @{ Id = 'MT.1201'; Command = 'Test-MtEntraAgentUserOrphaned' } + @{ Id = 'MT.1203'; Command = 'Test-MtEntraAgentBlueprintOrphaned' } + @{ Id = 'MT.1204'; Command = 'Test-MtEntraAgentOwner' } + @{ Id = 'MT.1205'; Command = 'Test-MtEntraAgentSponsor' } + @{ Id = 'MT.1206'; Command = 'Test-MtEntraAgentInactive' } + @{ Id = 'MT.1207'; Command = 'Test-MtEntraAgentForeignPrivileged' } + @{ Id = 'MT.1208'; Command = 'Test-MtEntraAgentBlueprintCredentialHygiene' } + @{ Id = 'MT.1209'; Command = 'Test-MtEntraAgentDirectoryRoles' } + @{ Id = 'MT.1210'; Command = 'Test-MtEntraAgentUserExcessiveAccess' } + @{ Id = 'MT.1211'; Command = 'Test-MtEntraAgentBlueprintAllAllowedInheritance' } + @{ Id = 'MT.1212'; Command = 'Test-MtEntraAgentBlueprintOpenAccess' } + @{ Id = 'MT.1213'; Command = 'Test-MtEntraAgentBlueprintRedirectUriHygiene' } + @{ Id = 'MT.1223'; Command = 'Test-MtEntraAgentHighRiskGraphPermissions' } + ) + $names = @( + @{ Shape = 'disguised link'; Text = '[Sign in](https://phish.example/login)'; Escaped = '\[Sign in\]\(https\://phish.example/login\)' } + @{ Shape = 'image'; Text = '![beacon](https://phish.example/pixel.png)'; Escaped = '\!\[beacon\]\(https\://phish.example/pixel.png\)' } + @{ Shape = 'formatting'; Text = 'Contoso **admin** _app_ ~test~ \path `code` & Support'; Escaped = 'Contoso \*\*admin\*\* \_app\_ \~test\~ \\path \`code\` <owner> & Support' } + @{ Shape = 'bare URL'; Text = 'https://phish.example/login'; Escaped = 'https\://phish.example/login' } + @{ Shape = 'www URL'; Text = 'www.phish.example/login'; Escaped = 'www\.phish.example/login' } + @{ Shape = 'email'; Text = 'admin@phish.example'; Escaped = 'admin\@phish.example' } + @{ Shape = 'table and line breaks'; Text = "Contoso | Admin`r`nSecond row`rThird row`nFourth"; Escaped = 'Contoso \| Admin Second row Third row Fourth' } + # Keep the existing HTML encoding as well: Markdown decodes a raw © or A. + @{ Shape = 'literal HTML entities'; Text = 'Contoso © A A &'; Escaped = 'Contoso &copy; &#65; &#x41; &amp;' } + ) + $findingCases = foreach ($check in $checks) { + foreach ($name in $names) { $check + $name } + } + $observationCases = foreach ($check in $checks | Where-Object Id -In 'MT.1207', 'MT.1209', 'MT.1210') { + foreach ($name in $names) { $check + $name } + } +} + +Describe 'Entra Agent ID result Markdown' { + BeforeAll { + Import-Module "$PSScriptRoot/../../Maester.psd1" -Force + $script:AgentMarkdownCaptures = [System.Collections.Generic.List[object]]::new() + } + + BeforeEach { + $script:AgentMarkdownTest = @{ + Id = ''; Shape = ''; Text = ''; Name = ''; RoleName = 'Global Administrator' + ObserveOnly = $false; Result = $null; SkippedBecause = $null + } + Mock Add-MtTestResultDetail -ModuleName Maester { + $script:AgentMarkdownTest.Result = $Result + $script:AgentMarkdownTest.SkippedBecause = $SkippedBecause + } + Mock Get-MtDirectoryRoleAssignment -ModuleName Maester { + @{ + Assignments = @([pscustomobject]@{ principalId = 'user-1'; roleDefinitionId = 'role-1'; directoryScopeId = '/' }) + EligibleChecked = $true + } + } + Mock Get-MtEntraAgentHighRiskGraphPermissionFinding -ModuleName Maester { + @{ + AgentCount = 1 + Findings = @([pscustomobject]@{ + AgentId = 'agent-1'; DisplayName = $script:AgentMarkdownTest.Name; AppId = 'agent-app' + Permission = 'Policy.ReadWrite.ConditionalAccess'; PermissionType = 'Application'; AttackPath = 'Direct' + }) + } + } + Mock Invoke-MtGraphRequest -ModuleName Maester { + $test = $script:AgentMarkdownTest + switch -Wildcard ($RelativeUri) { + 'organization' { return @([pscustomobject]@{ id = 'local-tenant' }) } + 'servicePrincipals/microsoft.graph.agentIdentity' { + return @([pscustomobject]@{ + id = 'agent-1'; appId = 'agent-app'; displayName = $test.Name + agentIdentityBlueprintId = 'blueprint-app'; accountEnabled = $true; createdDateTime = '2000-01-01T00:00:00Z' + }) + } + 'servicePrincipals/microsoft.graph.agentIdentityBlueprintPrincipal' { + if ($test.Id -eq 'MT.1200') { return @() } + return @([pscustomobject]@{ + id = 'principal-1'; appId = 'blueprint-app'; displayName = $test.Name + appOwnerOrganizationId = 'foreign-tenant'; appRoleAssignmentRequired = $false + appRoles = @([pscustomobject]@{ id = 'app-role'; isEnabled = $true }) + }) + } + 'applications/microsoft.graph.agentIdentityBlueprint' { + if ($test.Id -eq 'MT.1203') { return @() } + return @([pscustomobject]@{ + id = 'blueprint-1'; appId = 'blueprint-app'; displayName = $test.Name + passwordCredentials = @([pscustomobject]@{ keyId = 'secret-1'; startDateTime = '2000-01-01T00:00:00Z'; endDateTime = '2001-01-01T00:00:00Z' }) + keyCredentials = @([pscustomobject]@{ keyId = 'key-1'; startDateTime = '2020-01-01T00:00:00Z'; endDateTime = '2100-01-01T00:00:00Z' }) + web = @{ redirectUris = @('http://phish.example/*') } + }) + } + 'users/microsoft.graph.agentUser' { + return @([pscustomobject]@{ + id = 'user-1'; displayName = $test.Name; userPrincipalName = 'agent_user@phish.example'; identityParentId = 'missing-agent' + }) + } + 'roleManagement/directory/roleAssignments' { + return @( + [pscustomobject]@{ principalId = 'principal-1'; roleDefinitionId = 'role-1'; directoryScopeId = '/' } + [pscustomobject]@{ principalId = 'agent-1'; roleDefinitionId = 'role-1'; directoryScopeId = '/' } + ) + } + 'roleManagement/directory/roleDefinitions' { + return @([pscustomobject]@{ id = 'role-1'; displayName = $test.RoleName }) + } + '*/transitiveMemberOf/microsoft.graph.group' { + if ($test.ObserveOnly) { return @() } + return @([pscustomobject]@{ id = 'group-1'; displayName = $test.Name; isAssignableToRole = $true }) + } + '*/appRoleAssignments' { + if ($test.ObserveOnly) { return @() } + return @([pscustomobject]@{ id = 'assignment-1'; resourceDisplayName = $test.Name }) + } + '*/inheritablePermissions' { + return @([pscustomobject]@{ + resourceAppId = 'resource-app'; inheritableScopes = @{ kind = 'allAllowed' }; inheritableRoles = @{ kind = 'allAllowed' } + }) + } + '*/owners' { return @() } + '*/sponsors' { return @() } + 'reports/*' { return @() } + default { throw "Unexpected Graph request: $RelativeUri" } + } + } + } + + AfterEach { + $script:AgentMarkdownCaptures.Add([pscustomobject]@{ + Id = $script:AgentMarkdownTest.Id; Shape = $script:AgentMarkdownTest.Shape + ExpectedText = ($script:AgentMarkdownTest.Text -replace '\r\n|\r|\n', ' ') + Markdown = $script:AgentMarkdownTest.Result + }) + Remove-Variable -Name AgentMarkdownTest -Scope Script -ErrorAction SilentlyContinue + } + + AfterAll { + # Optional evidence for checking these exact results with the report's Markdown renderer. + if ($MarkdownCapturePath) { + $script:AgentMarkdownCaptures | ConvertTo-Json -Depth 5 | Set-Content -LiteralPath $MarkdownCapturePath -Encoding utf8 + } + Remove-Variable -Name AgentMarkdownCaptures -Scope Script -ErrorAction SilentlyContinue + } + + It ' keeps a literal in findings' -ForEach $findingCases { + $script:AgentMarkdownTest.Id = $Id + $script:AgentMarkdownTest.Shape = $Shape + $script:AgentMarkdownTest.Text = $Text + $script:AgentMarkdownTest.Name = $Text + + $verdict = InModuleScope Maester -Parameters @{ CheckCommand = $Command } { + param($CheckCommand) + & $CheckCommand + } + + $verdict | Should -BeFalse + $script:AgentMarkdownTest.SkippedBecause | Should -BeNullOrEmpty + $script:AgentMarkdownTest.Result | Should -Match ([regex]::Escape($Escaped)) + $script:AgentMarkdownTest.Result | Should -Not -Match ([regex]::Escape($Text)) + if ($Id -in 'MT.1201', 'MT.1210') { + $script:AgentMarkdownTest.Result | Should -Match 'agent\\_user\\@phish\.example' + } + if ($Id -eq 'MT.1213') { + $script:AgentMarkdownTest.Result | Should -Match 'http\\://phish\.example/\\\*' + } + } + + It ' keeps a custom role containing a literal in passing observations' -ForEach $observationCases { + $script:AgentMarkdownTest.Id = $Id + $script:AgentMarkdownTest.Shape = "observation: $Shape" + $script:AgentMarkdownTest.Text = $Text + $script:AgentMarkdownTest.Name = 'Ordinary agent' + $script:AgentMarkdownTest.RoleName = $Text + $script:AgentMarkdownTest.ObserveOnly = $true + + $verdict = InModuleScope Maester -Parameters @{ CheckCommand = $Command } { + param($CheckCommand) + & $CheckCommand + } + + $verdict | Should -BeTrue + $script:AgentMarkdownTest.SkippedBecause | Should -BeNullOrEmpty + $script:AgentMarkdownTest.Result | Should -Match 'Observation:' + $script:AgentMarkdownTest.Result | Should -Match ([regex]::Escape($Escaped)) + $script:AgentMarkdownTest.Result | Should -Not -Match ([regex]::Escape($Text)) + } +} diff --git a/powershell/tests/functions/Test-MtEntraAgentSecurity.Tests.ps1 b/powershell/tests/functions/Test-MtEntraAgentSecurity.Tests.ps1 index e4e691c798..22db1be24c 100644 --- a/powershell/tests/functions/Test-MtEntraAgentSecurity.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraAgentSecurity.Tests.ps1 @@ -700,7 +700,7 @@ Test-MtEntraAgentUserExcessiveAccess | Should -BeTrue $script:TestResult | Should -Match 'Custom Agent Role' $script:TestResult | Should -Match 'Agent User 1' - $script:TestResult | Should -Match 'au1@ contoso.com' + $script:TestResult | Should -Match 'au1\\@ contoso.com' $script:TestResult | Should -Not -Match "Agent`nUser 1" $script:TestResult | Should -Not -Match "au1@`r`ncontoso.com" $script:TestResult | Should -Not -Match "Custom`r`nAgent Role" diff --git a/tests/maester/entra/Test.MT.1200.ps1 b/tests/maester/entra/Test.MT.1200.ps1 index c9d4ceaa3d..55c275ab0c 100644 --- a/tests/maester/entra/Test.MT.1200.ps1 +++ b/tests/maester/entra/Test.MT.1200.ps1 @@ -101,8 +101,7 @@ if ([string]::IsNullOrWhiteSpace($DisplayName)) { $DisplayName = '(unnamed)' } - $DisplayName = [System.Net.WebUtility]::HtmlEncode($DisplayName) -replace '\|', '|' - $DisplayName = $DisplayName -replace "`r?`n", ' ' + $DisplayName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($DisplayName)) $Reason = [string]$OrphanedIdentity.Reason -replace '\|', '\|' $Result += "`n| $($OrphanedIdentity.AgentIdentityId) | $DisplayName | " + "$($OrphanedIdentity.BlueprintAppId) | $Reason |" diff --git a/tests/maester/entra/Test.MT.1201.ps1 b/tests/maester/entra/Test.MT.1201.ps1 index 071ac864f1..cb0d39397c 100644 --- a/tests/maester/entra/Test.MT.1201.ps1 +++ b/tests/maester/entra/Test.MT.1201.ps1 @@ -102,12 +102,8 @@ if ([string]::IsNullOrWhiteSpace($DisplayName)) { $DisplayName = '(unnamed)' } - $DisplayName = [System.Net.WebUtility]::HtmlEncode($DisplayName) -replace '\|', '|' - $DisplayName = $DisplayName -replace "`r?`n", ' ' - $UserPrincipalName = [System.Net.WebUtility]::HtmlEncode( - [string]$OrphanedUser.UserPrincipalName - ) -replace '\|', '|' - $UserPrincipalName = $UserPrincipalName -replace "`r?`n", ' ' + $DisplayName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($DisplayName)) + $UserPrincipalName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$OrphanedUser.UserPrincipalName)) $Reason = [string]$OrphanedUser.Reason -replace '\|', '\|' $Result += "`n| $($OrphanedUser.AgentUserId) | $DisplayName | " + "$UserPrincipalName | $($OrphanedUser.ParentIdentityId) | $Reason |" diff --git a/tests/maester/entra/Test.MT.1203.ps1 b/tests/maester/entra/Test.MT.1203.ps1 index f344d1f7e3..fc42671202 100644 --- a/tests/maester/entra/Test.MT.1203.ps1 +++ b/tests/maester/entra/Test.MT.1203.ps1 @@ -92,9 +92,7 @@ $Result += "`n`n| Blueprint Principal object ID | Display name | Blueprint App ID | " + "Linked Agent Identity object IDs |`n| --- | --- | --- | --- |" foreach ($Principal in $OrphanedPrincipals) { - $Name = [System.Net.WebUtility]::HtmlEncode($Principal.DisplayName) - $Name = $Name -replace '\|', '|' - $Name = $Name -replace "`r?`n", ' ' + $Name = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($Principal.DisplayName)) if ([string]::IsNullOrWhiteSpace($Name)) { $Name = '(unnamed)' } $IdentityIds = if ($Principal.AgentIdentityIds.Count -gt 0) { $Principal.AgentIdentityIds -join ', ' diff --git a/tests/maester/entra/Test.MT.1204.ps1 b/tests/maester/entra/Test.MT.1204.ps1 index a1dffcb4be..31fffd8963 100644 --- a/tests/maester/entra/Test.MT.1204.ps1 +++ b/tests/maester/entra/Test.MT.1204.ps1 @@ -159,9 +159,8 @@ foreach ($Item in $OwnerlessObjects) { $DisplayName = [string]$Item.DisplayName if ([string]::IsNullOrWhiteSpace($DisplayName)) { $DisplayName = '(unnamed)' } - $DisplayName = [System.Net.WebUtility]::HtmlEncode($DisplayName) -replace '\|', '|' - $DisplayName = $DisplayName -replace "`r?`n", ' ' - $Reason = [System.Net.WebUtility]::HtmlEncode([string]$Item.Reason) -replace '\|', '|' + $DisplayName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($DisplayName)) + $Reason = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.Reason)) $Result += "`n| $($Item.ObjectId) | $DisplayName | $($Item.ObjectType) | $Reason |" } diff --git a/tests/maester/entra/Test.MT.1205.ps1 b/tests/maester/entra/Test.MT.1205.ps1 index c5054f8bb3..df462a5dad 100644 --- a/tests/maester/entra/Test.MT.1205.ps1 +++ b/tests/maester/entra/Test.MT.1205.ps1 @@ -125,9 +125,8 @@ foreach ($Item in $SponsorlessObjects) { $DisplayName = [string]$Item.DisplayName if ([string]::IsNullOrWhiteSpace($DisplayName)) { $DisplayName = '(unnamed)' } - $DisplayName = [System.Net.WebUtility]::HtmlEncode($DisplayName) -replace '\|', '|' - $DisplayName = $DisplayName -replace "`r?`n", ' ' - $Reason = [System.Net.WebUtility]::HtmlEncode([string]$Item.Reason) -replace '\|', '|' + $DisplayName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($DisplayName)) + $Reason = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.Reason)) $Result += "`n| $($Item.ObjectId) | $DisplayName | $($Item.ObjectType) | $Reason |" } diff --git a/tests/maester/entra/Test.MT.1206.ps1 b/tests/maester/entra/Test.MT.1206.ps1 index aab1ab9f64..0063fd650a 100644 --- a/tests/maester/entra/Test.MT.1206.ps1 +++ b/tests/maester/entra/Test.MT.1206.ps1 @@ -246,9 +246,8 @@ foreach ($Item in $InactiveAgents) { $DisplayName = [string]$Item.DisplayName if ([string]::IsNullOrWhiteSpace($DisplayName)) { $DisplayName = '(unnamed)' } - $DisplayName = [System.Net.WebUtility]::HtmlEncode($DisplayName) -replace '\|', '|' - $DisplayName = $DisplayName -replace "`r?`n", ' ' - $Reason = [System.Net.WebUtility]::HtmlEncode([string]$Item.Reason) -replace '\|', '|' + $DisplayName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($DisplayName)) + $Reason = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.Reason)) $Result += "`n| $($Item.ObjectId) | $DisplayName | $($Item.AppId) | " + "$($Item.LastSignIn) | $($Item.InactiveDays) | $Reason |" } @@ -260,9 +259,8 @@ foreach ($Item in $DormantFleetFindings) { $Name = [string]$Item.DisplayName if ([string]::IsNullOrWhiteSpace($Name)) { $Name = '(unnamed)' } - $Name = [System.Net.WebUtility]::HtmlEncode($Name) -replace '\|', '|' - $Name = $Name -replace "`r?`n", ' ' - $Reason = [System.Net.WebUtility]::HtmlEncode([string]$Item.Reason) -replace '\|', '|' + $Name = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($Name)) + $Reason = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.Reason)) $Result += "`n| $($Item.BlueprintId) | $Name | $($Item.AppId) | " + "$($Item.AgentCount) | $Reason |" } diff --git a/tests/maester/entra/Test.MT.1207.ps1 b/tests/maester/entra/Test.MT.1207.ps1 index cc4ac1e854..df1ecd2496 100644 --- a/tests/maester/entra/Test.MT.1207.ps1 +++ b/tests/maester/entra/Test.MT.1207.ps1 @@ -243,15 +243,9 @@ $ObservationNote += "`n`n| Object ID | Display name | Object type | Directory role |" $ObservationNote += "`n| --- | --- | --- | --- |" foreach ($Item in $RoleObservations) { - $DisplayName = [System.Net.WebUtility]::HtmlEncode( - [string]$Item.DisplayName - ) -replace '\|', '|' - $DisplayName = $DisplayName -replace '[\r\n]+', ' ' + $DisplayName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.DisplayName)) if ([string]::IsNullOrWhiteSpace($DisplayName)) { $DisplayName = '(unnamed)' } - $RoleName = [System.Net.WebUtility]::HtmlEncode( - [string]$Item.RoleName - ) -replace '\|', '|' - $RoleName = $RoleName -replace '[\r\n]+', ' ' + $RoleName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.RoleName)) $ObservationNote += "`n| $($Item.ObjectId) | $DisplayName | " + "$($Item.ObjectType) | $RoleName |" } @@ -265,10 +259,8 @@ foreach ($Item in $PermissionObservations) { $DisplayName = [string]$Item.DisplayName if ([string]::IsNullOrWhiteSpace($DisplayName)) { $DisplayName = '(unnamed)' } - $DisplayName = [System.Net.WebUtility]::HtmlEncode($DisplayName) -replace '\|', '|' - $DisplayName = $DisplayName -replace '[\r\n]+', ' ' - $Resources = [System.Net.WebUtility]::HtmlEncode([string]$Item.Resources) -replace '\|', '|' - $Resources = $Resources -replace '[\r\n]+', ' ' + $DisplayName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($DisplayName)) + $Resources = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.Resources)) $ObservationNote += "`n| $($Item.ObjectId) | $DisplayName | " + "$($Item.ForeignTenant) | $($Item.Count) | $Resources |" } @@ -287,9 +279,8 @@ foreach ($Item in $ForeignFindings) { $DisplayName = [string]$Item.DisplayName if ([string]::IsNullOrWhiteSpace($DisplayName)) { $DisplayName = '(unnamed)' } - $DisplayName = [System.Net.WebUtility]::HtmlEncode($DisplayName) -replace '\|', '|' - $DisplayName = $DisplayName -replace "`r?`n", ' ' - $PrivName = [System.Net.WebUtility]::HtmlEncode([string]$Item.PrivilegeName) -replace '\|', '|' + $DisplayName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($DisplayName)) + $PrivName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.PrivilegeName)) $Result += "`n| $($Item.ObjectId) | $DisplayName | $($Item.ObjectType) | " + "$($Item.ForeignTenant) | $($Item.PrivilegeType) | $PrivName |" } diff --git a/tests/maester/entra/Test.MT.1208.ps1 b/tests/maester/entra/Test.MT.1208.ps1 index 173fb8a8f2..c3e89b7c1b 100644 --- a/tests/maester/entra/Test.MT.1208.ps1 +++ b/tests/maester/entra/Test.MT.1208.ps1 @@ -164,9 +164,8 @@ foreach ($Item in $CredentialFindings) { $Name = [string]$Item.DisplayName if ([string]::IsNullOrWhiteSpace($Name)) { $Name = '(unnamed)' } - $Name = [System.Net.WebUtility]::HtmlEncode($Name) -replace '\|', '|' - $Name = $Name -replace "`r?`n", ' ' - $Issue = [System.Net.WebUtility]::HtmlEncode([string]$Item.Issue) -replace '\|', '|' + $Name = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($Name)) + $Issue = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.Issue)) $Result += "`n| $($Item.BlueprintId) | $Name | $($Item.AppId) | " + "$($Item.KeyId) | $($Item.Type) | $Issue |" } diff --git a/tests/maester/entra/Test.MT.1209.ps1 b/tests/maester/entra/Test.MT.1209.ps1 index 3336495e49..ef9989210e 100644 --- a/tests/maester/entra/Test.MT.1209.ps1 +++ b/tests/maester/entra/Test.MT.1209.ps1 @@ -141,15 +141,9 @@ $ObservationNote += "`n`n| Object ID | Display name | Object type | Directory role |" $ObservationNote += "`n| --- | --- | --- | --- |" foreach ($Item in $RoleObservations) { - $Name = [System.Net.WebUtility]::HtmlEncode( - [string]$Item.DisplayName - ) -replace '\|', '|' - $Name = $Name -replace '[\r\n]+', ' ' + $Name = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.DisplayName)) if ([string]::IsNullOrWhiteSpace($Name)) { $Name = '(unnamed)' } - $RoleName = [System.Net.WebUtility]::HtmlEncode( - [string]$Item.RoleName - ) -replace '\|', '|' - $RoleName = $RoleName -replace '[\r\n]+', ' ' + $RoleName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.RoleName)) $ObservationNote += "`n| $($Item.ObjectId) | $Name | " + "$($Item.ObjectType) | $RoleName |" } @@ -169,9 +163,8 @@ foreach ($Item in $RoleFindings) { $Name = [string]$Item.DisplayName if ([string]::IsNullOrWhiteSpace($Name)) { $Name = '(unnamed)' } - $Name = [System.Net.WebUtility]::HtmlEncode($Name) -replace '\|', '|' - $Name = $Name -replace "`r?`n", ' ' - $RoleName = [System.Net.WebUtility]::HtmlEncode([string]$Item.RoleName) -replace '\|', '|' + $Name = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($Name)) + $RoleName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.RoleName)) $Scope = if ([string]::IsNullOrWhiteSpace($Item.Scope) -or $Item.Scope -eq '/') { 'Tenant-wide (/)' } else { $Item.Scope } $Result += "`n| $($Item.ObjectId) | $Name | $($Item.ObjectType) | $RoleName | $Scope |" } diff --git a/tests/maester/entra/Test.MT.1210.ps1 b/tests/maester/entra/Test.MT.1210.ps1 index eebb0bfa0a..4f12b06503 100644 --- a/tests/maester/entra/Test.MT.1210.ps1 +++ b/tests/maester/entra/Test.MT.1210.ps1 @@ -172,19 +172,10 @@ 'User principal name | Directory role |' $ObservationNote += "`n| --- | --- | --- | --- |" foreach ($Item in $RoleObservations) { - $Name = [System.Net.WebUtility]::HtmlEncode( - [string]$Item.DisplayName - ) -replace '\|', '|' - $Name = $Name -replace '[\r\n]+', ' ' + $Name = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.DisplayName)) if ([string]::IsNullOrWhiteSpace($Name)) { $Name = '(unnamed)' } - $UPN = [System.Net.WebUtility]::HtmlEncode( - [string]$Item.UserPrincipalName - ) -replace '\|', '|' - $UPN = $UPN -replace '[\r\n]+', ' ' - $RoleName = [System.Net.WebUtility]::HtmlEncode( - [string]$Item.RoleName - ) -replace '\|', '|' - $RoleName = $RoleName -replace '[\r\n]+', ' ' + $UPN = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.UserPrincipalName)) + $RoleName = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.RoleName)) $ObservationNote += "`n| $($Item.UserId) | $Name | $UPN | $RoleName |" } } @@ -203,11 +194,10 @@ foreach ($Item in $ExcessiveAccessFindings) { $Name = [string]$Item.DisplayName if ([string]::IsNullOrWhiteSpace($Name)) { $Name = '(unnamed)' } - $Name = [System.Net.WebUtility]::HtmlEncode($Name) -replace '\|', '|' - $Name = $Name -replace "`r?`n", ' ' - $UPN = [System.Net.WebUtility]::HtmlEncode([string]$Item.UserPrincipalName) -replace '\|', '|' - $PrivItem = [System.Net.WebUtility]::HtmlEncode([string]$Item.PrivilegedItem) -replace '\|', '|' - $Reason = [System.Net.WebUtility]::HtmlEncode([string]$Item.Reason) -replace '\|', '|' + $Name = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($Name)) + $UPN = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.UserPrincipalName)) + $PrivItem = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.PrivilegedItem)) + $Reason = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.Reason)) $Result += "`n| $($Item.UserId) | $Name | $UPN | $($Item.AccessType) | " + "$PrivItem | $Reason |" } diff --git a/tests/maester/entra/Test.MT.1211.ps1 b/tests/maester/entra/Test.MT.1211.ps1 index 2f2e9220c8..dc0ac77f27 100644 --- a/tests/maester/entra/Test.MT.1211.ps1 +++ b/tests/maester/entra/Test.MT.1211.ps1 @@ -108,9 +108,8 @@ foreach ($Item in $InheritanceFindings) { $Name = [string]$Item.DisplayName if ([string]::IsNullOrWhiteSpace($Name)) { $Name = '(unnamed)' } - $Name = [System.Net.WebUtility]::HtmlEncode($Name) -replace '\|', '|' - $Name = $Name -replace "`r?`n", ' ' - $Issue = [System.Net.WebUtility]::HtmlEncode([string]$Item.Issue) -replace '\|', '|' + $Name = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($Name)) + $Issue = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.Issue)) $Result += "`n| $($Item.BlueprintId) | $Name | $($Item.AppId) | " + "$($Item.Resource) | $($Item.Kind) | $Issue |" } diff --git a/tests/maester/entra/Test.MT.1212.ps1 b/tests/maester/entra/Test.MT.1212.ps1 index d313a874d0..0b7ceb8151 100644 --- a/tests/maester/entra/Test.MT.1212.ps1 +++ b/tests/maester/entra/Test.MT.1212.ps1 @@ -82,8 +82,7 @@ foreach ($Item in $OpenAccessFindings) { $Name = [string]$Item.DisplayName if ([string]::IsNullOrWhiteSpace($Name)) { $Name = '(unnamed)' } - $Name = [System.Net.WebUtility]::HtmlEncode($Name) -replace '\|', '|' - $Name = $Name -replace "`r?`n", ' ' + $Name = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($Name)) $Result += "`n| $($Item.ObjectId) | $Name | $($Item.AppId) | $($Item.RoleCount) |" } diff --git a/tests/maester/entra/Test.MT.1213.ps1 b/tests/maester/entra/Test.MT.1213.ps1 index a335f7dd95..d71bd49c4a 100644 --- a/tests/maester/entra/Test.MT.1213.ps1 +++ b/tests/maester/entra/Test.MT.1213.ps1 @@ -112,10 +112,9 @@ foreach ($Item in $RedirectFindings) { $Name = [string]$Item.DisplayName if ([string]::IsNullOrWhiteSpace($Name)) { $Name = '(unnamed)' } - $Name = [System.Net.WebUtility]::HtmlEncode($Name) -replace '\|', '|' - $Name = $Name -replace "`r?`n", ' ' - $Uri = [System.Net.WebUtility]::HtmlEncode([string]$Item.Uri) -replace '\|', '|' - $Issue = [System.Net.WebUtility]::HtmlEncode([string]$Item.Issue) -replace '\|', '|' + $Name = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($Name)) + $Uri = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.Uri)) + $Issue = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Item.Issue)) $Result += "`n| $($Item.BlueprintId) | $Name | $($Item.AppId) | $Uri | $Issue |" } diff --git a/tests/maester/entra/Test.MT.1223.ps1 b/tests/maester/entra/Test.MT.1223.ps1 index 8005bed01b..dc1b701c1f 100644 --- a/tests/maester/entra/Test.MT.1223.ps1 +++ b/tests/maester/entra/Test.MT.1223.ps1 @@ -59,9 +59,8 @@ foreach ($Finding in $Findings) { $Name = [string]$Finding.DisplayName if ([string]::IsNullOrWhiteSpace($Name)) { $Name = '(unnamed)' } - $Name = [System.Net.WebUtility]::HtmlEncode($Name) -replace '\|', '|' - $Name = $Name -replace "`r?`n", ' ' - $Permission = [System.Net.WebUtility]::HtmlEncode([string]$Finding.Permission) + $Name = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode($Name)) + $Permission = Get-MtSafeMarkdown -Text ([System.Net.WebUtility]::HtmlEncode([string]$Finding.Permission)) $Result += "`n| $($Finding.AgentId) | $Name | $($Finding.AppId) | " + "$Permission | $($Finding.PermissionType) | $($Finding.AttackPath) |" } From b804a78be1f9a4727bae442d04ac9dd571b2f870 Mon Sep 17 00:00:00 2001 From: Merill Fernando Date: Wed, 11 Nov 2026 06:52:50 +1100 Subject: [PATCH 2/2] test(entra): drop the Markdown capture parameter from the Agent ID tests The capture only served a renderer check outside the repo. Co-Authored-By: Claude Opus 5.5 Entire-Checkpoint: 01M73GAEKXR7XGF02CNA7498VS --- .../Test-MtEntraAgentMarkdown.Tests.ps1 | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraAgentMarkdown.Tests.ps1 b/powershell/tests/functions/Test-MtEntraAgentMarkdown.Tests.ps1 index 45e8e008e7..3b8b066b81 100644 --- a/powershell/tests/functions/Test-MtEntraAgentMarkdown.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraAgentMarkdown.Tests.ps1 @@ -1,6 +1,4 @@ -param([string] $MarkdownCapturePath) - -BeforeDiscovery { +BeforeDiscovery { $checks = @( @{ Id = 'MT.1200'; Command = 'Test-MtEntraAgentIdentityOrphaned' } @{ Id = 'MT.1201'; Command = 'Test-MtEntraAgentUserOrphaned' } @@ -39,7 +37,6 @@ BeforeDiscovery { Describe 'Entra Agent ID result Markdown' { BeforeAll { Import-Module "$PSScriptRoot/../../Maester.psd1" -Force - $script:AgentMarkdownCaptures = [System.Collections.Generic.List[object]]::new() } BeforeEach { @@ -128,21 +125,8 @@ Describe 'Entra Agent ID result Markdown' { } } - AfterEach { - $script:AgentMarkdownCaptures.Add([pscustomobject]@{ - Id = $script:AgentMarkdownTest.Id; Shape = $script:AgentMarkdownTest.Shape - ExpectedText = ($script:AgentMarkdownTest.Text -replace '\r\n|\r|\n', ' ') - Markdown = $script:AgentMarkdownTest.Result - }) - Remove-Variable -Name AgentMarkdownTest -Scope Script -ErrorAction SilentlyContinue - } - AfterAll { - # Optional evidence for checking these exact results with the report's Markdown renderer. - if ($MarkdownCapturePath) { - $script:AgentMarkdownCaptures | ConvertTo-Json -Depth 5 | Set-Content -LiteralPath $MarkdownCapturePath -Encoding utf8 - } - Remove-Variable -Name AgentMarkdownCaptures -Scope Script -ErrorAction SilentlyContinue + Remove-Variable -Name AgentMarkdownTest -Scope Script -ErrorAction SilentlyContinue } It ' keeps a literal in findings' -ForEach $findingCases {