perf: make is_config_initialized cheap — no scrypt on the per-request probe (D1)

This commit is contained in:
Marrrrrrrrry committed 2026-09-09 18:33:27 +08:00
1 parent 5121db6edc
commit ec424dd909
2 files changed
+30

No files matched your search

+9
View File
@@ -29,9 +29,18 @@ def is_valid_jwt_secret(secret: Any) -> bool:
def is_config_initialized(config: dict[str, Any]) -> bool:
"""Cheap initialization probe — runs on EVERY request via the middleware.
Must never run a slow hash: a scrypt-stored token cannot be the legacy
default (which only ever existed as plaintext or sha256), so those tokens
are initialized without any verification. Only plaintext/sha256 tokens go
through the legacy-default comparison, which is microseconds.
"""
admin_token = str(config.get("admin_token") or "")
if not admin_token:
return False
if is_password_hashed(admin_token) and admin_token.startswith("scrypt$"):
return True
return not verify_password(LEGACY_DEFAULT_ADMIN_TOKEN, admin_token)
+21
View File
@@ -5,6 +5,7 @@ hashes are scrypt (memory-hard). A successful login with a legacy-stored
password upgrades the stored hash in place.
"""
import asyncio
import time
import hashlib
import unittest
@@ -87,3 +88,23 @@ class TransparentRehashTests(SettingsOverrideMixin, unittest.TestCase):
if __name__ == "__main__":
unittest.main()
class CheapInitProbeTests(unittest.TestCase):
"""D1 回归:is_config_initialized 在每个请求都会被中间件调用,
对 scrypt 口令绝不允许跑慢哈希。"""
def test_scrypt_token_is_initialized_without_slow_hash(self):
from core.security import is_config_initialized
from core.utils import hash_password as hp
token = hp("some-real-password")
t0 = time.perf_counter()
self.assertTrue(is_config_initialized({"admin_token": token}))
self.assertLess(time.perf_counter() - t0, 0.01, "初始化探测不应执行 scrypt")
def test_empty_and_legacy_default_are_not_initialized(self):
from core.security import is_config_initialized, LEGACY_DEFAULT_ADMIN_TOKEN
self.assertFalse(is_config_initialized({"admin_token": ""}))
self.assertFalse(is_config_initialized({"admin_token": LEGACY_DEFAULT_ADMIN_TOKEN}))