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
4 changes: 3 additions & 1 deletion src/supabase/src/supabase/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from supabase_utils.http.request import validate_request_timeout
from supabase_utils.http.retry import RetryPolicy

from .client import AsyncClient
from .client import AsyncClient, _validate_string_argument
from .types import (
AuthClientOptions,
FunctionsClientOptions,
Expand Down Expand Up @@ -36,6 +36,8 @@ def create_async_client(
client. An `http_client` passed in is left open. See `AsyncClient` for the
other arguments.
"""
_validate_string_argument("supabase_url", supabase_url)
_validate_string_argument("supabase_key", supabase_key)
validate_request_timeout(request_timeout)
client = AsyncClient(
supabase_url,
Expand Down
13 changes: 9 additions & 4 deletions src/supabase/src/supabase/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ class SupabaseException(SupabaseError):
when `auth` is used on a client returned by `with_token()`."""


def _validate_string_argument(name: str, value: object) -> None:
if value is not None and not isinstance(value, str):
raise SupabaseException(f"{name} must be a string, not {type(value).__name__}")
if not value:
raise SupabaseException(f"{name} is required")


def _find_authorization(headers: Mapping[str, str]) -> str | None:
return next(
(value for key, value in headers.items() if key.lower() == "authorization"),
Expand Down Expand Up @@ -258,10 +265,8 @@ def __init__(
functions
Options for the edge functions client.
"""
if not supabase_url:
raise SupabaseException("supabase_url is required")
if not supabase_key:
raise SupabaseException("supabase_key is required")
_validate_string_argument("supabase_url", supabase_url)
_validate_string_argument("supabase_key", supabase_key)

try:
self.supabase_url = URL(
Expand Down
6 changes: 5 additions & 1 deletion src/supabase/src/supabase/httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from supabase_utils.http.request import validate_request_timeout
from supabase_utils.http.retry import RetryPolicy

from .client import AsyncClient, Client
from .client import AsyncClient, Client, _validate_string_argument
from .types import (
AuthClientOptions,
FunctionsClientOptions,
Expand Down Expand Up @@ -36,6 +36,8 @@ def create_client(
client. An `http_client` passed in is left open. See `Client` for the
other arguments.
"""
_validate_string_argument("supabase_url", supabase_url)
_validate_string_argument("supabase_key", supabase_key)
validate_request_timeout(request_timeout)
httpx_client = http_client or SyncHttpxClient(
http2=True,
Expand Down Expand Up @@ -78,6 +80,8 @@ def create_async_client(
client. An `http_client` passed in is left open. See `AsyncClient` for the
other arguments.
"""
_validate_string_argument("supabase_url", supabase_url)
_validate_string_argument("supabase_key", supabase_key)
validate_request_timeout(request_timeout)
httpx_client = http_client or AsyncHttpxClient(
http2=True,
Expand Down
85 changes: 68 additions & 17 deletions src/supabase/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from concurrent.futures import ThreadPoolExecutor
from unittest.mock import AsyncMock, MagicMock, patch

Expand Down Expand Up @@ -37,37 +38,87 @@
LEGACY_KEY = "header.payload.signature"

INVALID_URLS = [
"",
"example.supabase.co",
"ftp://example.supabase.co",
"https://",
"http://[::1",
"https://example.supabase.co:99999",
]

MISSING_OR_NON_STRING_URL_AND_KEY: list[tuple[object, object, str]] = [
("", KEY, "supabase_url is required"),
(None, KEY, "supabase_url is required"),
(139, KEY, "supabase_url must be a string, not int"),
(b"https://example.supabase.co", KEY, "supabase_url must be a string, not bytes"),
({}, KEY, "supabase_url must be a string, not dict"),
([], KEY, "supabase_url must be a string, not list"),
(URL, "", "supabase_key is required"),
(URL, None, "supabase_key is required"),
(URL, 139, "supabase_key must be a string, not int"),
(URL, b"publishable-key", "supabase_key must be a string, not bytes"),
(URL, {}, "supabase_key must be a string, not dict"),
(URL, [], "supabase_key must be a string, not list"),
(None, 139, "supabase_url is required"),
(139, None, "supabase_url must be a string, not int"),
]

@pytest.mark.parametrize("url", INVALID_URLS)
def test_sync_client_rejects_invalid_urls(url: str) -> None:
with SyncHttpxClient() as http_client, pytest.raises(SupabaseException):
Client(url, KEY, HttpxSession(client=http_client))
INVALID_URL_AND_KEY = [
*[(url, KEY, "Invalid URL") for url in INVALID_URLS],
*MISSING_OR_NON_STRING_URL_AND_KEY,
]


@pytest.mark.parametrize("url", INVALID_URLS)
async def test_async_client_rejects_invalid_urls(url: str) -> None:
async with AsyncHttpxClient() as http_client:
with pytest.raises(SupabaseException):
AsyncClient(url, KEY, AsyncHttpxSession(client=http_client))
@pytest.mark.parametrize(("url", "key", "message"), INVALID_URL_AND_KEY)
def test_sync_client_rejects_an_invalid_url_or_key(
url: object, key: object, message: str
) -> None:
with (
SyncHttpxClient() as http_client,
pytest.raises(SupabaseException, match=re.escape(message)),
):
Client(
url, # pyrefly: ignore[bad-argument-type]
key, # pyrefly: ignore[bad-argument-type]
HttpxSession(client=http_client),
)


def test_sync_client_requires_a_key() -> None:
with SyncHttpxClient() as http_client, pytest.raises(SupabaseException):
Client(URL, "", HttpxSession(client=http_client))
@pytest.mark.parametrize(("url", "key", "message"), INVALID_URL_AND_KEY)
async def test_async_client_rejects_an_invalid_url_or_key(
url: object, key: object, message: str
) -> None:
async with AsyncHttpxClient() as http_client:
with pytest.raises(SupabaseException, match=re.escape(message)):
AsyncClient(
url, # pyrefly: ignore[bad-argument-type]
key, # pyrefly: ignore[bad-argument-type]
AsyncHttpxSession(client=http_client),
)


async def test_async_client_requires_a_key() -> None:
async with AsyncHttpxClient() as http_client:
with pytest.raises(SupabaseException):
AsyncClient(URL, "", AsyncHttpxSession(client=http_client))
@pytest.mark.parametrize(("url", "key", "message"), MISSING_OR_NON_STRING_URL_AND_KEY)
def test_factories_reject_an_invalid_url_or_key_before_creating_a_session(
url: object, key: object, message: str
) -> None:
factories = [
supabase.httpx.create_client,
supabase.httpx.create_async_client,
supabase.aiohttp.create_async_client,
]
for factory in factories:
with (
patch("supabase.aiohttp.ClientSession") as aiohttp_session,
patch("supabase.httpx.SyncHttpxClient") as sync_httpx_client,
patch("supabase.httpx.AsyncHttpxClient") as async_httpx_client,
pytest.raises(SupabaseException, match=re.escape(message)),
):
factory(
url, # pyrefly: ignore[bad-argument-type]
key, # pyrefly: ignore[bad-argument-type]
)
aiohttp_session.assert_not_called()
sync_httpx_client.assert_not_called()
async_httpx_client.assert_not_called()


def test_sync_client_keeps_a_lowercase_authorization_header() -> None:
Expand Down
Loading