fix: 拒绝过短密钥,并把对外端口只留在本机

This commit is contained in:
2026-11-16 02:41:13 +08:00
parent 42f2b457a0
commit ec41af9489
11 changed files with 85 additions and 12 deletions
+4 -2
View File
@@ -8,10 +8,11 @@ _backend = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(_backend))
_root = _backend.parent / "Temp" / "pytest"
_root.mkdir(parents=True, exist_ok=True)
os.environ["EUNIV_SECRET_KEY"] = "test-secret-key"
os.environ["EUNIV_SECRET_KEY"] = "test-secret-key-0123456789abcdef"
os.environ["EUNIV_DB_PATH"] = str(_root / "euniv.db")
os.environ["EUNIV_DISABLE_REFRESH"] = "1"
os.environ["EUNIV_ADMIN_PASSWORD"] = "test-admin-pass"
os.environ["EUNIV_COOKIE_SECURE"] = "0"
os.environ.pop("EUNIV_ADMIN_RESET", None)
@@ -19,7 +20,7 @@ os.environ.pop("EUNIV_ADMIN_RESET", None)
def fresh_database():
from sqlalchemy import text
from admin.settings import ensure_admin_seed, load_admin_identity
from admin.settings import ensure_admin_seed, ensure_key_check, load_admin_identity
from euniv.db import engine, init_db
with engine.begin() as connection:
@@ -33,6 +34,7 @@ def fresh_database():
connection.execute(text("DROP TABLE IF EXISTS alembic_version"))
init_db()
ensure_admin_seed()
ensure_key_check()
load_admin_identity()
from admin.login_limit import reset
+12
View File
@@ -14,6 +14,18 @@ from euniv.encrypt import (
)
def test_short_secret_is_rejected(monkeypatch) -> None:
from euniv.crypto import require_secret
monkeypatch.setenv("EUNIV_SECRET_KEY", "too-short")
try:
require_secret()
except RuntimeError as exc:
assert "32" in str(exc)
else:
raise AssertionError("过短的密钥应当被拒绝")
def test_encrypt_payload_wraps_progress_json_as_string() -> None:
body = '{"knowledgeId":"abc","end":false}'
assert encrypt_payload(body) == b'{"body":"{\\"knowledgeId\\":\\"abc\\",\\"end\\":false}"}'