Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -18,19 +18,37 @@ AfterAll {
Describe 'Test-MtSpExchangeAppAccessPolicy' {
BeforeEach {
$script:Result = $null
$script:Investigate = $false
Mock -ModuleName Maester Add-MtTestResultDetail {
param($Result)
param($Result, [switch] $Investigate)
$script:Result = $Result
$script:Investigate = $Investigate.IsPresent
}
Mock -ModuleName Maester Get-MtLinkServicePrincipal { 'Contoso Mail App' }
Mock -ModuleName Maester Get-ApplicationAccessPolicy { @() }
Mock -ModuleName Maester Invoke-MtGraphRequest { throw "Unexpected Graph request: $RelativeUri" }
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals' -and $Filter
$RelativeUri -eq 'servicePrincipals' -and $Filter -eq "appId eq '00000003-0000-0000-c000-000000000000'"
} {
[pscustomobject]@{
Id = 'graph-sp'
AppRoles = @([pscustomobject]@{ Id = 'mail-read-role'; Value = 'Mail.Read' })
DisplayName = 'Microsoft Graph'
AppRoles = @(
[pscustomobject]@{ Id = 'mail-read-role'; Value = 'Mail.Read' }
[pscustomobject]@{ Id = 'user-read-all-role'; Value = 'User.Read.All' }
)
}
}
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals' -and $Filter -eq "appId eq '00000002-0000-0ff1-ce00-000000000000'"
} {
[pscustomobject]@{
Id = 'exchange-sp'
DisplayName = 'Office 365 Exchange Online'
AppRoles = @(
[pscustomobject]@{ Id = 'ews-role'; Value = 'full_access_as_app' }
[pscustomobject]@{ Id = 'exchange-manage-role'; Value = 'Exchange.ManageAsApp' }
)
}
}
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
Expand All @@ -41,7 +59,7 @@ Describe 'Test-MtSpExchangeAppAccessPolicy' {
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals/app-sp/appRoleAssignments'
} {
[pscustomobject]@{ AppRoleId = 'mail-read-role' }
[pscustomobject]@{ AppRoleId = 'mail-read-role'; ResourceId = 'graph-sp' }
}
}

Expand Down Expand Up @@ -88,6 +106,23 @@ Describe 'Test-MtSpExchangeAppAccessPolicy' {
Test-MtSpExchangeAppAccessPolicy | Should -BeTrue
}

It 'Passes when a RestrictAccess policy applies to all applications' {
Mock -ModuleName Maester Get-ApplicationAccessPolicy {
[pscustomobject]@{ AppId = @('*'); AccessRight = 'RestrictAccess' }
}

Test-MtSpExchangeAppAccessPolicy | Should -BeTrue
$script:Result | Should -Match 'All \*\*1\*\* applications'
}

It 'Does not count a DenyAccess policy that applies to all applications' {
Mock -ModuleName Maester Get-ApplicationAccessPolicy {
[pscustomobject]@{ AppId = @('*'); AccessRight = 'DenyAccess' }
}

Test-MtSpExchangeAppAccessPolicy | Should -BeFalse
}

It 'Fails when no application access policies exist' {
Test-MtSpExchangeAppAccessPolicy | Should -BeFalse
}
Expand Down Expand Up @@ -142,18 +177,130 @@ Describe 'Test-MtSpExchangeAppAccessPolicy' {

{ Test-MtSpExchangeAppAccessPolicy } | Should -Throw '*Graph unavailable*'
}

It 'Resolves an app role against the API the assignment points to' {
# Microsoft Graph's Mail.Read role ID, but assigned on another API: it is not Mail.Read there.
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals/app-sp/appRoleAssignments'
} {
[pscustomobject]@{ AppRoleId = 'mail-read-role'; ResourceId = 'other-api-sp' }
}

Test-MtSpExchangeAppAccessPolicy | Should -BeTrue
$script:Result | Should -BeExactly 'Well done. No applications with Exchange permissions were found.'
}

It 'Lists an assignment whose role the API does not define instead of dropping it' {
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals/app-sp/appRoleAssignments'
} {
[pscustomobject]@{ AppRoleId = 'mail-read-role'; ResourceId = 'graph-sp' }
[pscustomobject]@{ AppRoleId = 'unknown-role'; ResourceId = 'graph-sp' }
}

Test-MtSpExchangeAppAccessPolicy | Should -BeFalse
$script:Investigate | Should -BeFalse -Because 'a definite failure stays a failure'
$script:Result | Should -Match ([regex]::Escape('| Contoso Mail App | mail-app | Mail.Read |'))
$script:Result | Should -Match '### App Role Assignments Not Evaluated'
$script:Result | Should -Match ([regex]::Escape('| Contoso Mail App | Microsoft Graph | unknown-role |'))
}

