Skip to content

fix(report): preserve tenant names in CIS and Entra check results - #2800

Open
FiraSenax wants to merge 6 commits into
maester365:mainfrom
FiraSenax:codex/fix-report-name-formatting
Open

FiraSenax wants to merge 6 commits into
maester365:mainfrom
FiraSenax:codex/fix-report-name-formatting

Conversation

@FiraSenax

@FiraSenax FiraSenax commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

While reviewing name escaping for Conditional Access policy plans in #2733, I came across existing formatting issues in other checks. These also affect standalone policies and do not depend on the plans feature.

Tenant-controlled names can shift report columns, break portal links, or turn $& / $$ into %TestResult% / $.

Use literal placeholder insertion in 19 CIS result tables. Recover escaped affected-object link labels without losing unmatched or nested brackets, while retaining ID-based identity. Escape named-location and anti-spam policy names, baseline placeholder app link labels (MT.1259), and authentication-strength names (MT.1224). Existing scope, enforcement-state and pass/fail criteria are unchanged.

Validation:

  • 61 new regression cases cover all 17 affected CA table callers in all three enforcement states, plus named locations, EXO, app links and authentication strengths. The original 56 cases reproduce the defects on unchanged main; all 61 cases pass with the fix.
  • Graph and EXO live tests confirmed these names can be saved and read back unchanged. Fixed output was replayed with the captured tenant payloads and checked with the report's Markdown renderer; the baseline-scope setting was mocked for the app-link branch. All temporary tenant objects were removed.
  • Full combined runner before the scope cleanup: 8,854 passed, 94 skipped, no failures (8,948 cases across 200 files). The five additional name-matching regressions and affected-object suite also passed separately (94 cases). The standalone full runner covered 8,814 cases; two macOS hidden-attribute cases passed isolated rechecks with identical source. These earlier full runs included the unrelated BOM correction now delegated to fix(tests): add the missing byte order mark to Invoke-MtPackGitHubRequest.Tests.ps1 #2795.
  • After merging current main (2d659e8) in ad49941: the repository unit-test runner reports 681 passed, 1 failed across 8 files covering affected objects, literal CIS names, security-check Markdown, Invoke-Maester, selection, help and encoding. The sole failure is the known missing BOM in Invoke-MtPackGitHubRequest.Tests.ps1, addressed separately by fix(tests): add the missing byte order mark to Invoke-MtPackGitHubRequest.Tests.ps1 #2795. Build-LocalMaester.ps1 passes. The earlier post-removal pack-request run also passed all 24 tests.

Merge compatibility: current main (2d659e8) is merged via ad49941; the 24-file report-fix diff is byte-for-byte unchanged against the updated base. Independent of #2733. The existing CA policy/plan table escaping stays in that PR. The generic Markdown-label parser correction is identical in both branches. Both merge orders were simulated and produce the same conflict-free tree; the combined version was tested. The unrelated UTF-8 BOM correction in powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 is excluded from this PR and handled separately by #2795. Until that fix lands in main, the known file-encoding check failure remains.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Markdown report handling for policy, location, and authentication-strength names containing special characters, preserving names and table formatting.
    • Improved affected-object parsing for escaped brackets and bracketed text in Markdown links while retaining support for HTTPS links and optional titles.
  • Tests
    • Added coverage for literal policy names across multiple policy states, report formatting with special characters, and affected-object parsing.

@FiraSenax
FiraSenax requested review from a team as code owners November 12, 2026 01:04
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The changes update Markdown handling in selected CIS and Entra reports, expand affected-object link parsing, and add tests for special-character names. CIS result templates now use literal string substitution.

Changes: Report Markdown handling

