Skip to content

URL-encode client_id in Azure IMDS token request - #2787

Merged
Jibola merged 2 commits into
mongodb:masterfrom
ikow:fix/url-encode-azure-imds-client-id
May 13, 2026
Merged

Jibola merged 2 commits into
mongodb:masterfrom
ikow:fix/url-encode-azure-imds-client-id

Conversation

@ikow

@ikow ikow commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

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

@codecov-commenter

codecov-commenter commented May 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@Jibola Jibola left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Thank you for the contribution!

@Jibola
Jibola merged commit a505505 into mongodb:master May 13, 2026
103 of 104 checks passed
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.

4 participants