security: P3+ — 面板密码哈希 + 上传类型白名单 + CSRF Token

面板密码安全:
- 支持哈希存储 (password_hash/password_salt)
- 登录使用 secrets.compare_digest 时序安全比较
- 回退明文兼容旧配置
- 默认密码 admin 时打印 CRITICAL 警告

文件上传安全:
- 拒绝危险扩展名: .exe/.dll/.so/.sh/.bat/.ps1 等
- 拒绝敏感文件名: .htaccess/Makefile/Dockerfile 等
- 上传时检查并返回 403

CSRF 防护:
- panel_auth(csrf_protect=True) 参数
- 写操作需 X-CSRF-Token header 匹配 session token
- 保护范围: 文件删除/写入/创建/上传/重命名 + 项目启停 + 代理管理

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
qinglong
2026-06-13 14:07:38 +08:00
parent d835b66ca9
commit 2da52e80d0
8 changed files with 84 additions and 29 deletions
+19 -10
View File
@@ -4,31 +4,40 @@
import functools
from aiohttp import web
def panel_auth(handler):
"""面板专用鉴权装饰器基于面板自有的 Session Store 验证"""
def panel_auth(handler, csrf_protect: bool = False):
"""面板专用鉴权装饰器基于面板自有的 Session Store 验证
csrf_protect=True 时额外检查 X-CSRF-Token 头 (用于写操作)
"""
@functools.wraps(handler)
async def wrapper(request, *args, **kwargs):
# 1. 获取 Token
token = request.cookies.get("panel_token")
if not token and request.headers.get("Authorization", "").startswith("Bearer "):
token = request.headers["Authorization"].split(" ", 1)[1]
is_valid = False
# 2. 从面板 Session Store 验证
session_store = request.app.get('panel_session_store', {})
if token and token in session_store:
is_valid = True
# 验证通过,将用户信息注入 request 供后续使用
request['user'] = session_store[token]
# 3. 拦截逻辑 (不再依赖外部 AuthService,确保安全隔离)
# 3. 拦截逻辑
if not is_valid:
# 返回 401 并附带提示,前端可据此判断状态
return web.json_response({
"error": "未认证或会话已过期",
"error": "未认证或会话已过期",
"status": 401
}, status=401)
# 4. CSRF 检查 — 写操作需要 X-CSRF-Token 头 (同 token)
if csrf_protect:
csrf = request.headers.get("X-CSRF-Token", "")
if csrf != token:
return web.json_response({
"error": "CSRF 验证失败",
"status": 403,
}, status=403)
return await handler(request, *args, **kwargs)
return wrapper