From ddc2ef784be91584f9120e189d03f9ee3af8d036 Mon Sep 17 00:00:00 2001 From: Nixevol Date: Mon, 16 Nov 2026 02:31:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=98=BB=E6=AD=A2=E7=94=A8=E6=89=AB?= =?UTF-8?q?=E7=A0=81=E6=A0=87=E8=AE=B0=E5=86=92=E5=85=85=E4=BB=96=E4=BA=BA?= =?UTF-8?q?=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/euniv/service.py | 10 +++++++++- backend/tests/test_api.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/backend/euniv/service.py b/backend/euniv/service.py index 8b6b91b..9f876f6 100644 --- a/backend/euniv/service.py +++ b/backend/euniv/service.py @@ -2,6 +2,7 @@ from __future__ import annotations +import hmac import json import secrets import threading @@ -127,9 +128,16 @@ def login_and_store( auto_attempts: int = 3, ) -> dict: _purge_pending() + if is_qr_password(password): + raise RuntimeError("请使用扫码登录") account = db.get(Account, employee_id) if account and account.password_cipher and account.credential_cipher: - if decrypt_secret(account.password_cipher) == password and _credential_still_valid(account, client_factory): + stored = decrypt_secret(account.password_cipher) + same_password = ( + not is_qr_password(stored) + and hmac.compare_digest(stored.encode("utf-8"), password.encode("utf-8")) + ) + if same_password and _credential_still_valid(account, client_factory): _request_course_sync(employee_id) return {"employee_id": employee_id} _drop_employee(employee_id) diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index e38fc6d..6b22ecc 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -169,6 +169,35 @@ def test_login_stores_password_and_credential_only() -> None: assert "测试用户" not in decrypt_secret(account.password_cipher) +def test_qr_mark_cannot_password_login_and_unicode_password_is_safe() -> None: + import euniv.api as api_module + import euniv.service as service_module + from euniv.client import QR_PASSWORD_MARK + from euniv.crypto import encrypt_secret + + api_module.login_and_store = lambda db, employee_id, password, manual=False, auto_attempts=3: service_module.login_and_store( + db, employee_id, password, FakeClient, manual=manual, auto_attempts=auto_attempts + ) + client = TestClient(api_module.app) + with SessionLocal() as db: + db.add( + Account( + employee_id="7001", + password_cipher=encrypt_secret(QR_PASSWORD_MARK), + credential_cipher=encrypt_secret(json.dumps({"yxt_token": "yxt-1"})), + ) + ) + db.commit() + stolen = client.post("/api/login", json={"employee_id": "7001", "password": QR_PASSWORD_MARK}) + assert stolen.status_code == 400 + assert stolen.json()["detail"] == "请使用扫码登录" + + first = client.post("/api/login", json={"employee_id": "7002", "password": "密码一"}) + assert first.status_code == 200 + second = client.post("/api/login", json={"employee_id": "7002", "password": "密码一"}) + assert second.status_code == 200 + + def test_manual_jigsaw_keeps_image_pixels_and_retries() -> None: import euniv.api as api_module import euniv.service as service_module