Skip to content

Add Header parameter Invoke-ZtGraphRequest#894

Merged
merill merged 6 commits intomainfrom
header-parameter-Invoke-ZtGraphRequest
Mar 2, 2026
Merged

Add Header parameter Invoke-ZtGraphRequest#894
merill merged 6 commits intomainfrom
header-parameter-Invoke-ZtGraphRequest

Conversation

@komalp2025
Copy link
Copy Markdown
Collaborator

@komalp2025 komalp2025 commented Feb 13, 2026

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

@komalp2025 komalp2025 changed the title Header parameter invoke zt graph request Add Header parameter Invoke-ZtGraphRequest Feb 13, 2026
@komalp2025 komalp2025 added the ready for review PR is ready for review and merging label Feb 13, 2026
Copy link
Copy Markdown
Collaborator

@merill merill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

@komalp2025
Copy link
Copy Markdown
Collaborator Author

@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               eventual
PS 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

@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

Copy link
Copy Markdown
Collaborator

@alexandair alexandair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Collaborator

@merill merill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@merill merill merged commit c91b017 into main Mar 2, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready for review PR is ready for review and merging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants