Reject non-ASCII Basic payloads before decoding
This commit is contained in:
@@ -86,6 +86,34 @@ async def test_login_without_valid_basic(client: AsyncClient, headers: dict) ->
|
||||
challenged(await client.post("/api/v1/auth/sessions", headers=headers), BASIC)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"raw", [b"Basic \x80", b"Basic \xff", b"Basic \xc3\xa9", b"Basic YWxpY2U6c2VjcmV0\xff"]
|
||||
)
|
||||
async def test_non_ascii_basic_matches_wrong_password(client: AsyncClient, raw: bytes) -> None:
|
||||
alice = await register(client, "non_ascii_basic")
|
||||
count = (
|
||||
select(func.count()).select_from(SessionRow).where(SessionRow.user_id == UUID(alice["id"]))
|
||||
)
|
||||
before = await scalar(count)
|
||||
wrong = await client.post(
|
||||
"/api/v1/auth/sessions",
|
||||
headers={**basic(alice["username"], "wrong-password"), "X-Request-Id": "non-ascii-basic"},
|
||||
)
|
||||
invalid = await client.post(
|
||||
"/api/v1/auth/sessions",
|
||||
headers={b"Authorization": raw, b"X-Request-Id": b"non-ascii-basic"},
|
||||
)
|
||||
for response in [wrong, invalid]:
|
||||
challenged(response, BASIC)
|
||||
assert response.headers["X-Instance-Id"]
|
||||
assert response.headers["content-type"].startswith("application/json")
|
||||
assert set(response.json()) == {"error"}
|
||||
assert set(response.json()["error"]) == {"code", "message", "request_id"}
|
||||
assert response.json()["error"]["message"]
|
||||
assert wrong.json() == invalid.json()
|
||||
assert await scalar(count) == before
|
||||
|
||||
|
||||
async def test_password_with_colon_logs_in(client: AsyncClient) -> None:
|
||||
body = new_user("colon", password="пароль:с:двоеточием")
|
||||
assert (await client.post("/api/v1/users", json=body)).status_code == 201
|
||||
|
||||
@@ -1,12 +1,24 @@
|
||||
import json
|
||||
from base64 import b64encode
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
|
||||
import pytest
|
||||
from starlette.types import Scope
|
||||
|
||||
from messenger.identity.application.queries import VerifyCredentials
|
||||
from messenger.identity.entrypoints.authentication import (
|
||||
BASIC_CHALLENGE,
|
||||
LOGIN_PATH,
|
||||
basic_credentials,
|
||||
bearer_token,
|
||||
challenge_for,
|
||||
guard,
|
||||
)
|
||||
from messenger.shared.application.ports import Logger
|
||||
from messenger.shared.entrypoints.pipeline import RequestPipeline
|
||||
|
||||
NON_ASCII_BASIC = [b"Basic \x80", b"Basic \xff", b"Basic \xc3\xa9", b"Basic YWxpY2U6c2VjcmV0\xff"]
|
||||
|
||||
|
||||
def encoded(raw: bytes) -> str:
|
||||
@@ -43,6 +55,46 @@ def test_basic_rejects_malformed(raw: str) -> None:
|
||||
assert basic_credentials(raw) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("raw", NON_ASCII_BASIC)
|
||||
def test_basic_rejects_non_ascii(raw: bytes) -> None:
|
||||
assert basic_credentials(raw.decode("latin-1")) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("raw", NON_ASCII_BASIC)
|
||||
async def test_non_ascii_basic_returns_401_without_credential_lookup(raw: bytes) -> None:
|
||||
verify = AsyncMock(return_value=None)
|
||||
container = SimpleNamespace(get=AsyncMock(return_value=verify))
|
||||
scope: Scope = {
|
||||
"type": "http",
|
||||
"method": "POST",
|
||||
"path": LOGIN_PATH,
|
||||
"headers": [(b"authorization", raw), (b"x-request-id", b"non-ascii-basic")],
|
||||
"app": SimpleNamespace(state=SimpleNamespace(dishka_container=container)),
|
||||
}
|
||||
endpoint, send = AsyncMock(), AsyncMock()
|
||||
pipeline = RequestPipeline(endpoint, Mock(spec=Logger), 1024, 1, guard, "test-instance")
|
||||
await pipeline(scope, AsyncMock(), send)
|
||||
start, body = [call.args[0] for call in send.await_args_list]
|
||||
assert start["status"] == 401
|
||||
headers = dict(start["headers"])
|
||||
assert headers[b"www-authenticate"] == BASIC_CHALLENGE.encode()
|
||||
assert headers[b"x-instance-id"] == b"test-instance"
|
||||
assert headers[b"x-request-id"] == b"non-ascii-basic"
|
||||
assert headers[b"content-type"] == b"application/json"
|
||||
assert json.loads(body["body"]) == {
|
||||
"error": {
|
||||
"code": "unauthorized",
|
||||
"message": "Аутентификация не предоставлена или недействительна",
|
||||
"request_id": "non-ascii-basic",
|
||||
}
|
||||
}
|
||||
container.get.assert_awaited_once_with(VerifyCredentials)
|
||||
verify.assert_not_awaited()
|
||||
endpoint.assert_not_awaited()
|
||||
assert "user" not in scope["state"]
|
||||
assert scope["state"]["actor"] is None
|
||||
|
||||
|
||||
def test_bearer_accepts_token68() -> None:
|
||||
assert bearer_token("Bearer abc-DEF_123.~+/==") == "abc-DEF_123.~+/=="
|
||||
assert bearer_token("bearer token") == "token"
|
||||
|
||||
Reference in New Issue
Block a user