Layer / File(s) Summary
Affected-object parsing and Markdown report tests
powershell/internal/report/affected-objects/Get-MtAffectedObjectFromMarkdown.ps1, powershell/tests/functions/Test-MtSecurityCheckMarkdown.Tests.ps1
The link-name pattern accepts escaped characters and additional bracket forms. Tests check affected-object parsing and report output for names containing special Markdown characters.
CIS result rendering and tests
powershell/tests/functions/Test-MtLiteralCisPolicyNames.Tests.ps1, tests/cis/Test.CIS.M365.1.3.2.ps1, tests/cis/Test.CIS.M365.2.1.14.ps1, tests/cis/Test.CIS.M365.5.2.2.*.ps1
Selected reports escape policy or location names. CIS templates use literal string replacement for result placeholders. New tests verify results with policy names containing PowerShell replacement tokens.
Entra result name escaping
tests/maester/entra/Test.MT.1224.ps1, tests/maester/entra/Test.MT.1259.ps1
Selected Entra reports escape authentication-strength names and placeholder application names. Grant-control text no longer receives an additional pipe-replacement step.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related issues

  • maester365/maester#2796 — The PR escapes names at selected CIS and Entra report sites. The issue also covers AD report names, which this PR does not change.
  • maester365/maester#2711 — The PR escapes names at selected Entra output sites. The issue describes additional Entra call sites that this PR does not change.

Possibly related PRs

Suggested reviewers: merill, samerde, cloud-architekt

Merge Risk: Minimal · up to 729fb

The change makes report output preserve tenant names containing special characters. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: preserving tenant-controlled names in CIS and Entra check results. It matches the changeset, which uses literal placeholder insertion in CIS tables and escapes names i…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description explains the purpose and scope of the change. It covers literal placeholder insertion in 19 CIS result tables, escaping of named-location, anti-spam, MT.1259 and MT.1224 names, and aff…
Full details: Title check

Explanation

The title names the main change: preserving tenant-controlled names in CIS and Entra check results. It matches the changeset, which uses literal placeholder insertion in CIS tables and escapes names in named-location, app-link, and authentication-strength output. The conventional-commit prefix 'fix(report):' is accurate for a bug-fix PR. The title is 65 characters, which is concise.

Full details: Description check

Explanation

The description explains the purpose and scope of the change. It covers literal placeholder insertion in 19 CIS result tables, escaping of named-location, anti-spam, MT.1259 and MT.1224 names, and affected-object link label recovery. It also gives validation results and merge-compatibility notes. It does not fill the template's 'Closes #' issue reference, the Checks checklist, or the Additional Information section. It also does not state whether the Maester 3.0 code freeze applies, and it contains no explicit confirmation that the checklist items were completed. These are minor gaps, so the description is mostly complete.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 2a20df8 and 729fbcd.

Files selected for processing: 1

  • powershell/tests/functions/Test-MtLiteralCisPolicyNames.Tests.ps1

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@earbona23

Copy link
Copy Markdown
Contributor

This is the same class I measured in #2796 — 270 AD checks, 268 with tables, 77 interpolating a name-like field in 95 places, and zero calling Get-MtSafeMarkdown — but your scope is wider and better targeted, because it covers the CIS tables and the affected-object link recovery, which my census did not reach.

Two things, one actionable and one I verified so you don't have to take it on faith.

The byte order mark hunk duplicates #2795

powershell/tests/functions/Invoke-MtPackGitHubRequest.Tests.ps1 +1/-1 is the UTF-8 BOM (ef bb bf). That exact byte is the whole content of #2795, which is open and MERGEABLE. main still does not have it — I checked the first three bytes of the file on main and they are 42 65 66 (Bef).

Why it is worth separating rather than leaving both: that one byte is what FileEncoding.Tests.ps1 fails on, and it is one of the two causes of the red build-validation (ubuntu-latest) leg. A one-byte PR can land in a minute; a 25-file PR with 61 new cases reasonably takes longer to review. If this one merges first, #2795 becomes an empty diff; if #2795 merges first, this PR gets a conflict on that file. Dropping the hunk here keeps both clean and gets the Linux leg green sooner.

