From ad32cb4f04e66c0315679bc0120a5e7acd31c985 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Wed, 25 Nov 2026 22:54:20 +0000 Subject: [PATCH 1/3] fix(auth): add compare_universe_domains helper in _helpers Wrapper credentials such as impersonated and downscoped credentials need a consistent helper to verify that a configured universe_domain matches an expected universe domain (for example, from wrapped source credentials or an endpoint host). Add _helpers.compare_universe_domains() next to _helpers.validate_universe_domain() along with unit tests. b/589828921 --- packages/google-auth/google/auth/_helpers.py | 21 ++++++++++++++++++++ packages/google-auth/tests/test__helpers.py | 13 ++++++++++++ 2 files changed, 34 insertions(+) diff --git a/packages/google-auth/google/auth/_helpers.py b/packages/google-auth/google/auth/_helpers.py index 5599f2004f85..5808084cccd2 100644 --- a/packages/google-auth/google/auth/_helpers.py +++ b/packages/google-auth/google/auth/_helpers.py @@ -298,6 +298,27 @@ def validate_universe_domain(universe_domain: Any) -> None: ) +def compare_universe_domains( + universe_domain: str, expected_universe_domain: str +) -> None: + """Verifies that ``universe_domain`` matches ``expected_universe_domain``. + + Args: + universe_domain (str): The configured universe domain. + expected_universe_domain (str): The expected universe domain (for + example, from wrapped source credentials). + + Raises: + google.auth.exceptions.InvalidValue: If the two universe domains do + not match. + """ + if universe_domain != expected_universe_domain: + raise exceptions.InvalidValue( + f"The configured universe domain ({universe_domain}) does not " + f"match the expected universe domain ({expected_universe_domain})." + ) + + def scopes_to_string(scopes): """Converts scope value to a string suitable for sending to OAuth 2.0 authorization servers. diff --git a/packages/google-auth/tests/test__helpers.py b/packages/google-auth/tests/test__helpers.py index 41fd4befb71c..692aafd2980b 100644 --- a/packages/google-auth/tests/test__helpers.py +++ b/packages/google-auth/tests/test__helpers.py @@ -798,3 +798,16 @@ def test_validate_universe_domain_invalid(invalid_value): match="Universe domain must be a valid non-empty domain string", ): _helpers.validate_universe_domain(invalid_value) + + +def test_compare_universe_domains_match(): + _helpers.compare_universe_domains("example.com", "example.com") + + +def test_compare_universe_domains_mismatch(): + with pytest.raises( + exceptions.InvalidValue, + match=r"The configured universe domain \(example\.com\) does not " + r"match the expected universe domain \(googleapis\.com\)\.", + ): + _helpers.compare_universe_domains("example.com", "googleapis.com") From 9fc0026b6852471bd9765ff8f1b7ff066c616ff8 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Fri, 27 Nov 2026 15:34:15 +0000 Subject: [PATCH 2/3] fix(auth): validate inputs in compare_universe_domains --- packages/google-auth/google/auth/_helpers.py | 12 ++++---- packages/google-auth/tests/test__helpers.py | 30 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/packages/google-auth/google/auth/_helpers.py b/packages/google-auth/google/auth/_helpers.py index 5808084cccd2..334b1bb8d859 100644 --- a/packages/google-auth/google/auth/_helpers.py +++ b/packages/google-auth/google/auth/_helpers.py @@ -299,19 +299,21 @@ def validate_universe_domain(universe_domain: Any) -> None: def compare_universe_domains( - universe_domain: str, expected_universe_domain: str + universe_domain: Any, expected_universe_domain: Any ) -> None: """Verifies that ``universe_domain`` matches ``expected_universe_domain``. Args: - universe_domain (str): The configured universe domain. - expected_universe_domain (str): The expected universe domain (for + universe_domain (Any): The configured universe domain. + expected_universe_domain (Any): The expected universe domain (for example, from wrapped source credentials). Raises: - google.auth.exceptions.InvalidValue: If the two universe domains do - not match. + google.auth.exceptions.InvalidValue: If either universe domain is + invalid or the two universe domains do not match. """ + validate_universe_domain(universe_domain) + validate_universe_domain(expected_universe_domain) if universe_domain != expected_universe_domain: raise exceptions.InvalidValue( f"The configured universe domain ({universe_domain}) does not " diff --git a/packages/google-auth/tests/test__helpers.py b/packages/google-auth/tests/test__helpers.py index 692aafd2980b..6db9932e71f9 100644 --- a/packages/google-auth/tests/test__helpers.py +++ b/packages/google-auth/tests/test__helpers.py @@ -811,3 +811,33 @@ def test_compare_universe_domains_mismatch(): r"match the expected universe domain \(googleapis\.com\)\.", ): _helpers.compare_universe_domains("example.com", "googleapis.com") + + +def test_compare_universe_domains_case_sensitive_mismatch(): + with pytest.raises( + exceptions.InvalidValue, + match=r"The configured universe domain \(Googleapis\.com\) does not " + r"match the expected universe domain \(googleapis\.com\)\.", + ): + _helpers.compare_universe_domains("Googleapis.com", "googleapis.com") + + +@pytest.mark.parametrize( + "universe_domain,expected_universe_domain", + [ + (None, None), + ("", ""), + (" ", " "), + ("example.com", None), + (None, "example.com"), + ("example.com", " "), + ], +) +def test_compare_universe_domains_invalid_inputs( + universe_domain, expected_universe_domain +): + with pytest.raises( + exceptions.InvalidValue, + match="Universe domain must be a valid non-empty domain string", + ): + _helpers.compare_universe_domains(universe_domain, expected_universe_domain) From afe06885da891fbc3b85b812204c0816ed147965 Mon Sep 17 00:00:00 2001 From: ohmayr Date: Fri, 27 Nov 2026 15:49:41 +0000 Subject: [PATCH 3/3] fix(auth): compare universe domains case-insensitively --- packages/google-auth/google/auth/_helpers.py | 4 ++- packages/google-auth/tests/test__helpers.py | 26 ++++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/google-auth/google/auth/_helpers.py b/packages/google-auth/google/auth/_helpers.py index 334b1bb8d859..08c7bf0bbe2b 100644 --- a/packages/google-auth/google/auth/_helpers.py +++ b/packages/google-auth/google/auth/_helpers.py @@ -303,6 +303,8 @@ def compare_universe_domains( ) -> None: """Verifies that ``universe_domain`` matches ``expected_universe_domain``. + Performs a case-insensitive comparison after validating both domain strings. + Args: universe_domain (Any): The configured universe domain. expected_universe_domain (Any): The expected universe domain (for @@ -314,7 +316,7 @@ def compare_universe_domains( """ validate_universe_domain(universe_domain) validate_universe_domain(expected_universe_domain) - if universe_domain != expected_universe_domain: + if universe_domain.lower() != expected_universe_domain.lower(): raise exceptions.InvalidValue( f"The configured universe domain ({universe_domain}) does not " f"match the expected universe domain ({expected_universe_domain})." diff --git a/packages/google-auth/tests/test__helpers.py b/packages/google-auth/tests/test__helpers.py index 6db9932e71f9..94dcbb720842 100644 --- a/packages/google-auth/tests/test__helpers.py +++ b/packages/google-auth/tests/test__helpers.py @@ -800,26 +800,26 @@ def test_validate_universe_domain_invalid(invalid_value): _helpers.validate_universe_domain(invalid_value) -def test_compare_universe_domains_match(): - _helpers.compare_universe_domains("example.com", "example.com") +@pytest.mark.parametrize( + "universe_domain,expected_universe_domain", + [ + ("example.com", "example.com"), + ("Googleapis.com", "googleapis.com"), + ("googleapis.com", "GOOGLEAPIS.COM"), + ("My-Universe.Example.Com", "my-universe.example.com"), + ], +) +def test_compare_universe_domains_match(universe_domain, expected_universe_domain): + _helpers.compare_universe_domains(universe_domain, expected_universe_domain) def test_compare_universe_domains_mismatch(): with pytest.raises( exceptions.InvalidValue, - match=r"The configured universe domain \(example\.com\) does not " - r"match the expected universe domain \(googleapis\.com\)\.", - ): - _helpers.compare_universe_domains("example.com", "googleapis.com") - - -def test_compare_universe_domains_case_sensitive_mismatch(): - with pytest.raises( - exceptions.InvalidValue, - match=r"The configured universe domain \(Googleapis\.com\) does not " + match=r"The configured universe domain \(Example\.com\) does not " r"match the expected universe domain \(googleapis\.com\)\.", ): - _helpers.compare_universe_domains("Googleapis.com", "googleapis.com") + _helpers.compare_universe_domains("Example.com", "googleapis.com") @pytest.mark.parametrize(