Reject non-ASCII Basic payloads before decoding
This commit is contained in:
@@ -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