(The other cause is the runner's own $config leaking into tests under coverage mode, which is #2797.)

I tested the new link pattern rather than eyeballing it

Nested quantifiers over a tenant-controlled string is exactly where catastrophic backtracking lives, so I ran the new pattern against .NET regex directly with a 5 s timeout.

Correctness:

input matches captured name
[Mi Politica](https://portal.azure.com/x) yes Mi Politica
[[RaviK] - Policy](https://portal.azure.com/x) yes [RaviK] - Policy
[Policy \] raro](https://portal.azure.com/x) yes Policy \] raro
[A](https://x.com/y "t") yes A
[A](javascript:alert(1)) no —

That last row is the one I most wanted to see hold: the https-only restriction still rejects the smuggled scheme after the pattern was widened.

Backtracking, with ('\a' + 'b' + '[c]') repeated and no closing bracket:

   301 chars -> 2 ms
  3001 chars -> 0 ms
 30001 chars -> 3 ms

Linear, and the reason is structural rather than luck: the three alternatives inside (?:\\.|[^\[\]\\]|\[(?:\\.|[^\[\]\\])*\])+ are mutually exclusive on their first character — backslash, neither-bracket-nor-backslash, open bracket. For any position only one branch can match, so there is no ambiguous partitioning for the engine to explore. Worth having that written down, because the obvious-looking simplification of dropping \\ from the negated class would reintroduce the ambiguity and with it the exponential case.

No other findings. The # Must stay a superset of what Get-MtSafeMarkdown escapes invariant is the one thing in here that can drift silently, and I see Get-MtAffectedObject.Tests.ps1 already references it, so that is covered.

@FiraSenax

Copy link
Copy Markdown
Contributor Author

Thanks @earbona23 — removed the unrelated BOM correction in 53a0442. Invoke-MtPackGitHubRequest.Tests.ps1 now matches this PR's base exactly and is no longer part of the PR diff; the encoding fix is handled separately by #2795.

Rechecked through powershell/tests/pester.ps1: all 24 pack-request tests pass, and the encoding suite has 4 passes plus the single known missing-BOM failure addressed by #2795. Build-LocalMaester.ps1 also passes. Updated the PR description to make that dependency and the validation results explicit.

@earbona23

Copy link
Copy Markdown
Contributor

Verified on 53a0442. Invoke-MtPackGitHubRequest.Tests.ps1 is no longer among the 24 files in this PR's diff, so the encoding fix is isolated in #2795 and this PR is back to one subject. Thank you for splitting it.

That also removes the ordering problem: whichever of the two lands first, neither now rewrites the other's file.

Two factual notes, neither a blocker:

@julien-ly

Copy link
Copy Markdown
Contributor

Ran the 20 Graph-only checks this PR touches at ad49941 on a tenant (CIS 1.3.2, 5.2.2.1 to 5.2.2.17, MT.1224, MT.1259). Main and the PR return the same results, 17 Failed and 3 Passed, and the same result Markdown character for character, so no change with ordinary names.

Then, with a disabled Conditional Access policy named Maester 2800 $& $$ test (MFA grant, no users, no apps), CIS.M365.5.2.2.1 lists it as Maester 2800 %TestResult% $ test on main and as Maester 2800 $& $$ test with this PR.

@earbona23

Copy link
Copy Markdown
Contributor

-replace to .Replace() is the right shape for this, and it is worth saying why in one line so it survives review: String.Replace takes its second argument literally, while -replace goes through Regex.Replace, which interprets $&, $$, $1 and ${name} in the replacement string. That is the whole of what @julien-ly saw on his tenant — $& is the entire match, so it expands back to %TestResult%, and $$ collapses to a single $.

Two things I measured on main (2d659e8) while reading this, because they decide how far the fix has to travel.

Get-MtSafeMarkdown does not help here. It escapes \ ` * _ [ ] ( ) ! | < > ~ : @ and the dot after www (L66). $ is not in the set, and it does not need to be — $ has no meaning in Markdown. So a check that escapes its display names correctly is still affected by this. The two defences are orthogonal, which is why this did not show up in the earlier escaping work.

The idiom is wide, but narrower than a raw grep suggests. On main:

files
contain -replace '%TestResult%' or '%ResultDetail%' with a variable replacement 501
of those, also interpolate a directory or tenant name into that variable 133
converted to .Replace() by this PR 22

I started with the 501 and cut it down deliberately: the first three I opened (Test.AD-USER-01/02/03) build their table out of counts only, so nothing tenant-controlled reaches the replacement and they are fine as they are. The 133 are the ones where a name does reach it. One I read in full, as a concrete case rather than a number: tests/ad/computer/Test.AD-COMP-10.ps1 puts $($_.Name) and $($_.DistinguishedName) into $result at L92 and L105, then passes $result as the replacement at L114. A computer object named WS-$&-01 renders %TestResult% inside the table.

None of that is a request to widen this PR — 22 call sites in one change is already the right size, and the CIS and Entra set is where it was reported. I am noting the rest so whoever decides the follow-up has the number in front of them, and I will keep it with the AD escaping work in #2796 rather than open a separate issue for it.

@earbona23 earbona23 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. First external review. This fixes a corruption I would not have found by reading, so I measured it from both ends.

The -replace to .Replace change is the substantive fix

-replace treats its replacement string as a substitution pattern, so $ is a metacharacter there. I ran the two forms side by side against the template this code uses:

name in the result markdown -replace (today's main) .Replace (this PR)
Policy $& Draft Policy %TestResult% Draft Policy $& Draft
A $$ B A $ B A $$ B
Policy $1 Draft unchanged unchanged
Costs $100 unchanged unchanged

Two concrete corruptions. $& expands to the matched text, so a policy named with it makes the placeholder itself appear in the report as if it were tenant data — an operator reading Policy %TestResult% Draft has no way to tell that is not the real name. $$ silently collapses to a single $. $1 happens to survive only because the pattern %TestResult% has no capture groups, so the blast radius is narrower than "any dollar sign" but it is real and it is silent either way.

Narrow trigger, but this is a report an operator uses to decide what to change in a tenant, and a name that is not the name is the kind of wrong that costs someone an afternoon.

The tests pin it, and I checked that they do

61 cases across Test-MtLiteralCisPolicyNames.Tests.ps1 (51) and Test-MtSecurityCheckMarkdown.Tests.ps1 (10), all green on the head. The fixtures go straight at it:

$name = 'Policy $& $$ $1 ${name}'

and assert the literal survives, including the combination with a pipe — HQ | Berlin $& $$ is asserted to come out as HQ \| Berlin $& $$, pipe escaped and dollars intact. That is the right assertion: escape what breaks the table, preserve what is part of the name.

Then the control that matters. I reverted the 21 check files to main and kept your test files:

CONTROL against main: passed=5  failed=56
  [-] <Id> preserves replacement tokens and the <State> verdict   (x56)

So the suite fails against the old code in 56 places and passes against the new one. The tests are not passing for an unrelated reason, and they cover the change across all 21 checks rather than one sample.

The affected-objects regex change is the consequence, and it is the part worth reading twice

Escaping display names in result markdown is only half the job — Get-MtAffectedObjectFromMarkdown then has to parse names that now carry backslash escapes:

-$linkPattern = '\[(?<name>(?:[^\[\]]|\[[^\[\]]*\])+)\]\((?<url>https://[^)\s]+)...'
+$linkPattern = '\[(?<name>(?:\\.|[^\[\]\\]|\[(?:\\.|[^\[\]\\])*\])+)\]\((?<url>https://[^)\s]+)...'

Consuming \\. first means an escaped bracket no longer terminates the name, so a name like \[RaviK\] - Policy stays whole instead of being cut at the first escaped bracket. Without this half, the first half would have broken the affected-objects page for exactly the names it set out to protect. Catching that the two have to move together is the thing I would most expect a PR like this to miss.

Worth noting the file already carried the reasoning for why this surface matters:

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.

That comment described a gap and worked around it at the parser. This PR closes it at the source and updates the workaround to match, which is the right order to do those two things in.

What is left, and it is not yours

The Get-MtSafeMarkdown additions cover tests/cis and tests/maester/entra. tests/ad is untouched and still at 0 of 270 files using the helper while 131 print a directory value into a table — that is my issue #2796 and a separate change. Mentioning it only so the scope of this PR is not mistaken for the whole problem.

BOM correct throughout, and nothing in the diff changes a verdict — every change is to what the result says, not to whether a check passes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants