Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/google-auth/google/auth/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 2 additions & 13 deletions packages/google-auth/google/auth/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
58 changes: 57 additions & 1 deletion packages/google-auth/tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
"[email protected]",
"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)
18 changes: 12 additions & 6 deletions packages/google-auth/tests/test_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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"}
Expand Down
Loading