Revert "fix(auth): parse hostname for mTLS and PSC endpoint certifica… - #18189
Conversation
…te rotat… (googleapis#18153)" This reverts commit b642373.
|
Just for additional context - we wanted to await additional approvals before merging the PR being reverted here |
There was a problem hiding this comment.
Code Review
This pull request removes the is_mtls_endpoint helper function and replaces it with inline substring checks for mTLS URL prefixes in both the requests and urllib3 transport modules. The review feedback highlights a potential TypeError if the url parameter is a bytes or urllib3.util.Url object rather than a string, recommending robust type-checking and decoding. Additionally, it is advised to avoid list comprehensions inside any() in the urllib3 transport to preserve short-circuiting behavior.
| MTLS_URL_PREFIXES = [ | ||
| "mtls.googleapis.com", | ||
| "mtls.sandbox.googleapis.com", | ||
| ] | ||
| use_mtls = self.is_mtls and any( | ||
| prefix in url for prefix in MTLS_URL_PREFIXES | ||
| ) |
There was a problem hiding this comment.
Using a simple substring check (prefix in url) on the raw url parameter can raise a TypeError if url is a bytes object or a urllib3.util.Url object. To ensure robust type handling, check for a .url attribute, safely decode bytes to UTF-8, and fall back to string conversion.
MTLS_URL_PREFIXES = [
"mtls.googleapis.com",
"mtls.sandbox.googleapis.com",
]
url_str = url.url if hasattr(url, "url") else (url.decode("utf-8") if isinstance(url, bytes) else str(url))
use_mtls = self.is_mtls and any(
prefix in url_str for prefix in MTLS_URL_PREFIXES
)References
- When parsing or validating URLs that may be passed as urllib3.util.Url objects, bytes, or strings, ensure robust type handling by checking for a .url attribute, safely decoding bytes to UTF-8, and falling back to string conversion to prevent TypeErrors during parsing.
| use_mtls = False | ||
| if self._is_mtls: | ||
| MTLS_URL_PREFIXES = ["mtls.googleapis.com", "mtls.sandbox.googleapis.com"] | ||
| use_mtls = any([prefix in url for prefix in MTLS_URL_PREFIXES]) |
There was a problem hiding this comment.
Using a list comprehension inside any() defeats short-circuiting. Additionally, url can be a urllib3.util.Url object, bytes, or str. To prevent TypeError, check for a .url attribute, safely decode bytes to UTF-8, and fall back to string conversion.
| use_mtls = False | |
| if self._is_mtls: | |
| MTLS_URL_PREFIXES = ["mtls.googleapis.com", "mtls.sandbox.googleapis.com"] | |
| use_mtls = any([prefix in url for prefix in MTLS_URL_PREFIXES]) | |
| use_mtls = False | |
| if self._is_mtls: | |
| MTLS_URL_PREFIXES = ["mtls.googleapis.com", "mtls.sandbox.googleapis.com"] | |
| url_str = url.url if hasattr(url, "url") else (url.decode("utf-8") if isinstance(url, bytes) else str(url)) | |
| use_mtls = any(prefix in url_str for prefix in MTLS_URL_PREFIXES) |
References
- When parsing or validating URLs that may be passed as urllib3.util.Url objects, bytes, or strings, ensure robust type handling by checking for a .url attribute, safely decoding bytes to UTF-8, and falling back to string conversion to prevent TypeErrors during parsing.
(#18153)
This reverts commit b642373.