Skip to content

Commit 02e1323

Browse files
committed
fix: ensure TOTP verification uses UTC for accurate time comparison
1 parent d9b76b4 commit 02e1323

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

astrbot/core/utils/totp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def _get_verified_totp_timecode(secret: str, code: str) -> int | None:
4848
code = code.strip()
4949
try:
5050
totp = pyotp.TOTP(secret.strip())
51-
now = datetime.datetime.now()
51+
now = datetime.datetime.now(datetime.timezone.utc)
5252
for offset in (-1, 0, 1):
5353
candidate_time = now + datetime.timedelta(seconds=offset * totp.interval)
5454
if hmac.compare_digest(str(totp.at(candidate_time)), code):

astrbot/dashboard/server.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,12 @@
4242
from .routes.subagent import SubAgentRoute
4343
from .routes.t2i import T2iRoute
4444

45-
_RATE_LIMITED_ENDPOINTS: frozenset = frozenset(
46-
{
47-
"/api/auth/totp/disable",
48-
"/api/auth/totp/setup",
49-
"/api/auth/login",
50-
"/api/auth/totp/verify-setup",
51-
}
52-
)
45+
_RATE_LIMITED_ENDPOINTS: frozenset = frozenset({
46+
"/api/auth/totp/disable",
47+
"/api/auth/totp/setup",
48+
"/api/auth/login",
49+
"/api/auth/totp/verify-setup",
50+
})
5351

5452

5553
class _AuthRateLimiter:
@@ -98,15 +96,13 @@ def _match_registered_web_api(registered_web_apis, subpath: str, method: str):
9896
if request_method not in allowed_methods:
9997
continue
10098

101-
url_map = Map(
102-
[
103-
Rule(
104-
_normalize_plugin_api_route(route),
105-
endpoint="plugin_api",
106-
methods=allowed_methods,
107-
),
108-
]
109-
)
99+
url_map = Map([
100+
Rule(
101+
_normalize_plugin_api_route(route),
102+
endpoint="plugin_api",
103+
methods=allowed_methods,
104+
),
105+
])
110106
try:
111107
_, path_values = url_map.bind("").match(
112108
request_path,
@@ -279,7 +275,10 @@ async def auth_middleware(self):
279275
await self.db.touch_api_key(api_key.key_id)
280276
return None
281277

282-
if os.environ.get("ASTRBOT_TEST_MODE") != "true" and request.path in _RATE_LIMITED_ENDPOINTS:
278+
if (
279+
os.environ.get("ASTRBOT_TEST_MODE") != "true"
280+
and request.path in _RATE_LIMITED_ENDPOINTS
281+
):
283282
limiter = _rate_limiters.get(request.path)
284283
if limiter is None:
285284
limiter = _AuthRateLimiter(capacity=3, refill_rate=1.0)

0 commit comments

Comments
 (0)