Skip to content

Fix: ExchangeOnline provider InvokeSafely fails with variable-not-set error on bearer token sanitization#225

Merged
blindzero merged 5 commits intomainfrom
copilot/fix-bearertoken-authentication-error
Feb 23, 2026
Merged

Fix: ExchangeOnline provider InvokeSafely fails with variable-not-set error on bearer token sanitization#225
blindzero merged 5 commits intomainfrom
copilot/fix-bearertoken-authentication-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 23, 2026

  • Identify root cause: $bearerTokenPattern and $tokenAssignmentPattern are defined in the outer function scope but not captured in the $invokeSafely ScriptMethod closure
  • Fix: Move $bearerTokenPattern and $tokenAssignmentPattern inside the $invokeSafely scriptblock
  • Add regression test for the InvokeSafely closure behavior
  • Address review feedback: replace function global:Invoke-ThrowBearerError with Invoke-IdleTestBearerTokenError in _testHelpers.Providers.ps1; dot-source helpers in the context BeforeAll to ensure availability at run time
  • All 29 tests pass
  • Code review clean
Original prompt

This section details on the original issue you should resolve

<issue_title>Provider.ExchangeOnline cant authenticate to bearerToken error</issue_title>
<issue_description>## Description

Whenever involving any ExchangeOnline provider capability, I get the error

Exception calling "EnsureMailboxType" with "3" argument(s): "Exception calling "GetMailbox" with "2" argument(s): "Exception
               calling "GetMailbox" with "2" argument(s): "Exception calling "InvokeSafely" with "2" argument(s): "The variable
               '$bearerTokenPattern' cannot be retrieved because it has not been set."""" 

Steps to Reproduce

  1. Create any ExchangeOnline Workflow step
  2. Create AuthSession with Scopes
$scopes = @(
  'User.ReadWrite.All'
  'Group.Read.All'
  'GroupMember.ReadWrite.All'
  'MailboxSettings.ReadWrite'
)

and

$entraToken = Get-MsalToken -TenantId $tenantId -ClientId $clientId -Scopes $scopes -DeviceCode`
$entraAccessToken = $entraToken.AccessToken

and using this accesstoken for Idle-AuthSessionBroker setup.

Expected Behavior

Authentication with token provided.

Actual Behavior

It brings the error.

Environment

  • PowerShell version: 7.5.4
  • OS: Windows Server 2019
  • IdLE version / commit: main

Additional Context

  • Same procedure works with Entra Provider, so assuming something provider specific. Maybe to adopt the authentication methods from AD Provider?
  • I was using the same $entraAccessToken variable for both provider AuthSessions:
$authSessions = New-IdleAuthSession -SessionMap @{
    @{ AuthSessionName = 'AD' }  = @{ AuthSessionType = 'Credential'; Credential = $adCred }
    @{ AuthSessionName = 'Entra' } = @{ AuthSessionType = 'OAuth'; Credential = $entraAccessToken }
    @{ AuthSessionName = 'EXO' } = @{ AuthSessionType = 'OAuth'; Credential = $entraAccessToken }
}
$providerAD = New-IdleADIdentityProvider
$providerEntra = New-IdleEntraIDIdentityProvider
$providerEXO = New-IdleExchangeOnlineProvider
$providers = @{
    AD = $providerAD
    Entra = $providerEntra
    EXO = $providerEXO
    AuthSessionBroker = $authSessions
}

But also duplicating the tokens to a separate $entraAccessToken and $exoAccessToken does not change the behavior.
</issue_description>

<agent_instructions>please check this issue and try to reproduce / find a possible cause for it.
Do not implement a direct change yet, consult first and explain the root cause and possible fix.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…k to fix variable-not-set error

Co-authored-by: blindzero <13959569+blindzero@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ExchangeOnline provider authentication to bearerToken error Fix: ExchangeOnline provider InvokeSafely fails with variable-not-set error on bearer token sanitization Feb 23, 2026
Copilot AI requested a review from blindzero February 23, 2026 16:20
@blindzero blindzero marked this pull request as ready for review February 23, 2026 16:48
Copilot AI review requested due to automatic review settings February 23, 2026 16:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a scoping bug in the Exchange Online provider’s internal adapter where InvokeSafely could throw a variable-not-set error when called via $this.InvokeSafely() from another ScriptMethod, and adds a regression test to prevent recurrence.

Changes:

  • Move bearer/token sanitization regex patterns into the InvokeSafely ScriptMethod body to ensure they’re always in scope at call time.
  • Add Pester regression coverage ensuring InvokeSafely works when invoked via $this and that bearer tokens are redacted in thrown error messages.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/IdLE.Provider.ExchangeOnline/Private/New-IdleExchangeOnlineAdapter.ps1 Fixes InvokeSafely scoping by defining sanitization regex patterns inside the ScriptMethod body.
tests/Providers/ExchangeOnlineProvider.Tests.ps1 Adds a regression test context covering ScriptMethod-to-ScriptMethod invocation and bearer token redaction behavior.

Comment thread tests/Providers/ExchangeOnlineProvider.Tests.ps1 Outdated
Comment thread tests/Providers/ExchangeOnlineProvider.Tests.ps1
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Comment thread tests/Providers/ExchangeOnlineProvider.Tests.ps1 Outdated
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 23, 2026

Code Coverage Report

Overall Project 73.65% 🍏

There is no coverage information present for the Files changed

Copilot AI and others added 2 commits February 23, 2026 17:00
@blindzero blindzero merged commit 94a4079 into main Feb 23, 2026
5 checks passed
@blindzero blindzero deleted the copilot/fix-bearertoken-authentication-error branch February 27, 2026 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provider.ExchangeOnline cant authenticate to bearerToken error

3 participants