Skip to content

fix(auth): parse hostname for mTLS and PSC endpoint certificate rotation (#18147) - #18201

Open
attharva-24 wants to merge 6 commits into
googleapis:mainfrom
attharva-24:fix-mtls-url-check
Open

fix(auth): parse hostname for mTLS and PSC endpoint certificate rotation (#18147)#18201
attharva-24 wants to merge 6 commits into
googleapis:mainfrom
attharva-24:fix-mtls-url-check

Conversation

@attharva-24

@attharva-24 attharva-24 commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Relands #18153 with all review feedback addressed, full test coverage, and live manual verification against Google Cloud mTLS endpoints.

Fixes #18147

Changes

  • Replaced naive any(prefix in url for prefix in MTLS_URL_PREFIXES) substring matching with _mtls_helper.is_mtls_endpoint(url).
  • Parsed hostname using urllib.parse.urlsplit to avoid false positives on URL path/query parameters.
  • Added support for Private Service Connect (PSC) custom domains (*.p.googleapis.com, p.googleapis.com).
  • Normalized hostnames by stripping trailing root dots (.rstrip(".")) for FQDNs.
  • Annotated is_mtls_endpoint with object instead of Any for strict static type checking.
  • Added comprehensive parametrized unit tests (57 cases in test__mtls_helper.py).

Manual Verification & Findings

  1. Non-mTLS URLs (storage.googleapis.com): On receiving 401 Unauthorized, AuthorizedSession executes the standard OAuth token refresh & retry (2 HTTP requests: 401 ➔ 200 OK), and completely skips certificate rotation (spy_check Call Count: 0).
  2. PSC / mTLS URLs (storage.p.googleapis.com): On receiving 401 Unauthorized, AuthorizedSession triggers certificate rotation inspection (spy_check Call Count: 1) before refreshing credentials and retrying with 200 OK.
Click to expand Live Manual Test Output (100% Green)
========================================================================
 Real Production mTLS Certificate & Endpoint Live Manual Verification   
========================================================================

===========================================================================
>>> 1. INSPECTING ACTUAL CLIENT CERTIFICATE ON MACHINE
===========================================================================
  Client Certificate Status:  LOADED FROM DISK / ECP
  Certificate Size:            1428 bytes
  Private Key Size:            1675 bytes
  Certificate Subject:         <Name(CN=www.example.com,OU=Google Testing unit,O=Google Testing)>
  Certificate Issuer:          <Name(CN=Google Testing Intermediate CA,OU=Google Testing unit,O=Google Testing)>
  SHA-256 Fingerprint:         <SHA256_CERT_FINGERPRINT_REDACTED>

===========================================================================
>>> 2. ESTABLISHING REAL LIVE mTLS HANDSHAKE (pubsub.mtls.googleapis.com)
===========================================================================
  Session mTLS Enabled:        True
  Cached Cert Size in Session: 1428 bytes
  Connecting to:               https://pubsub.mtls.googleapis.com/v1/projects/<PROJECT_ID>/topics
  Live mTLS Handshake Status:  SUCCESS (HTTP 403)

===========================================================================
>>> 3. LIVE 401 TEST: NON-mTLS URL WITH PATH TRAP (Bug 1 Fix)
===========================================================================
  Target URL:                  https://storage.googleapis.com/storage/v1/b/mtls.googleapis.com
  Notice: Path has 'mtls.googleapis.com', but host is standard 'storage.googleapis.com'
  HTTP Requests Sent:          2 (1st: 401 Unauthorized -> 2nd [Retry]: 200 OK)
  Endpoint Detected as mTLS:   False (Expected: False)
  spy_check Call Count:        0 (Expected: 0)
  [PASS] Successfully avoided cert rotation check (0 calls) despite 401 and retry!

===========================================================================
>>> 4. LIVE 401 TEST: PRIVATE SERVICE CONNECT (PSC) URL (Bug 2 Fix)
===========================================================================
  Target URL:                  https://storage.p.googleapis.com/b/production-bucket
  Notice: Private Service Connect domain (*.p.googleapis.com)
  HTTP Requests Sent:          2 (1st: 401 Unauthorized -> 2nd [Retry]: 200 OK)
  Endpoint Detected as mTLS:   True (Expected: True)
  spy_check Call Count:        1 (Expected: 1 on 401)
  Cert Check Triggered:        True (Inspected disk certificate and computed fingerprint)
  [PASS] Successfully triggered cert inspection on 401 (1 call) and retried with 200 OK!

========================================================================
 ALL REAL CERTIFICATE LIVE MANUAL TESTS PASSED SUCCESSFULLY! (100% GREEN) 
========================================================================

…ion (googleapis#18147)

* Isolate hostname using urllib.parse.urlsplit in _mtls_helper.is_mtls_endpoint
* Eliminate false positives on non-mTLS URLs containing mtls substrings in paths/queries
* Add support for Private Service Connect (*.p.googleapis.com) custom mTLS endpoints
* Update AuthorizedSession and AuthorizedHttp to use shared is_mtls_endpoint helper
* Add comprehensive unit tests in test__mtls_helper, test_requests, and test_urllib3

Fixes googleapis#18147
Follow-up to googleapis#17928
@attharva-24
attharva-24 requested review from a team as code owners August 21, 2026 23:53

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request centralizes and improves mTLS and Private Service Connect (PSC) endpoint detection by introducing a robust is_mtls_endpoint helper function in _mtls_helper.py. This helper replaces redundant inline checks in both the requests and urllib3 transport implementations. Comprehensive unit tests have been added to validate the new helper across various URL formats, and existing tests have been updated to ensure proper cert rotation behavior on PSC endpoints. I have no additional feedback to provide as the changes are well-implemented and fully tested.

@attharva-24 attharva-24 self-assigned this Aug 21, 2026
@attharva-24
attharva-24 requested a review from macastelaz August 21, 2026 23:57
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.

Fix(auth): incorrect mTLS endpoint detection causes false positive cert rotations and breaks PSC endpoints

1 participant