feat: 重构控制面安全机制与分页规范

本次提交包含多项安全加固和体验优化:
1. 分离管理密钥与传输凭证,避免持 API Token 者伪造管理员票据
2. 钉钉回调地址增加白名单校验,防止开放重定向钓鱼
3. 初始管理员密码改为随机生成并日志记录,移除硬编码 123456
4. 新增登录防爆破计数表,支持 MySQL 多 worker 共享
5. 统一分页规范,新增 usePagedList composable 和 PagerBar 组件
6. 控制面 CORS 收敛为白名单,避免公网环境下任意站点跨域
7. 插件管理、凭证管理等列表接口支持分页和搜索
8. 强制初始密码修改,未改密前禁止使用管理功能
9. 定时任务触发被拒日志节流,避免授权失效时刷屏
10. 健康检查接口移除环境信息泄露,返回固定响应
This commit is contained in:
2026-09-23 15:19:54 +08:00
parent a1cf59f09c
commit 8a196d6c87
80 changed files with 9029 additions and 493 deletions
+29 -7
View File
@@ -6,9 +6,10 @@
"""
from __future__ import annotations
import ipaddress
import re
import secrets
from urllib.parse import quote, urlencode
from urllib.parse import quote, urlencode, urlparse
from fastapi import APIRouter, Header, HTTPException, Query
from fastapi.responses import RedirectResponse
@@ -23,6 +24,27 @@ from controlplane.core.notify import notify_account_created
router = APIRouter()
def _safe_back_url(url: str) -> bool:
"""回跳地址白名单:仅允许 http(s) + 回环 / 内网私有网段 / 配置的控制面域名。
防止开放重定向钓鱼(原实现仅校验 http(s) 前缀,可被引导到任意站点并携带身份信息)。
"""
if not url or not (url.startswith("http://") or url.startswith("https://")):
return False
host = (urlparse(url).hostname or "").lower()
if host in ("localhost", "127.0.0.1", "::1"):
return True
if config.DINGTALK_CALLBACK:
cb_host = (urlparse(config.DINGTALK_CALLBACK).hostname or "").lower()
if host == cb_host:
return True
try:
ip = ipaddress.ip_address(host)
return bool(ip.is_loopback or ip.is_private)
except ValueError:
return False
@router.post("/api/admin/dingtalk/auth")
def admin_dingtalk_auth(d: schemas.DingTalkIn,
x_api_token: str = Header(default="", alias="X-API-Token")) -> dict:
@@ -80,8 +102,8 @@ def dingtalk_scan(back: str = Query(..., description="扫码成功后的回跳
"""扫码入口:302 到钉钉扫码登录页;state 携带部署机回跳地址,扫码成功后由 /cb 带回。"""
if not config.DINGTALK_APP_KEY or not config.DINGTALK_APP_SECRET:
raise HTTPException(503, "未配置钉钉开放平台应用(DINGTALK_APP_KEY / DINGTALK_APP_SECRET)")
if not (back.startswith("http://") or back.startswith("https://")):
raise HTTPException(400, "回跳地址非法(防止开放重定向)")
if not _safe_back_url(back):
raise HTTPException(400, "回跳地址非法(仅允许回环/内网/控制面域名,防止开放重定向)")
if not config.DINGTALK_CALLBACK:
raise HTTPException(400, "未配置 DINGTALK_CALLBACK(钉钉后台登记的回调地址)")
login_url = "https://login.dingtalk.com/oauth2/challenge.htm?" + urlencode({
@@ -97,8 +119,8 @@ def dingtalk_scan(back: str = Query(..., description="扫码成功后的回跳
@router.get("/api/admin/dingtalk/cb")
def dingtalk_cb(code: str = Query(...), state: str = Query(...)) -> RedirectResponse:
"""钉钉回调:换 token 拉工号/姓名后,302 回部署机(state 里的回跳地址)携带身份。"""
if not (state.startswith("http://") or state.startswith("https://")):
raise HTTPException(400, "非法回跳地址")
if not _safe_back_url(state):
raise HTTPException(400, "非法回跳地址(仅允许回环/内网/控制面域名)")
info = _dingtalk_userinfo(code)
qs = urlencode({"account": info["account"] or "", "nickname": info["nickname"],
"unionid": info["unionid"] or ""})
@@ -144,8 +166,8 @@ def dingtalk_login_url(back: str = Query(...),
_check_token(x_api_token)
if not config.DINGTALK_APP_KEY or not config.DINGTALK_APP_SECRET:
raise HTTPException(503, "控制面未配置钉钉开放平台应用(DINGTALK_APP_KEY / DINGTALK_APP_SECRET)")
if not (back.startswith("http://") or back.startswith("https://")):
raise HTTPException(400, "回跳地址非法(防止开放重定向)")
if not _safe_back_url(back):
raise HTTPException(400, "回跳地址非法(仅允许回环/内网/控制面域名)")
login_url = "https://login.dingtalk.com/oauth2/challenge.htm?" + urlencode({
"redirect_uri": back,
"response_type": "code",