diff --git a/powershell/public/connect/Get-MtGraphScope.ps1 b/powershell/public/connect/Get-MtGraphScope.ps1 index 5d2f7fc8c1..e6ba650257 100644 --- a/powershell/public/connect/Get-MtGraphScope.ps1 +++ b/powershell/public/connect/Get-MtGraphScope.ps1 @@ -100,6 +100,7 @@ 'AgentIdentityBlueprintPrincipal.Read.All' 'Application.Read.All' 'IdentityRiskyUser.Read.All' + 'SecurityEvents.Read.All' ) if ($IncludePreview) { diff --git a/powershell/tests/fixtures/golden/selection.json b/powershell/tests/fixtures/golden/selection.json index ae970e7cfd..212523d944 100644 --- a/powershell/tests/fixtures/golden/selection.json +++ b/powershell/tests/fixtures/golden/selection.json @@ -1951,7 +1951,7 @@ "AD" ] }, - "SelectedCount": 609, + "SelectedCount": 610, "SelectedFamilies": [ "MT.1024", "MT.1059", @@ -2499,6 +2499,7 @@ "MT.1252", "MT.1259", "MT.1260", + "MT.1264", "MT.1265", "ORCA.100", "ORCA.101", @@ -2581,7 +2582,7 @@ "AD" ] }, - "SelectedCount": 639, + "SelectedCount": 640, "SelectedFamilies": [ "MT.1024", "MT.1033", @@ -3161,6 +3162,7 @@ "MT.1252", "MT.1259", "MT.1260", + "MT.1264", "MT.1265", "ORCA.100", "ORCA.101", @@ -4006,7 +4008,7 @@ "AD" ] }, - "SelectedCount": 595, + "SelectedCount": 596, "SelectedFamilies": [ "MT.1024", "MT.1033", @@ -4542,6 +4544,7 @@ "MT.1252", "MT.1259", "MT.1260", + "MT.1264", "MT.1265", "ORCA.100", "ORCA.101", diff --git a/powershell/tests/functions/Get-MtGraphScope.Tests.ps1 b/powershell/tests/functions/Get-MtGraphScope.Tests.ps1 index e974fbe6ed..6c05bea7a5 100644 --- a/powershell/tests/functions/Get-MtGraphScope.Tests.ps1 +++ b/powershell/tests/functions/Get-MtGraphScope.Tests.ps1 @@ -30,6 +30,7 @@ $scopes | Should -Not -Contain 'AgentIdentityBlueprint.Read.All' $scopes | Should -Not -Contain 'AgentIdentityBlueprintPrincipal.Read.All' $scopes | Should -Not -Contain 'Application.Read.All' + $scopes | Should -Not -Contain 'SecurityEvents.Read.All' $scopes | Should -Not -Contain 'IdentityRiskyUser.Read.All' $scopes | Should -Contain 'Directory.Read.All' $scopes | Should -Not -Contain 'User.ReadBasic.All' @@ -41,6 +42,7 @@ $scopes | Should -Contain 'AgentIdentity.Read.All' $scopes | Should -Contain 'AgentIdentityBlueprint.Read.All' $scopes | Should -Contain 'AgentIdentityBlueprintPrincipal.Read.All' + $scopes | Should -Contain 'SecurityEvents.Read.All' $scopes | Should -Contain 'Application.Read.All' $scopes | Should -Contain 'IdentityRiskyUser.Read.All' $scopes | Should -Contain 'AuditLog.Read.All' diff --git a/powershell/tests/functions/Test-MtSecureScoreControl.Tests.ps1 b/powershell/tests/functions/Test-MtSecureScoreControl.Tests.ps1 new file mode 100644 index 0000000000..85e3305bf7 --- /dev/null +++ b/powershell/tests/functions/Test-MtSecureScoreControl.Tests.ps1 @@ -0,0 +1,431 @@ +Describe 'Microsoft Secure Score improvement actions (MT.1264)' { + BeforeAll { + Import-Module $PSScriptRoot/../../Maester.psd1 -Force + . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" + # The native-test functions are not exported; reach them the same way the agent tests do. + Use-MtModuleFunction -Name Get-MtSecureScoreControlInstance + Use-MtModuleFunction -Name Get-MtSecureScoreControlSeverity + Use-MtModuleFunction -Name Get-MtSecureScoreControlProduct + Use-MtModuleFunction -Name ConvertTo-MtSecureScoreInstanceId + Use-MtModuleFunction -Name ConvertFrom-MtSecureScoreHtml + Use-MtModuleFunction -Name Test-MtSecureScoreControl + + function Get-FakeControlProfile { + param([string] $Id, [double] $MaxScore = 5, [string] $Service = 'MDO', [object[]] $StateUpdates = @(), [bool] $Deprecated = $false) + [pscustomobject]@{ + id = $Id + title = "Title of $Id" + maxScore = $MaxScore + service = $Service + controlCategory = 'Apps' + deprecated = $Deprecated + remediation = "Fix $Id" + remediationImpact = 'Low' + actionUrl = "https://security.microsoft.com/$Id" + controlStateUpdates = @($StateUpdates) + } + } + function Get-FakeControlScore { + param([string] $Name, [double] $Score) + [pscustomobject]@{ controlName = $Name; score = $Score; description = "About $Name"; implementationStatus = 'Status text'; controlCategory = 'Apps' } + } + } + + BeforeEach { + $script:TestResult = $null + $script:Description = $null + $script:SkippedBecause = $null + $script:SkippedCustomReason = $null + $script:Severity = $null + + Mock -ModuleName Maester Add-MtTestResultDetail { + param($Result, $Description, $Severity, $SkippedBecause, $SkippedCustomReason) + $script:TestResult = $Result + $script:Description = $Description + $script:Severity = $Severity + $script:SkippedBecause = $SkippedBecause + $script:SkippedCustomReason = $SkippedCustomReason + } + + # Default tenant: three scored controls; the catalog also holds a control the tenant is not scored on. + $script:Snapshot = [pscustomobject]@{ + value = @([pscustomobject]@{ + controlScores = @( + (Get-FakeControlScore -Name 'mdo_safelinks' -Score 9), + (Get-FakeControlScore -Name 'AdminMFAV2' -Score 0.15), + (Get-FakeControlScore -Name 'exo_mailboxaudit' -Score 0) + ) + }) + } + $script:Profiles = @( + (Get-FakeControlProfile -Id 'mdo_safelinks' -MaxScore 9), + (Get-FakeControlProfile -Id 'AdminMFAV2' -MaxScore 10 -Service 'AzureAD'), + (Get-FakeControlProfile -Id 'exo_mailboxaudit' -MaxScore 3 -Service 'EXO'), + (Get-FakeControlProfile -Id 'mda_okta_unscored' -MaxScore 5 -Service 'MDA_Okta') + ) + $script:Recommendations = @{ value = @(@{ recommendationType = 'adminMFAV2' }) } + + Mock -ModuleName Maester Invoke-MtGraphRequest { $script:Snapshot } -ParameterFilter { $RelativeUri -eq 'security/secureScores' } + Mock -ModuleName Maester Invoke-MtGraphRequest { $script:Profiles } -ParameterFilter { $RelativeUri -eq 'security/secureScoreControlProfiles' } + Mock -ModuleName Maester Invoke-MtGraphRequest { $script:Recommendations } -ParameterFilter { $RelativeUri -like 'directory/recommendations*' } + } + + AfterEach { + # Result text must not contain backtick-wrapped code spans (repo result-text lint). + if ($null -ne $script:TestResult) { + $script:TestResult | Should -Not -Match '`[^`]+`' + } + } + + Context 'Instance source' { + It 'returns one instance per scored control, not per catalog profile' { + $instances = @(Get-MtSecureScoreControlInstance) + + $instances.Count | Should -Be 3 + $instances.Id | Should -Not -Contain 'mda_okta_unscored' + ($instances | Where-Object Id -EQ 'mdo_safelinks').Title | Should -Be 'Title of mdo_safelinks.' + } + + It 'reads only the latest snapshot, without paging through the history' { + $null = @(Get-MtSecureScoreControlInstance) + + Should -Invoke Invoke-MtGraphRequest -ModuleName Maester -Exactly -Times 1 -ParameterFilter { + $RelativeUri -eq 'security/secureScores' -and $DisablePaging -and $QueryParameters['$top'] -eq 1 + } + } + + It 'sets the severity from the maximum points' { + $instances = @(Get-MtSecureScoreControlInstance) + + ($instances | Where-Object Id -EQ 'AdminMFAV2').Severity | Should -Be 'High' + ($instances | Where-Object Id -EQ 'mdo_safelinks').Severity | Should -Be 'High' + ($instances | Where-Object Id -EQ 'exo_mailboxaudit').Severity | Should -Be 'Low' + } + + It 'tags each instance with the control service and category' { + $instance = @(Get-MtSecureScoreControlInstance) | Where-Object Id -EQ 'AdminMFAV2' + + $instance.Tag | Should -Contain 'AzureAD' + $instance.Tag | Should -Contain 'Apps' + } + + It 'places each instance under the product its service is about' { + $instances = @(Get-MtSecureScoreControlInstance) + + ($instances | Where-Object Id -EQ 'AdminMFAV2').Product | Should -Be 'entra' + ($instances | Where-Object Id -EQ 'exo_mailboxaudit').Product | Should -Be 'exchange' + # Defender for Office keeps the family's Defender product and Exposure Management feature. + ($instances | Where-Object Id -EQ 'mdo_safelinks').Product | Should -BeNullOrEmpty + ($instances | Where-Object Id -EQ 'mdo_safelinks').Feature | Should -BeNullOrEmpty + } + + It 'marks a control that is also an Entra recommendation, matching the name without regard to case' { + $instances = @(Get-MtSecureScoreControlInstance) + + ($instances | Where-Object Id -EQ 'AdminMFAV2').Data.InMt1024 | Should -BeTrue + ($instances | Where-Object Id -EQ 'exo_mailboxaudit').Data.InMt1024 | Should -BeFalse + } + + It 'does not mark an AzureAD control that is not an Entra recommendation' { + $script:Snapshot.value[0].controlScores = @(Get-FakeControlScore -Name 'AATP_AccountWithLeakedCredentials' -Score 5) + $script:Profiles = @(Get-FakeControlProfile -Id 'AATP_AccountWithLeakedCredentials' -Service 'AzureAD') + + $instance = @(Get-MtSecureScoreControlInstance)[0] + + $instance.Data.InMt1024 | Should -BeFalse + } + + It 'still returns instances when the Entra recommendations cannot be read' { + Mock -ModuleName Maester Invoke-MtGraphRequest { throw 'Forbidden' } -ParameterFilter { $RelativeUri -like 'directory/recommendations*' } + + $instances = @(Get-MtSecureScoreControlInstance) + + $instances.Count | Should -Be 3 + $instances.Data.InMt1024 | Should -Not -Contain $true + } + + It 'turns a control name that is not a valid instance ID into one, so the family still runs' { + $script:Snapshot.value[0].controlScores = @( + (Get-FakeControlScore -Name 'mdo_safelinks' -Score 9), + (Get-FakeControlScore -Name '_Turn on audit (EXO)' -Score 0) + ) + $script:Profiles = @((Get-FakeControlProfile -Id 'mdo_safelinks' -MaxScore 9), (Get-FakeControlProfile -Id '_Turn on audit (EXO)' -MaxScore 3)) + + $instances = @(Get-MtSecureScoreControlInstance) + + $instances.Id | Should -Be @('mdo_safelinks', 'Turn-on-audit--EXO-') + foreach ($id in $instances.Id) { $id | Should -Match '^[A-Za-z0-9][A-Za-z0-9._-]{0,127}\z' } + ($instances | Where-Object Id -EQ 'Turn-on-audit--EXO-').Title | Should -Be 'Title of _Turn on audit (EXO).' + } + + It 'keeps instance IDs unique when two control names map to the same ID' { + $script:Snapshot.value[0].controlScores = @((Get-FakeControlScore -Name 'a b' -Score 0), (Get-FakeControlScore -Name 'a-b' -Score 0), (Get-FakeControlScore -Name 'A_B' -Score 0)) + $script:Profiles = @((Get-FakeControlProfile -Id 'a b'), (Get-FakeControlProfile -Id 'a-b'), (Get-FakeControlProfile -Id 'A_B')) + + $instances = @(Get-MtSecureScoreControlInstance) + + $instances.Id | Should -Be @('a-b', 'a-b-2', 'A_B') + } + + It 'cuts a long control name to the 128 characters an instance ID allows' { + $long = 'x' * 200 + $script:Snapshot.value[0].controlScores = @((Get-FakeControlScore -Name $long -Score 0), (Get-FakeControlScore -Name ($long + 'y') -Score 0)) + $script:Profiles = @((Get-FakeControlProfile -Id $long), (Get-FakeControlProfile -Id ($long + 'y'))) + + $instances = @(Get-MtSecureScoreControlInstance) + + $instances[0].Id.Length | Should -Be 128 + $instances[1].Id | Should -Be (('x' * 126) + '-2') + } + + It 'skips a control whose name has no letter or digit' { + $script:Snapshot.value[0].controlScores = @((Get-FakeControlScore -Name 'mdo_safelinks' -Score 9), (Get-FakeControlScore -Name ' -- ' -Score 0)) + + $instances = @(Get-MtSecureScoreControlInstance) + + $instances.Id | Should -Be @('mdo_safelinks') + } + + It 'runs the whole family when a control name is not a valid instance ID' { + $script:Snapshot.value[0].controlScores = @((Get-FakeControlScore -Name 'mdo_safelinks' -Score 9), (Get-FakeControlScore -Name 'Turn on audit' -Score 0)) + $script:Profiles = @((Get-FakeControlProfile -Id 'mdo_safelinks' -MaxScore 9), (Get-FakeControlProfile -Id 'Turn on audit' -MaxScore 3)) + + # Offline run: Graph forced connected through the run config, licences Unknown (never a skip). + Mock -ModuleName Maester Get-MtShippedMaesterConfig { [pscustomobject]@{} } + $runConfig = [pscustomobject]@{ Environment = [pscustomobject]@{ Services = [pscustomobject]@{ Graph = $true } } } + Push-Location $TestDrive + try { $rows = @(Invoke-MtTest -Id 'MT.1264' -Config $runConfig 3>$null 6>$null) } finally { Pop-Location } + + $rows.ReasonCode | Should -Not -Contain 'InvalidInstanceId' + $rows.Id | Should -Contain 'MT.1264.Turn-on-audit' + } + + It 'keeps a scored control that has no profile so the test reports it as an error' { + $script:Profiles = @($script:Profiles | Where-Object id -NE 'exo_mailboxaudit') + + $instances = @(Get-MtSecureScoreControlInstance) + + $instances.Count | Should -Be 3 + $missing = $instances | Where-Object Id -EQ 'exo_mailboxaudit' + $missing.Data.Profile | Should -BeNullOrEmpty + { Test-MtSecureScoreControl -Instance $missing } | Should -Throw '*no control profile*' + } + + It 'skips a control whose portal state update reaches the test through instance creation' { + $script:Profiles = @(Get-FakeControlProfile -Id 'exo_mailboxaudit' -MaxScore 5 -StateUpdates @([pscustomobject]@{ state = 'Ignored'; updatedBy = 'admin@contoso.com'; comment = 'Handled elsewhere'; updatedDateTime = '2026-10-01T00:00:00Z' })) + $script:Snapshot.value[0].controlScores = @(Get-FakeControlScore -Name 'exo_mailboxaudit' -Score 0) + + $instance = @(Get-MtSecureScoreControlInstance) | Where-Object Id -EQ 'exo_mailboxaudit' + + Test-MtSecureScoreControl -Instance $instance | Should -BeNullOrEmpty + } + + It 'skips a deprecated control' { + $script:Profiles = @($script:Profiles | Where-Object id -NE 'exo_mailboxaudit') + @(Get-FakeControlProfile -Id 'exo_mailboxaudit' -MaxScore 3 -Deprecated $true) + + @(Get-MtSecureScoreControlInstance).Id | Should -Not -Contain 'exo_mailboxaudit' + } + + It 'carries the snapshot date on every instance' { + $script:Snapshot.value[0] | Add-Member -NotePropertyName createdDateTime -NotePropertyValue '2026-11-12T03:00:00Z' + + $instances = @(Get-MtSecureScoreControlInstance) + $instances.Count | Should -Be 3 + $instances | ForEach-Object { $_.Data.SnapshotDate | Should -Be '2026-11-12T03:00:00Z' } + } + + It 'returns no instances when the tenant has no snapshot yet' { + $script:Snapshot = [pscustomobject]@{ value = @() } + + @(Get-MtSecureScoreControlInstance).Count | Should -Be 0 + } + + It 'skips the family as NotAuthorized on a 403 from Graph' { + Mock -ModuleName Maester Invoke-MtGraphRequest { throw '403 Forbidden: Authorization_RequestDenied' } -ParameterFilter { $RelativeUri -eq 'security/secureScores' } + + @(Get-MtSecureScoreControlInstance).Count | Should -Be 0 + $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' } -ParameterFilter { $RelativeUri -eq 'security/secureScores' } + + { Get-MtSecureScoreControlInstance } | Should -Throw '*Service unavailable*' + } + } + + Context 'Severity' { + It 'maps points to ' -ForEach @( + @{ MaxScore = 10; Expected = 'High' } + @{ MaxScore = 8; Expected = 'High' } + @{ MaxScore = 7; Expected = 'Medium' } + @{ MaxScore = 4; Expected = 'Medium' } + @{ MaxScore = 3; Expected = 'Low' } + @{ MaxScore = 1; Expected = 'Low' } + ) { + Get-MtSecureScoreControlSeverity -MaxScore $MaxScore | Should -Be $Expected + } + } + + Context 'Product' { + It 'maps the service to ' -ForEach @( + @{ Service = 'AzureAD'; Product = 'entra'; Feature = $null } + @{ Service = 'EXO'; Product = 'exchange'; Feature = $null } + @{ Service = 'MS Teams'; Product = 'teams'; Feature = $null } + @{ Service = 'SPO'; Product = 'sharepoint'; Feature = $null } + @{ Service = 'MIP'; Product = 'purview'; Feature = $null } + @{ Service = 'Admincenter'; Product = 'microsoft-365'; Feature = $null } + @{ Service = 'Azure ATP'; Product = 'defender'; Feature = 'defender-for-identity' } + @{ Service = 'MDO'; Product = $null; Feature = $null } + @{ Service = 'MDA_Okta'; Product = $null; Feature = $null } + @{ Service = ''; Product = $null; Feature = $null } + ) { + $placement = Get-MtSecureScoreControlProduct -Service $Service + $placement.Product | Should -Be $Product + $placement.Feature | Should -Be $Feature + } + + It 'uses only product and feature keys that are in the Maester taxonomy' { + $taxonomy = Import-PowerShellDataFile "$PSScriptRoot/../../assets/MaesterTaxonomy.psd1" + foreach ($service in 'AzureAD', 'EXO', 'MS Teams', 'SPO', 'MIP', 'FORMS', 'SWAY', 'Admincenter', 'Azure ATP') { + $placement = Get-MtSecureScoreControlProduct -Service $service + ($taxonomy | ConvertTo-Json -Depth 10) | Should -Match ([regex]::Escape("`"$($placement.Product)`"")) + if ($placement.Feature) { + ($taxonomy | ConvertTo-Json -Depth 10) | Should -Match ([regex]::Escape("`"$($placement.Feature)`"")) + } + } + } + } + + Context 'HTML conversion' { + It 'turns links, list items and line breaks into markdown and drops other tags' { + $html = '

Turn it on.
Then check it.

  • One 
  • Two
' + + $md = ConvertFrom-MtSecureScoreHtml -Html $html + + $md | Should -Match '\[check it\]\(https://contoso.com/x\)' + $md | Should -Match '(?m)^- One$' + $md | Should -Match '(?m)^- Two$' + $md | Should -Not -Match '<[^>]+>' + $md | Should -Not -Match ' ' + } + + It 'does not turn encoded markup back into HTML' -ForEach @( + @{ Html = '<script>alert(1)</script>'; Expected = '\alert(1)\' } + @{ Html = '<img src=x onerror=alert(1)>'; Expected = '\' } + @{ Html = '

<iframe src="https://example.com"></iframe>

'; Expected = '\