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