diff --git a/packages/google-auth/google/auth/_helpers.py b/packages/google-auth/google/auth/_helpers.py index 780c99c40d34..5599f2004f85 100644 --- a/packages/google-auth/google/auth/_helpers.py +++ b/packages/google-auth/google/auth/_helpers.py @@ -276,6 +276,28 @@ def has_default_universe_host(url): return False +_UNIVERSE_DOMAIN_RE = re.compile(r"[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)*") + + +def validate_universe_domain(universe_domain: Any) -> None: + """Validates that ``universe_domain`` is a valid non-empty domain string. + + Args: + universe_domain (Any): The universe domain to validate. + + Raises: + google.auth.exceptions.InvalidValue: If ``universe_domain`` is not a + non-empty ASCII domain string (dot-separated labels of letters, + digits, hyphens, or underscores). + """ + if not isinstance(universe_domain, str) or not _UNIVERSE_DOMAIN_RE.fullmatch( + universe_domain + ): + raise exceptions.InvalidValue( + "Universe domain must be a valid non-empty domain string." + ) + + 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/google/auth/jwt.py b/packages/google-auth/google/auth/jwt.py index 827d893bd14f..0c0c1f176191 100644 --- a/packages/google-auth/google/auth/jwt.py +++ b/packages/google-auth/google/auth/jwt.py @@ -75,17 +75,6 @@ _ALGORITHM_TO_VERIFIER_CLASS["ES384"] = es.EsVerifier # type: ignore -def _validate_universe_domain(universe_domain): - """Raises if the universe domain passed to ``with_universe_domain`` is empty. - - Raises: - google.auth.exceptions.InvalidValue: If the universe domain is None, - empty, or whitespace-only. - """ - if not universe_domain or not universe_domain.strip(): - raise exceptions.InvalidValue("Universe domain cannot be empty.") - - def encode(signer, payload, header=None, key_id=None): """Make a signed JWT. @@ -605,7 +594,7 @@ def with_quota_project(self, quota_project_id): @_helpers.copy_docstring(google.auth.credentials.CredentialsWithUniverseDomain) def with_universe_domain(self, universe_domain): - _validate_universe_domain(universe_domain) + _helpers.validate_universe_domain(universe_domain) cred = self.__class__( self._signer, issuer=self._issuer, @@ -899,7 +888,7 @@ def with_quota_project(self, quota_project_id): @_helpers.copy_docstring(google.auth.credentials.CredentialsWithUniverseDomain) def with_universe_domain(self, universe_domain): - _validate_universe_domain(universe_domain) + _helpers.validate_universe_domain(universe_domain) return self.__class__( self._signer, issuer=self._issuer, diff --git a/packages/google-auth/tests/test__helpers.py b/packages/google-auth/tests/test__helpers.py index 9b431401aca0..41fd4befb71c 100644 --- a/packages/google-auth/tests/test__helpers.py +++ b/packages/google-auth/tests/test__helpers.py @@ -20,7 +20,7 @@ import pytest # type: ignore -from google.auth import _helpers +from google.auth import _helpers, exceptions # _MOCK_BASE_LOGGER_NAME is the base logger namespace used for testing. _MOCK_BASE_LOGGER_NAME = "foogle" @@ -742,3 +742,59 @@ def test_out_of_range(self): ) def test_has_default_universe_host(url, expected): assert _helpers.has_default_universe_host(url) is expected + + +@pytest.mark.parametrize( + "universe_domain", + [ + "googleapis.com", + "example.com", + "my-universe.example.com", + "s3nsapis.fr", + "fake_universe_domain", + ], +) +def test_validate_universe_domain_valid(universe_domain): + _helpers.validate_universe_domain(universe_domain) + + +@pytest.mark.parametrize( + "invalid_value", + [ + None, + "", + " ", + " example.com", + "example.com ", + "foo bar.com", + "example.com\n", + "example.com\x00", + "https://example.com", + "googleapis.com@evil.com", + "example.com/path", + "example.com\\path", + "example.com?q=1", + "example.com#frag", + "example.com:443", + "{}.evil.com", + "example.com}", + "[example.com]", + "example.com%2fevil.com", + "googleapis.com\uff20evil.com", + "example.com\uff0fpath", + ".", + ".example.com", + "example.com.", + "example..com", + b"googleapis.com", + False, + 123, + [], + ], +) +def test_validate_universe_domain_invalid(invalid_value): + with pytest.raises( + exceptions.InvalidValue, + match="Universe domain must be a valid non-empty domain string", + ): + _helpers.validate_universe_domain(invalid_value) diff --git a/packages/google-auth/tests/test_jwt.py b/packages/google-auth/tests/test_jwt.py index 7347dab47818..79871deb46f8 100644 --- a/packages/google-auth/tests/test_jwt.py +++ b/packages/google-auth/tests/test_jwt.py @@ -856,10 +856,13 @@ def test_with_universe_domain(self, signer): assert cloned._quota_project_id == "project-foo" assert cloned.universe_domain == FAKE_UNIVERSE_DOMAIN - @pytest.mark.parametrize("empty_universe", [None, "", " "]) - def test_with_universe_domain_empty_raises(self, empty_universe): + @pytest.mark.parametrize( + "invalid_universe", + [None, "", " ", " example.com", "https://example.com", 123], + ) + def test_with_universe_domain_invalid_raises(self, invalid_universe): with pytest.raises(exceptions.InvalidValue): - self.credentials.with_universe_domain(empty_universe) + self.credentials.with_universe_domain(invalid_universe) def test_before_request_with_universe_domain_clears_cached_rab(self, monkeypatch): monkeypatch.setenv( @@ -1215,10 +1218,13 @@ def test_with_universe_domain(self, signer): assert cloned._quota_project_id == "project-foo" assert cloned.universe_domain == FAKE_UNIVERSE_DOMAIN - @pytest.mark.parametrize("empty_universe", [None, "", " "]) - def test_with_universe_domain_empty_raises(self, empty_universe): + @pytest.mark.parametrize( + "invalid_universe", + [None, "", " ", " example.com", "https://example.com", 123], + ) + def test_with_universe_domain_invalid_raises(self, invalid_universe): with pytest.raises(exceptions.InvalidValue): - self.credentials.with_universe_domain(empty_universe) + self.credentials.with_universe_domain(invalid_universe) def test_with_claims(self): new_claims = {"meep": "moop"}