From 305d458334e1f4e6bc8484734a596223bf154ea1 Mon Sep 17 00:00:00 2001 From: Nixevol Date: Tue, 17 Nov 2026 21:49:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=AE=A1=E7=90=86=E5=91=98=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=8F=A3=E4=BB=A4=E6=94=B9=E4=B8=BA=E5=85=AD=E4=BD=8D?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 2 +- backend/admin/passwords.py | 11 ++++++++--- backend/admin/settings.py | 20 +++++++------------- backend/tests/test_admin.py | 16 ++++++++++++---- docs/部署说明.md | 4 ++-- 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/.env.example b/.env.example index 68624fb..75bf2c5 100644 --- a/.env.example +++ b/.env.example @@ -4,7 +4,7 @@ EUNIV_SECRET_KEY= EUNIV_SECRET_KEY_OLD= # 下面这行只对直接启动的进程生效。docker compose 里数据库路径写死为 /data/euniv.db。 EUNIV_DB_PATH=data/euniv.db -# 不设置时,首次启动会在日志里打印一次随机管理员口令 +# 不设置时,首次启动的管理员口令是 242520 EUNIV_ADMIN_PASSWORD= # 设为 1 时,启动按上面的口令重置管理员密码 EUNIV_ADMIN_RESET= diff --git a/backend/admin/passwords.py b/backend/admin/passwords.py index a173946..7b3dbda 100644 --- a/backend/admin/passwords.py +++ b/backend/admin/passwords.py @@ -10,7 +10,8 @@ _N = 2**14 _R = 8 _P = 1 _PREFIX = "scrypt$" -_LEGACY_PASSWORD = "242520" +_DEFAULT_PASSWORD = "242520" +_MIN_LENGTH = 6 def hash_password(password: str) -> str: @@ -34,5 +35,9 @@ def is_hashed(stored: str) -> bool: return stored.startswith(_PREFIX) -def legacy_password() -> str: - return _LEGACY_PASSWORD +def default_password() -> str: + return _DEFAULT_PASSWORD + + +def min_length() -> int: + return _MIN_LENGTH diff --git a/backend/admin/settings.py b/backend/admin/settings.py index c75059e..71b6c3b 100644 --- a/backend/admin/settings.py +++ b/backend/admin/settings.py @@ -2,9 +2,7 @@ from __future__ import annotations -import logging import os -import secrets from datetime import datetime, timezone from sqlalchemy import select @@ -12,11 +10,10 @@ from sqlalchemy.orm import Session from admin.identity import identity from admin.models import SystemConfig -from admin.passwords import hash_password, is_hashed, legacy_password +from admin.passwords import default_password, hash_password, is_hashed, min_length from euniv.crypto import decrypt_secret _INITIAL_USERNAME = "admin" -_LOG = logging.getLogger("admin.settings") class SettingsError(ValueError): @@ -36,8 +33,8 @@ def _configured_admin_password() -> str | None: value = os.environ.get("EUNIV_ADMIN_PASSWORD", "").strip() if not value: return None - if len(value) < 8: - raise RuntimeError("EUNIV_ADMIN_PASSWORD 至少需要 8 位") + if len(value) < min_length(): + raise RuntimeError(f"EUNIV_ADMIN_PASSWORD 至少需要 {min_length()} 位") return value @@ -45,9 +42,7 @@ def _new_admin_password() -> str: configured = _configured_admin_password() if configured is not None: return configured - generated = secrets.token_urlsafe(12) - _LOG.warning("已生成随机管理员口令,请立即保存:%s", generated) - return generated + return default_password() def _reversible_password(stored: str) -> str | None: @@ -90,8 +85,7 @@ def ensure_admin_seed() -> None: plain = _reversible_password(row.admin_password_cipher) if plain is None: return - replacement = _new_admin_password() if plain == legacy_password() else plain - row.admin_password_cipher = hash_password(replacement) + row.admin_password_cipher = hash_password(plain) db.commit() @@ -216,8 +210,8 @@ def save_settings(db: Session, payload: dict) -> SystemConfig: raise SettingsError("自动滑动次数须为 1 到 10 的整数") password = payload.get("admin_password") if password is not None and password != "": - if len(str(password)) < 8: - raise SettingsError("管理员密码至少 8 位") + if len(str(password)) < min_length(): + raise SettingsError(f"管理员密码至少 {min_length()} 位") if len(str(password)) > 128: raise SettingsError("管理员密码过长") diff --git a/backend/tests/test_admin.py b/backend/tests/test_admin.py index 1e1f5bf..547b857 100644 --- a/backend/tests/test_admin.py +++ b/backend/tests/test_admin.py @@ -107,7 +107,7 @@ def test_admin_login_succeeds_and_wrong_password_skips_euniv(monkeypatch) -> Non event.remove(engine, "before_cursor_execute", hear) -def test_legacy_default_password_is_replaced_and_failures_are_limited() -> None: +def test_default_password_is_kept_when_hashed_and_failures_are_limited() -> None: from admin.settings import ensure_admin_seed with SessionLocal() as db: @@ -119,14 +119,22 @@ def test_legacy_default_password_is_replaced_and_failures_are_limited() -> None: load_admin_identity() api = _app() client = TestClient(api.app) - assert client.post("/api/login", json={"employee_id": "admin", "password": "242520"}).status_code == 400 - assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 200 + assert client.post("/api/login", json={"employee_id": "admin", "password": "242520"}).status_code == 200 + assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 400 short = client.put( "/api/admin/settings", - json=_settings_body(admin_password="short", admin_password_current=INITIAL_PASSWORD), + json=_settings_body(admin_password="short", admin_password_current="242520"), ) assert short.status_code == 400 + accepted = client.put( + "/api/admin/settings", + json=_settings_body(admin_password="242520", admin_password_current="242520"), + ) + assert accepted.status_code == 200 + from admin.login_limit import reset + + reset() locked = TestClient(api.app) for _ in range(4): assert locked.post("/api/login", json={"employee_id": "admin", "password": "wrong-pass"}).status_code == 400 diff --git a/docs/部署说明.md b/docs/部署说明.md index 417c96b..ae7b5c7 100644 --- a/docs/部署说明.md +++ b/docs/部署说明.md @@ -16,9 +16,9 @@ python -c "import secrets;print(secrets.token_urlsafe(48))" ## 管理员 -首次启动如果没有设置 `EUNIV_ADMIN_PASSWORD`,会生成随机口令,并用 WARNING 打一次日志。错过这次日志就无法登录。可以设置 `EUNIV_ADMIN_RESET=1`,并同时给出 `EUNIV_ADMIN_PASSWORD`,启动时按这个口令重置一次。重置后把 `EUNIV_ADMIN_RESET` 改回空。 +首次启动如果没有设置 `EUNIV_ADMIN_PASSWORD`,管理员用户名是 `admin`,口令是 `242520`。可以设置 `EUNIV_ADMIN_RESET=1`,并同时给出 `EUNIV_ADMIN_PASSWORD`,启动时按这个口令重置一次。重置后把 `EUNIV_ADMIN_RESET` 改回空。 -管理员用户名不能和已有工号相同。新口令至少 8 位。 +管理员用户名不能和已有工号相同。新口令至少 6 位。 ## 反向代理