Skip to content

Commit 918edf3

Browse files
committed
feat: make dashboard auth rate-limit configurable via system settings
Add auth_rate_limit config block to dashboard settings with enable (default: true), average_interval (default: 1.0s), and max_burst (default: 3) options. The dashboard auth middleware now reads from config instead of using hardcoded values. The average_interval and max_burst fields are conditionally shown only when rate limiting is enabled.
1 parent 2d12187 commit 918edf3

5 files changed

Lines changed: 92 additions & 13 deletions

File tree

astrbot/core/config/default.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@
253253
"port": 6185,
254254
"disable_access_log": True,
255255
"trust_proxy_headers": False,
256+
"auth_rate_limit": {
257+
"enable": True,
258+
"average_interval": 1.0,
259+
"max_burst": 3,
260+
},
256261
"totp": {
257262
"enable": False,
258263
"secret": "",
@@ -2973,6 +2978,9 @@
29732978
},
29742979
"dashboard.ssl.enable": {"type": "bool"},
29752980
"dashboard.trust_proxy_headers": {"type": "bool"},
2981+
"dashboard.auth_rate_limit.enable": {"type": "bool"},
2982+
"dashboard.auth_rate_limit.average_interval": {"type": "float"},
2983+
"dashboard.auth_rate_limit.max_burst": {"type": "int"},
29762984
"dashboard.ssl.cert_file": {
29772985
"type": "string",
29782986
"condition": {"dashboard.ssl.enable": True},
@@ -4220,6 +4228,23 @@
42204228
"type": "bool",
42214229
"hint": "关闭时忽略 X-Forwarded-For/X-Real-IP,仅使用连接地址。",
42224230
},
4231+
"dashboard.auth_rate_limit.enable": {
4232+
"description": "启用登录验证速率限制",
4233+
"type": "bool",
4234+
"hint": "关闭后将不对登录、TOTP 等身份验证接口进行速率限制。",
4235+
},
4236+
"dashboard.auth_rate_limit.average_interval": {
4237+
"description": "登录验证速率限制平均间隔(秒)",
4238+
"type": "float",
4239+
"hint": "两次身份验证请求之间的最小平均间隔时间。例如设置为 1.0 表示每秒最多处理 1 个请求。",
4240+
"condition": {"dashboard.auth_rate_limit.enable": True},
4241+
},
4242+
"dashboard.auth_rate_limit.max_burst": {
4243+
"description": "登录验证速率限制最大突发数",
4244+
"type": "int",
4245+
"hint": "允许的瞬时最大突发请求数。例如设置为 3 表示在短时间内最多连续处理 3 个请求。",
4246+
"condition": {"dashboard.auth_rate_limit.enable": True},
4247+
},
42234248
"dashboard.totp.enable": {
42244249
"description": "启用 WebUI TOTP 双因素认证",
42254250
"type": "bool",

astrbot/dashboard/server.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,31 @@ async def auth_middleware(self):
283283
os.environ.get("ASTRBOT_TEST_MODE") != "true"
284284
and request.path in _RATE_LIMITED_ENDPOINTS
285285
):
286-
client_ip = self._get_request_client_ip()
287-
limiter = _rate_limiters.get(client_ip)
288-
if limiter is None:
289-
limiter = _AuthRateLimiter(capacity=3, refill_rate=1.0)
290-
_rate_limiters[client_ip] = limiter
291-
if not await limiter.acquire():
292-
r = jsonify(
293-
Response()
294-
.error("验证尝试过于频繁,系统可能正在遭受暴力破解")
295-
.__dict__
296-
)
297-
r.status_code = 429
298-
return r
286+
rl_config = self.config.get("dashboard", {}).get("auth_rate_limit", {})
287+
rl_enabled = rl_config.get("enable", True)
288+
if rl_enabled:
289+
average_interval = float(rl_config.get("average_interval", 1.0))
290+
max_burst = int(rl_config.get("max_burst", 3))
291+
if average_interval <= 0:
292+
average_interval = 1.0
293+
if max_burst <= 0:
294+
max_burst = 3
295+
refill_rate = 1.0 / average_interval
296+
client_ip = self._get_request_client_ip()
297+
limiter = _rate_limiters.get(client_ip)
298+
if limiter is None:
299+
limiter = _AuthRateLimiter(
300+
capacity=max_burst, refill_rate=refill_rate
301+
)
302+
_rate_limiters[client_ip] = limiter
303+
if not await limiter.acquire():
304+
r = jsonify(
305+
Response()
306+
.error("验证尝试过于频繁,系统可能正在遭受暴力破解")
307+
.__dict__
308+
)
309+
r.status_code = 429
310+
return r
299311