It 'Asks for investigation instead of passing when the only finding is an assignment the API does not define' {
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals/app-sp/appRoleAssignments'
} {
[pscustomobject]@{ AppRoleId = 'unknown-role'; ResourceId = 'graph-sp' }
}

$returned = Test-MtSpExchangeAppAccessPolicy
$returned | Should -BeNullOrEmpty -Because 'an Investigate result that returns $true is exported to NUnit/JUnit as a pass'
$script:Investigate | Should -BeTrue
$script:Result | Should -Not -Match 'Well done'
$script:Result | Should -Match ([regex]::Escape('| Contoso Mail App | Microsoft Graph | unknown-role |'))
}

It 'Does not list the default access role as not evaluated' {
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals/app-sp/appRoleAssignments'
} {
[pscustomobject]@{ AppRoleId = '00000000-0000-0000-0000-000000000000'; ResourceId = 'graph-sp' }
}

Test-MtSpExchangeAppAccessPolicy | Should -BeTrue
$script:Result | Should -Not -Match 'Not Evaluated'
}

It 'Counts an app whose only mailbox permission is full_access_as_app' {
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals/app-sp/appRoleAssignments'
} {
[pscustomobject]@{ AppRoleId = 'ews-role'; ResourceId = 'exchange-sp' }
}

Test-MtSpExchangeAppAccessPolicy | Should -BeFalse
$script:Result | Should -Match ([regex]::Escape('Found **1** applications with Exchange permissions.'))
$script:Result | Should -Match ([regex]::Escape('| Contoso Mail App | mail-app | full_access_as_app |'))
}

It 'Passes for full_access_as_app when a RestrictAccess policy includes the app' {
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals/app-sp/appRoleAssignments'
} {
[pscustomobject]@{ AppRoleId = 'ews-role'; ResourceId = 'exchange-sp' }
}
Mock -ModuleName Maester Get-ApplicationAccessPolicy {
[pscustomobject]@{ AppId = @('mail-app'); AccessRight = 'RestrictAccess' }
}

Test-MtSpExchangeAppAccessPolicy | Should -BeTrue
}

It 'Lists the Microsoft Graph and EWS permissions of one app together' {
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals/app-sp/appRoleAssignments'
} {
[pscustomobject]@{ AppRoleId = 'mail-read-role'; ResourceId = 'graph-sp' }
[pscustomobject]@{ AppRoleId = 'ews-role'; ResourceId = 'exchange-sp' }
}

Test-MtSpExchangeAppAccessPolicy | Should -BeFalse
$script:Result | Should -Match ([regex]::Escape('| Contoso Mail App | mail-app | Mail.Read, full_access_as_app |'))
}

It 'Does not count an Exchange Online app role other than full_access_as_app' {
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals/app-sp/appRoleAssignments'
} {
[pscustomobject]@{ AppRoleId = 'exchange-manage-role'; ResourceId = 'exchange-sp' }
}

Test-MtSpExchangeAppAccessPolicy | Should -BeTrue
}

It 'Still evaluates Microsoft Graph permissions when the tenant has no Office 365 Exchange Online service principal' {
# Without that service principal, no app role assignment can point at it.
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals' -and $Filter -eq "appId eq '00000002-0000-0ff1-ce00-000000000000'"
} { @() }

Test-MtSpExchangeAppAccessPolicy | Should -BeFalse
$script:Result | Should -Match ([regex]::Escape('| Contoso Mail App | mail-app | Mail.Read |'))
$script:Result | Should -Not -Match 'Not Evaluated'
}
}

