Skip to content

URL-encode client_id in Azure IMDS token request#2787

Open
ikow wants to merge 1 commit intomongodb:masterfrom
ikow:fix/url-encode-azure-imds-client-id
Open

URL-encode client_id in Azure IMDS token request#2787
ikow wants to merge 1 commit intomongodb:masterfrom
ikow:fix/url-encode-azure-imds-client-id

Conversation

@ikow
Copy link
Copy Markdown

@ikow ikow commented Apr 29, 2026

Summary

Apply urllib.parse.quote() to the client_id parameter in _get_azure_response() before interpolating it into the Azure IMDS URL, consistent with the existing treatment of resource (which is already encoded via quote() in _OIDCAzureCallback.__init__).

Motivation

The resource parameter is URL-encoded at the call site (auth_oidc_shared.py:103), but client_id is interpolated raw via f-string (_azure_helpers.py:32). This inconsistency means a client_id containing URL-special characters (&, =, #, etc.) could unintentionally alter the query string structure.

For comparison, the Node.js driver uses the safe url.searchParams.append('client_id', username) API for the same operation (src/client-side-encryption/providers/azure.ts:127), which automatically handles encoding.

Changes

  • pymongo/_azure_helpers.py: Apply quote() to client_id before URL interpolation
  • test/test_azure_helpers.py: Add test_client_id_is_url_encoded to verify special characters are properly percent-encoded

Test

All 13 tests in test/test_azure_helpers.py pass, including the new encoding test.

The `_get_azure_response()` function constructs the Azure IMDS URL by
interpolating `client_id` via f-string without URL encoding. While
`resource` is already encoded (via `quote()` at the call site in
`auth_oidc_shared.py`), `client_id` is not, creating an inconsistency.

Apply `urllib.parse.quote()` to `client_id` before interpolation,
consistent with the handling of `resource` and with the Node.js driver's
use of `url.searchParams.append()` for the same parameter.

Add a test to verify special characters in `client_id` are properly
percent-encoded and cannot introduce additional query parameters.
@ikow ikow requested a review from a team as a code owner April 29, 2026 20:58
@ikow ikow requested a review from Jibola April 29, 2026 20:58
@evergreen-ci-prod
Copy link
Copy Markdown

There is an existing patch(es) for this commit SHA:

Please note that the status that is posted is not in the context of this PR but rather the (latest) existing patch and that may affect some tests that may depend on the particular PR. If your tests do not rely on any PR-specific values (like base or head branch name) then your tests will report the same status. If you would like a patch to run in the context of this PR and abort the other(s), comment 'evergreen retry'.

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.

2 participants