From c2c457bf76b62814c318d46550eea6171216264e Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 27 Nov 2026 15:26:41 -0400 Subject: [PATCH] fix: Reject empty resource IDs in the resource client getters An empty ID failed only on the first request, and `client.user('')` fell back to `me`, silently reading the token owner's account. Co-Authored-By: Claude Sonnet 5.5 --- .../_resource_clients/_resource_client.py | 3 ++ src/apify_client/_resource_clients/user.py | 4 +- tests/unit/test_empty_resource_id.py | 44 +++++++++++++++++++ tests/unit/test_url_path_encoding.py | 4 +- 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 tests/unit/test_empty_resource_id.py diff --git a/src/apify_client/_resource_clients/_resource_client.py b/src/apify_client/_resource_clients/_resource_client.py index f64843a8..1bd993c4 100644 --- a/src/apify_client/_resource_clients/_resource_client.py +++ b/src/apify_client/_resource_clients/_resource_client.py @@ -63,6 +63,9 @@ def __init__( if resource_path.endswith('/'): raise ValueError('resource_path must not end with "/"') + if resource_id == '': + raise ValueError(f'resource_id of the `{resource_path}` client must not be an empty string') + self._base_url = base_url self._api_base_url = api_base_url or base_url self._public_base_url = public_base_url diff --git a/src/apify_client/_resource_clients/user.py b/src/apify_client/_resource_clients/user.py index d60470b2..8f96093f 100644 --- a/src/apify_client/_resource_clients/user.py +++ b/src/apify_client/_resource_clients/user.py @@ -38,7 +38,7 @@ def __init__( **kwargs: Any, ) -> None: super().__init__( - resource_id=resource_id or 'me', + resource_id='me' if resource_id is None else resource_id, resource_path=resource_path, **kwargs, ) @@ -161,7 +161,7 @@ def __init__( **kwargs: Any, ) -> None: super().__init__( - resource_id=resource_id or 'me', + resource_id='me' if resource_id is None else resource_id, resource_path=resource_path, **kwargs, ) diff --git a/tests/unit/test_empty_resource_id.py b/tests/unit/test_empty_resource_id.py new file mode 100644 index 00000000..50d7a350 --- /dev/null +++ b/tests/unit/test_empty_resource_id.py @@ -0,0 +1,44 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + +import pytest + +from apify_client import ApifyClient, ApifyClientAsync + +if TYPE_CHECKING: + from collections.abc import Callable + +_GETTERS = [ + pytest.param(lambda client: client.actor(''), id='actor'), + pytest.param(lambda client: client.dataset(''), id='dataset'), + pytest.param(lambda client: client.log(''), id='log'), + pytest.param(lambda client: client.request_queue(''), id='request queue'), + pytest.param(lambda client: client.user(''), id='user'), + pytest.param(lambda client: client.actor('actor-id').version(''), id='actor version'), + pytest.param(lambda client: client.actor('actor-id').version('0.1').env_var(''), id='env var'), +] + + +@pytest.mark.parametrize('get_client', _GETTERS) +@pytest.mark.parametrize('client_class', [ApifyClient, ApifyClientAsync]) +def test_empty_resource_id_is_rejected_by_the_getter( + client_class: type[ApifyClient | ApifyClientAsync], + get_client: Callable[[ApifyClient | ApifyClientAsync], object], +) -> None: + """A getter refuses an empty ID as soon as it is called.""" + client = client_class(token='test_token') + + with pytest.raises(ValueError, match='resource_id'): + get_client(client) + + +@pytest.mark.parametrize('client_class', [ApifyClient, ApifyClientAsync]) +@pytest.mark.parametrize('user_id', [pytest.param((), id='omitted'), pytest.param((None,), id='none')]) +def test_user_without_an_id_is_the_current_user( + client_class: type[ApifyClient | ApifyClientAsync], user_id: tuple[None, ...] +) -> None: + """Only an omitted or `None` user ID stands for the current user.""" + client = client_class(token='test_token') + + assert client.user(*user_id).resource_id == 'me' diff --git a/tests/unit/test_url_path_encoding.py b/tests/unit/test_url_path_encoding.py index 5c3c05dc..0704498c 100644 --- a/tests/unit/test_url_path_encoding.py +++ b/tests/unit/test_url_path_encoding.py @@ -400,14 +400,14 @@ def test_resource_id_is_encoded_into_a_single_path_segment( assert captured_targets == [f'/v2/datasets/{encoded_resource_id}'] -@pytest.mark.parametrize('resource_id', _DEGENERATE_SEGMENTS) +@pytest.mark.parametrize('resource_id', [pytest.param('.', id='current'), pytest.param('..', id='parent')]) def test_degenerate_resource_id_is_rejected_before_the_request( *, api_url: str, captured_targets: list[str], resource_id: str, ) -> None: - """An empty or dot resource ID would address the whole collection, so it is refused instead.""" + """A dot resource ID would address another endpoint, so it is refused instead.""" client = ApifyClient(token='test_token', api_url=api_url) with pytest.raises(ValueError, match='cannot be used as a URL path segment'):