forked from mtusi2026/mtusi2026lab7
wip: apply auth-hardening (interrupted by session end)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -21,6 +21,7 @@ from src.config.settings import (
|
||||
BASE_DIR,
|
||||
ApiTls,
|
||||
App,
|
||||
Auth,
|
||||
Settings,
|
||||
Telemetry,
|
||||
Uploads,
|
||||
@@ -261,3 +262,139 @@ def test_example_toml_loads_as_config_toml() -> None:
|
||||
loaded = _ExampleToml()
|
||||
assert loaded.telemetry.trace_sample_ratio == 1
|
||||
assert loaded.telemetry.metric_buckets is MetricBucketsEnum.DEFAULT
|
||||
|
||||
|
||||
# параметры пароля и входа (design change `auth-hardening`, решения 1, 3, 5;
|
||||
# спек `service-configuration`, «Параметры пароля и входа»)
|
||||
|
||||
# итог замеров: строка «Выбрано» evidence/auth/argon2-params.md и сводка
|
||||
# метки профиля login (QUESTIONS.md, раздел auth-hardening)
|
||||
MEASURED_AUTH_DEFAULTS = {
|
||||
"argon2_time_cost": 2,
|
||||
"argon2_memory_kib": 65536,
|
||||
"argon2_parallelism": 1,
|
||||
"kdf_concurrency": 1,
|
||||
"kdf_max_waiting": 16,
|
||||
"kdf_wait_timeout_seconds": 2,
|
||||
"login_window_seconds": 300,
|
||||
"login_max_failures_per_account": 10,
|
||||
"login_max_failures_per_address": 200,
|
||||
}
|
||||
EVIDENCE_ARGON2 = BASE_DIR.joinpath("evidence/auth/argon2-params.md")
|
||||
|
||||
|
||||
def test_auth_defaults_match_measurements() -> None:
|
||||
loaded = Auth()
|
||||
assert {
|
||||
field: getattr(loaded, field) for field in MEASURED_AUTH_DEFAULTS
|
||||
} == MEASURED_AUTH_DEFAULTS
|
||||
|
||||
|
||||
def test_argon2_defaults_named_in_evidence() -> None:
|
||||
"""Строка выбора evidence совпадает с умолчаниями (задача 2.3)."""
|
||||
text = EVIDENCE_ARGON2.read_text(encoding="utf-8")
|
||||
chosen = (
|
||||
f"Выбрано: m={MEASURED_AUTH_DEFAULTS['argon2_memory_kib']} KiB, "
|
||||
f"t={MEASURED_AUTH_DEFAULTS['argon2_time_cost']}, "
|
||||
f"p={MEASURED_AUTH_DEFAULTS['argon2_parallelism']}"
|
||||
)
|
||||
assert chosen in text
|
||||
|
||||
|
||||
def test_example_toml_keeps_auth_defaults() -> None:
|
||||
loaded = _ExampleToml()
|
||||
assert loaded.auth == Auth()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("field", "value"),
|
||||
[
|
||||
("argon2_memory_kib", 19455),
|
||||
("argon2_memory_kib", 8192),
|
||||
("argon2_time_cost", 1),
|
||||
("argon2_parallelism", 0),
|
||||
("kdf_concurrency", 0),
|
||||
("kdf_max_waiting", -1),
|
||||
("kdf_wait_timeout_seconds", 0),
|
||||
("kdf_wait_timeout_seconds", float("inf")),
|
||||
("login_window_seconds", 0),
|
||||
("login_max_failures_per_account", -1),
|
||||
("login_max_failures_per_address", -1),
|
||||
],
|
||||
)
|
||||
def test_wrong_auth_value_names_field(field: str, value: object) -> None:
|
||||
with pytest.raises(ValidationError) as caught:
|
||||
Auth(**{field: value})
|
||||
assert [error["loc"] for error in caught.value.errors()] == [(field,)]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("field", "value"),
|
||||
[
|
||||
("argon2_memory_kib", 19456),
|
||||
("argon2_time_cost", 2),
|
||||
("argon2_parallelism", 1),
|
||||
("kdf_max_waiting", 0),
|
||||
("login_max_failures_per_account", 0),
|
||||
("login_max_failures_per_address", 0),
|
||||
],
|
||||
)
|
||||
def test_auth_lower_bounds_accepted(field: str, value: object) -> None:
|
||||
assert getattr(Auth(**{field: value}), field) == value
|
||||
|
||||
|
||||
@pytest.mark.parametrize("wait", [6, 7])
|
||||
def test_kdf_wait_not_shorter_than_request_names_field(wait: float) -> None:
|
||||
with pytest.raises(ValidationError) as caught:
|
||||
_ArgumentsOnly(
|
||||
database={"request_timeout_seconds": 6},
|
||||
auth={"kdf_wait_timeout_seconds": wait},
|
||||
)
|
||||
errors = caught.value.errors()
|
||||
assert [error["loc"] for error in errors] == [("auth",)]
|
||||
assert "auth.kdf_wait_timeout_seconds" in errors[0]["msg"]
|
||||
# в ошибке нет значения секции database (там пароль)
|
||||
assert "password" not in str(caught.value)
|
||||
|
||||
|
||||
def test_kdf_wait_shorter_than_request() -> None:
|
||||
loaded = _ArgumentsOnly(
|
||||
database={"request_timeout_seconds": 2},
|
||||
auth={"kdf_wait_timeout_seconds": 1},
|
||||
)
|
||||
assert loaded.auth.kdf_wait_timeout_seconds == 1
|
||||
|
||||
|
||||
def test_auth_from_environment_over_file(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
tmp_path: Any,
|
||||
) -> None:
|
||||
toml = tmp_path.joinpath("config.toml")
|
||||
toml.write_text(
|
||||
"[auth]\nlogin_max_failures_per_account = 7\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
class _EnvAndToml(_ArgumentsOnly):
|
||||
model_config = SettingsConfigDict(
|
||||
**{**_ArgumentsOnly.model_config, "toml_file": toml},
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def settings_customise_sources(
|
||||
cls,
|
||||
settings_cls: type[BaseSettings],
|
||||
init_settings: PydanticBaseSettingsSource,
|
||||
env_settings: PydanticBaseSettingsSource,
|
||||
*args: PydanticBaseSettingsSource, # noqa: ARG003
|
||||
**kwargs: Any, # noqa: ARG003
|
||||
) -> tuple[PydanticBaseSettingsSource, ...]:
|
||||
return (
|
||||
init_settings,
|
||||
env_settings,
|
||||
TomlConfigSettingsSource(settings_cls),
|
||||
)
|
||||
|
||||
assert _EnvAndToml().auth.login_max_failures_per_account == 7
|
||||
monkeypatch.setenv("AUTH__LOGIN_MAX_FAILURES_PER_ACCOUNT", "3")
|
||||
assert _EnvAndToml().auth.login_max_failures_per_account == 3
|
||||
|
||||
Reference in New Issue
Block a user