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
35 changes: 35 additions & 0 deletions tests/unit/sqlalchemy/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest import mock

import pytest
from sqlalchemy import exc
from sqlalchemy.engine.url import make_url
from sqlalchemy.engine.url import URL

Expand Down Expand Up @@ -314,3 +315,37 @@ def test_trino_connection_oauth2_auth():
_, cparams = dialect.create_connect_args(url)

assert isinstance(cparams['auth'], OAuth2Authentication)


@pytest.mark.parametrize(
"host",
[
"https://trino.example.com", # scheme prefix (the common mistake)
"http://trino.example.com",
"trino.example.com:443", # port embedded in host
"trino.example.com/path", # path embedded in host
"user@trino.example.com", # credentials embedded in host
"::1", # unbracketed IPv6 literal
"[::1", # unbalanced bracket
"example.com]", # stray bracket
"https://[::1", # scheme plus malformed IPv6 literal
],
)
def test_url_rejects_non_hostname_host(host):
with pytest.raises(exc.ArgumentError, match="host must be a hostname only"):
trino_url(host=host, port=443, user="user")


@pytest.mark.parametrize(
"host",
[
"trino.example.com",
"localhost",
"192.168.0.1",
"[::1]", # bracketed IPv6 literal
"host_with.dots-and_underscores",
],
)
def test_url_accepts_bare_hostname(host):
# A valid host must round-trip through make_url without error.
assert make_url(trino_url(host=host, port=443, user="user")).host is not None
26 changes: 26 additions & 0 deletions trino/sqlalchemy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Tuple
from typing import Union
from urllib.parse import quote_plus
from urllib.parse import urlparse

from sqlalchemy import exc

Expand All @@ -14,6 +15,29 @@ def _rfc_1738_quote(text):
return re.sub(r"[:@/]", lambda m: "%%%X" % ord(m.group(0)), text)


def _assert_valid_host(host: str) -> None:
# Parse with a leading "//" so a bare hostname is treated as the authority
# rather than a path. `urlparse(host).scheme` catches an embedded scheme.
try:
parsed = urlparse("//" + host)
invalid = bool(
urlparse(host).scheme
or parsed.port is not None
or parsed.path
or parsed.username
or parsed.password
)
except ValueError:
# A malformed port or IPv6 literal raises here.
invalid = True
if invalid:
raise exc.ArgumentError(
f"host must be a hostname only, without a scheme, port, path, or credentials. Got {host!r}. "
"For example, use 'example.com' instead of 'https://example.com' and pass the port via the "
"'port' argument."
)


def _url(
host: str,
port: Optional[int] = 8080,
Expand Down Expand Up @@ -56,6 +80,8 @@ def _url(
if not host:
raise exc.ArgumentError("host must be specified.")

_assert_valid_host(host)

trino_url += host

if not port:
Expand Down
Loading