Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
38cf131
feat(entra): add MT.1261 - medium-risk users should not be left unrem…
Dfrank77 Nov 5, 2026
637e78a
fix(entra): MT.1261 declare the role-read scope it uses
Dfrank77 Nov 5, 2026
4eb9ee8
fix(entra): MT.1261 raise privileged users to High severity, per #2518
Dfrank77 Nov 5, 2026
823f2e5
fix(entra): MT.1261 declare supported clouds
Dfrank77 Nov 5, 2026
73a346d
fix(entra): MT.1261 skip deleted users
Dfrank77 Nov 5, 2026
5e1e549
docs(entra): MT.1261 clarify that dismissing does not change the pass…
Dfrank77 Nov 5, 2026
1253ca7
fix(entra): MT.1261 stop claiming a severity raise that never applies
Dfrank77 Nov 7, 2026
48fae55
MT.1261: escape tenant names with Get-MtSafeMarkdown
Dfrank77 Nov 7, 2026
38bed12
Merge remote-tracking branch 'upstream/main' into feat/mt1261-idp-unr…
Dfrank77 Nov 8, 2026
14241dd
MT.1260: align with MT.1261 (isDeleted filter, Cloud, RoleManagement …
Dfrank77 Nov 8, 2026
8189ed6
Move the privileged-role lookup into Get-MtPrincipalPrivilegedRole
Dfrank77 Nov 8, 2026
02c1a16
MT.1260/MT.1261: list risky users on the Affected objects page
Dfrank77 Nov 8, 2026
f7b980e
Merge remote-tracking branch 'upstream/main' into feat/mt1261-idp-unr…
Dfrank77 Nov 8, 2026
cb63679
MT.1261 tests: tolerate Get-MtSafeMarkdown escaping @ and : (#2715)
Dfrank77 Nov 8, 2026
ccd983e
MT.1260/MT.1261 tests: tolerate #2715 escaping and require both users…
Dfrank77 Nov 8, 2026
2be5774
Merge remote-tracking branch 'upstream/main' into feat/mt1261-idp-unr…
Dfrank77 Nov 16, 2026
e053107
MT.1260/MT.1261: give the deleted-user exclusion a reason the docs su…
Dfrank77 Nov 16, 2026
0ca2ca9
Get-MtPrincipalPrivilegedRole: note that role scope is not checked
Dfrank77 Nov 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 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).

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 }
# 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.
$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
}
}
9 changes: 6 additions & 3 deletions powershell/tests/fixtures/golden/selection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1951,7 +1951,7 @@
"AD"
]
},
"SelectedCount": 609,
"SelectedCount": 610,
"SelectedFamilies": [
"MT.1024",
"MT.1059",
Expand Down Expand Up @@ -2499,6 +2499,7 @@
"MT.1252",
"MT.1259",
"MT.1260",
"MT.1261",
"MT.1265",
"ORCA.100",
"ORCA.101",
Expand Down Expand Up @@ -2581,7 +2582,7 @@
"AD"
]
},
"SelectedCount": 639,
"SelectedCount": 640,
"SelectedFamilies": [
"MT.1024",
"MT.1033",
Expand Down Expand Up @@ -3161,6 +3162,7 @@
"MT.1252",
"MT.1259",
"MT.1260",
"MT.1261",
"MT.1265",
"ORCA.100",
"ORCA.101",
Expand Down Expand Up @@ -4006,7 +4008,7 @@
"AD"
]
},
"SelectedCount": 595,
"SelectedCount": 596,
"SelectedFamilies": [
"MT.1024",
"MT.1033",
Expand Down Expand Up @@ -4542,6 +4544,7 @@
"MT.1252",
"MT.1259",
"MT.1260",
"MT.1261",
"MT.1265",
"ORCA.100",
"ORCA.101",
Expand Down
Original file line number Diff line number Diff line change
@@ -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*'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -157,6 +161,52 @@
$script:TestResult | Should -Match 'Found 1 high-risk'
}

It 'does not report a deleted user, who cannot sign in and leaves nothing to act on' {
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 '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' }

Expand Down Expand Up @@ -215,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)
}
Expand Down
Loading
Loading