Reject noncanonical cursor dates and noninteger cursor versions
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import base64
|
||||
import json
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from messenger.domain import Failure, boundary
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"version,date",
|
||||
[
|
||||
(True, "2026-01-01T00:00:00Z"),
|
||||
(1.0, "2026-01-01T00:00:00Z"),
|
||||
(1, "2026-W01-1T00:00:00Z"),
|
||||
(1, "20260101T000000Z"),
|
||||
(1, "2026-01-01 00:00:00Z"),
|
||||
(1, "2026-01-01X00:00:00Z"),
|
||||
(1, "2026-02-30T00:00:00Z"),
|
||||
(1, "2026-01-01T00:00:00.1234567Z"),
|
||||
],
|
||||
)
|
||||
def test_boundary_rejects_python_iso_extensions(version, date):
|
||||
payload = [version, "scope", date, str(uuid4())]
|
||||
cursor = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
|
||||
with pytest.raises(Failure) as error:
|
||||
boundary("scope", cursor)
|
||||
assert error.value.status == 400
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"date", ["2026-01-01T00:00:00Z", "2026-01-01T00:00:00.1Z", "2026-01-01T00:00:00.123456Z"]
|
||||
)
|
||||
def test_boundary_preserves_generated_format(date):
|
||||
uid = str(uuid4())
|
||||
cursor = (
|
||||
base64.urlsafe_b64encode(json.dumps([1, "scope", date, uid]).encode()).decode().rstrip("=")
|
||||
)
|
||||
assert boundary("scope", cursor) == (date, uid)
|
||||
@@ -143,3 +143,39 @@ def test_login_body_is_rejected(shared):
|
||||
raw=body,
|
||||
headers={"Authorization": "Basic " + basic},
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"date",
|
||||
[
|
||||
"2026-W01-1T00:00:00Z",
|
||||
"20260101T000000Z",
|
||||
"2026-01-01 00:00:00Z",
|
||||
"2026-01-01X00:00:00Z",
|
||||
"2026-01-01T00:00:00+00:00",
|
||||
"2026-02-30T00:00:00Z",
|
||||
"2026-01-01T24:00:00Z",
|
||||
"2026-01-01T00:00:00.1234567Z",
|
||||
],
|
||||
)
|
||||
def test_cursor_rejects_noncanonical_dates_over_http(shared, date):
|
||||
c, alice, _, _, chat = shared
|
||||
for path, scope in [
|
||||
("/api/v1/chats", "chats:" + alice["id"]),
|
||||
(f"/api/v1/chats/{chat['id']}/messages", "messages:" + chat["id"]),
|
||||
]:
|
||||
payload = [1, scope, date, str(uuid.uuid4())]
|
||||
cursor = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
|
||||
c.call("GET", path + "?cursor=" + cursor, 400, actor=alice)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("version", [True, 1.0])
|
||||
def test_cursor_rejects_noninteger_version_over_http(shared, version):
|
||||
c, alice, _, _, chat = shared
|
||||
for path, scope in [
|
||||
("/api/v1/chats", "chats:" + alice["id"]),
|
||||
(f"/api/v1/chats/{chat['id']}/messages", "messages:" + chat["id"]),
|
||||
]:
|
||||
payload = [version, scope, "2026-01-01T00:00:00Z", str(uuid.uuid4())]
|
||||
cursor = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
|
||||
c.call("GET", path + "?cursor=" + cursor, 400, actor=alice)
|
||||
|
||||
Reference in New Issue
Block a user