From 8d835c4172eb90de35049d7a7e3d423281de9c6b Mon Sep 17 00:00:00 2001 From: Eduard Arbona Date: Fri, 6 Nov 2026 15:25:12 -0400 Subject: [PATCH 1/3] fix(MT.1087): no pass when the hunting query returned nothing at all MT.1087 decided its verdict with [string]::IsNullOrEmpty($Devices), and Invoke-MtGraphSecurityQuery returns nothing both when no device matches and when the query could not run: its non-KQL error path returns without a result. So a tenant with no device onboarded to Defender for Endpoint, or one whose query failed, was reported as "Well done. No public exposed devices ... were found" on a High severity check. An empty result is now the question rather than the answer. The check asks whether Defender has any vulnerability data for the tenant, with a summarize that has no by clause so it returns one row even over no input, and skips as NotApplicable when that count is zero. Zero exposed devices out of zero assessed devices is not a pass. The second query only runs when the first came back empty, so a populated result does not pay for it, and a unit test asserts that. Two more defects in the same expression: $return in `if ($return -or [string]::IsNullOrEmpty($Devices))` is referenced once and assigned nowhere in the file, so the first leg was dead and would throw under Set-StrictMode. Removed. $result held the markdown table inside the failure branch and was then reassigned to the boolean the function returns. It was also only assigned on that branch while the -replace below consumed it on both, so the pass path substituted an undefined variable. The markdown now has its own name, is initialised on both paths, and the return value is the comparison itself rather than a reused variable. Five unit tests: the unassessed tenant skips, an assessed tenant with nothing exposed passes, the pass path leaves no %TestResult% placeholder, an exposed device fails and is listed, and the denominator query is not issued when devices were already found. --- ...lyExploitableHighExposureDevices.Tests.ps1 | 94 +++++++++++++++++++ tests/maester/xspm/Test.MT.1087.ps1 | 32 +++++-- 2 files changed, 119 insertions(+), 7 deletions(-) create mode 100644 powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 diff --git a/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 b/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 new file mode 100644 index 0000000000..06128bc0bd --- /dev/null +++ b/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 @@ -0,0 +1,94 @@ +Describe 'MT.1087 public exposed devices with a remotely exploitable CVE' { + BeforeAll { + Import-Module $PSScriptRoot/../../Maester.psd1 -Force + . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" + # The check is a native test, so the function is not exported. + Use-MtModuleFunction -Name Test-MtXspmPublicRemotelyExploitableHighExposureDevices + } + + BeforeEach { + $script:TestResult = $null + $script:SkippedBecause = $null + + Mock -ModuleName Maester Get-MtLicenseInformation { return $true } + Mock -ModuleName Maester Add-MtTestResultDetail { + param($Result, $SkippedBecause, $Severity) + + $script:TestResult = $Result + $script:SkippedBecause = $SkippedBecause + } + } + + It 'skips instead of passing when Defender has no vulnerability data for the tenant' { + # Invoke-MtGraphSecurityQuery returns nothing both when no device matches and when the query + # could not run, so an empty result alone used to be the pass. Zero exposed devices out of + # zero assessed devices is not a pass. + Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { + if ($Query -match 'summarize AssessedDeviceCount') { + return @([pscustomobject]@{ AssessedDeviceCount = 0 }) + } + return @() + } + + $null -eq (Test-MtXspmPublicRemotelyExploitableHighExposureDevices) | Should -BeTrue + $script:SkippedBecause | Should -Be 'NotApplicable' + } + + It 'passes when the fleet is assessed and no device is exposed' { + Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { + if ($Query -match 'summarize AssessedDeviceCount') { + return @([pscustomobject]@{ AssessedDeviceCount = 120 }) + } + return @() + } + + Test-MtXspmPublicRemotelyExploitableHighExposureDevices | Should -BeTrue + $script:TestResult | Should -Match 'Well done' + $script:SkippedBecause | Should -BeNullOrEmpty + } + + It 'renders no placeholder on the pass path' { + # The markdown accumulator used to be assigned only on the failure path while the -replace + # consumed it either way. + Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { + if ($Query -match 'summarize AssessedDeviceCount') { + return @([pscustomobject]@{ AssessedDeviceCount = 5 }) + } + return @() + } + + $null = Test-MtXspmPublicRemotelyExploitableHighExposureDevices + $script:TestResult | Should -Not -Match '%TestResult%' + } + + It 'fails and lists the devices when any is exposed' { + Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { + if ($Query -match 'summarize AssessedDeviceCount') { + return @([pscustomobject]@{ AssessedDeviceCount = 120 }) + } + return @([pscustomobject]@{ + DeviceName = 'web-01' + MaxEpssScore = 0.72 + MaxCvssScore = 9.8 + CveList = @('CVE-2026-0001', 'CVE-2026-0002') + PublicOpenPortList = @(443, 8080) + }) + } + + Test-MtXspmPublicRemotelyExploitableHighExposureDevices | Should -BeFalse + $script:TestResult | Should -Match 'web-01' + $script:TestResult | Should -Match 'CVE-2026-0001, CVE-2026-0002' + $script:TestResult | Should -Not -Match '%TestResult%' + } + + It 'does not ask for the denominator when devices were already found' { + # The second query is only the tie-breaker for an empty result; a populated result must not + # pay for it. + Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { + @([pscustomobject]@{ DeviceName = 'web-01'; CveList = @('CVE-2026-0003'); PublicOpenPortList = @(443) }) + } + + $null = Test-MtXspmPublicRemotelyExploitableHighExposureDevices + Should -Invoke -ModuleName Maester Invoke-MtGraphSecurityQuery -Times 1 -Exactly + } +} diff --git a/tests/maester/xspm/Test.MT.1087.ps1 b/tests/maester/xspm/Test.MT.1087.ps1 index 281b44d21e..699bdea484 100644 --- a/tests/maester/xspm/Test.MT.1087.ps1 +++ b/tests/maester/xspm/Test.MT.1087.ps1 @@ -128,23 +128,41 @@ $Severity = "High" - if ($return -or [string]::IsNullOrEmpty($Devices)) { + if ([string]::IsNullOrEmpty($Devices)) { + # Invoke-MtGraphSecurityQuery returns nothing both when no device matches and when the query + # could not run at all, so an empty result on its own cannot be read as a clean fleet. This + # asks whether Defender has any vulnerability data for this tenant before calling it a pass. + # A summarize with no by clause returns exactly one row even over no input, so the count + # comes back either way. + $assessed = Invoke-MtGraphSecurityQuery -Timespan "P1D" -Query @' +DeviceTvmSoftwareVulnerabilities +| summarize AssessedDeviceCount = dcount(DeviceId) +'@ + $assessedCount = [int](($assessed | Select-Object -First 1).AssessedDeviceCount) + if ($assessedCount -le 0) { + # No device is onboarded to Defender for Endpoint, or the source could not be read. + # Zero exposed devices out of zero assessed devices is not a pass. + Add-MtTestResultDetail -SkippedBecause NotApplicable + return $null + } + $testResultMarkdown = "Well done. No public exposed devices with high or critical CVE's and high changes of exploitation were found." + $resultMarkdown = "" } else { $testResultMarkdown = "At least one public exposed device with high or critical CVE's and high changes of exploitation was found.`n`n%TestResult%" Write-Verbose "Found $($Devices.Count) public exposed devices with high or critical CVE's and high changes of exploitation." - $result = "| DeviceName | MaxEpssScore | MaxCvssScore | CveList | PublicOpenPortList | `n" - $result += "| --- | --- | --- | --- | --- |`n" + $resultMarkdown = "| DeviceName | MaxEpssScore | MaxCvssScore | CveList | PublicOpenPortList | `n" + $resultMarkdown += "| --- | --- | --- | --- | --- |`n" foreach ($Device in $Devices) { $CveList = $($Device.CveList) -join ', ' # "user1, user2, user3" $PublicOpenPortList = $($Device.PublicOpenPortList) -join ', ' # "user1, user2, user3" - $result += "| $($Device.DeviceName) | $($Device.MaxEpssScore) | $($Device.MaxCvssScore) | $($CveList) | $($PublicOpenPortList) |`n" + $resultMarkdown += "| $($Device.DeviceName) | $($Device.MaxEpssScore) | $($Device.MaxCvssScore) | $($CveList) | $($PublicOpenPortList) |`n" } } - $testResultMarkdown = $testResultMarkdown -replace "%TestResult%", $result + + $testResultMarkdown = $testResultMarkdown -replace "%TestResult%", $resultMarkdown Add-MtTestResultDetail -Result $testResultMarkdown -Severity $Severity - $result = [string]::IsNullOrEmpty($Devices) - return $result + return [string]::IsNullOrEmpty($Devices) } From 19eea38c1df4a49c099e51b2d0236b067a0d0e32 Mon Sep 17 00:00:00 2001 From: Eduard Arbona <175124143+earbona23@users.noreply.github.com> Date: Sat, 7 Nov 2026 17:07:03 -0400 Subject: [PATCH 2/3] chore: save the new PowerShell files as UTF-8 with BOM Per the ruling on #2663: every .ps1, .psm1 and .psd1 is UTF-8 with a byte order mark. These files were written without one, so they would fail the check #2714 adds once it lands. Only the three bytes EF BB BF are prepended; nothing else in the files changes. --- ...MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 b/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 index 06128bc0bd..6ed97c11f3 100644 --- a/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 +++ b/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 @@ -1,4 +1,4 @@ -Describe 'MT.1087 public exposed devices with a remotely exploitable CVE' { +Describe 'MT.1087 public exposed devices with a remotely exploitable CVE' { BeforeAll { Import-Module $PSScriptRoot/../../Maester.psd1 -Force . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" From 979a7f4bf8c0bbc25305371dfb1979e32842b5fd Mon Sep 17 00:00:00 2001 From: Eduard Arbona <175124143+earbona23@users.noreply.github.com> Date: Sat, 7 Nov 2026 17:23:28 -0400 Subject: [PATCH 3/3] fix(MT.1087): read the denominator from the inventory, not from found vulnerabilities The probe that decides whether an empty result is a pass or a skip counted distinct devices in DeviceTvmSoftwareVulnerabilities. That table holds vulnerabilities that were FOUND, so an onboarded fleet with nothing vulnerable returns zero there and the check skipped as NotApplicable. That is the wrong way round, and worse than the bug this pull request set out to fix: it traded a false pass for a false skip on the one tenant that most deserves to pass. The source is now DeviceTvmSoftwareInventory. A device in the inventory is a device Defender Vulnerability Management assessed, whether or not it turned up anything. Also drops the unused $Severity from the mock's param block, which is code scanning alert 3149. The new case mocks the semantics of each table rather than the summarize clause, because the main exposure query names DeviceTvmSoftwareVulnerabilities three times: the denominator query is identified by its summarize clause first, and only then does the table decide the answer. 6 of 6 with the fix; with Test.MT.1087.ps1 reverted it is the only case that fails. My first attempt at that mock matched on the table alone and failed for the wrong reason, which is exactly the shape of mistake the case exists to catch. --- ...lyExploitableHighExposureDevices.Tests.ps1 | 25 ++++++++++++++++++- tests/maester/xspm/Test.MT.1087.ps1 | 10 ++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 b/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 index 6ed97c11f3..9642f4767f 100644 --- a/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 +++ b/powershell/tests/functions/Test-MtXspmPublicRemotelyExploitableHighExposureDevices.Tests.ps1 @@ -12,7 +12,7 @@ Mock -ModuleName Maester Get-MtLicenseInformation { return $true } Mock -ModuleName Maester Add-MtTestResultDetail { - param($Result, $SkippedBecause, $Severity) + param($Result, $SkippedBecause) $script:TestResult = $Result $script:SkippedBecause = $SkippedBecause @@ -34,6 +34,29 @@ $script:SkippedBecause | Should -Be 'NotApplicable' } + It 'passes a clean fleet, because the denominator comes from the inventory' { + # The mock honours the semantics of each table instead of matching on the summarize clause: + # a fleet that is assessed but has nothing vulnerable has rows in the inventory and none in + # DeviceTvmSoftwareVulnerabilities. Reading the denominator from the vulnerabilities table + # returned zero here and skipped the tenant that most deserves to pass, so this case fails + # against the previous query. + Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { + # The denominator query is identified by its summarize clause first, because the main + # exposure query also names DeviceTvmSoftwareVulnerabilities. Only then does the table + # decide the answer: inventory has the assessed fleet, the vulnerabilities table has + # nothing because nothing is vulnerable. + if ($Query -match 'summarize AssessedDeviceCount') { + if ($Query -match 'DeviceTvmSoftwareInventory') { return @([pscustomobject]@{ AssessedDeviceCount = 120 }) } + return @([pscustomobject]@{ AssessedDeviceCount = 0 }) + } + return @() + } + + Test-MtXspmPublicRemotelyExploitableHighExposureDevices | Should -BeTrue + $script:TestResult | Should -Match 'Well done' + $script:SkippedBecause | Should -BeNullOrEmpty + } + It 'passes when the fleet is assessed and no device is exposed' { Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { if ($Query -match 'summarize AssessedDeviceCount') { diff --git a/tests/maester/xspm/Test.MT.1087.ps1 b/tests/maester/xspm/Test.MT.1087.ps1 index 699bdea484..82c3ea8286 100644 --- a/tests/maester/xspm/Test.MT.1087.ps1 +++ b/tests/maester/xspm/Test.MT.1087.ps1 @@ -131,11 +131,17 @@ if ([string]::IsNullOrEmpty($Devices)) { # Invoke-MtGraphSecurityQuery returns nothing both when no device matches and when the query # could not run at all, so an empty result on its own cannot be read as a clean fleet. This - # asks whether Defender has any vulnerability data for this tenant before calling it a pass. + # asks whether Defender assessed anything in this tenant before calling it a pass. # A summarize with no by clause returns exactly one row even over no input, so the count # comes back either way. + # + # The source is the software INVENTORY, not DeviceTvmSoftwareVulnerabilities. That table holds + # vulnerabilities that were found, so an onboarded fleet with nothing vulnerable returns zero + # there and the check below would skip the one tenant that most deserves to pass. A device in + # the inventory is a device Defender Vulnerability Management assessed, whether or not it + # turned up anything. $assessed = Invoke-MtGraphSecurityQuery -Timespan "P1D" -Query @' -DeviceTvmSoftwareVulnerabilities +DeviceTvmSoftwareInventory | summarize AssessedDeviceCount = dcount(DeviceId) '@ $assessedCount = [int](($assessed | Select-Object -First 1).AssessedDeviceCount)