Reject non-ASCII Basic payloads before decoding

This commit is contained in:
Artyom
2026-11-06 02:18:38 +03:00
parent f7bfd1d473
commit 7e19d4e33e
6 changed files with 351 additions and 5 deletions
@@ -0,0 +1,189 @@
date: 2026-11-05T23:17:40.020055+00:00 commit: f7bfd1d4731baf70eef18d3f95a4a655128990ba
Application code unchanged from d60e29221a553c62ee5dc27615c17fdaa6d27895; new regression tests in working tree.
$ .venv/bin/pytest -q student-tests/unit/test_authentication.py -k non_ascii --tb=short
FFFFFFFF [100%]
=================================== FAILURES ===================================
___________________ test_basic_rejects_non_ascii[Basic \x80] ___________________
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:34: in _bytes_from_decode_data
return s.encode('ascii')
^^^^^^^^^^^^^^^^^
E UnicodeEncodeError: 'ascii' codec can't encode character '\x80' in position 0: ordinal not in range(128)
During handling of the above exception, another exception occurred:
student-tests/unit/test_authentication.py:60: in test_basic_rejects_non_ascii
assert basic_credentials(raw.decode("latin-1")) is None
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
src/messenger/identity/entrypoints/authentication.py:36: in basic_credentials
decoded = b64decode(encoded.strip(), validate=True).decode("utf-8")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:80: in b64decode
s = _bytes_from_decode_data(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:36: in _bytes_from_decode_data
raise ValueError('string argument should contain only ASCII characters')
E ValueError: string argument should contain only ASCII characters
___________________ test_basic_rejects_non_ascii[Basic \xff] ___________________
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:34: in _bytes_from_decode_data
return s.encode('ascii')
^^^^^^^^^^^^^^^^^
E UnicodeEncodeError: 'ascii' codec can't encode character '\xff' in position 0: ordinal not in range(128)
During handling of the above exception, another exception occurred:
student-tests/unit/test_authentication.py:60: in test_basic_rejects_non_ascii
assert basic_credentials(raw.decode("latin-1")) is None
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
src/messenger/identity/entrypoints/authentication.py:36: in basic_credentials
decoded = b64decode(encoded.strip(), validate=True).decode("utf-8")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:80: in b64decode
s = _bytes_from_decode_data(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:36: in _bytes_from_decode_data
raise ValueError('string argument should contain only ASCII characters')
E ValueError: string argument should contain only ASCII characters
_________________ test_basic_rejects_non_ascii[Basic \xc3\xa9] _________________
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:34: in _bytes_from_decode_data
return s.encode('ascii')
^^^^^^^^^^^^^^^^^
E UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
During handling of the above exception, another exception occurred:
student-tests/unit/test_authentication.py:60: in test_basic_rejects_non_ascii
assert basic_credentials(raw.decode("latin-1")) is None
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
src/messenger/identity/entrypoints/authentication.py:36: in basic_credentials
decoded = b64decode(encoded.strip(), validate=True).decode("utf-8")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:80: in b64decode
s = _bytes_from_decode_data(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:36: in _bytes_from_decode_data
raise ValueError('string argument should contain only ASCII characters')
E ValueError: string argument should contain only ASCII characters
___________ test_basic_rejects_non_ascii[Basic YWxpY2U6c2VjcmV0\xff] ___________
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:34: in _bytes_from_decode_data
return s.encode('ascii')
^^^^^^^^^^^^^^^^^
E UnicodeEncodeError: 'ascii' codec can't encode character '\xff' in position 16: ordinal not in range(128)
During handling of the above exception, another exception occurred:
student-tests/unit/test_authentication.py:60: in test_basic_rejects_non_ascii
assert basic_credentials(raw.decode("latin-1")) is None
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
src/messenger/identity/entrypoints/authentication.py:36: in basic_credentials
decoded = b64decode(encoded.strip(), validate=True).decode("utf-8")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:80: in b64decode
s = _bytes_from_decode_data(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:36: in _bytes_from_decode_data
raise ValueError('string argument should contain only ASCII characters')
E ValueError: string argument should contain only ASCII characters
____ test_non_ascii_basic_returns_401_without_credential_lookup[Basic \x80] ____
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:34: in _bytes_from_decode_data
return s.encode('ascii')
^^^^^^^^^^^^^^^^^
E UnicodeEncodeError: 'ascii' codec can't encode character '\x80' in position 0: ordinal not in range(128)
During handling of the above exception, another exception occurred:
student-tests/unit/test_authentication.py:76: in test_non_ascii_basic_returns_401_without_credential_lookup
await pipeline(scope, AsyncMock(), send)
src/messenger/shared/entrypoints/pipeline.py:118: in __call__
rejection = self._oversized(scope) or await self._guard(scope)
^^^^^^^^^^^^^^^^^^^^^^^^
src/messenger/identity/entrypoints/authentication.py:65: in guard
credentials = basic_credentials(raw)
^^^^^^^^^^^^^^^^^^^^^^
src/messenger/identity/entrypoints/authentication.py:36: in basic_credentials
decoded = b64decode(encoded.strip(), validate=True).decode("utf-8")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:80: in b64decode
s = _bytes_from_decode_data(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:36: in _bytes_from_decode_data
raise ValueError('string argument should contain only ASCII characters')
E ValueError: string argument should contain only ASCII characters
____ test_non_ascii_basic_returns_401_without_credential_lookup[Basic \xff] ____
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:34: in _bytes_from_decode_data
return s.encode('ascii')
^^^^^^^^^^^^^^^^^
E UnicodeEncodeError: 'ascii' codec can't encode character '\xff' in position 0: ordinal not in range(128)
During handling of the above exception, another exception occurred:
student-tests/unit/test_authentication.py:76: in test_non_ascii_basic_returns_401_without_credential_lookup
await pipeline(scope, AsyncMock(), send)
src/messenger/shared/entrypoints/pipeline.py:118: in __call__
rejection = self._oversized(scope) or await self._guard(scope)
^^^^^^^^^^^^^^^^^^^^^^^^
src/messenger/identity/entrypoints/authentication.py:65: in guard
credentials = basic_credentials(raw)
^^^^^^^^^^^^^^^^^^^^^^
src/messenger/identity/entrypoints/authentication.py:36: in basic_credentials
decoded = b64decode(encoded.strip(), validate=True).decode("utf-8")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:80: in b64decode
s = _bytes_from_decode_data(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:36: in _bytes_from_decode_data
raise ValueError('string argument should contain only ASCII characters')
E ValueError: string argument should contain only ASCII characters
__ test_non_ascii_basic_returns_401_without_credential_lookup[Basic \xc3\xa9] __
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:34: in _bytes_from_decode_data
return s.encode('ascii')
^^^^^^^^^^^^^^^^^
E UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
During handling of the above exception, another exception occurred:
student-tests/unit/test_authentication.py:76: in test_non_ascii_basic_returns_401_without_credential_lookup
await pipeline(scope, AsyncMock(), send)
src/messenger/shared/entrypoints/pipeline.py:118: in __call__
rejection = self._oversized(scope) or await self._guard(scope)
^^^^^^^^^^^^^^^^^^^^^^^^
src/messenger/identity/entrypoints/authentication.py:65: in guard
credentials = basic_credentials(raw)
^^^^^^^^^^^^^^^^^^^^^^
src/messenger/identity/entrypoints/authentication.py:36: in basic_credentials
decoded = b64decode(encoded.strip(), validate=True).decode("utf-8")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:80: in b64decode
s = _bytes_from_decode_data(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:36: in _bytes_from_decode_data
raise ValueError('string argument should contain only ASCII characters')
E ValueError: string argument should contain only ASCII characters
_ test_non_ascii_basic_returns_401_without_credential_lookup[Basic YWxpY2U6c2VjcmV0\xff] _
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:34: in _bytes_from_decode_data
return s.encode('ascii')
^^^^^^^^^^^^^^^^^
E UnicodeEncodeError: 'ascii' codec can't encode character '\xff' in position 16: ordinal not in range(128)
During handling of the above exception, another exception occurred:
student-tests/unit/test_authentication.py:76: in test_non_ascii_basic_returns_401_without_credential_lookup
await pipeline(scope, AsyncMock(), send)
src/messenger/shared/entrypoints/pipeline.py:118: in __call__
rejection = self._oversized(scope) or await self._guard(scope)
^^^^^^^^^^^^^^^^^^^^^^^^
src/messenger/identity/entrypoints/authentication.py:65: in guard
credentials = basic_credentials(raw)
^^^^^^^^^^^^^^^^^^^^^^
src/messenger/identity/entrypoints/authentication.py:36: in basic_credentials
decoded = b64decode(encoded.strip(), validate=True).decode("utf-8")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:80: in b64decode
s = _bytes_from_decode_data(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../../../.local/share/uv/python/cpython-3.14.5-macos-aarch64-none/lib/python3.14/base64.py:36: in _bytes_from_decode_data
raise ValueError('string argument should contain only ASCII characters')
E ValueError: string argument should contain only ASCII characters
=========================== short test summary info ============================
FAILED student-tests/unit/test_authentication.py::test_basic_rejects_non_ascii[Basic \x80]
FAILED student-tests/unit/test_authentication.py::test_basic_rejects_non_ascii[Basic \xff]
FAILED student-tests/unit/test_authentication.py::test_basic_rejects_non_ascii[Basic \xc3\xa9]
FAILED student-tests/unit/test_authentication.py::test_basic_rejects_non_ascii[Basic YWxpY2U6c2VjcmV0\xff]
FAILED student-tests/unit/test_authentication.py::test_non_ascii_basic_returns_401_without_credential_lookup[Basic \x80]
FAILED student-tests/unit/test_authentication.py::test_non_ascii_basic_returns_401_without_credential_lookup[Basic \xff]
FAILED student-tests/unit/test_authentication.py::test_non_ascii_basic_returns_401_without_credential_lookup[Basic \xc3\xa9]
FAILED student-tests/unit/test_authentication.py::test_non_ascii_basic_returns_401_without_credential_lookup[Basic YWxpY2U6c2VjcmV0\xff]
8 failed, 21 deselected in 0.12s
exit_code: 1
@@ -0,0 +1,77 @@
date: 2026-11-05T23:18:05.005253+00:00 commit: f7bfd1d4731baf70eef18d3f95a4a655128990ba
Working tree: non-ASCII Basic fix and regression tests.
$ .venv/bin/pytest -q student-tests/unit
........................................................................ [ 50%]
........................................................................ [100%]
144 passed in 0.10s
exit_code: 0
$ .venv/bin/pytest --collect-only -q student-tests/api/test_sessions.py -k non_ascii
student-tests/api/test_sessions.py::test_non_ascii_basic_matches_wrong_password[Basic \x80]
student-tests/api/test_sessions.py::test_non_ascii_basic_matches_wrong_password[Basic \xff]
student-tests/api/test_sessions.py::test_non_ascii_basic_matches_wrong_password[Basic \xc3\xa9]
student-tests/api/test_sessions.py::test_non_ascii_basic_matches_wrong_password[Basic YWxpY2U6c2VjcmV0\xff]
4/22 tests collected (18 deselected) in 0.03s
exit_code: 0
$ make check
python3 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
exit_code: 0
$ make validate
.venv/bin/python scripts/validate_openapi.py
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
exit_code: 0
$ make lint
.venv/bin/ruff check src student-tests alembic scripts/probe.py scripts/limits.py scripts/scaling.py tools
All checks passed!
.venv/bin/ruff format --check src student-tests alembic scripts/probe.py scripts/limits.py scripts/scaling.py tools
unformatted: File would be reformatted
--> student-tests/api/test_sessions.py:94:13
|
93 | alice = await register(client, "non_ascii_basic")
- count = select(func.count()).select_from(SessionRow).where(
- SessionRow.user_id == UUID(alice["id"])
94 + count = (
95 + select(func.count()).select_from(SessionRow).where(SessionRow.user_id == UUID(alice["id"]))
96 | )
|
1 file would be reformatted, 112 files already formatted
make: *** [lint] Error 1
exit_code: 2
Formatting-only correction: ruff format student-tests/api/test_sessions.py; 1 file reformatted.
$ make lint
.venv/bin/ruff check src student-tests alembic scripts/probe.py scripts/limits.py scripts/scaling.py tools
All checks passed!
.venv/bin/ruff format --check src student-tests alembic scripts/probe.py scripts/limits.py scripts/scaling.py tools
113 files already formatted
.venv/bin/ty check src student-tests alembic
All checks passed!
exit_code: 0
$ openspec validate --all --strict
- Validating...
✓ spec/lab1-event-publication
✓ spec/lab1-messenger-chats
✓ spec/lab1-messenger-history
✓ spec/lab1-messenger-identity
✓ spec/lab1-service-operability
✓ spec/lab2-auth-sessions
✓ spec/lab2-dependency-failures
✓ spec/lab2-history-index
✓ spec/lab2-persistent-storage
✓ spec/lab2-session-cache
✓ spec/lab3-failover-operations
✓ spec/lab3-idempotent-messages
✓ spec/lab3-load-balancing
✓ spec/lab3-scaling-cost
✓ change/reject-non-ascii-basic
Totals: 15 passed, 0 failed (15 items)
exit_code: 0
@@ -1,10 +1,10 @@
## 1. Identity — минимум уровня «3»
- [ ] 1.1 Добавить offline-регрессии не-ASCII Basic для парсера и ASGI-конвейера; сохранить ожидаемые падения старого кода.
- [ ] 1.2 Отклонять не-ASCII до декодирования Base64; проверить стандартный 401, заголовки, тело и отсутствие вызова VerifyCredentials через unit-тесты.
- [x] 1.1 Добавить offline-регрессии не-ASCII Basic для парсера и ASGI-конвейера; сохранить ожидаемые падения старого кода.
- [x] 1.2 Отклонять не-ASCII до декодирования Base64; проверить стандартный 401, заголовки, тело и отсутствие вызова VerifyCredentials через unit-тесты.
## 2. Проверки и передача
- [ ] 2.1 Добавить HTTP-регрессию одинакового 401 с неверным паролем и отсутствия новой сессии; собрать её через pytest --collect-only без Docker.
- [ ] 2.2 Выполнить offline unit-тесты, make check, make validate, make lint и OpenSpec strict; сохранить evidence с SHA и командами.
- [x] 2.1 Добавить HTTP-регрессию одинакового 401 с неверным паролем и отсутствия новой сессии; собрать её через pytest --collect-only без Docker.
- [x] 2.2 Выполнить offline unit-тесты, make check, make validate, make lint и OpenSpec strict; сохранить evidence с SHA и командами.
- [ ] 2.3 Перечислить обязательные динамические проверки финального агента в evidence, синхронизировать спецификацию и архивировать change.
@@ -30,7 +30,7 @@ def challenge_for(path: str) -> str:
def basic_credentials(value: str) -> tuple[str, str] | None:
scheme, _, encoded = value.partition(" ")
if scheme.lower() != "basic":
if scheme.lower() != "basic" or not encoded.isascii():
return None
try:
decoded = b64decode(encoded.strip(), validate=True).decode("utf-8")
+28
View File
@@ -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
+52
View File
@@ -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"