Repository navigation
fix(auth): guard default-universe get_project_id by endpoint host - #19200
Conversation
External account configs that omit `universe_domain` default to `googleapis.com`. If the config points `token_url` or `service_account_impersonation_url` at another domain (such as another universe or a custom proxy), `get_project_id()` would mint a token from that endpoint and send it to `cloudresourcemanager.googleapis.com`. When `universe_domain` is `googleapis.com`, skip the Cloud Resource Manager lookup and return `None` unless `token_url` (and `service_account_impersonation_url`, when set) has a `googleapis.com` host. Configs targeting `googleapis.com` that omit `universe_domain` continue to work unchanged, as do configs that set a custom `universe_domain` explicitly. This change only addresses external_account.Credentials.get_project_id. Tracking whether universe_domain was explicitly configured (for Regional Access Boundary lookups) will be handled in a follow-up PR. b/588990592
There was a problem hiding this comment.
Code Review
This pull request introduces a security enhancement to prevent sending tokens minted for non-googleapis.com endpoints (such as other universe domains or custom proxies) to the Cloud Resource Manager in the default universe domain. It adds a helper function _has_default_universe_host to validate the host of the token URL and service account impersonation URL, returning None from get_project_id if they do not match the default universe domain. Corresponding unit tests have been added to verify this behavior. There are no review comments to address.
| _DEFAULT_TOKEN_URL = "https://sts.{universe_domain}/v1/token" | ||
|
|
||
|
|
||
| def _has_default_universe_host(url): |
There was a problem hiding this comment.
nit: I wonder if we should move this into a shared helper? It seems like we might have to do this kind of thing in multiple places
There was a problem hiding this comment.
Right now external_account.py is the only place checking endpoint URLs (the RAB fix won't need this), so I'd rather keep it local until something else actually uses it. Happy to move it later if another caller comes up.
| # Avoid sending tokens minted for non-googleapis.com endpoints | ||
| # (e.g. another universe domain or custom proxy) to | ||
| # cloudresourcemanager.googleapis.com. | ||
| if self.universe_domain == credentials.DEFAULT_UNIVERSE_DOMAIN: |
There was a problem hiding this comment.
I think we need to tighten up how self.universe_domain is validated in this class.
__init__, with_universe_domain, and from_info all allow None or "" universes.
with_universe_domain updates the internal self._universe_domain attribute, but not self._cloud_resource_manager_url. So it would skip over this check here, but then either leak or crash during the request
Maybe you should work with Tony to make sure we have consistent ways of validating universe domains across all credential types. Because it seems like each credential is running into this same problem
There was a problem hiding this comment.
Good catch. Because with_universe_domain doesn't update _cloud_resource_manager_url, checking self.universe_domain let with_universe_domain("example.com" / None / "") skip this guard and still hit cloudresourcemanager.googleapis.com.
I changed the check to inspect self._cloud_resource_manager_url directly (the URL we actually send the request to) and added tests for with_universe_domain in c464fe5, so that leak is blocked either way.
I want to keep this PR focused on the get_project_id() leak, so I'll sync with @parthea and handle universe_domain validation (None/"") and updating _cloud_resource_manager_url in with_universe_domain() in a separate PR.
Check `_has_default_universe_host(self._cloud_resource_manager_url)`
instead of `self.universe_domain == DEFAULT_UNIVERSE_DOMAIN` in
`external_account.Credentials.get_project_id()`. Because
`with_universe_domain()` currently does not update
`_cloud_resource_manager_url`, guarding the actual destination URL
prevents non-default tokens from leaking to
`cloudresourcemanager.googleapis.com` even after
`with_universe_domain("example.com" / None / "")`.
b/588990592
Reject URL fragments ("#"), userinfo ("@" in netloc), and invalid DNS
labels in _has_default_universe_host() to align with #19163. Check "@"
on parsed.netloc rather than the raw URL so valid
service_account_impersonation_url paths containing service account
emails remain allowed.
b/588990592
Move _has_default_universe_host() from external_account.py to google.auth._helpers.has_default_universe_host() so other google-auth modules can reuse it, and add direct unit tests in test__helpers.py. b/588990592
…9200) ## Context & Problem When an `external_account` credential config omits `universe_domain`, `google-auth` defaults it to `googleapis.com`. If that config actually points to another universe (or a custom proxy) via `token_url` or `service_account_impersonation_url`, calling `get_project_id()` mints a token from that configured endpoint and sends it to `cloudresourcemanager.googleapis.com`. We cannot simply skip `get_project_id()` whenever `universe_domain` is omitted, because many existing `googleapis.com` external account configs omit `universe_domain` and rely on project ID lookup continuing to work. ## Summary of Changes - Added `_has_default_universe_host()` in `external_account.py` to check whether a URL's hostname is `googleapis.com` or a subdomain of `googleapis.com`. - Updated `external_account.Credentials.get_project_id()` so that when the Cloud Resource Manager URL points to `googleapis.com`, it skips the lookup and returns `None` if `token_url` (or `service_account_impersonation_url`, when configured) does not point to a `googleapis.com` host. - Added unit tests in `tests/test_external_account.py` covering non-default endpoints, malformed/missing URLs, spoofed host suffixes, `with_universe_domain()` with a stale default CRM URL, and the backwards-compatibility case where `universe_domain` is omitted with a `googleapis.com` `token_url`. ## What This PR Does Not Change - **Explicit custom `universe_domain` configs:** If `universe_domain` is set to something other than `googleapis.com`, `get_project_id()` continues calling `cloudresourcemanager.<universe_domain>` as before. - **Regional Access Boundary (RAB) lookups:** Distinguishing an omitted `universe_domain` from an explicit `googleapis.com` (the other half of b/588990592) touches multiple credential classes and will be handled in a follow-up PR. - **Downstream API calls:** This only guards the automatic Cloud Resource Manager call inside `get_project_id()`; callers targeting another universe still need to set `universe_domain` for their API requests. ## Verification - `pytest tests/test_external_account.py tests/test_identity_pool.py tests/test_aws.py tests/test_pluggable.py tests/test__default.py` - `ruff check` and `ruff format --check` on modified files - `mypy google/auth/external_account.py` b/588990592
Context & Problem
When an
external_accountcredential config omitsuniverse_domain,google-authdefaults it togoogleapis.com. If that config actually points to another universe (or a custom proxy) viatoken_urlorservice_account_impersonation_url, callingget_project_id()mints a token from that configured endpoint and sends it tocloudresourcemanager.googleapis.com.We cannot simply skip
get_project_id()wheneveruniverse_domainis omitted, because many existinggoogleapis.comexternal account configs omituniverse_domainand rely on project ID lookup continuing to work.Summary of Changes
_has_default_universe_host()inexternal_account.pyto check whether a URL's hostname isgoogleapis.comor a subdomain ofgoogleapis.com.external_account.Credentials.get_project_id()so that when the Cloud Resource Manager URL points togoogleapis.com, it skips the lookup and returnsNoneiftoken_url(orservice_account_impersonation_url, when configured) does not point to agoogleapis.comhost.tests/test_external_account.pycovering non-default endpoints, malformed/missing URLs, spoofed host suffixes,with_universe_domain()with a stale default CRM URL, and the backwards-compatibility case whereuniverse_domainis omitted with agoogleapis.comtoken_url.What This PR Does Not Change
universe_domainconfigs: Ifuniverse_domainis set to something other thangoogleapis.com,get_project_id()continues callingcloudresourcemanager.<universe_domain>as before.universe_domainfrom an explicitgoogleapis.com(the other half of b/588990592) touches multiple credential classes and will be handled in a follow-up PR.get_project_id(); callers targeting another universe still need to setuniverse_domainfor their API requests.Verification
pytest tests/test_external_account.py tests/test_identity_pool.py tests/test_aws.py tests/test_pluggable.py tests/test__default.pyruff checkandruff format --checkon modified filesmypy google/auth/external_account.pyb/588990592