Repository navigation
fix(python-sdk): reject non-finite vectors and bad limits before signing (WALM-854) #1264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harrymove-ctrl
wants to merge
2
commits into
dev
Choose a base branch
from
fix/issue-1218-py-nonfinite-validation
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
238 changes: 238 additions & 0 deletions
238
packages/python-sdk-memwal/tests/test_nonfinite_validation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| """#1218: non-finite vectors and bad limits must fail client-side. | ||
|
|
||
| json.dumps emits bare NaN/Infinity tokens, which are not JSON, so the | ||
| relayer rejected the body with an opaque parse error. Limits went out as | ||
| NaN, -5 or ?limit=nan. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import math | ||
|
|
||
| import httpx | ||
| import pytest | ||
|
|
||
| from memwal import ( | ||
| MemWal, | ||
| MemWalError, | ||
| MemWalMock, | ||
| RecallManualOptions, | ||
| RecallParams, | ||
| RememberManualOptions, | ||
| ) | ||
|
|
||
|
|
||
| class CaptureMemWal(MemWal): | ||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| self.captured = [] | ||
|
|
||
| async def _ensure_compatible_relayer(self): | ||
| return { | ||
| "apiVersion": "1.0.0", | ||
| "relayerVersion": "capture", | ||
| "minSupportedSdk": {"python": "0.0.0"}, | ||
| } | ||
|
|
||
| async def _resolve_owner(self): | ||
| return "0xowner" | ||
|
|
||
| async def _signed_request( | ||
| self, | ||
| method, | ||
| path, | ||
| body, | ||
| accepted_statuses=(200,), | ||
| include_seal_session=True, | ||
| timeout=None, | ||
| ): | ||
| self.captured.append((method, path, body)) | ||
| if path.startswith("/v1/owners"): | ||
| return {"namespaces": [], "has_more": False, "snapshot_version": 1} | ||
| if path == "/api/remember/manual": | ||
| return {"id": "1", "blob_id": "b", "owner": "0x1", "namespace": "default"} | ||
| if path == "/api/restore": | ||
| return {"restored": 0, "skipped": 0, "total": 0, "namespace": "ns", "owner": "0x1"} | ||
| return {"results": [], "total": 0} | ||
|
|
||
|
|
||
| class FreshOwnerMemWal(MemWal): | ||
| """Keeps the real ``_resolve_owner``, so owner lookup goes through signing. | ||
|
|
||
| Every signed request is recorded and then fails, like a relayer outage: | ||
| a bad limit must surface as ValueError, not as this error. | ||
| """ | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| self.signed = [] | ||
|
|
||
| async def _ensure_compatible_relayer(self): | ||
| return { | ||
| "apiVersion": "1.0.0", | ||
| "relayerVersion": "capture", | ||
| "minSupportedSdk": {"python": "0.0.0"}, | ||
| } | ||
|
|
||
| async def _signed_request( | ||
| self, | ||
| method, | ||
| path, | ||
| body, | ||
| accepted_statuses=(200,), | ||
| include_seal_session=True, | ||
| timeout=None, | ||
| ): | ||
| self.signed.append((method, path)) | ||
| raise MemWalError("relayer unavailable") | ||
|
|
||
|
|
||
| def _client(): | ||
| return CaptureMemWal.create( | ||
| key="11" * 32, account_id="0x" + "ab" * 32, server_url="https://relayer.example" | ||
| ) | ||
|
|
||
|
|
||
| BAD_VECTORS = [ | ||
| [0.1, math.nan], | ||
| [math.inf], | ||
| [0.1, -math.inf], | ||
| [], | ||
| [0.1, "0.2"], | ||
| [0.1, None], | ||
| [True, 0.2], | ||
| "0.1,0.2", | ||
| None, | ||
| ] | ||
| BAD_LIMITS = [math.nan, math.inf, -5, -1, 0, 2.5, "5", True] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("vector", BAD_VECTORS, ids=repr) | ||
| async def test_recall_manual_rejects_bad_vector(vector): | ||
| c = _client() | ||
| with pytest.raises(ValueError, match="vector"): | ||
| await c.recall_manual(RecallManualOptions(vector=vector)) | ||
| assert c.captured == [] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("vector", BAD_VECTORS, ids=repr) | ||
| async def test_remember_manual_rejects_bad_vector(vector): | ||
| c = _client() | ||
| with pytest.raises(ValueError, match="vector"): | ||
| await c.remember_manual(RememberManualOptions(encrypted_data="AAAA", vector=vector)) | ||
| assert c.captured == [] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("limit", BAD_LIMITS, ids=repr) | ||
| @pytest.mark.parametrize( | ||
| "call", | ||
| [ | ||
| ("recall", lambda c, n: c.recall("q", limit=n)), | ||
| ("recall_params", lambda c, n: c.recall(RecallParams(query="q", limit=n))), | ||
| ("recall_manual", lambda c, n: c.recall_manual(RecallManualOptions(vector=[0.1], limit=n))), | ||
| ("restore", lambda c, n: c.restore("ns", limit=n)), | ||
| ("list_namespaces", lambda c, n: c.list_namespaces(limit=n)), | ||
| ], | ||
| ids=lambda x: x[0], | ||
| ) | ||
| async def test_bad_limit_is_rejected_before_signing(call, limit): | ||
| c = _client() | ||
| with pytest.raises(ValueError, match="limit"): | ||
| await call[1](c, limit) | ||
| assert c.captured == [] | ||
|
|
||
|
|
||
| async def test_valid_inputs_still_go_out_unchanged(): | ||
| c = _client() | ||
| await c.recall_manual(RecallManualOptions(vector=[0.1, -2, 0], limit=3)) | ||
| await c.remember_manual(RememberManualOptions(encrypted_data="AAAA", vector=[1, 0.5])) | ||
| await c.recall("q", limit=1) | ||
| await c.restore("ns", limit=50) | ||
| await c.list_namespaces(limit=500) | ||
| await c.list_namespaces() | ||
| bodies = [b for _, _, b in c.captured] | ||
| assert bodies[0]["vector"] == [0.1, -2, 0] and bodies[0]["limit"] == 3 | ||
| assert bodies[1]["vector"] == [1, 0.5] | ||
| assert bodies[2]["limit"] == 1 | ||
| assert bodies[3]["limit"] == 50 | ||
| assert c.captured[4][1].endswith("?limit=500") | ||
| assert "limit" not in c.captured[5][1] | ||
|
|
||
|
|
||
| async def test_signed_request_never_emits_nan_tokens(monkeypatch): | ||
| """Backstop: even if a NaN reaches the signer, it must not be serialised.""" | ||
| client = MemWal.create( | ||
| key="11" * 32, account_id="0x" + "ab" * 32, server_url="https://relayer.example" | ||
| ) | ||
|
|
||
| async def compatible(): | ||
| return { | ||
| "apiVersion": "1.0.0", | ||
| "relayerVersion": "t", | ||
| "minSupportedSdk": {"python": "0.0.0"}, | ||
| } | ||
|
|
||
| sent = [] | ||
|
|
||
| async def fake_post(*args, **kwargs): # pragma: no cover - must not be reached | ||
| sent.append(kwargs) | ||
| raise AssertionError("request sent with a non-JSON body") | ||
|
|
||
| monkeypatch.setattr(client, "_ensure_compatible_relayer", compatible) | ||
| monkeypatch.setattr(httpx.AsyncClient, "send", fake_post) | ||
| with pytest.raises(ValueError): | ||
| await client._signed_request( | ||
| "POST", "/api/recall", {"limit": math.nan}, include_seal_session=False | ||
| ) | ||
| assert sent == [] | ||
| await client.close() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("limit", BAD_LIMITS, ids=repr) | ||
| async def test_list_namespaces_bad_limit_raises_before_owner_lookup(limit): | ||
| """The owner lookup is a signed POST /api/stats; a bad limit must not reach it.""" | ||
| c = FreshOwnerMemWal.create( | ||
| key="11" * 32, account_id="0x" + "ab" * 32, server_url="https://relayer.example" | ||
| ) | ||
| with pytest.raises(ValueError, match="limit"): | ||
| await c.list_namespaces(limit=limit) | ||
| assert c.signed == [] | ||
| await c.close() | ||
|
|
||
|
|
||
| async def test_list_namespaces_valid_limit_still_resolves_owner(): | ||
| c = FreshOwnerMemWal.create( | ||
| key="11" * 32, account_id="0x" + "ab" * 32, server_url="https://relayer.example" | ||
| ) | ||
| with pytest.raises(MemWalError, match="relayer unavailable"): | ||
| await c.list_namespaces(limit=5) | ||
| assert c.signed == [("POST", "/api/stats")] | ||
| await c.close() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("limit", BAD_LIMITS, ids=repr) | ||
| @pytest.mark.parametrize( | ||
| "call", | ||
| [ | ||
| ("recall", lambda m, n: m.recall("q", limit=n)), | ||
| ("recall_params", lambda m, n: m.recall(RecallParams(query="q", limit=n))), | ||
| ("ask", lambda m, n: m.ask("q", limit=n)), | ||
| ("restore", lambda m, n: m.restore("ns", limit=n)), | ||
| ("list_namespaces", lambda m, n: m.list_namespaces(limit=n)), | ||
| ], | ||
| ids=lambda x: x[0], | ||
| ) | ||
| async def test_mock_rejects_the_same_bad_limits_as_the_client(call, limit): | ||
| mock = MemWalMock.create() | ||
| await mock.remember("a fact") | ||
| with pytest.raises(ValueError, match="limit"): | ||
| await call[1](mock, limit) | ||
|
|
||
|
|
||
| async def test_mock_accepts_positive_integer_limits(): | ||
| mock = MemWalMock.create() | ||
| await mock.remember("a fact") | ||
| assert len((await mock.recall("fact", limit=1)).results) == 1 | ||
| assert (await mock.restore("ns", limit=1)).restored == 0 | ||
| assert len((await mock.list_namespaces(limit=1)).namespaces) == 1 | ||
| assert len((await mock.list_namespaces()).namespaces) == 1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion] This rejects
0andTrue, matching the TypeScript SDK.MemWalMock.recallstill useslimit < 0, solimit=0returns an empty page andlimit=Trueis treated as1.MemWalMock.list_namespacesstill slices with a negative limit. Offline tests can pass calls the real client now rejects.Suggestion: Use the same positive-integer check in
MemWalMock.recall,MemWalMock.list_namespaces, andrestore.