Skip to content

auth: trim trailing slash from LoginEndpoint in tokenEndpoint - #1330

Open
randomnote1 wants to merge 1 commit into
hashicorp:mainfrom
randomnote1:fix/trim-trailing-slash-token-endpoint
Open

auth: trim trailing slash from LoginEndpoint in tokenEndpoint#1330
randomnote1 wants to merge 1 commit into
hashicorp:mainfrom
randomnote1:fix/trim-trailing-slash-token-endpoint

Conversation

@randomnote1

Copy link
Copy Markdown

Community Note

  • Please vote on this PR by adding a 👍 reaction to the original PR to help the community and maintainers prioritize for review
  • Please do not leave comments along the lines of "+1", "me too" or "any updates", they generate extra noise for PR followers and do not help prioritize for review

Description

When custom cloud (AGC) metadata discovery returns a login endpoint with a trailing slash (e.g., https://login.microsoftonline.<suffix>/), the tokenEndpoint function in sdk/auth/client_credentials.go constructs a token URL with a double slash before the tenant ID:

https://login.microsoftonline.<suffix>//<tenant-id>/oauth2/v2.0/token

This causes client certificate authentication to fail because the signed JWT assertion uses the malformed URL as the aud claim, which does not match the canonical realm issuer. The token service rejects the request with:

AADSTS700023: Client assertion audience claim does not match Realm issuer

Client secret authentication is unaffected because it does not use the token URL as an audience claim.

Fix

Apply strings.TrimRight to normalize endpoint.LoginEndpoint before constructing the token URL:

return fmt.Sprintf("%s/%s/oauth2/v2.0/token", strings.TrimRight(endpoint.LoginEndpoint, "/"), tenant)

This is a single-line change. The strings package was already imported in the file.

Tests

Added two integration tests in sdk/auth/token_endpoint_test.go that exercise the full authorizer flow with a trailing-slash login endpoint:

  • TestClientCertificateAuthorizer_TrailingSlashLoginEndpoint
  • TestClientSecretAuthorizer_TrailingSlashLoginEndpoint

Both tests use the existing AzureADAccessTokenMockClient, whose path regex inherently validates that the constructed URL does not contain a double slash.

This is a (please select all that apply):

  • Bug Fix
  • New Feature
  • Enhancement
  • Breaking Change

Related Issue(s)

Fixes #1329

Rollback Plan

If a change needs to be reverted, we will publish an updated version of the provider.

Changes to Security Controls

No changes to security controls. This change normalizes URL path construction only.

Note

If this PR changes meaningfully during the course of review please update the title and description as required.

When custom cloud metadata discovery returns a login endpoint with a
trailing slash (e.g. https://login.microsoftonline.<suffix>/), the
tokenEndpoint function constructs a URL with a double slash before the
tenant ID. This causes client certificate authentication to fail because
the signed JWT assertion uses the malformed URL as the aud claim, which
does not match the canonical realm issuer.

Apply strings.TrimRight to normalize the LoginEndpoint before building
the token URL. Add comprehensive unit tests covering trailing slash,
multiple trailing slashes, custom cloud endpoints, and the default
tenant fallback.

Fixes hashicorp#1329

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@randomnote1
randomnote1 requested a review from a team as a code owner May 14, 2026 23:25
@github-actions github-actions Bot added the release-once-merged The SDK should be released once this PR is merged label May 14, 2026
@hashicorp-cla-app

hashicorp-cla-app Bot commented May 14, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@randomnote1 randomnote1 changed the title auth: trim trailing slash from LoginEndpoint in tokenEndpoint auth: trim trailing slash from LoginEndpoint in tokenEndpoint May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-once-merged The SDK should be released once this PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue with Client Certificate Auth / API Version oauth2 v2.0

1 participant