security: P2 生产深度加固 — 路径/鉴权/脱敏/校验/持久化/过期
4.3 文件管理器路径收紧:
- 默认移除 Path('/') 全文件系统访问
- 仅允许项目目录 + data/ + 环境变量 SENSU_FILE_ROOTS 指定路径
3.4 插件路由鉴权修复:
- _check_plugin_auth 增加 panel_token 用户身份验证
- 先验证用户登录, 再检查插件权限
4.2 错误脱敏:
- security middleware 捕获异常 → 通用 'Internal server error'
- 堆栈详情仅写入日志, 不暴露给客户端
4.4 命令参数校验:
- POST /api/command 拒绝 shell 元字符 (;&|`$(){}!#~<>)
- 防止命令注入
4.5 Session 持久化:
- 登录/退出时保存到 SenSuDB.config_kv
- 框架重启后自动恢复已持久化会话
4.6 Token 过期: 24h → 2h
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,15 +1,28 @@
|
||||
from aiohttp import web
|
||||
from ..utils.auth import panel_auth
|
||||
import re
|
||||
|
||||
# Shell 元字符黑名单 — 防止命令注入
|
||||
_SHELL_DANGER = re.compile(r'[;&|`$(){}!#~<>]')
|
||||
|
||||
|
||||
def setup_routes(app, prefix=''):
|
||||
app.router.add_post(f'{prefix}/api/command', panel_auth(exec_cmd))
|
||||
|
||||
|
||||
async def exec_cmd(req):
|
||||
d = await req.json()
|
||||
raw = d.get('command', '')
|
||||
# 安全检查: 拒绝含 shell 元字符的命令
|
||||
if _SHELL_DANGER.search(raw):
|
||||
return web.json_response(
|
||||
{"success": False, "error": "命令包含不允许的字符"}, status=400
|
||||
)
|
||||
cs = req.app.get('service_manager').get_service("command")
|
||||
if not cs: return web.json_response({"error": "Missing"}, 503)
|
||||
if not cs:
|
||||
return web.json_response({"error": "Missing"}, status=503)
|
||||
try:
|
||||
res = await cs.execute_command(d.get('command',''))
|
||||
res = await cs.execute_command(raw)
|
||||
return web.json_response({"success": True, "output": str(res)})
|
||||
except Exception as e:
|
||||
return web.json_response({"success": False, "error": str(e)})
|
||||
except Exception:
|
||||
return web.json_response({"success": False, "error": "命令执行失败"})
|
||||
|
||||
Reference in New Issue
Block a user