Describe 'MT.1058 native engine results' {
BeforeEach {
Mock -ModuleName Maester Get-MgContext { $null }
Mock -ModuleName Maester Invoke-MtGraphRequest {
if ($RelativeUri -eq 'servicePrincipals' -and $Filter) {
[pscustomobject]@{ AppRoles = @([pscustomobject]@{ Id = 'mail-read-role'; Value = 'Mail.Read' }) }
if ($RelativeUri -eq 'servicePrincipals' -and $Filter -eq "appId eq '00000003-0000-0000-c000-000000000000'") {
[pscustomobject]@{ Id = 'graph-sp'; DisplayName = 'Microsoft Graph'; AppRoles = @([pscustomobject]@{ Id = 'mail-read-role'; Value = 'Mail.Read' }) }
} elseif ($RelativeUri -eq 'servicePrincipals' -and $Filter) {
@()
} elseif ($RelativeUri -eq 'servicePrincipals') {
[pscustomobject]@{ Id = 'app-sp'; AppId = 'mail-app'; DisplayName = 'Contoso Mail App' }
} elseif ($RelativeUri -eq 'servicePrincipals/app-sp/appRoleAssignments') {
[pscustomobject]@{ AppRoleId = 'mail-read-role' }
[pscustomobject]@{ AppRoleId = 'mail-read-role'; ResourceId = 'graph-sp' }
} else {
throw "Unexpected Graph request: $RelativeUri"
}
Expand All @@ -180,4 +327,17 @@ Describe 'MT.1058 native engine results' {
$rows[0].Result | Should -Be $Expected
$rows[0].ResultDetail.TestResult | Should -Not -BeNullOrEmpty
}

It 'Records an assignment the API does not define as Investigate through Invoke-MtTest' {
Mock -ModuleName Maester Get-ApplicationAccessPolicy { @() }
Mock -ModuleName Maester Invoke-MtGraphRequest -ParameterFilter {
$RelativeUri -eq 'servicePrincipals/app-sp/appRoleAssignments'
} {
[pscustomobject]@{ AppRoleId = 'unknown-role'; ResourceId = 'graph-sp' }
}

$rows = @(Invoke-MtTest -Id 'MT.1058' -Config $script:config)
$rows | Should -HaveCount 1
$rows[0].Result | Should -Be 'Investigate'
}
}
13 changes: 10 additions & 3 deletions tests/maester/entra/Test.MT.1058.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This check evaluates legacy application access policies in Exchange Online for applications with Microsoft Graph mail permissions.
This check evaluates legacy application access policies in Exchange Online for applications with Microsoft Graph mail permissions or the Exchange Web Services (EWS) permission `full_access_as_app`.

These permissions grant access to every mailbox unless an application access policy restricts them.

Expand All @@ -15,16 +15,21 @@ Microsoft Exchange related permissions that should be secured by application acc
- Calendars.ReadWrite
- Contacts.Read
- Contacts.ReadWrite
- full_access_as_app (Office 365 Exchange Online)

This check requires a `RestrictAccess` policy for each application. A `DenyAccess` policy only denies access to mailboxes in its scope; it does not restrict access to mailboxes outside that scope and does not satisfy this check.
Each permission is matched on the API that defines it. `full_access_as_app` gives access to every mailbox through EWS, which Microsoft is retiring in Exchange Online; it matters wherever EWS is still enabled.

An app role assignment to one of these two APIs that names a role the API does not define is listed under **App Role Assignments Not Evaluated**. When nothing else fails, the result is marked for investigation instead of passing, and `Test-MtSpExchangeAppAccessPolicy` returns `$null`, so NUnit/JUnit exports ignore it instead of reporting a pass.

This check requires a `RestrictAccess` policy for each application; a `RestrictAccess` policy created with `-AppId *` applies to every application. A `DenyAccess` policy only denies access to mailboxes in its scope; it does not restrict access to mailboxes outside that scope and does not satisfy this check.

### Remediation action

Microsoft has replaced application access policies with [RBAC for Applications](https://learn.microsoft.com/exchange/permissions-exo/application-rbac) and advises against creating new application access policies.

1. For an app still using legacy policies, review its existing policies and confirm a matching `RestrictAccess` policy limits it to the intended mailboxes. A deny-only policy does not provide that limit.
2. For new configurations or migration, follow Microsoft's RBAC for Applications guide to assign the required Exchange application role with a mailbox scope.
3. Remove equivalent or broader Microsoft Graph application permissions from the app's Entra grants after migration. Entra and Exchange grants are additive; retaining an unscoped Entra grant defeats the Exchange scope. See [MT.1252](https://maester.dev/docs/tests/MT.1252) for overlapping grants.
3. Remove equivalent or broader application permissions (Microsoft Graph, or `full_access_as_app` for EWS) from the app's Entra grants after migration. Entra and Exchange grants are additive; retaining an unscoped Entra grant defeats the Exchange scope. See [MT.1252](https://maester.dev/docs/tests/MT.1252) for overlapping grants.
4. Verify the app against both an intended mailbox and a mailbox outside its scope. `Test-ServicePrincipalAuthorization` evaluates the Exchange RBAC assignment; it does not include separate Entra grants.

MT.1058 does not evaluate RBAC assignments. An app whose mail permissions are granted only through Exchange RBAC is outside this check's scope.
Expand All @@ -40,7 +45,9 @@ Test-ApplicationAccessPolicy -Identity user@contoso.com -AppId '<your-app-id>'
#### Related links

- [Application access policy replacement notice - Microsoft Learn](https://learn.microsoft.com/powershell/module/exchangepowershell/new-applicationaccesspolicy)
- [Application access policies: supported permissions - Microsoft Learn](https://learn.microsoft.com/exchange/permissions-exo/application-access-policies#supported-permissions-and-other-resources)
- [RBAC for Applications in Exchange Online - Microsoft Learn](https://learn.microsoft.com/exchange/permissions-exo/application-rbac)
- [Exchange Online EWS retirement - Exchange Team Blog](https://techcommunity.microsoft.com/blog/exchange/exchange-online-ews-your-time-is-almost-up/4492361)

<!--- Results --->
%TestResult%
Loading
Loading