From 62f08438c09d2ba1fc36a79d150b3a1beb0ac65c Mon Sep 17 00:00:00 2001 From: agnivesh <320655+agnivesh@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:14:49 +0000 Subject: [PATCH 1/2] fix(report): escape an ampersand that starts an HTML entity Get-MtSafeMarkdown left an HTML entity in a tenant-controlled name as it was, so the report showed "Ops ©" as "Ops (c)" and two different names could look the same. Escape an ampersand that is followed by an entity body and a semicolon (©, ©, ©) with a backslash, which the markdown renderer consumes. A plain ampersand, as in R&D or Ops & Support, is left alone, so the JSON output does not fill with backslashes. --- .../public/report/Get-MtSafeMarkdown.ps1 | 17 ++++++++-- .../functions/Get-MtSafeMarkdown.Tests.ps1 | 33 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/powershell/public/report/Get-MtSafeMarkdown.ps1 b/powershell/public/report/Get-MtSafeMarkdown.ps1 index 436d6d251a..8c6060bf82 100644 --- a/powershell/public/report/Get-MtSafeMarkdown.ps1 +++ b/powershell/public/report/Get-MtSafeMarkdown.ps1 @@ -14,8 +14,10 @@ Characters with meaning in markdown are backslash-escaped, and so are the ones that make GFM turn text into a link on its own: a bare 'https://host', 'ftp://host', 'www.host', 'a@b' or 'mailto:a@b' is an autolink literal, not markdown syntax, and the report renders with - 'gfm: true'. Line breaks are replaced with spaces. Every backslash added here is consumed by - the renderer, so the value is displayed exactly as entered. + 'gfm: true'. An ampersand that starts an HTML entity, such as '©' or '©', is + escaped as well. Without it the renderer would show the character the entity stands for + and not the text. Line breaks are replaced with spaces. Every backslash added here is + consumed by the renderer, so the value is displayed exactly as entered. .PARAMETER Text The text to escape @@ -63,5 +65,14 @@ # tenant-controlled name each render as a live link without them. A plain `contoso.com` # is deliberately left alone: GFM does not autolink it, and escaping every dot would be # noise in the JSON output for no gain. - ($Text -replace '([\\`*_\[\]()!|<>~:@])', '\$1') -replace '(?<=www)\.', '\.' + $Text = ($Text -replace '([\\`*_\[\]()!|<>~:@])', '\$1') -replace '(?<=www)\.', '\.' + + # Third pass: an ampersand that starts an HTML entity (`©`, `©`, `©`). The report + # renders markdown to HTML, and a browser turns the entity into its character, so `Ops ©` + # would show as `Ops (c)` and two different names could look the same. `\&` is a markdown + # escape that the renderer consumes, so the entity is shown as typed. It runs last, so the + # backslash it adds is not escaped again, and it only matches an ampersand followed by an + # entity body and a semicolon: a plain `R&D` or `Ops & Support` is left alone, so the JSON + # output does not fill with backslashes. + $Text -replace '&(?=#[0-9]+;|#[xX][0-9A-Fa-f]+;|[A-Za-z][A-Za-z0-9]*;)', '\&' } diff --git a/powershell/tests/functions/Get-MtSafeMarkdown.Tests.ps1 b/powershell/tests/functions/Get-MtSafeMarkdown.Tests.ps1 index e0efa0b1e7..ced9777b94 100644 --- a/powershell/tests/functions/Get-MtSafeMarkdown.Tests.ps1 +++ b/powershell/tests/functions/Get-MtSafeMarkdown.Tests.ps1 @@ -61,6 +61,39 @@ Get-MtSafeMarkdown $Text | Should -BeExactly $Text } + # The report renders markdown to HTML, and a browser turns `©` into its character. A + # tenant-controlled name that contains an entity would then show as a different text, so the + # ampersand is escaped. The backslash is consumed by the renderer, and the reader sees the + # original text. + It 'Escapes an ampersand that starts an HTML entity: ' -ForEach @( + @{ Text = 'Ops ©'; Expected = 'Ops \©' } + @{ Text = '© sign'; Expected = '\© sign' } + @{ Text = '©'; Expected = '\©' } + @{ Text = '©'; Expected = '\©' } + @{ Text = 'Q & A'; Expected = 'Q \& A' } + @{ Text = 'R&D & Q'; Expected = 'R&D \& Q' } + @{ Text = '[a](b) ©'; Expected = '\[a\]\(b\) \©' } + # Over-escaped on purpose: `&Marketing;` has the shape of an entity, and the backslash + # costs nothing because the renderer consumes it. + @{ Text = 'Sales&Marketing; Europe'; Expected = 'Sales\&Marketing; Europe' } + ) { + Get-MtSafeMarkdown $Text | Should -BeExactly $Expected + } + + It 'Leaves an ampersand that does not start an entity alone: ' -ForEach @( + @{ Text = 'R&D' } + @{ Text = 'Ops & Support' } + @{ Text = 'AT&T Inc' } + @{ Text = 'a && b' } + @{ Text = '&' } + @{ Text = 'x © y' } + @{ Text = 'a &= b;' } + ) { + # No entity body or no semicolon. Escaping these would fill the JSON output with + # backslashes for no benefit. + Get-MtSafeMarkdown $Text | Should -BeExactly $Text + } + It 'Replaces line breaks so values stay on one line' { Get-MtSafeMarkdown "line1`r`n| --- |`nline3" | Should -BeExactly 'line1 \| --- \| line3' } From 85bc6b9f6f34d626fc60c9a1faf04546dfdfb2f2 Mon Sep 17 00:00:00 2001 From: agnivesh <320655+agnivesh@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:01:33 +0000 Subject: [PATCH 2/2] fix(report): read the entity escape back in affected-object names Get-MtAffectedObjectFromMarkdown undoes the backslash escapes that Get-MtSafeMarkdown adds, and says its set must stay a superset of the helper's. The new escape for an ampersand that starts an HTML entity was not in it, so a name such as "Ops ©" kept the backslash in the affected-objects data. Add the ampersand to the set and test the round trip for entity names. The existing loop over single characters builds "Contoso&App", which is not an entity and is not escaped, so it did not cover this change. --- .../Get-MtAffectedObjectFromMarkdown.ps1 | 4 ++- .../functions/Get-MtAffectedObject.Tests.ps1 | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/powershell/internal/report/affected-objects/Get-MtAffectedObjectFromMarkdown.ps1 b/powershell/internal/report/affected-objects/Get-MtAffectedObjectFromMarkdown.ps1 index 86eddea483..6da33e991c 100644 --- a/powershell/internal/report/affected-objects/Get-MtAffectedObjectFromMarkdown.ps1 +++ b/powershell/internal/report/affected-objects/Get-MtAffectedObjectFromMarkdown.ps1 @@ -71,7 +71,9 @@ # 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 # literals; `~` has been escaped by the helper for longer and was missing here too. - $markdownEscape = '\\([\\`*_{}\[\]()#+\-.!|<>~:@])' + # `&` is escaped when it starts an HTML entity (`©`), so `Ops \©` is read back + # as `Ops ©`. + $markdownEscape = '\\([\\`*_{}\[\]()#+\-.!|<>~:@&])' foreach ($link in [regex]::Matches($Markdown, $linkPattern)) { $url = $link.Groups['url'].Value diff --git a/powershell/tests/functions/Get-MtAffectedObject.Tests.ps1 b/powershell/tests/functions/Get-MtAffectedObject.Tests.ps1 index 86d42d3f8f..c8247aaaf8 100644 --- a/powershell/tests/functions/Get-MtAffectedObject.Tests.ps1 +++ b/powershell/tests/functions/Get-MtAffectedObject.Tests.ps1 @@ -209,6 +209,31 @@ Describe 'Affected objects' { } } + It 'Should remove the escape Get-MtSafeMarkdown adds to an HTML entity: ' -ForEach @( + @{ Name = 'Ops ©' } + @{ Name = '© sign' } + @{ Name = '© sign' } + @{ Name = 'Q & A' } + @{ Name = 'R&D & Q' } + @{ Name = 'R&D' } + @{ Name = 'Ops & Support' } + ) { + # '&' is not in the character loop above because "Contoso&App" is not an entity, so the + # helper leaves it alone and the case would pass without testing the new escape. + $Policy = 'Microsoft_AAD_ConditionalAccess/PolicyBlade/policyId' + $Link = "https://entra.microsoft.com/#view/$Policy/44444444-4444-4444-4444-444444444444" + $escaped = InModuleScope Maester -Parameters @{ Name = $Name } { + param($Name) + Get-MtSafeMarkdown $Name + } + $affectedObject = InModuleScope Maester -Parameters @{ Markdown = "- [$escaped]($Link)" } { + param($Markdown) + Get-MtAffectedObjectFromMarkdown -Markdown $Markdown + } + + $affectedObject.DisplayName | Should -BeExactly $Name + } + It 'Should capture an enterprise application linked by object id only' { $markdown = '- [Mailbox Migration Account](https://portal.azure.com/#view/Microsoft_AAD_IAM/ManagedAppMenuBlade/~/Overview/objectId/55555555-5555-5555-5555-555555555555) with Global Administrator'