From 38cf13143f74b47266474afca9d765e92cbfee8b Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Thu, 5 Nov 2026 08:39:32 -0700 Subject: [PATCH 01/15] feat(entra): add MT.1261 - medium-risk users should not be left unremediated in Entra ID Protection Part of #2518 Co-Authored-By: Claude Opus 5.5 --- .../tests/fixtures/golden/selection.json | 9 +- ...tEntraUnremediatedMediumRiskUser.Tests.ps1 | 235 ++++++++++++++++++ tests/maester/entra/Test.MT.1260.md | 2 +- tests/maester/entra/Test.MT.1261.md | 30 +++ tests/maester/entra/Test.MT.1261.ps1 | 162 ++++++++++++ 5 files changed, 434 insertions(+), 4 deletions(-) create mode 100644 powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 create mode 100644 tests/maester/entra/Test.MT.1261.md create mode 100644 tests/maester/entra/Test.MT.1261.ps1 diff --git a/powershell/tests/fixtures/golden/selection.json b/powershell/tests/fixtures/golden/selection.json index 05f976972c..73e4bea98c 100644 --- a/powershell/tests/fixtures/golden/selection.json +++ b/powershell/tests/fixtures/golden/selection.json @@ -1918,7 +1918,7 @@ "AD" ] }, - "SelectedCount": 588, + "SelectedCount": 589, "SelectedFamilies": [ "MT.1024", "MT.1059", @@ -2446,6 +2446,7 @@ "MT.1251", "MT.1252", "MT.1260", + "MT.1261", "ORCA.100", "ORCA.101", "ORCA.102", @@ -2527,7 +2528,7 @@ "AD" ] }, - "SelectedCount": 618, + "SelectedCount": 619, "SelectedFamilies": [ "MT.1024", "MT.1033", @@ -3087,6 +3088,7 @@ "MT.1251", "MT.1252", "MT.1260", + "MT.1261", "ORCA.100", "ORCA.101", "ORCA.102", @@ -3920,7 +3922,7 @@ "AD" ] }, - "SelectedCount": 574, + "SelectedCount": 575, "SelectedFamilies": [ "MT.1024", "MT.1033", @@ -4436,6 +4438,7 @@ "MT.1251", "MT.1252", "MT.1260", + "MT.1261", "ORCA.100", "ORCA.101", "ORCA.102", diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 new file mode 100644 index 0000000000..6953fec079 --- /dev/null +++ b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 @@ -0,0 +1,235 @@ +Describe 'Entra ID Protection unremediated medium-risk users (MT.1261)' { + BeforeAll { + Import-Module $PSScriptRoot/../../Maester.psd1 -Force + . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" + Use-MtModuleFunction -Name Test-MtEntraUnremediatedMediumRiskUser + } + + BeforeEach { + $script:TestResult = $null + $script:SkippedBecause = $null + $script:SkippedError = $null + $script:Severity = $null + + Mock -ModuleName Maester Test-MtConnection { return $true } + Mock -ModuleName Maester Add-MtTestResultDetail { + param($Result, $SkippedBecause, $SkippedError, $Severity) + + $script:TestResult = $Result + $script:SkippedBecause = $SkippedBecause + $script:SkippedError = $SkippedError + $script:Severity = $Severity + } + + Mock -ModuleName Maester Invoke-MtGraphRequest { return @() } -ParameterFilter { + $RelativeUri -eq 'roleManagement/directory/roleDefinitions' + } + Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { + return [pscustomobject]@{ Assignments = @(); EligibleChecked = $true } + } + } + + AfterEach { + if ($null -ne $script:TestResult) { + $script:TestResult | Should -Not -Match '`[^`]+`' + } + } + + It 'passes when there are no medium-risk users' { + Mock -ModuleName Maester Invoke-MtGraphRequest { return @() } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeTrue + $script:TestResult | Should -Match 'Well done' + # The medium-risk filter is what separates this check from MT.1260. + Should -Invoke Invoke-MtGraphRequest -ModuleName Maester -Exactly -Times 1 -ParameterFilter { + $ApiVersion -eq 'v1.0' -and + $RelativeUri -eq 'identityProtection/riskyUsers' -and + $Filter -eq "riskLevel eq 'medium'" + } + Should -Invoke Get-MtDirectoryRoleAssignment -ModuleName Maester -Exactly -Times 0 + } + + It 'fails when a medium-risk user is atRisk' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return @( + [pscustomobject]@{ + id = 'user-1' + userPrincipalName = 'alice@contoso.com' + userDisplayName = 'Alice' + riskLevel = 'medium' + riskState = 'atRisk' + riskDetail = 'none' + riskLastUpdatedDateTime = '2026-10-01T00:00:00Z' + } + ) + } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse + $script:TestResult | Should -Match 'alice@contoso.com' + $script:TestResult | Should -Match 'atRisk' + } + + It 'fails when a medium-risk user is confirmedCompromised (must not be treated as remediated)' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return @( + [pscustomobject]@{ + id = 'user-2' + userPrincipalName = 'bob@contoso.com' + userDisplayName = 'Bob' + riskLevel = 'medium' + riskState = 'confirmedCompromised' + riskDetail = 'adminConfirmedUserCompromised' + riskLastUpdatedDateTime = '2026-10-02T00:00:00Z' + } + ) + } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse + $script:TestResult | Should -Match 'bob@contoso.com' + $script:TestResult | Should -Match 'confirmedCompromised' + } + + It 'passes when the only medium-risk users are already remediated or dismissed' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return @( + [pscustomobject]@{ + id = 'user-3'; userPrincipalName = 'carol@contoso.com'; userDisplayName = 'Carol' + riskLevel = 'medium'; riskState = 'remediated'; riskDetail = 'userPerformedSecuredPasswordReset' + riskLastUpdatedDateTime = '2026-10-03T00:00:00Z' + }, + [pscustomobject]@{ + id = 'user-4'; userPrincipalName = 'dave@contoso.com'; userDisplayName = 'Dave' + riskLevel = 'medium'; riskState = 'dismissed'; riskDetail = 'adminDismissedAllRiskForUser' + riskLastUpdatedDateTime = '2026-10-04T00:00:00Z' + } + ) + } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeTrue + $script:TestResult | Should -Match 'Well done' + } + + It 'reports only the unremediated users in a mixed set' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return @( + [pscustomobject]@{ + id = 'user-1'; userPrincipalName = 'alice@contoso.com'; userDisplayName = 'Alice' + riskLevel = 'medium'; riskState = 'atRisk'; riskDetail = 'none' + riskLastUpdatedDateTime = '2026-10-01T00:00:00Z' + }, + [pscustomobject]@{ + id = 'user-3'; userPrincipalName = 'carol@contoso.com'; userDisplayName = 'Carol' + riskLevel = 'medium'; riskState = 'remediated'; riskDetail = 'userPerformedSecuredPasswordReset' + riskLastUpdatedDateTime = '2026-10-03T00:00:00Z' + } + ) + } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse + $script:TestResult | Should -Match 'alice@contoso.com' + $script:TestResult | Should -Not -Match 'carol@contoso.com' + $script:TestResult | Should -Match 'Found 1 medium-risk' + } + + It 'skips as NotAuthorized on a 403 from Graph' { + Mock -ModuleName Maester Invoke-MtGraphRequest { throw '403 Forbidden: Authorization_RequestDenied' } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeNullOrEmpty + $script:SkippedBecause | Should -Be 'NotAuthorized' + } + + It 'lets an unexpected failure throw so the engine records it as Error' { + Mock -ModuleName Maester Invoke-MtGraphRequest { throw 'Service unavailable' } + + { Test-MtEntraUnremediatedMediumRiskUser } | Should -Throw '*Service unavailable*' + } + + Context 'Privileged users at risk' { + BeforeEach { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return @( + [pscustomobject]@{ id = 'role-ga'; displayName = 'Global Administrator' }, + [pscustomobject]@{ id = 'role-dr'; displayName = 'Directory Readers' } + ) + } -ParameterFilter { $RelativeUri -eq 'roleManagement/directory/roleDefinitions' } + + Mock -ModuleName Maester Invoke-MtGraphRequest { + return @( + [pscustomobject]@{ + id = 'user-1'; userPrincipalName = 'alice@contoso.com'; userDisplayName = 'Alice' + riskLevel = 'medium'; riskState = 'atRisk'; riskDetail = 'none' + riskLastUpdatedDateTime = '2026-10-01T00:00:00Z' + }, + [pscustomobject]@{ + id = 'user-2'; userPrincipalName = 'bob@contoso.com'; userDisplayName = 'Bob' + riskLevel = 'medium'; riskState = 'atRisk'; riskDetail = 'none' + riskLastUpdatedDateTime = '2026-10-02T00:00:00Z' + } + ) + } -ParameterFilter { $RelativeUri -eq 'identityProtection/riskyUsers' } + + Mock -ModuleName Maester Get-MtRoleInfo { + return [pscustomobject]@{ IsPrivileged = ($RoleName -eq 'GlobalAdministrator') } + } + } + + It 'raises severity to Medium and lists the role when an at-risk user holds a privileged role' { + Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { + return [pscustomobject]@{ + Assignments = @([pscustomobject]@{ principalId = 'user-2'; roleDefinitionId = 'role-ga'; AssignmentType = 'Active' }) + EligibleChecked = $true + } + } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse + $script:Severity | Should -Be 'Medium' + $script:TestResult | Should -Match 'Global Administrator' + $script:TestResult | Should -Match '1 of them hold a privileged directory role' + $script:TestResult.IndexOf('bob@contoso.com') | Should -BeLessThan $script:TestResult.IndexOf('alice@contoso.com') + } + + It 'treats a PIM-eligible privileged role as privileged' { + Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { + return [pscustomobject]@{ + Assignments = @([pscustomobject]@{ principalId = 'user-1'; roleDefinitionId = 'role-ga'; AssignmentType = 'Eligible' }) + EligibleChecked = $true + } + } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse + $script:Severity | Should -Be 'Medium' + $script:TestResult | Should -Match 'Global Administrator \(eligible\)' + } + + It 'does not raise severity for a non-privileged role' { + Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { + return [pscustomobject]@{ + Assignments = @([pscustomobject]@{ principalId = 'user-1'; roleDefinitionId = 'role-dr'; AssignmentType = 'Active' }) + EligibleChecked = $true + } + } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse + $script:Severity | Should -BeNullOrEmpty + $script:TestResult | Should -Not -Match 'Directory Readers' + } + + It 'notes when PIM-eligible assignments could not be read' { + Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { + return [pscustomobject]@{ Assignments = @(); EligibleChecked = $false } + } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse + $script:TestResult | Should -Match 'PIM-eligible role assignments were not checked' + } + + It 'still reports the users when role assignments cannot be read' { + Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { throw 'Forbidden' } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse + $script:TestResult | Should -Match 'alice@contoso.com' + $script:TestResult | Should -Match 'privileged users were not identified' + $script:Severity | Should -BeNullOrEmpty + } + } +} diff --git a/tests/maester/entra/Test.MT.1260.md b/tests/maester/entra/Test.MT.1260.md index 268e31267a..ad7eb59b73 100644 --- a/tests/maester/entra/Test.MT.1260.md +++ b/tests/maester/entra/Test.MT.1260.md @@ -8,7 +8,7 @@ If a reported user holds a privileged directory role, active or PIM-eligible, th The check needs the Microsoft Graph permission `IdentityRiskyUser.Read.All`, which Maester does not request by default. Without it the check is skipped. Connect with `Connect-Maester -IncludePreview`, or grant the permission to the app registration that runs Maester. -A companion check, MT.1261, is planned to report on medium-risk users. This check is about the outcome (users actually left unremediated). It is complementary to MT.1253, which checks that a Conditional Access policy requiring risk remediation is in place; one checks the control, this one checks the result. +A companion check, MT.1261, reports on medium-risk users. This check is about the outcome (users actually left unremediated). It is complementary to MT.1253, which checks that a Conditional Access policy requiring risk remediation is in place; one checks the control, this one checks the result. #### Remediation action diff --git a/tests/maester/entra/Test.MT.1261.md b/tests/maester/entra/Test.MT.1261.md new file mode 100644 index 0000000000..ed34027089 --- /dev/null +++ b/tests/maester/entra/Test.MT.1261.md @@ -0,0 +1,30 @@ +Medium-risk users should not be left unremediated in Entra ID Protection. + +Entra ID Protection rates a user's risk and tracks a risk state for each flagged user. A medium-risk flag only clears when the risk is remediated, for example by a password reset, or when an admin dismisses it or confirms the user safe. Until then the account stays exposed: the signals that triggered the flag have not been addressed. + +This check reports users whose risk level is **medium** and whose risk state is still active and unremediated (`atRisk` or `confirmedCompromised`). States that represent a handled flag (`remediated`, `dismissed`, `confirmedSafe`, `none`) are not reported. The check reads risk only and never changes a user's risk state. + +If a reported user holds a privileged directory role, active or PIM-eligible, the result lists the role and the severity is raised to Medium, since even a medium-risk flag on an admin account warrants prompt investigation. + +The check needs the Microsoft Graph permission `IdentityRiskyUser.Read.All`, which Maester does not request by default. Without it the check is skipped. Connect with `Connect-Maester -IncludePreview`, or grant the permission to the app registration that runs Maester. + +A companion check, MT.1260, reports on high-risk users. This check is about the outcome (users actually left unremediated). It is complementary to MT.1253, which checks that a Conditional Access policy requiring risk remediation is in place; one checks the control, this one checks the result. + +#### Remediation action + +1. Open the [Microsoft Entra admin center](https://entra.microsoft.com) and navigate to **Protection** > **Identity Protection** > **Risky users**. +2. Locate each reported user by their user principal name. Start with any user listed with a privileged role. +3. Investigate the risk detections behind the flag and decide whether the account is compromised. +4. Remediate the risk by the appropriate path: + - If compromised, require a password reset, revoke the user's refresh tokens, and review recent sign-ins and activity. + - If the activity is confirmed safe after investigation, dismiss the user's risk or confirm the user safe so the flag clears. +5. Consider a risk-based Conditional Access policy so that future medium-risk users are remediated automatically rather than left waiting for manual review. + +#### Related links + +* [Remediate risks and unblock users](https://learn.microsoft.com/entra/id-protection/howto-identity-protection-remediate-unblock) +* [Investigate risk with Microsoft Entra ID Protection](https://learn.microsoft.com/entra/id-protection/howto-identity-protection-investigate-risk) +* [What is risk in Microsoft Entra ID Protection](https://learn.microsoft.com/entra/id-protection/concept-identity-protection-risks) + + +%TestResult% diff --git a/tests/maester/entra/Test.MT.1261.ps1 b/tests/maester/entra/Test.MT.1261.ps1 new file mode 100644 index 0000000000..f8ba8d49d9 --- /dev/null +++ b/tests/maester/entra/Test.MT.1261.ps1 @@ -0,0 +1,162 @@ +function Test-MtEntraUnremediatedMediumRiskUser { + <# + .SYNOPSIS + Finds users that Entra ID Protection rates medium risk and that nobody has remediated. + + .DESCRIPTION + Entra ID Protection flags users it believes may be compromised and assigns a risk level and a + risk state. A flag only clears when someone remediates it (password reset) or an admin + dismisses or confirms-safe it. This check reports users whose risk level is medium and whose + risk state still shows active, unremediated risk, so an admin can act on flags that have been + left sitting. If a reported user holds a privileged directory role, the severity is raised to + Medium. It reads risk only; it never changes a user's risk state. + + .EXAMPLE + Test-MtEntraUnremediatedMediumRiskUser + + .LINK + https://maester.dev/docs/commands/Test-MtEntraUnremediatedMediumRiskUser + + .LINK + https://learn.microsoft.com/graph/api/resources/riskyuser?view=graph-rest-1.0 + #> + [MaesterTest( + Id = 'MT.1261', + Title = 'Medium-risk users should not be left unremediated in Entra ID Protection.', + Product = 'entra', + Feature = 'identity-protection', + Area = 'logging-and-detection', + ResourceType = 'User', + Severity = 'Low', + UserImpact = 'Low', + ImplementationCost = 'Low', + Mitre = 'T1078.004', + Control = ('nist-800-53-r5:AC-2(12)', 'nist-800-53-r5:IR-4'), + Service = 'Graph', + License = 'AAD_PREMIUM_P2', + Permission = 'Graph:Scope:IdentityRiskyUser.Read.All', + Lifecycle = 'Preview', + Author = 'Dfrank77' + )] + [CmdletBinding()] + [OutputType([bool])] + param() + + # Active, unremediated risk states. A flag an admin has handled moves to 'remediated', + # 'dismissed', 'confirmedSafe' or 'none'; those are intentionally excluded. + # 'confirmedCompromised' MUST be treated as unremediated alongside 'atRisk' -- confirming a + # user compromised does not clear the risk. + $UnremediatedStates = @('atRisk', 'confirmedCompromised') + + Write-Verbose 'Reading medium-risk users from Entra ID Protection.' + # Filter riskLevel server-side only. The riskState set is applied client-side on purpose: + # Graph rejects some compound / 'or' / 'in' filter shapes on this endpoint, so a single-property + # filter is the reliable shape, and the state list is tiny to post-filter. + try { + $MediumRiskUsers = @( + Invoke-MtGraphRequest -ApiVersion 'v1.0' ` + -RelativeUri 'identityProtection/riskyUsers' ` + -Filter "riskLevel eq 'medium'" ` + -Select @('id', 'userPrincipalName', 'userDisplayName', 'riskLevel', 'riskState', 'riskDetail', 'riskLastUpdatedDateTime') + ) + } catch { + if ($_.Exception.Message -match '(?i)403|forbidden|authorization') { + Add-MtTestResultDetail -SkippedBecause NotAuthorized + return $null + } + throw + } + + $Unremediated = @( + $MediumRiskUsers | Where-Object { $UnremediatedStates -contains [string]$_.riskState } + ) + Write-Verbose "Found $($MediumRiskUsers.Count) medium-risk user(s); $($Unremediated.Count) unremediated." + + if ($Unremediated.Count -eq 0) { + Add-MtTestResultDetail -Result ( + 'Well done. No medium-risk users are left unremediated in Entra ID Protection.' + ) + return $true + } + + # Privileged users at risk raise severity to Medium (#2518). Roles are only read when there is + # something to report, so a clean tenant makes no extra Graph calls. The role lookup is extra + # detail: if it fails, the unremediated users are still reported, with a note. + $PrivilegedRolesByUser = @{} + $RoleNote = '' + try { + $RoleDefinitions = @( + Invoke-MtGraphRequest -ApiVersion 'v1.0' ` + -RelativeUri 'roleManagement/directory/roleDefinitions' ` + -Select @('id', 'displayName') + ) + $RoleDefLookup = @{} + foreach ($Def in $RoleDefinitions) { $RoleDefLookup[[string]$Def.id] = $Def } + + # Same properties as MT.1209 and MT.1210 so the active assignments come from the Graph cache. + $RoleAssignmentResult = Get-MtDirectoryRoleAssignment -Select @('id', 'principalId', 'roleDefinitionId', 'directoryScopeId') + if (!$RoleAssignmentResult.EligibleChecked) { + $RoleNote = "`n`nNote: PIM-eligible role assignments were not checked because they need " + + 'Microsoft Entra ID P2 or Governance. Only active role assignments were checked.' + } + + $UnremediatedIds = @{} + foreach ($Item in $Unremediated) { $UnremediatedIds[[string]$Item.id] = $true } + + foreach ($Assignment in @($RoleAssignmentResult.Assignments)) { + $PrincipalId = [string]$Assignment.principalId + if (!$UnremediatedIds.ContainsKey($PrincipalId)) { continue } + $RoleDef = $RoleDefLookup[[string]$Assignment.roleDefinitionId] + if ($null -eq $RoleDef) { continue } + # isPrivileged is not queryable on roleDefinitions; use Maester's role catalog, as MT.1210 does. + $RoleInfo = Get-MtRoleInfo -RoleName ([string]$RoleDef.displayName -replace '\s', '') + if ($null -eq $RoleInfo -or !$RoleInfo.IsPrivileged) { continue } + $RoleName = [string]$RoleDef.displayName + if ($Assignment.AssignmentType -eq 'Eligible') { $RoleName += ' (eligible)' } + if (!$PrivilegedRolesByUser.ContainsKey($PrincipalId)) { + $PrivilegedRolesByUser[$PrincipalId] = [System.Collections.Generic.List[string]]::new() + } + $PrivilegedRolesByUser[$PrincipalId].Add($RoleName) + } + } catch { + Write-Verbose "Could not read directory role assignments: $($_.Exception.Message)" + $PrivilegedRolesByUser = @{} + $RoleNote = "`n`nNote: Directory role assignments could not be read, so privileged users were not identified." + } + + # Privileged users first, then by user principal name. + $Sorted = @( + $Unremediated | Sort-Object -Property @{ Expression = { -not $PrivilegedRolesByUser.ContainsKey([string]$_.id) } }, userPrincipalName + ) + $PrivilegedCount = @($Unremediated | Where-Object { $PrivilegedRolesByUser.ContainsKey([string]$_.id) }).Count + + $Result = "Found $($Unremediated.Count) medium-risk user(s) with unremediated risk." + if ($PrivilegedCount -gt 0) { + $Result += " $PrivilegedCount of them hold a privileged directory role, so severity is raised to Medium." + } + $Result += "`n`n| User | User principal name | Risk state | Risk detail | Risk last updated | Privileged role |" + $Result += "`n| --- | --- | --- | --- | --- | --- |" + foreach ($Item in $Sorted) { + $DisplayName = [string]$Item.userDisplayName + if ([string]::IsNullOrWhiteSpace($DisplayName)) { $DisplayName = '(unnamed)' } + $DisplayName = [System.Net.WebUtility]::HtmlEncode($DisplayName) -replace '\|', '|' + $Upn = [System.Net.WebUtility]::HtmlEncode([string]$Item.userPrincipalName) -replace '\|', '|' + $State = [string]$Item.riskState + $Detail = [System.Net.WebUtility]::HtmlEncode([string]$Item.riskDetail) -replace '\|', '|' + $Updated = if ($Item.riskLastUpdatedDateTime) { + ($Item.riskLastUpdatedDateTime -as [datetime]).ToUniversalTime().ToString('yyyy-MM-dd') + } else { 'Unknown' } + $Roles = if ($PrivilegedRolesByUser.ContainsKey([string]$Item.id)) { + [System.Net.WebUtility]::HtmlEncode(($PrivilegedRolesByUser[[string]$Item.id] -join ', ')) -replace '\|', '|' + } else { 'None' } + $Result += "`n| $DisplayName | $Upn | $State | $Detail | $Updated | $Roles |" + } + $Result += $RoleNote + + if ($PrivilegedCount -gt 0) { + Add-MtTestResultDetail -Result $Result -Severity 'Medium' + } else { + Add-MtTestResultDetail -Result $Result + } + return $false +} From 637e78aecc77284469b065fa0096ca7eea5657b2 Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Thu, 5 Nov 2026 09:43:57 -0700 Subject: [PATCH 02/15] fix(entra): MT.1261 declare the role-read scope it uses --- tests/maester/entra/Test.MT.1261.md | 2 +- tests/maester/entra/Test.MT.1261.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/maester/entra/Test.MT.1261.md b/tests/maester/entra/Test.MT.1261.md index ed34027089..f13dc2efb3 100644 --- a/tests/maester/entra/Test.MT.1261.md +++ b/tests/maester/entra/Test.MT.1261.md @@ -6,7 +6,7 @@ This check reports users whose risk level is **medium** and whose risk state is If a reported user holds a privileged directory role, active or PIM-eligible, the result lists the role and the severity is raised to Medium, since even a medium-risk flag on an admin account warrants prompt investigation. -The check needs the Microsoft Graph permission `IdentityRiskyUser.Read.All`, which Maester does not request by default. Without it the check is skipped. Connect with `Connect-Maester -IncludePreview`, or grant the permission to the app registration that runs Maester. +The check needs the Microsoft Graph permission `IdentityRiskyUser.Read.All`, which Maester does not request by default. Without it the check is skipped. To identify privileged users, it also reads directory roles, which needs `RoleManagement.Read.Directory` or `RoleManagement.Read.All`. If those reads fail, the users are still reported with a note. Connect with `Connect-Maester -IncludePreview`, or grant the permission to the app registration that runs Maester. A companion check, MT.1260, reports on high-risk users. This check is about the outcome (users actually left unremediated). It is complementary to MT.1253, which checks that a Conditional Access policy requiring risk remediation is in place; one checks the control, this one checks the result. diff --git a/tests/maester/entra/Test.MT.1261.ps1 b/tests/maester/entra/Test.MT.1261.ps1 index f8ba8d49d9..60b7407109 100644 --- a/tests/maester/entra/Test.MT.1261.ps1 +++ b/tests/maester/entra/Test.MT.1261.ps1 @@ -34,7 +34,7 @@ Control = ('nist-800-53-r5:AC-2(12)', 'nist-800-53-r5:IR-4'), Service = 'Graph', License = 'AAD_PREMIUM_P2', - Permission = 'Graph:Scope:IdentityRiskyUser.Read.All', + Permission = ('Graph:Scope:IdentityRiskyUser.Read.All', 'Graph:Scope:RoleManagement.Read.Directory|Graph:Scope:RoleManagement.Read.All'), Lifecycle = 'Preview', Author = 'Dfrank77' )] From 4eb9ee87820c79d0316e82d317dbaf9b0c4278be Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Thu, 5 Nov 2026 11:35:05 -0700 Subject: [PATCH 03/15] fix(entra): MT.1261 raise privileged users to High severity, per #2518 --- .../Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 | 6 +++--- tests/maester/entra/Test.MT.1261.md | 2 +- tests/maester/entra/Test.MT.1261.ps1 | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 index 6953fec079..ba95466ac5 100644 --- a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 @@ -173,7 +173,7 @@ } } - It 'raises severity to Medium and lists the role when an at-risk user holds a privileged role' { + It 'raises severity to High and lists the role when an at-risk user holds a privileged role' { Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { return [pscustomobject]@{ Assignments = @([pscustomobject]@{ principalId = 'user-2'; roleDefinitionId = 'role-ga'; AssignmentType = 'Active' }) @@ -182,7 +182,7 @@ } Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse - $script:Severity | Should -Be 'Medium' + $script:Severity | Should -Be 'High' $script:TestResult | Should -Match 'Global Administrator' $script:TestResult | Should -Match '1 of them hold a privileged directory role' $script:TestResult.IndexOf('bob@contoso.com') | Should -BeLessThan $script:TestResult.IndexOf('alice@contoso.com') @@ -197,7 +197,7 @@ } Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse - $script:Severity | Should -Be 'Medium' + $script:Severity | Should -Be 'High' $script:TestResult | Should -Match 'Global Administrator \(eligible\)' } diff --git a/tests/maester/entra/Test.MT.1261.md b/tests/maester/entra/Test.MT.1261.md index f13dc2efb3..671794408e 100644 --- a/tests/maester/entra/Test.MT.1261.md +++ b/tests/maester/entra/Test.MT.1261.md @@ -4,7 +4,7 @@ Entra ID Protection rates a user's risk and tracks a risk state for each flagged This check reports users whose risk level is **medium** and whose risk state is still active and unremediated (`atRisk` or `confirmedCompromised`). States that represent a handled flag (`remediated`, `dismissed`, `confirmedSafe`, `none`) are not reported. The check reads risk only and never changes a user's risk state. -If a reported user holds a privileged directory role, active or PIM-eligible, the result lists the role and the severity is raised to Medium, since even a medium-risk flag on an admin account warrants prompt investigation. +If a reported user holds a privileged directory role, active or PIM-eligible, the result lists the role and the severity is raised to High, since even a medium-risk flag on an admin account warrants prompt investigation. The check needs the Microsoft Graph permission `IdentityRiskyUser.Read.All`, which Maester does not request by default. Without it the check is skipped. To identify privileged users, it also reads directory roles, which needs `RoleManagement.Read.Directory` or `RoleManagement.Read.All`. If those reads fail, the users are still reported with a note. Connect with `Connect-Maester -IncludePreview`, or grant the permission to the app registration that runs Maester. diff --git a/tests/maester/entra/Test.MT.1261.ps1 b/tests/maester/entra/Test.MT.1261.ps1 index 60b7407109..0e85955941 100644 --- a/tests/maester/entra/Test.MT.1261.ps1 +++ b/tests/maester/entra/Test.MT.1261.ps1 @@ -9,7 +9,7 @@ dismisses or confirms-safe it. This check reports users whose risk level is medium and whose risk state still shows active, unremediated risk, so an admin can act on flags that have been left sitting. If a reported user holds a privileged directory role, the severity is raised to - Medium. It reads risk only; it never changes a user's risk state. + High. It reads risk only; it never changes a user's risk state. .EXAMPLE Test-MtEntraUnremediatedMediumRiskUser @@ -79,7 +79,7 @@ return $true } - # Privileged users at risk raise severity to Medium (#2518). Roles are only read when there is + # Privileged users at risk raise severity to High (#2518). Roles are only read when there is # something to report, so a clean tenant makes no extra Graph calls. The role lookup is extra # detail: if it fails, the unremediated users are still reported, with a note. $PrivilegedRolesByUser = @{} @@ -132,7 +132,7 @@ $Result = "Found $($Unremediated.Count) medium-risk user(s) with unremediated risk." if ($PrivilegedCount -gt 0) { - $Result += " $PrivilegedCount of them hold a privileged directory role, so severity is raised to Medium." + $Result += " $PrivilegedCount of them hold a privileged directory role, so severity is raised to High." } $Result += "`n`n| User | User principal name | Risk state | Risk detail | Risk last updated | Privileged role |" $Result += "`n| --- | --- | --- | --- | --- | --- |" @@ -154,7 +154,7 @@ $Result += $RoleNote if ($PrivilegedCount -gt 0) { - Add-MtTestResultDetail -Result $Result -Severity 'Medium' + Add-MtTestResultDetail -Result $Result -Severity 'High' } else { Add-MtTestResultDetail -Result $Result } From 823f2e5d5ebc85db2b87321f113ff45e95475258 Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Thu, 5 Nov 2026 12:52:42 -0700 Subject: [PATCH 04/15] fix(entra): MT.1261 declare supported clouds --- tests/maester/entra/Test.MT.1261.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/maester/entra/Test.MT.1261.ps1 b/tests/maester/entra/Test.MT.1261.ps1 index 0e85955941..3c4e6cf971 100644 --- a/tests/maester/entra/Test.MT.1261.ps1 +++ b/tests/maester/entra/Test.MT.1261.ps1 @@ -32,6 +32,7 @@ ImplementationCost = 'Low', Mitre = 'T1078.004', Control = ('nist-800-53-r5:AC-2(12)', 'nist-800-53-r5:IR-4'), + Cloud = ('Commercial', 'GCC', 'GCCHigh', 'DoD'), Service = 'Graph', License = 'AAD_PREMIUM_P2', Permission = ('Graph:Scope:IdentityRiskyUser.Read.All', 'Graph:Scope:RoleManagement.Read.Directory|Graph:Scope:RoleManagement.Read.All'), From 73a346d53110d311a6ce75dee306e15d64f54d97 Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Thu, 5 Nov 2026 13:16:32 -0700 Subject: [PATCH 05/15] fix(entra): MT.1261 skip deleted users --- ...st-MtEntraUnremediatedMediumRiskUser.Tests.ps1 | 15 +++++++++++++++ tests/maester/entra/Test.MT.1261.md | 2 +- tests/maester/entra/Test.MT.1261.ps1 | 5 +++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 index ba95466ac5..1c236a3c70 100644 --- a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 @@ -131,6 +131,21 @@ $script:TestResult | Should -Match 'Found 1 medium-risk' } + It 'does not report a deleted user, whose risk an admin cannot dismiss' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return @( + [pscustomobject]@{ + id = 'user-5'; isDeleted = $true; userPrincipalName = 'erin@contoso.com'; userDisplayName = 'Erin' + riskLevel = 'medium'; riskState = 'atRisk'; riskDetail = 'none' + riskLastUpdatedDateTime = '2026-10-05T00:00:00Z' + } + ) + } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeTrue + $script:TestResult | Should -Match 'Well done' + } + It 'skips as NotAuthorized on a 403 from Graph' { Mock -ModuleName Maester Invoke-MtGraphRequest { throw '403 Forbidden: Authorization_RequestDenied' } diff --git a/tests/maester/entra/Test.MT.1261.md b/tests/maester/entra/Test.MT.1261.md index 671794408e..0c13469602 100644 --- a/tests/maester/entra/Test.MT.1261.md +++ b/tests/maester/entra/Test.MT.1261.md @@ -2,7 +2,7 @@ Medium-risk users should not be left unremediated in Entra ID Protection. Entra ID Protection rates a user's risk and tracks a risk state for each flagged user. A medium-risk flag only clears when the risk is remediated, for example by a password reset, or when an admin dismisses it or confirms the user safe. Until then the account stays exposed: the signals that triggered the flag have not been addressed. -This check reports users whose risk level is **medium** and whose risk state is still active and unremediated (`atRisk` or `confirmedCompromised`). States that represent a handled flag (`remediated`, `dismissed`, `confirmedSafe`, `none`) are not reported. The check reads risk only and never changes a user's risk state. +This check reports users whose risk level is **medium** and whose risk state is still active and unremediated (`atRisk` or `confirmedCompromised`). States that represent a handled flag (`remediated`, `dismissed`, `confirmedSafe`, `none`) are not reported. Deleted users are not reported either, because they stay in the risk report and admins cannot dismiss their risk. The check reads risk only and never changes a user's risk state. If a reported user holds a privileged directory role, active or PIM-eligible, the result lists the role and the severity is raised to High, since even a medium-risk flag on an admin account warrants prompt investigation. diff --git a/tests/maester/entra/Test.MT.1261.ps1 b/tests/maester/entra/Test.MT.1261.ps1 index 3c4e6cf971..b00e309f55 100644 --- a/tests/maester/entra/Test.MT.1261.ps1 +++ b/tests/maester/entra/Test.MT.1261.ps1 @@ -43,6 +43,7 @@ [OutputType([bool])] param() + # Deleted users stay in the risk report and admins cannot dismiss them, so they are not reported. # Active, unremediated risk states. A flag an admin has handled moves to 'remediated', # 'dismissed', 'confirmedSafe' or 'none'; those are intentionally excluded. # 'confirmedCompromised' MUST be treated as unremediated alongside 'atRisk' -- confirming a @@ -58,7 +59,7 @@ Invoke-MtGraphRequest -ApiVersion 'v1.0' ` -RelativeUri 'identityProtection/riskyUsers' ` -Filter "riskLevel eq 'medium'" ` - -Select @('id', 'userPrincipalName', 'userDisplayName', 'riskLevel', 'riskState', 'riskDetail', 'riskLastUpdatedDateTime') + -Select @('id', 'isDeleted', 'userPrincipalName', 'userDisplayName', 'riskLevel', 'riskState', 'riskDetail', 'riskLastUpdatedDateTime') ) } catch { if ($_.Exception.Message -match '(?i)403|forbidden|authorization') { @@ -69,7 +70,7 @@ } $Unremediated = @( - $MediumRiskUsers | Where-Object { $UnremediatedStates -contains [string]$_.riskState } + $MediumRiskUsers | Where-Object { $UnremediatedStates -contains [string]$_.riskState -and $_.isDeleted -ne $true } ) Write-Verbose "Found $($MediumRiskUsers.Count) medium-risk user(s); $($Unremediated.Count) unremediated." From 5e1e549bffa238425d101323e74df2f4d3c85176 Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Thu, 5 Nov 2026 14:27:30 -0700 Subject: [PATCH 06/15] docs(entra): MT.1261 clarify that dismissing does not change the password --- tests/maester/entra/Test.MT.1261.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/maester/entra/Test.MT.1261.md b/tests/maester/entra/Test.MT.1261.md index 0c13469602..0998d0cce7 100644 --- a/tests/maester/entra/Test.MT.1261.md +++ b/tests/maester/entra/Test.MT.1261.md @@ -1,6 +1,6 @@ Medium-risk users should not be left unremediated in Entra ID Protection. -Entra ID Protection rates a user's risk and tracks a risk state for each flagged user. A medium-risk flag only clears when the risk is remediated, for example by a password reset, or when an admin dismisses it or confirms the user safe. Until then the account stays exposed: the signals that triggered the flag have not been addressed. +Entra ID Protection rates a user's risk and tracks a risk state for each flagged user. A medium-risk flag only clears when the risk is remediated, for example by a password reset, or when an admin dismisses it or confirms the user safe. Until then the signals that triggered the flag have not been addressed. Dismissing a flag clears it but does not change the user's password, so investigate first. This check reports users whose risk level is **medium** and whose risk state is still active and unremediated (`atRisk` or `confirmedCompromised`). States that represent a handled flag (`remediated`, `dismissed`, `confirmedSafe`, `none`) are not reported. Deleted users are not reported either, because they stay in the risk report and admins cannot dismiss their risk. The check reads risk only and never changes a user's risk state. From 1253ca7eeab5127db96b7d3108c87f45c508f362 Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Sat, 7 Nov 2026 01:12:53 +0000 Subject: [PATCH 07/15] fix(entra): MT.1261 stop claiming a severity raise that never applies Add-MtTestResultDetail -Severity is ignored in a single-result native test (website/docs/writing-tests/index.mdx), so the privileged-account raise to High never applied while the result text said it did. Remove the -Severity call and the "severity is raised to High" wording. Privileged accounts are still detected, listed first and shown with their roles. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WrYw8FTdwmyZ3ktKSYEJij --- ...t-MtEntraUnremediatedMediumRiskUser.Tests.ps1 | 8 ++++---- tests/maester/entra/Test.MT.1261.md | 2 +- tests/maester/entra/Test.MT.1261.ps1 | 16 ++++++---------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 index 1c236a3c70..c7e455d7b1 100644 --- a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 @@ -188,7 +188,7 @@ } } - It 'raises severity to High and lists the role when an at-risk user holds a privileged role' { + It 'lists the role first when an at-risk user holds a privileged role' { Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { return [pscustomobject]@{ Assignments = @([pscustomobject]@{ principalId = 'user-2'; roleDefinitionId = 'role-ga'; AssignmentType = 'Active' }) @@ -197,7 +197,7 @@ } Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse - $script:Severity | Should -Be 'High' + $script:Severity | Should -BeNullOrEmpty $script:TestResult | Should -Match 'Global Administrator' $script:TestResult | Should -Match '1 of them hold a privileged directory role' $script:TestResult.IndexOf('bob@contoso.com') | Should -BeLessThan $script:TestResult.IndexOf('alice@contoso.com') @@ -212,11 +212,11 @@ } Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse - $script:Severity | Should -Be 'High' + $script:Severity | Should -BeNullOrEmpty $script:TestResult | Should -Match 'Global Administrator \(eligible\)' } - It 'does not raise severity for a non-privileged role' { + It 'does not list a non-privileged role' { Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { return [pscustomobject]@{ Assignments = @([pscustomobject]@{ principalId = 'user-1'; roleDefinitionId = 'role-dr'; AssignmentType = 'Active' }) diff --git a/tests/maester/entra/Test.MT.1261.md b/tests/maester/entra/Test.MT.1261.md index 0998d0cce7..7a07563028 100644 --- a/tests/maester/entra/Test.MT.1261.md +++ b/tests/maester/entra/Test.MT.1261.md @@ -4,7 +4,7 @@ Entra ID Protection rates a user's risk and tracks a risk state for each flagged This check reports users whose risk level is **medium** and whose risk state is still active and unremediated (`atRisk` or `confirmedCompromised`). States that represent a handled flag (`remediated`, `dismissed`, `confirmedSafe`, `none`) are not reported. Deleted users are not reported either, because they stay in the risk report and admins cannot dismiss their risk. The check reads risk only and never changes a user's risk state. -If a reported user holds a privileged directory role, active or PIM-eligible, the result lists the role and the severity is raised to High, since even a medium-risk flag on an admin account warrants prompt investigation. +If a reported user holds a privileged directory role, active or PIM-eligible, the result lists the role and shows that entry first, since even a medium-risk flag on an admin account warrants prompt investigation. The check needs the Microsoft Graph permission `IdentityRiskyUser.Read.All`, which Maester does not request by default. Without it the check is skipped. To identify privileged users, it also reads directory roles, which needs `RoleManagement.Read.Directory` or `RoleManagement.Read.All`. If those reads fail, the users are still reported with a note. Connect with `Connect-Maester -IncludePreview`, or grant the permission to the app registration that runs Maester. diff --git a/tests/maester/entra/Test.MT.1261.ps1 b/tests/maester/entra/Test.MT.1261.ps1 index b00e309f55..5bc97ac9d5 100644 --- a/tests/maester/entra/Test.MT.1261.ps1 +++ b/tests/maester/entra/Test.MT.1261.ps1 @@ -8,8 +8,8 @@ risk state. A flag only clears when someone remediates it (password reset) or an admin dismisses or confirms-safe it. This check reports users whose risk level is medium and whose risk state still shows active, unremediated risk, so an admin can act on flags that have been - left sitting. If a reported user holds a privileged directory role, the severity is raised to - High. It reads risk only; it never changes a user's risk state. + left sitting. If a reported user holds a privileged directory role, it is listed first + with its roles. It reads risk only; it never changes a user's risk state. .EXAMPLE Test-MtEntraUnremediatedMediumRiskUser @@ -81,8 +81,8 @@ return $true } - # Privileged users at risk raise severity to High (#2518). Roles are only read when there is - # something to report, so a clean tenant makes no extra Graph calls. The role lookup is extra + # Privileged users at risk are listed first with their roles (#2518). Roles are only read when + # there is something to report, so a clean tenant makes no extra Graph calls. The role lookup is extra # detail: if it fails, the unremediated users are still reported, with a note. $PrivilegedRolesByUser = @{} $RoleNote = '' @@ -134,7 +134,7 @@ $Result = "Found $($Unremediated.Count) medium-risk user(s) with unremediated risk." if ($PrivilegedCount -gt 0) { - $Result += " $PrivilegedCount of them hold a privileged directory role, so severity is raised to High." + $Result += " $PrivilegedCount of them hold a privileged directory role and are listed first." } $Result += "`n`n| User | User principal name | Risk state | Risk detail | Risk last updated | Privileged role |" $Result += "`n| --- | --- | --- | --- | --- | --- |" @@ -155,10 +155,6 @@ } $Result += $RoleNote - if ($PrivilegedCount -gt 0) { - Add-MtTestResultDetail -Result $Result -Severity 'High' - } else { - Add-MtTestResultDetail -Result $Result - } + Add-MtTestResultDetail -Result $Result return $false } From 48fae550e5416e72c5f25329a9d1ebc0b340fb3b Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Sat, 7 Nov 2026 03:00:55 +0000 Subject: [PATCH 08/15] MT.1261: escape tenant names with Get-MtSafeMarkdown HtmlEncode escapes HTML, not markdown, so a display name like [Sign in again](https://phish.example) rendered as a live link. Use Get-MtSafeMarkdown as #2254 requires for tenant-controlled values. Raised in #2711. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WrYw8FTdwmyZ3ktKSYEJij --- ...tEntraUnremediatedMediumRiskUser.Tests.ps1 | 22 ++++++++++++++++++- tests/maester/entra/Test.MT.1261.ps1 | 8 +++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 index c7e455d7b1..dd24007355 100644 --- a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 @@ -69,6 +69,26 @@ $script:TestResult | Should -Match 'atRisk' } + It 'escapes a tenant-controlled display name so it cannot render as a link' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return @( + [pscustomobject]@{ + id = 'user-1' + userPrincipalName = 'alice@contoso.com' + userDisplayName = '[Sign in again](https://phish.example)' + riskLevel = 'medium' + riskState = 'atRisk' + riskDetail = 'none' + riskLastUpdatedDateTime = '2026-10-01T00:00:00Z' + } + ) + } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse + $script:TestResult | Should -Not -Match '\| \[Sign in again\]\(' + $script:TestResult | Should -Match ([regex]::Escape('\[Sign in again\]\(https://phish.example\)')) + } + It 'fails when a medium-risk user is confirmedCompromised (must not be treated as remediated)' { Mock -ModuleName Maester Invoke-MtGraphRequest { return @( @@ -213,7 +233,7 @@ Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse $script:Severity | Should -BeNullOrEmpty - $script:TestResult | Should -Match 'Global Administrator \(eligible\)' + $script:TestResult | Should -Match 'Global Administrator \\\(eligible\\\)' } It 'does not list a non-privileged role' { diff --git a/tests/maester/entra/Test.MT.1261.ps1 b/tests/maester/entra/Test.MT.1261.ps1 index 5bc97ac9d5..fbea909c06 100644 --- a/tests/maester/entra/Test.MT.1261.ps1 +++ b/tests/maester/entra/Test.MT.1261.ps1 @@ -141,15 +141,15 @@ foreach ($Item in $Sorted) { $DisplayName = [string]$Item.userDisplayName if ([string]::IsNullOrWhiteSpace($DisplayName)) { $DisplayName = '(unnamed)' } - $DisplayName = [System.Net.WebUtility]::HtmlEncode($DisplayName) -replace '\|', '|' - $Upn = [System.Net.WebUtility]::HtmlEncode([string]$Item.userPrincipalName) -replace '\|', '|' + $DisplayName = Get-MtSafeMarkdown $DisplayName + $Upn = Get-MtSafeMarkdown ([string]$Item.userPrincipalName) $State = [string]$Item.riskState - $Detail = [System.Net.WebUtility]::HtmlEncode([string]$Item.riskDetail) -replace '\|', '|' + $Detail = Get-MtSafeMarkdown ([string]$Item.riskDetail) $Updated = if ($Item.riskLastUpdatedDateTime) { ($Item.riskLastUpdatedDateTime -as [datetime]).ToUniversalTime().ToString('yyyy-MM-dd') } else { 'Unknown' } $Roles = if ($PrivilegedRolesByUser.ContainsKey([string]$Item.id)) { - [System.Net.WebUtility]::HtmlEncode(($PrivilegedRolesByUser[[string]$Item.id] -join ', ')) -replace '\|', '|' + Get-MtSafeMarkdown ($PrivilegedRolesByUser[[string]$Item.id] -join ', ') } else { 'None' } $Result += "`n| $DisplayName | $Upn | $State | $Detail | $Updated | $Roles |" } From 14241ddad3a0602eb02ba9711207e622bf893b30 Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Sun, 8 Nov 2026 07:42:46 -0700 Subject: [PATCH 09/15] MT.1260: align with MT.1261 (isDeleted filter, Cloud, RoleManagement permission) Skip deleted users, whose risk an admin cannot dismiss; declare the supported clouds; declare the RoleManagement read permission the privileged-role lookup needs. Adds a unit test for the deleted-user case. Co-Authored-By: Claude Opus 5.5 --- ...Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 | 15 +++++++++++++++ tests/maester/entra/Test.MT.1260.ps1 | 8 +++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 index 525dec0a8e..5670ca8374 100644 --- a/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 @@ -157,6 +157,21 @@ $script:TestResult | Should -Match 'Found 1 high-risk' } + It 'does not report a deleted user, whose risk an admin cannot dismiss' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return @( + [pscustomobject]@{ + id = 'user-5'; isDeleted = $true; userPrincipalName = 'erin@contoso.com'; userDisplayName = 'Erin' + riskLevel = 'high'; riskState = 'atRisk'; riskDetail = 'none' + riskLastUpdatedDateTime = '2026-10-05T00:00:00Z' + } + ) + } + + Test-MtEntraUnremediatedHighRiskUser | Should -BeTrue + $script:TestResult | Should -Match 'Well done' + } + It 'skips as NotAuthorized on a 403 from Graph' { Mock -ModuleName Maester Invoke-MtGraphRequest { throw '403 Forbidden: Authorization_RequestDenied' } diff --git a/tests/maester/entra/Test.MT.1260.ps1 b/tests/maester/entra/Test.MT.1260.ps1 index 51c863322a..c5b3078c37 100644 --- a/tests/maester/entra/Test.MT.1260.ps1 +++ b/tests/maester/entra/Test.MT.1260.ps1 @@ -28,9 +28,10 @@ ImplementationCost = 'Low', Mitre = 'T1078.004', Control = ('nist-800-53-r5:AC-2(12)', 'nist-800-53-r5:IR-4'), + Cloud = ('Commercial', 'GCC', 'GCCHigh', 'DoD'), Service = 'Graph', License = 'AAD_PREMIUM_P2', - Permission = 'Graph:Scope:IdentityRiskyUser.Read.All', + Permission = ('Graph:Scope:IdentityRiskyUser.Read.All', 'Graph:Scope:RoleManagement.Read.Directory|Graph:Scope:RoleManagement.Read.All'), Lifecycle = 'Preview', Author = 'Dfrank77' )] @@ -38,6 +39,7 @@ [OutputType([bool])] param() + # Deleted users stay in the risk report and admins cannot dismiss them, so they are not reported. # Active, unremediated risk states. A flag an admin has handled moves to 'remediated', # 'dismissed', 'confirmedSafe' or 'none'; those are intentionally excluded. # 'confirmedCompromised' MUST be treated as unremediated alongside 'atRisk' -- confirming a @@ -54,7 +56,7 @@ Invoke-MtGraphRequest -ApiVersion 'v1.0' ` -RelativeUri 'identityProtection/riskyUsers' ` -Filter "riskLevel eq 'high'" ` - -Select @('id', 'userPrincipalName', 'userDisplayName', 'riskLevel', 'riskState', 'riskDetail', 'riskLastUpdatedDateTime') + -Select @('id', 'isDeleted', 'userPrincipalName', 'userDisplayName', 'riskLevel', 'riskState', 'riskDetail', 'riskLastUpdatedDateTime') ) } catch { if ($_.Exception.Message -match '(?i)403|forbidden|authorization') { @@ -65,7 +67,7 @@ } $Unremediated = @( - $HighRiskUsers | Where-Object { $UnremediatedStates -contains [string]$_.riskState } + $HighRiskUsers | Where-Object { $UnremediatedStates -contains [string]$_.riskState -and $_.isDeleted -ne $true } ) Write-Verbose "Found $($HighRiskUsers.Count) high-risk user(s); $($Unremediated.Count) unremediated." From 8189ed6c6189b203a3fe99b13c1855c88c111cde Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Sun, 8 Nov 2026 07:54:42 -0700 Subject: [PATCH 10/15] Move the privileged-role lookup into Get-MtPrincipalPrivilegedRole MT.1260 and MT.1261 now share one helper in powershell/internal/services/entra/ for the privileged-role lookup, so MT.1262/1263 can use it too and the group-membership follow-up lands in one place. Behaviour is unchanged; the helper has its own unit tests. Co-Authored-By: Claude Opus 5.5 --- .../entra/Get-MtPrincipalPrivilegedRole.ps1 | 74 +++++++++++++++ .../Get-MtPrincipalPrivilegedRole.Tests.ps1 | 92 +++++++++++++++++++ tests/maester/entra/Test.MT.1260.ps1 | 33 +------ tests/maester/entra/Test.MT.1261.ps1 | 33 +------ 4 files changed, 172 insertions(+), 60 deletions(-) create mode 100644 powershell/internal/services/entra/Get-MtPrincipalPrivilegedRole.ps1 create mode 100644 powershell/tests/functions/Get-MtPrincipalPrivilegedRole.Tests.ps1 diff --git a/powershell/internal/services/entra/Get-MtPrincipalPrivilegedRole.ps1 b/powershell/internal/services/entra/Get-MtPrincipalPrivilegedRole.ps1 new file mode 100644 index 0000000000..0d60ab12e9 --- /dev/null +++ b/powershell/internal/services/entra/Get-MtPrincipalPrivilegedRole.ps1 @@ -0,0 +1,74 @@ +function Get-MtPrincipalPrivilegedRole { + <# + .SYNOPSIS + Returns the privileged directory roles held by the given principals. + + .DESCRIPTION + Shared by the Entra ID Protection risk checks (MT.1260 to MT.1263) so the privileged-role lookup lives in one place. + + For each principal ID passed in, returns the display names of the privileged directory roles assigned to it, + active or PIM-eligible. Eligible roles get ' (eligible)' after the name. A role counts as privileged when + Maester's role catalog (Get-MtRoleInfo) marks it IsPrivileged, as MT.1210 does. + + Only direct assignments are matched. A role held through a role-assignable group is not returned yet. + + The result has two properties: RolesByPrincipal, a hashtable from principal ID to a list of role names (only + principals that hold a privileged role are present), and EligibleChecked, which is true when PIM-eligible + assignments were read (they need Microsoft Entra ID P2 or Governance). + + Errors reading role definitions or assignments are not caught, so the caller can report that privileged + principals could not be identified. + + .EXAMPLE + $privileged = Get-MtPrincipalPrivilegedRole -PrincipalId $riskyUsers.id + $privileged.RolesByPrincipal[$user.id] + + Returns the privileged roles held by each risky user. + #> + [CmdletBinding()] + [OutputType([pscustomobject])] + param( + # Object IDs of the users or service principals to look up. + [Parameter(Mandatory)] + [AllowEmptyCollection()] + [string[]] $PrincipalId + ) + + $wanted = @{} + foreach ($id in $PrincipalId) { + if (-not [string]::IsNullOrWhiteSpace($id)) { $wanted[[string]$id] = $true } + } + + $roleDefinitions = @( + Invoke-MtGraphRequest -ApiVersion 'v1.0' ` + -RelativeUri 'roleManagement/directory/roleDefinitions' ` + -Select @('id', 'displayName') + ) + $roleDefinitionById = @{} + foreach ($definition in $roleDefinitions) { $roleDefinitionById[[string]$definition.id] = $definition } + + # Same properties as MT.1209 and MT.1210 so the active assignments come from the Graph cache. + $assignmentResult = Get-MtDirectoryRoleAssignment -Select @('id', 'principalId', 'roleDefinitionId', 'directoryScopeId') + + $rolesByPrincipal = @{} + foreach ($assignment in @($assignmentResult.Assignments)) { + $principal = [string]$assignment.principalId + if (-not $wanted.ContainsKey($principal)) { continue } + $definition = $roleDefinitionById[[string]$assignment.roleDefinitionId] + if ($null -eq $definition) { continue } + # isPrivileged is not queryable on roleDefinitions; use Maester's role catalog, as MT.1210 does. + $roleInfo = Get-MtRoleInfo -RoleName ([string]$definition.displayName -replace '\s', '') + if ($null -eq $roleInfo -or -not $roleInfo.IsPrivileged) { continue } + $roleName = [string]$definition.displayName + if ($assignment.AssignmentType -eq 'Eligible') { $roleName += ' (eligible)' } + if (-not $rolesByPrincipal.ContainsKey($principal)) { + $rolesByPrincipal[$principal] = [System.Collections.Generic.List[string]]::new() + } + $rolesByPrincipal[$principal].Add($roleName) + } + + [pscustomobject]@{ + RolesByPrincipal = $rolesByPrincipal + EligibleChecked = [bool]$assignmentResult.EligibleChecked + } +} diff --git a/powershell/tests/functions/Get-MtPrincipalPrivilegedRole.Tests.ps1 b/powershell/tests/functions/Get-MtPrincipalPrivilegedRole.Tests.ps1 new file mode 100644 index 0000000000..3c12cb3def --- /dev/null +++ b/powershell/tests/functions/Get-MtPrincipalPrivilegedRole.Tests.ps1 @@ -0,0 +1,92 @@ +BeforeAll { + Import-Module $PSScriptRoot/../../Maester.psd1 -Force + . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" + Use-MtModuleFunction -Name Get-MtPrincipalPrivilegedRole +} + +Describe 'Get-MtPrincipalPrivilegedRole' -Tag 'Unit' { + BeforeEach { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return @( + [pscustomobject]@{ id = 'role-ga'; displayName = 'Global Administrator' }, + [pscustomobject]@{ id = 'role-dr'; displayName = 'Directory Readers' } + ) + } -ParameterFilter { $RelativeUri -eq 'roleManagement/directory/roleDefinitions' } + + Mock -ModuleName Maester Get-MtRoleInfo { + return [pscustomobject]@{ IsPrivileged = ($RoleName -eq 'GlobalAdministrator') } + } + } + + It 'returns an active privileged role held by a requested principal' { + Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { + return [pscustomobject]@{ + Assignments = @([pscustomobject]@{ principalId = 'p-1'; roleDefinitionId = 'role-ga'; AssignmentType = 'Active' }) + EligibleChecked = $true + } + } + + $result = Get-MtPrincipalPrivilegedRole -PrincipalId 'p-1' + + @($result.RolesByPrincipal['p-1']) | Should -Be @('Global Administrator') + $result.EligibleChecked | Should -BeTrue + } + + It 'marks a PIM-eligible privileged role as eligible' { + Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { + return [pscustomobject]@{ + Assignments = @([pscustomobject]@{ principalId = 'p-1'; roleDefinitionId = 'role-ga'; AssignmentType = 'Eligible' }) + EligibleChecked = $true + } + } + + $result = Get-MtPrincipalPrivilegedRole -PrincipalId 'p-1' + + @($result.RolesByPrincipal['p-1']) | Should -Be @('Global Administrator (eligible)') + } + + It 'ignores principals that were not requested, even when they hold a privileged role' { + Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { + return [pscustomobject]@{ + Assignments = @( + [pscustomobject]@{ principalId = 'p-1'; roleDefinitionId = 'role-ga'; AssignmentType = 'Active' }, + [pscustomobject]@{ principalId = 'someone-else'; roleDefinitionId = 'role-ga'; AssignmentType = 'Active' } + ) + EligibleChecked = $true + } + } + + $result = Get-MtPrincipalPrivilegedRole -PrincipalId 'p-1' + + @($result.RolesByPrincipal.Keys) | Should -Be @('p-1') + } + + It 'ignores a role that is not privileged' { + Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { + return [pscustomobject]@{ + Assignments = @([pscustomobject]@{ principalId = 'p-1'; roleDefinitionId = 'role-dr'; AssignmentType = 'Active' }) + EligibleChecked = $true + } + } + + $result = Get-MtPrincipalPrivilegedRole -PrincipalId 'p-1' + + $result.RolesByPrincipal.Count | Should -Be 0 + } + + It 'reports when PIM-eligible assignments were not checked' { + Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { + return [pscustomobject]@{ Assignments = @(); EligibleChecked = $false } + } + + $result = Get-MtPrincipalPrivilegedRole -PrincipalId 'p-1' + + $result.EligibleChecked | Should -BeFalse + } + + It 'lets a failure to read role assignments throw, so the caller can report it' { + Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { throw '403 Forbidden' } + + { Get-MtPrincipalPrivilegedRole -PrincipalId 'p-1' } | Should -Throw '*403*' + } +} diff --git a/tests/maester/entra/Test.MT.1260.ps1 b/tests/maester/entra/Test.MT.1260.ps1 index c5b3078c37..09d1c58ffd 100644 --- a/tests/maester/entra/Test.MT.1260.ps1 +++ b/tests/maester/entra/Test.MT.1260.ps1 @@ -84,39 +84,12 @@ $PrivilegedRolesByUser = @{} $RoleNote = '' try { - $RoleDefinitions = @( - Invoke-MtGraphRequest -ApiVersion 'v1.0' ` - -RelativeUri 'roleManagement/directory/roleDefinitions' ` - -Select @('id', 'displayName') - ) - $RoleDefLookup = @{} - foreach ($Def in $RoleDefinitions) { $RoleDefLookup[[string]$Def.id] = $Def } - - # Same properties as MT.1209 and MT.1210 so the active assignments come from the Graph cache. - $RoleAssignmentResult = Get-MtDirectoryRoleAssignment -Select @('id', 'principalId', 'roleDefinitionId', 'directoryScopeId') - if (!$RoleAssignmentResult.EligibleChecked) { + $PrivilegedResult = Get-MtPrincipalPrivilegedRole -PrincipalId @($Unremediated | ForEach-Object { [string]$_.id }) + $PrivilegedRolesByUser = $PrivilegedResult.RolesByPrincipal + if (!$PrivilegedResult.EligibleChecked) { $RoleNote = "`n`nNote: PIM-eligible role assignments were not checked because they need " + 'Microsoft Entra ID P2 or Governance. Only active role assignments were checked.' } - - $UnremediatedIds = @{} - foreach ($Item in $Unremediated) { $UnremediatedIds[[string]$Item.id] = $true } - - foreach ($Assignment in @($RoleAssignmentResult.Assignments)) { - $PrincipalId = [string]$Assignment.principalId - if (!$UnremediatedIds.ContainsKey($PrincipalId)) { continue } - $RoleDef = $RoleDefLookup[[string]$Assignment.roleDefinitionId] - if ($null -eq $RoleDef) { continue } - # isPrivileged is not queryable on roleDefinitions; use Maester's role catalog, as MT.1210 does. - $RoleInfo = Get-MtRoleInfo -RoleName ([string]$RoleDef.displayName -replace '\s', '') - if ($null -eq $RoleInfo -or !$RoleInfo.IsPrivileged) { continue } - $RoleName = [string]$RoleDef.displayName - if ($Assignment.AssignmentType -eq 'Eligible') { $RoleName += ' (eligible)' } - if (!$PrivilegedRolesByUser.ContainsKey($PrincipalId)) { - $PrivilegedRolesByUser[$PrincipalId] = [System.Collections.Generic.List[string]]::new() - } - $PrivilegedRolesByUser[$PrincipalId].Add($RoleName) - } } catch { Write-Verbose "Could not read directory role assignments: $($_.Exception.Message)" $PrivilegedRolesByUser = @{} diff --git a/tests/maester/entra/Test.MT.1261.ps1 b/tests/maester/entra/Test.MT.1261.ps1 index fbea909c06..d2acd21f45 100644 --- a/tests/maester/entra/Test.MT.1261.ps1 +++ b/tests/maester/entra/Test.MT.1261.ps1 @@ -87,39 +87,12 @@ $PrivilegedRolesByUser = @{} $RoleNote = '' try { - $RoleDefinitions = @( - Invoke-MtGraphRequest -ApiVersion 'v1.0' ` - -RelativeUri 'roleManagement/directory/roleDefinitions' ` - -Select @('id', 'displayName') - ) - $RoleDefLookup = @{} - foreach ($Def in $RoleDefinitions) { $RoleDefLookup[[string]$Def.id] = $Def } - - # Same properties as MT.1209 and MT.1210 so the active assignments come from the Graph cache. - $RoleAssignmentResult = Get-MtDirectoryRoleAssignment -Select @('id', 'principalId', 'roleDefinitionId', 'directoryScopeId') - if (!$RoleAssignmentResult.EligibleChecked) { + $PrivilegedResult = Get-MtPrincipalPrivilegedRole -PrincipalId @($Unremediated | ForEach-Object { [string]$_.id }) + $PrivilegedRolesByUser = $PrivilegedResult.RolesByPrincipal + if (!$PrivilegedResult.EligibleChecked) { $RoleNote = "`n`nNote: PIM-eligible role assignments were not checked because they need " + 'Microsoft Entra ID P2 or Governance. Only active role assignments were checked.' } - - $UnremediatedIds = @{} - foreach ($Item in $Unremediated) { $UnremediatedIds[[string]$Item.id] = $true } - - foreach ($Assignment in @($RoleAssignmentResult.Assignments)) { - $PrincipalId = [string]$Assignment.principalId - if (!$UnremediatedIds.ContainsKey($PrincipalId)) { continue } - $RoleDef = $RoleDefLookup[[string]$Assignment.roleDefinitionId] - if ($null -eq $RoleDef) { continue } - # isPrivileged is not queryable on roleDefinitions; use Maester's role catalog, as MT.1210 does. - $RoleInfo = Get-MtRoleInfo -RoleName ([string]$RoleDef.displayName -replace '\s', '') - if ($null -eq $RoleInfo -or !$RoleInfo.IsPrivileged) { continue } - $RoleName = [string]$RoleDef.displayName - if ($Assignment.AssignmentType -eq 'Eligible') { $RoleName += ' (eligible)' } - if (!$PrivilegedRolesByUser.ContainsKey($PrincipalId)) { - $PrivilegedRolesByUser[$PrincipalId] = [System.Collections.Generic.List[string]]::new() - } - $PrivilegedRolesByUser[$PrincipalId].Add($RoleName) - } } catch { Write-Verbose "Could not read directory role assignments: $($_.Exception.Message)" $PrivilegedRolesByUser = @{} From 02c1a16326c58bb4b47de59bf5df1f43b6b6fab1 Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Sun, 8 Nov 2026 08:18:58 -0700 Subject: [PATCH 11/15] MT.1260/MT.1261: list risky users on the Affected objects page Pass the unremediated users to Add-MtTestResultDetail -GraphObjects with -GraphObjectType IdentityProtection (id, displayName, userPrincipalName), so each shows up on the report's Affected objects page. One result per test; the result table is unchanged. Unit tests cover both the fail and pass paths. Co-Authored-By: Claude Opus 5.5 --- ...-MtEntraUnremediatedHighRiskUser.Tests.ps1 | 37 ++++++++++++++++++- ...tEntraUnremediatedMediumRiskUser.Tests.ps1 | 37 ++++++++++++++++++- tests/maester/entra/Test.MT.1260.ps1 | 8 +++- tests/maester/entra/Test.MT.1261.ps1 | 8 +++- 4 files changed, 86 insertions(+), 4 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 index 5670ca8374..a9dabbf0e4 100644 --- a/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 @@ -11,15 +11,19 @@ $script:SkippedBecause = $null $script:SkippedError = $null $script:Severity = $null + $script:GraphObjects = $null + $script:GraphObjectType = $null Mock -ModuleName Maester Test-MtConnection { return $true } Mock -ModuleName Maester Add-MtTestResultDetail { - param($Result, $SkippedBecause, $SkippedError, $Severity) + param($Result, $SkippedBecause, $SkippedError, $Severity, $GraphObjects, $GraphObjectType) $script:TestResult = $Result $script:SkippedBecause = $SkippedBecause $script:SkippedError = $SkippedError $script:Severity = $Severity + $script:GraphObjects = $GraphObjects + $script:GraphObjectType = $GraphObjectType } # Role lookups default to "no roles". Tests that need roles override these. @@ -172,6 +176,37 @@ $script:TestResult | Should -Match 'Well done' } + It 'passes the unremediated users to the report as affected objects' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return @( + [pscustomobject]@{ + id = 'user-1'; userPrincipalName = 'alice@contoso.com'; userDisplayName = 'Alice' + riskLevel = 'high'; riskState = 'atRisk'; riskDetail = 'none' + riskLastUpdatedDateTime = '2026-10-01T00:00:00Z' + }, + [pscustomobject]@{ + id = 'user-2'; userPrincipalName = 'bob@contoso.com'; userDisplayName = 'Bob' + riskLevel = 'high'; riskState = 'confirmedCompromised'; riskDetail = 'none' + riskLastUpdatedDateTime = '2026-10-02T00:00:00Z' + } + ) + } -ParameterFilter { $RelativeUri -eq 'identityProtection/riskyUsers' } + + Test-MtEntraUnremediatedHighRiskUser | Should -BeFalse + $script:GraphObjectType | Should -Be 'IdentityProtection' + @($script:GraphObjects).Count | Should -Be 2 + @($script:GraphObjects.id) | Should -Be @('user-1', 'user-2') + @($script:GraphObjects.displayName) | Should -Be @('Alice', 'Bob') + @($script:GraphObjects.userPrincipalName) | Should -Be @('alice@contoso.com', 'bob@contoso.com') + } + + It 'passes no affected objects when no user is left unremediated' { + Mock -ModuleName Maester Invoke-MtGraphRequest { return @() } -ParameterFilter { $RelativeUri -eq 'identityProtection/riskyUsers' } + + Test-MtEntraUnremediatedHighRiskUser | Should -BeTrue + $script:GraphObjects | Should -BeNullOrEmpty + } + It 'skips as NotAuthorized on a 403 from Graph' { Mock -ModuleName Maester Invoke-MtGraphRequest { throw '403 Forbidden: Authorization_RequestDenied' } diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 index dd24007355..b29514e1f9 100644 --- a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 @@ -10,15 +10,19 @@ $script:SkippedBecause = $null $script:SkippedError = $null $script:Severity = $null + $script:GraphObjects = $null + $script:GraphObjectType = $null Mock -ModuleName Maester Test-MtConnection { return $true } Mock -ModuleName Maester Add-MtTestResultDetail { - param($Result, $SkippedBecause, $SkippedError, $Severity) + param($Result, $SkippedBecause, $SkippedError, $Severity, $GraphObjects, $GraphObjectType) $script:TestResult = $Result $script:SkippedBecause = $SkippedBecause $script:SkippedError = $SkippedError $script:Severity = $Severity + $script:GraphObjects = $GraphObjects + $script:GraphObjectType = $GraphObjectType } Mock -ModuleName Maester Invoke-MtGraphRequest { return @() } -ParameterFilter { @@ -166,6 +170,37 @@ $script:TestResult | Should -Match 'Well done' } + It 'passes the unremediated users to the report as affected objects' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + return @( + [pscustomobject]@{ + id = 'user-1'; userPrincipalName = 'alice@contoso.com'; userDisplayName = 'Alice' + riskLevel = 'medium'; riskState = 'atRisk'; riskDetail = 'none' + riskLastUpdatedDateTime = '2026-10-01T00:00:00Z' + }, + [pscustomobject]@{ + id = 'user-2'; userPrincipalName = 'bob@contoso.com'; userDisplayName = 'Bob' + riskLevel = 'medium'; riskState = 'confirmedCompromised'; riskDetail = 'none' + riskLastUpdatedDateTime = '2026-10-02T00:00:00Z' + } + ) + } -ParameterFilter { $RelativeUri -eq 'identityProtection/riskyUsers' } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse + $script:GraphObjectType | Should -Be 'IdentityProtection' + @($script:GraphObjects).Count | Should -Be 2 + @($script:GraphObjects.id) | Should -Be @('user-1', 'user-2') + @($script:GraphObjects.displayName) | Should -Be @('Alice', 'Bob') + @($script:GraphObjects.userPrincipalName) | Should -Be @('alice@contoso.com', 'bob@contoso.com') + } + + It 'passes no affected objects when no user is left unremediated' { + Mock -ModuleName Maester Invoke-MtGraphRequest { return @() } -ParameterFilter { $RelativeUri -eq 'identityProtection/riskyUsers' } + + Test-MtEntraUnremediatedMediumRiskUser | Should -BeTrue + $script:GraphObjects | Should -BeNullOrEmpty + } + It 'skips as NotAuthorized on a 403 from Graph' { Mock -ModuleName Maester Invoke-MtGraphRequest { throw '403 Forbidden: Authorization_RequestDenied' } diff --git a/tests/maester/entra/Test.MT.1260.ps1 b/tests/maester/entra/Test.MT.1260.ps1 index 09d1c58ffd..2d32863c23 100644 --- a/tests/maester/entra/Test.MT.1260.ps1 +++ b/tests/maester/entra/Test.MT.1260.ps1 @@ -125,6 +125,12 @@ } $Result += $RoleNote - Add-MtTestResultDetail -Result $Result + # Each risky user also appears on the report's Affected objects page; the table above stays as it is. + $AffectedUsers = @( + $Sorted | ForEach-Object { + [pscustomobject]@{ id = [string]$_.id; displayName = [string]$_.userDisplayName; userPrincipalName = [string]$_.userPrincipalName } + } + ) + Add-MtTestResultDetail -Result $Result -GraphObjects $AffectedUsers -GraphObjectType IdentityProtection return $false } diff --git a/tests/maester/entra/Test.MT.1261.ps1 b/tests/maester/entra/Test.MT.1261.ps1 index d2acd21f45..45fc3c3c8f 100644 --- a/tests/maester/entra/Test.MT.1261.ps1 +++ b/tests/maester/entra/Test.MT.1261.ps1 @@ -128,6 +128,12 @@ } $Result += $RoleNote - Add-MtTestResultDetail -Result $Result + # Each risky user also appears on the report's Affected objects page; the table above stays as it is. + $AffectedUsers = @( + $Sorted | ForEach-Object { + [pscustomobject]@{ id = [string]$_.id; displayName = [string]$_.userDisplayName; userPrincipalName = [string]$_.userPrincipalName } + } + ) + Add-MtTestResultDetail -Result $Result -GraphObjects $AffectedUsers -GraphObjectType IdentityProtection return $false } From cb6367978b9aa9e5383a3073d454e7652bda837f Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Sun, 8 Nov 2026 14:10:33 -0700 Subject: [PATCH 12/15] MT.1261 tests: tolerate Get-MtSafeMarkdown escaping @ and : (#2715) --- ...-MtEntraUnremediatedMediumRiskUser.Tests.ps1 | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 index b29514e1f9..5a2c54105c 100644 --- a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 @@ -69,7 +69,7 @@ } Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse - $script:TestResult | Should -Match 'alice@contoso.com' + $script:TestResult | Should -Match 'alice\\?@contoso\.com' $script:TestResult | Should -Match 'atRisk' } @@ -90,7 +90,7 @@ Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse $script:TestResult | Should -Not -Match '\| \[Sign in again\]\(' - $script:TestResult | Should -Match ([regex]::Escape('\[Sign in again\]\(https://phish.example\)')) + $script:TestResult | Should -Match ([regex]::Escape('\[Sign in again\]\(https\://phish.example\)')) } It 'fails when a medium-risk user is confirmedCompromised (must not be treated as remediated)' { @@ -109,7 +109,7 @@ } Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse - $script:TestResult | Should -Match 'bob@contoso.com' + $script:TestResult | Should -Match 'bob\\?@contoso\.com' $script:TestResult | Should -Match 'confirmedCompromised' } @@ -150,8 +150,8 @@ } Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse - $script:TestResult | Should -Match 'alice@contoso.com' - $script:TestResult | Should -Not -Match 'carol@contoso.com' + $script:TestResult | Should -Match 'alice\\?@contoso\.com' + $script:TestResult | Should -Not -Match 'carol\\?@contoso\.com' $script:TestResult | Should -Match 'Found 1 medium-risk' } @@ -255,7 +255,10 @@ $script:Severity | Should -BeNullOrEmpty $script:TestResult | Should -Match 'Global Administrator' $script:TestResult | Should -Match '1 of them hold a privileged directory role' - $script:TestResult.IndexOf('bob@contoso.com') | Should -BeLessThan $script:TestResult.IndexOf('alice@contoso.com') + # Matched as a regex rather than IndexOf, so the position is found whether or not the + # escaping helper put a backslash before the @. + [regex]::Match($script:TestResult, 'bob\\?@contoso\.com').Index | + Should -BeLessThan ([regex]::Match($script:TestResult, 'alice\\?@contoso\.com').Index) } It 'treats a PIM-eligible privileged role as privileged' { @@ -297,7 +300,7 @@ Mock -ModuleName Maester Get-MtDirectoryRoleAssignment { throw 'Forbidden' } Test-MtEntraUnremediatedMediumRiskUser | Should -BeFalse - $script:TestResult | Should -Match 'alice@contoso.com' + $script:TestResult | Should -Match 'alice\\?@contoso\.com' $script:TestResult | Should -Match 'privileged users were not identified' $script:Severity | Should -BeNullOrEmpty } From ccd983ea1f5401e6a4e399541de89969c1e439e0 Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Sun, 8 Nov 2026 14:12:52 -0700 Subject: [PATCH 13/15] MT.1260/MT.1261 tests: tolerate #2715 escaping and require both users before the order check --- .../functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 | 3 +++ .../functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 | 3 +++ 2 files changed, 6 insertions(+) diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 index 7c3f31fc1b..0822c30417 100644 --- a/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 @@ -265,6 +265,9 @@ # The privileged user is listed first. # Matched as a regex rather than IndexOf, so the position is found whether or not the # escaping helper put a backslash before the @. + # Both users must be present first; a missing match returns Index 0 and would pass the order check. + $script:TestResult | Should -Match 'bob\\?@contoso\.com' + $script:TestResult | Should -Match 'alice\\?@contoso\.com' [regex]::Match($script:TestResult, 'bob\\?@contoso\.com').Index | Should -BeLessThan ([regex]::Match($script:TestResult, 'alice\\?@contoso\.com').Index) } diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 index 5a2c54105c..f12f82e8c2 100644 --- a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 @@ -257,6 +257,9 @@ $script:TestResult | Should -Match '1 of them hold a privileged directory role' # Matched as a regex rather than IndexOf, so the position is found whether or not the # escaping helper put a backslash before the @. + # Both users must be present first; a missing match returns Index 0 and would pass the order check. + $script:TestResult | Should -Match 'bob\\?@contoso\.com' + $script:TestResult | Should -Match 'alice\\?@contoso\.com' [regex]::Match($script:TestResult, 'bob\\?@contoso\.com').Index | Should -BeLessThan ([regex]::Match($script:TestResult, 'alice\\?@contoso\.com').Index) } From e0531076be3e7ac99b837262032d2202d20bc6cf Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Mon, 16 Nov 2026 11:44:41 -0700 Subject: [PATCH 14/15] MT.1260/MT.1261: give the deleted-user exclusion a reason the docs support The comments, the MT.1261 guide and two test names said admins cannot dismiss a deleted user's risk. The Dismiss API documents no such restriction, so they now give the reason that holds on its own: a deleted account cannot sign in and leaves nothing to act on. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WrYw8C2gSDFxzWqZ2zKtJB --- .../functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 | 2 +- .../functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 | 2 +- tests/maester/entra/Test.MT.1260.ps1 | 2 +- tests/maester/entra/Test.MT.1261.md | 2 +- tests/maester/entra/Test.MT.1261.ps1 | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 index 0822c30417..2d819b766f 100644 --- a/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraUnremediatedHighRiskUser.Tests.ps1 @@ -161,7 +161,7 @@ $script:TestResult | Should -Match 'Found 1 high-risk' } - It 'does not report a deleted user, whose risk an admin cannot dismiss' { + It 'does not report a deleted user, who cannot sign in and leaves nothing to act on' { Mock -ModuleName Maester Invoke-MtGraphRequest { return @( [pscustomobject]@{ diff --git a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 index f12f82e8c2..fc860d599e 100644 --- a/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 +++ b/powershell/tests/functions/Test-MtEntraUnremediatedMediumRiskUser.Tests.ps1 @@ -155,7 +155,7 @@ $script:TestResult | Should -Match 'Found 1 medium-risk' } - It 'does not report a deleted user, whose risk an admin cannot dismiss' { + It 'does not report a deleted user, who cannot sign in and leaves nothing to act on' { Mock -ModuleName Maester Invoke-MtGraphRequest { return @( [pscustomobject]@{ diff --git a/tests/maester/entra/Test.MT.1260.ps1 b/tests/maester/entra/Test.MT.1260.ps1 index 2d32863c23..21d20ae116 100644 --- a/tests/maester/entra/Test.MT.1260.ps1 +++ b/tests/maester/entra/Test.MT.1260.ps1 @@ -39,7 +39,7 @@ [OutputType([bool])] param() - # Deleted users stay in the risk report and admins cannot dismiss them, so they are not reported. + # Deleted users can stay in the risk report, but a deleted account cannot sign in and leaves nothing to act on, so it is not reported. # Active, unremediated risk states. A flag an admin has handled moves to 'remediated', # 'dismissed', 'confirmedSafe' or 'none'; those are intentionally excluded. # 'confirmedCompromised' MUST be treated as unremediated alongside 'atRisk' -- confirming a diff --git a/tests/maester/entra/Test.MT.1261.md b/tests/maester/entra/Test.MT.1261.md index 7a07563028..3ddfd5aa48 100644 --- a/tests/maester/entra/Test.MT.1261.md +++ b/tests/maester/entra/Test.MT.1261.md @@ -2,7 +2,7 @@ Medium-risk users should not be left unremediated in Entra ID Protection. Entra ID Protection rates a user's risk and tracks a risk state for each flagged user. A medium-risk flag only clears when the risk is remediated, for example by a password reset, or when an admin dismisses it or confirms the user safe. Until then the signals that triggered the flag have not been addressed. Dismissing a flag clears it but does not change the user's password, so investigate first. -This check reports users whose risk level is **medium** and whose risk state is still active and unremediated (`atRisk` or `confirmedCompromised`). States that represent a handled flag (`remediated`, `dismissed`, `confirmedSafe`, `none`) are not reported. Deleted users are not reported either, because they stay in the risk report and admins cannot dismiss their risk. The check reads risk only and never changes a user's risk state. +This check reports users whose risk level is **medium** and whose risk state is still active and unremediated (`atRisk` or `confirmedCompromised`). States that represent a handled flag (`remediated`, `dismissed`, `confirmedSafe`, `none`) are not reported. Deleted users are not reported either: they can stay in the risk report, but a deleted account cannot sign in and leaves nothing to remediate. The check reads risk only and never changes a user's risk state. If a reported user holds a privileged directory role, active or PIM-eligible, the result lists the role and shows that entry first, since even a medium-risk flag on an admin account warrants prompt investigation. diff --git a/tests/maester/entra/Test.MT.1261.ps1 b/tests/maester/entra/Test.MT.1261.ps1 index 45fc3c3c8f..ecfc09593d 100644 --- a/tests/maester/entra/Test.MT.1261.ps1 +++ b/tests/maester/entra/Test.MT.1261.ps1 @@ -43,7 +43,7 @@ [OutputType([bool])] param() - # Deleted users stay in the risk report and admins cannot dismiss them, so they are not reported. + # Deleted users can stay in the risk report, but a deleted account cannot sign in and leaves nothing to act on, so it is not reported. # Active, unremediated risk states. A flag an admin has handled moves to 'remediated', # 'dismissed', 'confirmedSafe' or 'none'; those are intentionally excluded. # 'confirmedCompromised' MUST be treated as unremediated alongside 'atRisk' -- confirming a From 0ca2ca98a4166640b5dccca16aa84916187c29f3 Mon Sep 17 00:00:00 2001 From: Darius Frank Date: Tue, 17 Nov 2026 06:24:39 -0700 Subject: [PATCH 15/15] Get-MtPrincipalPrivilegedRole: note that role scope is not checked Say in the help and at the lookup that directoryScopeId is ignored, so a role scoped to an administrative unit counts as tenant-wide, as in MT.1210. Comments only. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WrYw8C2gSDFxzWqZ2zKtJB --- .../internal/services/entra/Get-MtPrincipalPrivilegedRole.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/powershell/internal/services/entra/Get-MtPrincipalPrivilegedRole.ps1 b/powershell/internal/services/entra/Get-MtPrincipalPrivilegedRole.ps1 index 0d60ab12e9..321321543b 100644 --- a/powershell/internal/services/entra/Get-MtPrincipalPrivilegedRole.ps1 +++ b/powershell/internal/services/entra/Get-MtPrincipalPrivilegedRole.ps1 @@ -12,6 +12,9 @@ Only direct assignments are matched. A role held through a role-assignable group is not returned yet. + The scope of an assignment (directoryScopeId) is not checked, so a role scoped to an administrative unit counts + the same as a tenant-wide one. MT.1210 treats scope the same way. + The result has two properties: RolesByPrincipal, a hashtable from principal ID to a list of role names (only principals that hold a privileged role are present), and EligibleChecked, which is true when PIM-eligible assignments were read (they need Microsoft Entra ID P2 or Governance). @@ -54,6 +57,7 @@ foreach ($assignment in @($assignmentResult.Assignments)) { $principal = [string]$assignment.principalId if (-not $wanted.ContainsKey($principal)) { continue } + # directoryScopeId is not checked: an administrative-unit-scoped role counts as tenant-wide, as in MT.1210. $definition = $roleDefinitionById[[string]$assignment.roleDefinitionId] if ($null -eq $definition) { continue } # isPrivileged is not queryable on roleDefinitions; use Maester's role catalog, as MT.1210 does.