From d1c4f1732eda89b0ba6dcb0d17d365748f95b199 Mon Sep 17 00:00:00 2001 From: Julien Ly Date: Fri, 20 Nov 2026 09:49:13 +0100 Subject: [PATCH] fix(entra): MT.1063 reads every owner when $expand stops at 20 $expand on a relationship of a directory object returns at most 20 items and no @odata.nextLink. MT.1063 read owners with applications?$expand=owners, so owners past the 20th were never checked and the test could pass while one of them had no MFA registered. Apps whose expanded owners reach 20 are now read again from applications/{id}/owners, which pages. --- ...tAppRegistrationOwnersWithoutMFA.Tests.ps1 | 82 +++++++++++++++++++ tests/maester/entra/Test.MT.1063.ps1 | 9 ++ 2 files changed, 91 insertions(+) create mode 100644 powershell/tests/functions/Test-MtAppRegistrationOwnersWithoutMFA.Tests.ps1 diff --git a/powershell/tests/functions/Test-MtAppRegistrationOwnersWithoutMFA.Tests.ps1 b/powershell/tests/functions/Test-MtAppRegistrationOwnersWithoutMFA.Tests.ps1 new file mode 100644 index 0000000000..05c81de175 --- /dev/null +++ b/powershell/tests/functions/Test-MtAppRegistrationOwnersWithoutMFA.Tests.ps1 @@ -0,0 +1,82 @@ +BeforeAll { + Import-Module $PSScriptRoot/../../Maester.psd1 -Force +} + +Describe 'Test-MtAppRegistrationOwnersWithoutMFA' -Tag 'Unit' { + BeforeEach { + # Owners 1 to 20 have MFA registered, owners 21 to 25 do not. + Mock -ModuleName Maester Invoke-MtGraphRequest { + 1..25 | ForEach-Object { + [pscustomobject]@{ + id = "owner-$_" + userPrincipalName = "owner$_@contoso.com" + userDisplayName = "Owner $_" + isMfaRegistered = ($_ -le 20) + } + } + } -ParameterFilter { $RelativeUri -like 'reports/authenticationMethods/userRegistrationDetails*' } + + Mock -ModuleName Maester Invoke-MtGraphRequest { + 1..25 | ForEach-Object { + [pscustomobject]@{ + '@odata.type' = '#microsoft.graph.user' + id = "owner-$_" + displayName = "Owner $_" + userPrincipalName = "owner$_@contoso.com" + } + } + } -ParameterFilter { $RelativeUri -eq 'applications/app-1/owners' } + + Mock -ModuleName Maester Add-MtTestResultDetail { } + } + + It 'reads every owner of an app whose expanded owners stop at the 20 that $expand returns' { + # $expand returns at most 20 items of a directory object relationship, here owners 1 to 20. + Mock -ModuleName Maester Invoke-MtGraphRequest { + [pscustomobject]@{ + id = 'app-1' + appId = '11111111-1111-1111-1111-111111111111' + displayName = 'App with 25 owners' + owners = @(1..20 | ForEach-Object { + [pscustomobject]@{ + '@odata.type' = '#microsoft.graph.user' + id = "owner-$_" + displayName = "Owner $_" + userPrincipalName = "owner$_@contoso.com" + } + }) + } + } -ParameterFilter { $RelativeUri -eq 'applications?$expand=owners' } + + InModuleScope Maester { + Test-MtAppRegistrationOwnersWithoutMFA | Should -BeFalse + } + + Should -Invoke Invoke-MtGraphRequest -ModuleName Maester -Exactly 1 -ParameterFilter { $RelativeUri -eq 'applications/app-1/owners' } + Should -Invoke Add-MtTestResultDetail -ModuleName Maester -Exactly 1 -ParameterFilter { $Result -match '\| Owner 25 \|' } + } + + It 'does not read the owners again when $expand returns fewer than 20' { + Mock -ModuleName Maester Invoke-MtGraphRequest { + [pscustomobject]@{ + id = 'app-1' + appId = '11111111-1111-1111-1111-111111111111' + displayName = 'App with 19 owners' + owners = @(1..19 | ForEach-Object { + [pscustomobject]@{ + '@odata.type' = '#microsoft.graph.user' + id = "owner-$_" + displayName = "Owner $_" + userPrincipalName = "owner$_@contoso.com" + } + }) + } + } -ParameterFilter { $RelativeUri -eq 'applications?$expand=owners' } + + InModuleScope Maester { + Test-MtAppRegistrationOwnersWithoutMFA | Should -BeTrue + } + + Should -Invoke Invoke-MtGraphRequest -ModuleName Maester -Exactly 0 -ParameterFilter { $RelativeUri -eq 'applications/app-1/owners' } + } +} diff --git a/tests/maester/entra/Test.MT.1063.ps1 b/tests/maester/entra/Test.MT.1063.ps1 index 1fd6df82ac..f810746a9b 100644 --- a/tests/maester/entra/Test.MT.1063.ps1 +++ b/tests/maester/entra/Test.MT.1063.ps1 @@ -44,6 +44,15 @@ # Retrieve all applications with their owners in a single API call # The $expand parameter includes owner details to minimize round trips $allApps = Invoke-MtGraphRequest -RelativeUri 'applications?$expand=owners' -ErrorAction Stop + + # $expand on a relationship of a directory object returns at most 20 items and no @odata.nextLink + # (https://learn.microsoft.com/graph/known-issues#query-parameters). An app that shows 20 owners can + # have more, so its owners are read again from the relationship itself, which pages. + foreach ($app in @($allApps | Where-Object { $_.owners.Count -ge 20 })) { + Write-Verbose "Reading all owners of $($app.displayName): the expanded list stops at 20." + $app.owners = @(Invoke-MtGraphRequest -RelativeUri "applications/$($app.id)/owners" -ErrorAction Stop) + } + $appsWithOwners = $allApps | Where-Object { $_.owners.Count -gt 0 } Write-Verbose "Found $($appsWithOwners.Count) app registrations with owners."