File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1717
1818import json
1919from typing import Any , Optional
20+ from urllib .parse import quote
2021
2122
2223def _get_azure_response (
@@ -29,7 +30,7 @@ def _get_azure_response(
2930 url += "?api-version=2018-02-01"
3031 url += f"&resource={ resource } "
3132 if client_id :
32- url += f"&client_id={ client_id } "
33+ url += f"&client_id={ quote ( client_id ) } "
3334 headers = {"Metadata" : "true" , "Accept" : "application/json" }
3435 request = Request (url , headers = headers ) # noqa: S310
3536 try :
Original file line number Diff line number Diff line change @@ -150,6 +150,20 @@ def test_timeout_passed_to_urlopen(self):
150150 _ , kwargs = mock_open .call_args
151151 self .assertEqual (kwargs ["timeout" ], 42 )
152152
153+ def test_client_id_is_url_encoded (self ):
154+ """Ensure special characters in client_id are percent-encoded."""
155+ body = json .dumps ({"access_token" : "tok" , "expires_in" : "3600" })
156+ with _mock_urlopen (200 , body ) as mock_open :
157+ self ._call (client_id = "id with spaces&special=chars" )
158+
159+ url = mock_open .call_args [0 ][0 ].full_url
160+ # '&' and '=' must be percent-encoded so they don't inject extra query params
161+ self .assertIn ("client_id=id%20with%20spaces%26special%3Dchars" , url )
162+ # The encoded client_id should not introduce a raw '&'
163+ # Count params: api-version, resource, client_id — exactly 3
164+ query_string = url .split ("?" , 1 )[1 ]
165+ self .assertEqual (query_string .count ("&" ), 2 )
166+
153167
154168if __name__ == "__main__" :
155169 unittest .main ()
You can’t perform that action at this time.
0 commit comments