Skip to content

fix(auth): validate and use service_account_impersonation_url host - #19224

Open
ohmayr wants to merge 3 commits into
mainfrom
fix/auth-impersonated-url-validation
Open

ohmayr wants to merge 3 commits into
mainfrom
fix/auth-impersonated-url-validation

Conversation

@ohmayr

@ohmayr ohmayr commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Context & Problem

impersonated_credentials.Credentials.from_impersonated_service_account_info() sliced the target principal out of service_account_impersonation_url and discarded the URL's host and origin. Impersonated configs targeting a regional, custom-universe, or proxy endpoint therefore fell back to iamcredentials.googleapis.com and could bypass universe consistency checks.

Summary of Changes

  • Add _parse_impersonation_url() and _extract_universe_from_iam_host() in packages/google-auth/google/auth/impersonated_credentials.py to validate HTTPS :generateAccessToken impersonation URLs and recognize standard, regional, REP, PSC, and mTLS Google IAM Credentials hostnames.
  • Update from_impersonated_service_account_info() to enforce host-vs-universe consistency using _helpers.compare_universe_domains() (from chore(auth): add compare_universe_domains helper in _helpers #19220) and _helpers.has_default_universe_host(), infer non-default universes when omitted, and pass iam_endpoint_override for non-default URLs while keeping mTLS selection intact for the standard default URL.
  • Add unit tests in packages/google-auth/tests/test_impersonated_credentials.py.

Verification

  • ruff check, ruff check --select I, ruff format --check, and mypy --config-file=../../mypy.ini passed with zero issues.
  • pytest passed (496 passed) with 100% statement and branch coverage on both google/auth/_helpers.py and google/auth/impersonated_credentials.py.

b/589828921

@ohmayr
ohmayr added this pull request to stack #19223 November 25, 2026 23:51

@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 introduces robust parsing and validation of the service account impersonation URL in impersonated_credentials.py. It extracts the target principal and hostname, infers the universe domain from the host, and validates it against explicit universe domains. Additionally, it preserves custom origins and only overrides the IAM endpoint when a non-default URL is used. Extensive unit tests have been added to cover these scenarios. The review feedback points out a potential AttributeError if the source_credentials field is missing from the input dictionary, suggesting an explicit validation check.

@@ -511,7 +567,36 @@ def from_impersonated_service_account_info(cls, info, scopes=None):
"""

source_credentials_info = info.get("source_credentials")

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.

medium

If source_credentials is missing from the info dictionary, source_credentials_info will be None. This will lead to a cryptic AttributeError when trying to access .get("universe_domain") or .get("type") on it later in the method. It is highly recommended to validate that source_credentials_info is not None and raise a clear exceptions.InvalidValue exception if it is missing.

Suggested change
source_credentials_info = info.get("source_credentials")
source_credentials_info = info.get("source_credentials")
if source_credentials_info is None:
raise exceptions.InvalidValue("The 'source_credentials' field is required in the service account info.")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added an explicit Mapping check for source_credentials in commit be24566 so missing or non-mapping values raise InvalidValue instead of AttributeError, along with unit tests.

@ohmayr
ohmayr force-pushed the fix/auth-impersonated-url-validation branch from 5a5c9a0 to 0ac36bb Compare November 25, 2026 23:56
@ohmayr
ohmayr force-pushed the fix/auth-impersonated-url-validation branch from 0ac36bb to 7cbec2a Compare November 27, 2026 15:37
@parthea
parthea force-pushed the fix/auth-impersonated-url-validation branch from 7cbec2a to 1ced26c Compare November 27, 2026 15:48
@ohmayr
ohmayr force-pushed the fix/auth-impersonated-url-validation branch from 1ced26c to 6467757 Compare November 27, 2026 15:51
@parthea
parthea force-pushed the fix/auth-impersonated-url-validation branch from 6467757 to fad933e Compare November 27, 2026 16:04
Credentials.from_impersonated_service_account_info() ignored a
top-level universe_domain field and delegated _universe_domain solely
to source_credentials. When source_credentials omitted universe_domain,
impersonated credentials defaulted to googleapis.com and sent source
tokens to iamcredentials.googleapis.com.

Add an optional universe_domain parameter to Credentials.__init__()
(validated via _helpers.validate_universe_domain() and compared against
source_credentials.universe_domain via
_helpers.compare_universe_domains()), forward it in _make_copy(), and
propagate top-level universe_domain into a shallow copy of
source_credentials_info in from_impersonated_service_account_info()
without mutating caller input.

b/589828921
Credentials.from_impersonated_service_account_info() sliced the target
principal out of service_account_impersonation_url and discarded the
URL's host and origin. Impersonated configs targeting a regional,
custom-universe, or proxy endpoint therefore fell back to
iamcredentials.googleapis.com and could bypass universe consistency
checks.

Validate service_account_impersonation_url with
_parse_impersonation_url() and extract Google IAM Credentials host
universes with _extract_universe_from_iam_host(). Reuse
_helpers.compare_universe_domains() and
_helpers.has_default_universe_host() to enforce host-vs-universe
consistency, infer non-default universes when omitted, and pass
iam_endpoint_override for non-default URLs while keeping mTLS selection
intact for the standard default URL.

b/589828921
@ohmayr
ohmayr force-pushed the fix/auth-impersonated-url-validation branch from be24566 to e9f028f Compare November 30, 2026 20:09
Base automatically changed from fix/auth-impersonated-url-host to main November 30, 2026 23:28
@ohmayr
ohmayr marked this pull request as ready for review November 30, 2026 23:32
@ohmayr
ohmayr requested review from a team as code owners November 30, 2026 23:32

This branch has not been deployed

No deployments
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.

1 participant