Repository navigation
fix(xspm): MT.1087 probes advanced hunting before querying it #2819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
earbona23
wants to merge
3
commits into
maester365:main
Choose a base branch
from
earbona23:fix/mt1087-advanced-hunting-probe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
powershell/internal/services/defender/Test-MtDefenderAdvancedHuntingAvailable.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| function Test-MtDefenderAdvancedHuntingAvailable { | ||
| <# | ||
| .SYNOPSIS | ||
| Returns whether the Defender for Endpoint device tables answer advanced hunting on this tenant. | ||
|
|
||
| .DESCRIPTION | ||
| The Defender Vulnerability Management checks read DeviceTvm* tables through advanced hunting. On a | ||
| tenant where those tables are not served, the query comes back as an execution error and the check | ||
| ends in Error with the API's own text, which on some tenants is reduced to | ||
| ". Fix semantic errors in your query." and does not even name the table. That reads as a defect in | ||
| the check rather than as a tenant that cannot answer the question. | ||
|
|
||
| A licence gate cannot tell the two apart. A tenant holding both Defender for Business (from Business | ||
| Premium) and Defender for Endpoint Plan 2 (for instance from an E5 trial) keeps the Defender for | ||
| Business experience until Support switches it, and advanced hunting belongs to Plan 2 only. Both | ||
| DEFENDER_XDR and a gate on Plan 2 pass on such a tenant while the tables stay silent. | ||
|
|
||
| So this probes instead of inferring. The query is a constant and minimal one over DeviceInfo, the | ||
| table every Defender for Endpoint device reports into. It cannot fail because of anything a caller | ||
| composed, which is what makes its failure attributable to the tenant rather than to the query. | ||
|
|
||
| An empty result is availability, not absence: a table that resolves and returns no row has answered. | ||
| The distinction matters, because a tenant with no onboarded device must still be skipped by the | ||
| caller's own no-data path, with its own reason. | ||
|
|
||
| The answer is a property of the tenant, not of the caller, so it is read once per run and kept in | ||
| $__MtSession.DefenderHuntingTables. Three checks depend on it and advanced hunting is rate limited; | ||
| asking the same question three times a run is a cost paid by every tenant, including the ones where | ||
| nothing is wrong. Invoke-Maester clears it between runs. | ||
|
|
||
| .OUTPUTS | ||
| [bool] - True when the Defender for Endpoint tables answer, false when they do not. | ||
|
|
||
| .EXAMPLE | ||
| Test-MtDefenderAdvancedHuntingAvailable | ||
|
|
||
| Returns $true on a tenant whose Defender for Endpoint tables are served by advanced hunting. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([bool])] | ||
| param() | ||
|
|
||
| if ($null -ne $__MtSession.DefenderHuntingTables) { | ||
| return [bool]$__MtSession.DefenderHuntingTables | ||
| } | ||
|
|
||
| try { | ||
| $null = Invoke-MtGraphSecurityQuery -Query 'DeviceInfo | project DeviceId | take 1' -Timespan 'P1D' | ||
| $__MtSession.DefenderHuntingTables = $true | ||
| return $true | ||
| } catch { | ||
| # Deliberately broad. Invoke-MtGraphSecurityQuery surfaces the API's message, and that message is | ||
| # not reliable enough to match on: the same unavailability reaches us as | ||
| # "Failed to resolve table or column expression named ..." for some tables and as | ||
| # ". Fix semantic errors in your query." for others. Matching the text would make the probe fail | ||
| # open on the tenants it exists for. The query being a constant is what bounds this catch. | ||
| Write-Verbose "Defender for Endpoint tables did not answer advanced hunting: $($_.Exception.Message)" | ||
| $__MtSession.DefenderHuntingTables = $false | ||
| return $false | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
powershell/tests/functions/Test-MtDefenderAdvancedHuntingAvailable.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| Describe 'Test-MtDefenderAdvancedHuntingAvailable' { | ||
| BeforeAll { | ||
| Import-Module $PSScriptRoot/../../Maester.psd1 -Force | ||
| . "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1" | ||
| # An internal helper, so it is not exported. | ||
| Use-MtModuleFunction -Name Test-MtDefenderAdvancedHuntingAvailable | ||
| } | ||
|
|
||
| BeforeEach { | ||
| # The answer is cached for the run. Without this each case would read the previous one's result | ||
| # and the probe would appear to work no matter what it was given. | ||
| InModuleScope Maester { $__MtSession.DefenderHuntingTables = $null } | ||
| } | ||
|
|
||
| It 'reports available when the probe query answers' { | ||
| Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { @([pscustomobject]@{ DeviceId = 'd1' }) } | ||
|
|
||
| Test-MtDefenderAdvancedHuntingAvailable | Should -BeTrue | ||
| } | ||
|
|
||
| It 'reports available when the table answers with no row' { | ||
| # A table that resolves and returns nothing has answered. Reading an empty result as | ||
| # unavailable would skip every tenant with no onboarded device for the wrong reason, and those | ||
| # tenants have their own skip with its own wording. | ||
| Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { @() } | ||
|
|
||
| Test-MtDefenderAdvancedHuntingAvailable | Should -BeTrue | ||
| } | ||
|
|
||
| It 'reports unavailable when the probe query throws' { | ||
| Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { throw '. Fix semantic errors in your query.' } | ||
|
|
||
| Test-MtDefenderAdvancedHuntingAvailable | Should -BeFalse | ||
| } | ||
|
|
||
| It 'reports unavailable on the message that names the table too' { | ||
| # The same unavailability reaches the caller as either message depending on the table, which is | ||
| # why the catch does not match on the text. | ||
| Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { throw "Failed to resolve table or column expression named 'DeviceInfo'" } | ||
|
|
||
| Test-MtDefenderAdvancedHuntingAvailable | Should -BeFalse | ||
| } | ||
|
|
||
| It 'asks only for what it needs, and only once per run' { | ||
| # Three checks depend on this and advanced hunting is rate limited, so the question is asked | ||
| # once and kept for the run. A probe that pulled rows, or asked again per check, would be a cost | ||
| # on every tenant, including the ones where nothing is wrong. | ||
| Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { @() } | ||
|
|
||
| $null = Test-MtDefenderAdvancedHuntingAvailable | ||
| $null = Test-MtDefenderAdvancedHuntingAvailable | ||
| $null = Test-MtDefenderAdvancedHuntingAvailable | ||
|
|
||
| Should -Invoke -ModuleName Maester Invoke-MtGraphSecurityQuery -Times 1 -Exactly | ||
| Should -Invoke -ModuleName Maester Invoke-MtGraphSecurityQuery -ParameterFilter { | ||
| $Query -eq 'DeviceInfo | project DeviceId | take 1' | ||
| } -Times 1 -Exactly | ||
| } | ||
|
|
||
| It 'caches the unavailable answer too, rather than retrying per check' { | ||
| # The expensive case is the one that fails: without this the tenant pays a failing query for | ||
| # every check that depends on the probe. | ||
| Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { throw 'Query execution has failed.' } | ||
|
|
||
| Test-MtDefenderAdvancedHuntingAvailable | Should -BeFalse | ||
| Test-MtDefenderAdvancedHuntingAvailable | Should -BeFalse | ||
|
|
||
| Should -Invoke -ModuleName Maester Invoke-MtGraphSecurityQuery -Times 1 -Exactly | ||
| } | ||
|
|
||
| It 'asks again once the run cache is cleared' { | ||
| # Invoke-Maester clears the per-run cache, so a second run on a tenant whose licensing changed | ||
| # must not be answered from the first. | ||
| Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { @() } | ||
|
|
||
| $null = Test-MtDefenderAdvancedHuntingAvailable | ||
| InModuleScope Maester { $__MtSession.DefenderHuntingTables = $null } | ||
| $null = Test-MtDefenderAdvancedHuntingAvailable | ||
|
|
||
| Should -Invoke -ModuleName Maester Invoke-MtGraphSecurityQuery -Times 2 -Exactly | ||
| } | ||
|
|
||
| It 'does not swallow the failure silently' { | ||
| Mock -ModuleName Maester Invoke-MtGraphSecurityQuery { throw 'boom' } | ||
|
|
||
| $verbose = Test-MtDefenderAdvancedHuntingAvailable -Verbose 4>&1 | | ||
| Where-Object { $_ -is [System.Management.Automation.VerboseRecord] } | ||
|
|
||
| ($verbose -join ' ') | Should -Match 'did not answer advanced hunting' | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throw when HTTP 429 retries are exhausted so the probe does not report availability.
After three HTTP 429 responses,
Invoke-MtGraphSecurityQueryexits its retry loop without throwing and returns no rows.Test-MtDefenderAdvancedHuntingAvailablethen reachesreturn $trueon lines 39-40, so a throttled probe reports the tables as available. MT.1087 then runs its main query instead of taking the capability skip path. A successful empty response also returns no rows, so a$nullcheck cannot tell the two cases apart. Throw inInvoke-MtGraphSecurityQuerywhen the retry budget is exhausted. This changes behavior for all callers: sustained throttling now raises an error instead of returning no rows.Suggested fix for powershell/public/services/graph/Invoke-MtGraphSecurityQuery.ps1