300312
allowed_exact_endpoints = {
301313
"/api/auth/login",

dashboard/src/i18n/locales/en-US/features/config-metadata.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,20 @@
10921092
"description": "Trust Proxy Headers for Client IP",
10931093
"hint": "When disabled, ignore X-Forwarded-For/X-Real-IP and use the connection address only."
10941094
},
1095+
"auth_rate_limit": {
1096+
"enable": {
1097+
"description": "Enable Login Rate Limiting",
1098+
"hint": "When disabled, authentication endpoints (login, TOTP, etc.) will not be rate-limited."
1099+
},
1100+
"average_interval": {
1101+
"description": "Rate Limit Average Interval (seconds)",
1102+
"hint": "Minimum average interval between authentication requests. For example, 1.0 means at most 1 request per second."
1103+
},
1104+
"max_burst": {
1105+
"description": "Rate Limit Max Burst",
1106+
"hint": "Maximum number of consecutive burst requests allowed. For example, 3 allows up to 3 requests in a short burst."
1107+
}
1108+
},
10951109
"ssl": {
10961110
"enable": {
10971111
"description": "Enable WebUI HTTPS",

dashboard/src/i18n/locales/ru-RU/features/config-metadata.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,20 @@
10931093
"description": "Доверять прокси-заголовкам для IP клиента",
10941094
"hint": "Если выключено, X-Forwarded-For/X-Real-IP игнорируются и используется только адрес соединения."
10951095
},
1096+
"auth_rate_limit": {
1097+
"enable": {
1098+
"description": "Включить ограничение скорости входа",
1099+
"hint": "Если выключено, конечные точки аутентификации (вход, TOTP и т.д.) не будут ограничены по скорости."
1100+
},
1101+
"average_interval": {
1102+
"description": "Средний интервал ограничения скорости (сек)",
1103+
"hint": "Минимальный средний интервал между запросами аутентификации. Например, 1.0 означает не более 1 запроса в секунду."
1104+
},
1105+
"max_burst": {
1106+
"description": "Максимальный всплеск ограничения скорости",
1107+
"hint": "Максимальное количество последовательных всплесков запросов. Например, 3 допускает до 3 запросов за короткий всплеск."
1108+
}
1109+
},
10961110
"ssl": {
10971111
"enable": {
10981112
"description": "Включить HTTPS для WebUI",

dashboard/src/i18n/locales/zh-CN/features/config-metadata.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,20 @@
10941094
"description": "信任代理请求头获取客户端 IP",
10951095
"hint": "关闭时忽略 X-Forwarded-For/X-Real-IP,仅使用连接地址。"
10961096
},
1097+
"auth_rate_limit": {
1098+
"enable": {
1099+
"description": "启用登录验证速率限制",
1100+
"hint": "关闭后将不对登录、TOTP 等身份验证接口进行速率限制。"
1101+
},
1102+
"average_interval": {
1103+
"description": "登录验证速率限制平均间隔(秒)",
1104+
"hint": "两次身份验证请求之间的最小平均间隔时间。例如设置为 1.0 表示每秒最多处理 1 个请求。"
1105+
},
1106+
"max_burst": {
1107+
"description": "登录验证速率限制最大突发数",
1108+
"hint": "允许的瞬时最大突发请求数。例如设置为 3 表示在短时间内最多连续处理 3 个请求。"
1109+
}
1110+
},
10971111
"ssl": {
10981112
"enable": {
10991113
"description": "启用 WebUI HTTPS",

0 commit comments

Comments
 (0)