Add Header parameter Invoke-ZtGraphRequest#894
Conversation
merill
left a comment
There was a problem hiding this comment.
@komalp2025 In this new logic, if a Header is passed it is not including the ConsistencyLevel in the header collection. We need to include it based on the value provided in the ConsistencyLevel parameter
Hi @merill , this is already taken care of. Also if user has provided consistency level in header, it will also get reset to the value provided in consistency parameter. Please see below code snippet PS C:\GitRepo\zerotrustassessment> $requestHeaders = if ($Headers) { $Headers.Clone() } else { @{} }
PS C:\GitRepo\zerotrustassessment> $requestHeaders
Name Value
---- -----
Content-Type application/json
PS C:\GitRepo\zerotrustassessment> $ConsistencyLevel = 'eventual'
PS C:\GitRepo\zerotrustassessment> $requestHeaders['ConsistencyLevel'] = $ConsistencyLevel
PS C:\GitRepo\zerotrustassessment> $requestHeaders
Name Value
---- -----
Content-Type application/json
ConsistencyLevel eventualPS C:\GitRepo\zerotrustassessment> $headers = @{"Content-Type" = "application/json"
>> "ConsistencyLevel" = 'testvalue'}
PS C:\GitRepo\zerotrustassessment> $requestHeaders = if ($Headers) { $Headers.Clone() } else { @{} }
PS C:\GitRepo\zerotrustassessment> $requestHeaders
Name Value
---- -----
Content-Type application/json
ConsistencyLevel testvalue
PS C:\GitRepo\zerotrustassessment> $requestHeaders['ConsistencyLevel'] = $ConsistencyLevel
PS C:\GitRepo\zerotrustassessment> $requestHeaders
Name Value
---- -----
ConsistencyLevel eventual
Content-Type application/json
|
alexandair
left a comment
There was a problem hiding this comment.
@komalp2025 I've made some changes to the PR:
Pre-existing bug: $GraphBaseUri parameter is always ignored
if (-not $GraphBaseUri) {
if (-not $script:__ZtSession.GraphBaseUri) {
$script:__ZtSession.GraphBaseUri = (Get-MgEnvironment -Name ...).GraphEndpoint
}
}
$GraphBaseUri = $script:__ZtSession.GraphBaseUri # ← ALWAYS overwrites!Even if a caller passes -GraphBaseUri https://custom.endpoint, the very last line unconditionally overwrites it with the session value. The outer if correctly skips the initialization, but the reassignment destroys the caller's value. This has been broken since before this PR.
Fix: The last line should be else-guarded or inside the outer if:
if (-not $GraphBaseUri) {
...
$GraphBaseUri = $script:__ZtSession.GraphBaseUri
}The cache key in Invoke-ZtGraphRequestCache.ps1 is just $Uri.AbsoluteUri.
Before this PR, the only header was ConsistencyLevel = eventual (a constant), so this was harmless. Now that callers can pass arbitrary headers, two requests to the same URL with different Prefer or other headers will share the same cache entry and return incorrect data.
Example scenario:
# Call 1 — with Prefer header, result gets cached
Invoke-ZtGraphRequest -RelativeUri "users" -Headers @{ Prefer = 'outlook.body-content-type="text"' }
# Call 2 — no Prefer header, silently returns Call 1's cached result
Invoke-ZtGraphRequest -RelativeUri "users"Fix: Incorporate serialized headers into the cache key.
Adding Header parameter to provide extra headers in Invoke-ZTGraphRequest. Earlier it was hardcoded to Consistency Level only.
PR results are updated at https://github.com/microsoft/ztspecs/issues/314