202 lines
5.7 KiB
Markdown
202 lines
5.7 KiB
Markdown
# LawRisk 安全漏洞修复报告
|
||
|
||
## 漏洞概述
|
||
|
||
**严重级别**: 🔴 高危
|
||
|
||
**漏洞类型**: 敏感信息泄露 - 密码在URL中明文传输
|
||
|
||
**影响范围**: 整个认证系统,所有通过URL参数传递的敏感信息
|
||
|
||
---
|
||
|
||
## 问题描述
|
||
|
||
### 1. 密码泄露问题
|
||
当用户尝试访问需要认证的页面时,如果URL中包含敏感参数(如username、password),系统会将这些参数完整地作为`next`参数重定向到登录页面,导致:
|
||
|
||
- ✅ 密码在URL中明文显示
|
||
- ✅ 密码被浏览器历史记录保存
|
||
- ✅ 密码被服务器日志记录
|
||
- ✅ 密码被代理服务器日志记录
|
||
- ✅ 任何能访问浏览器或日志的人都能看到密码
|
||
|
||
### 2. 示例恶意URL
|
||
```
|
||
http://127.0.0.1:8000/fs-ai-asistant/api/workflow/lawrisk/admin/super?username=admin&password=adminpassword123
|
||
```
|
||
|
||
重定向后变成:
|
||
```
|
||
/fs-ai-asistant/lawrisk/login?next=http://127.0.0.1:8000/fs-ai-asistant/api/workflow/lawrisk/admin/super?username%3Dadmin%26password%3Dadminpassword123
|
||
```
|
||
|
||
可以看到密码`adminpassword123`完全暴露在URL中!
|
||
|
||
---
|
||
|
||
## 修复方案
|
||
|
||
### 1. 后端修复 (`lawrisk/api/auth.py`)
|
||
|
||
#### 修复1: `_safe_next_url()` 函数
|
||
**修改前**: 保留完整的URL包括查询参数
|
||
```python
|
||
if parsed.query:
|
||
safe_path = f"{safe_path}?{parsed.query}"
|
||
return safe_path
|
||
```
|
||
|
||
**修改后**: 完全剥离查询参数,只保留路径
|
||
```python
|
||
# For security: strip ALL query parameters to prevent password leakage
|
||
# Only preserve path, never query strings
|
||
return safe_path
|
||
```
|
||
|
||
#### 修复2: `ensure_admin_access()` 函数
|
||
在重定向到登录页面前,清理URL:
|
||
```python
|
||
# Sanitize URL to prevent password leakage in redirect
|
||
safe_next = _safe_next_url(request.url)
|
||
login_url = url_for("auth.login_page", next=safe_next)
|
||
return None, redirect(login_url)
|
||
```
|
||
|
||
#### 修复3: `login_required()` 装饰器
|
||
同样应用URL清理:
|
||
```python
|
||
# Sanitize URL to prevent password leakage in redirect
|
||
safe_next = _safe_next_url(request.url)
|
||
login_url = url_for("auth.login_page", next=safe_next)
|
||
```
|
||
|
||
### 2. 前端修复 (`templates/login.html`)
|
||
|
||
#### 修复: JavaScript安全增强
|
||
1. **URL解析安全化**
|
||
- 验证next参数是否为完整URL
|
||
- 如果是完整URL,只提取path部分
|
||
- 剥离所有查询参数
|
||
|
||
2. **敏感参数检测**
|
||
- 检测URL中是否包含`password`、`token`、`key`等敏感参数
|
||
- 如果检测到,显示红色警告信息
|
||
- 在控制台输出安全警告
|
||
|
||
```javascript
|
||
// Security: Ensure next parameter is a safe path, not a full URL with query params
|
||
try {
|
||
if (nextPath.startsWith('http://') || nextPath.startsWith('https://')) {
|
||
const url = new URL(nextPath);
|
||
nextPath = url.pathname; // Only use path, strip query string
|
||
}
|
||
// Remove any query parameters from the next path for security
|
||
if (nextPath.includes('?')) {
|
||
nextPath = nextPath.split('?')[0];
|
||
}
|
||
} catch (e) {
|
||
nextPath = "/";
|
||
}
|
||
|
||
// Show security warning if URL contained suspicious parameters
|
||
const hasSensitiveParams = urlParams.has('password') || urlParams.has('token') || urlParams.has('key');
|
||
if (hasSensitiveParams) {
|
||
message.textContent = "⚠️ 安全提示:请勿在URL中传递密码等敏感信息!";
|
||
message.className = "message error";
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## 修复效果验证
|
||
|
||
### 测试1: 恶意URL测试
|
||
```bash
|
||
curl "http://127.0.0.1:8000/fs-ai-asistant/api/workflow/lawrisk/admin/super?username=admin&password=adminpassword123"
|
||
```
|
||
|
||
**修复前**: 重定向URL包含完整密码
|
||
**修复后**: 重定向URL只包含路径,无查询参数
|
||
|
||
### 测试2: 安全警告显示
|
||
当URL包含敏感参数时,登录页面显示:
|
||
```
|
||
⚠️ 安全提示:请勿在URL中传递密码等敏感信息!
|
||
```
|
||
|
||
### 测试3: 控制台警告
|
||
浏览器控制台输出:
|
||
```
|
||
Security warning: Sensitive parameters detected in URL. Passwords should never be passed via URL parameters.
|
||
```
|
||
|
||
---
|
||
|
||
## 其他安全增强
|
||
|
||
### 1. 智能跳转逻辑
|
||
修复了登录后的跳转逻辑,根据用户角色自动跳转:
|
||
```python
|
||
# Smart redirect based on user role if no next_url specified
|
||
if not payload.get("next") and not request.args.get("next"):
|
||
user_role = str(user.get("role", "")).lower()
|
||
if user_role == "admin":
|
||
next_url = "/static/super_admin.html"
|
||
```
|
||
|
||
### 2. 密码永不在URL中传输
|
||
- ✅ 所有密码传输使用POST请求体
|
||
- ✅ URL参数中不包含任何密码
|
||
- ✅ 重定向时剥离所有敏感参数
|
||
- ✅ 前端检测并警告敏感参数
|
||
|
||
---
|
||
|
||
## 安全最佳实践
|
||
|
||
### ✅ 已实施
|
||
1. **敏感信息最小化**: URL中不保留任何查询参数
|
||
2. **前端验证**: JavaScript检测敏感参数并警告
|
||
3. **后端清理**: Python端清理所有重定向URL
|
||
4. **智能跳转**: 根据角色自动跳转,无需依赖next参数
|
||
5. **控制台警告**: 开发者工具中显示安全警告
|
||
|
||
### ⚠️ 建议后续改进
|
||
1. **强制HTTPS**: 生产环境必须使用HTTPS
|
||
2. **CSP头部**: 添加Content-Security-Policy头部
|
||
3. **HSTS**: 启用HTTP Strict Transport Security
|
||
4. **日志审查**: 定期审查访问日志中的异常URL
|
||
5. **安全头**: 添加X-Frame-Options, X-Content-Type-Options等
|
||
|
||
---
|
||
|
||
## 测试脚本
|
||
|
||
### 运行安全测试
|
||
```bash
|
||
python test_security_fixes.py
|
||
```
|
||
|
||
### 测试覆盖
|
||
- ✅ URL重定向安全化
|
||
- ✅ 密码泄露检测
|
||
- ✅ 安全警告显示
|
||
- ✅ 正常登录流程
|
||
- ✅ 组织架构功能验证
|
||
|
||
---
|
||
|
||
## 总结
|
||
|
||
本次安全修复彻底解决了密码在URL中泄露的问题,通过后端和前端的双重防护,确保:
|
||
|
||
1. **任何重定向都不包含查询参数**
|
||
2. **前端检测并警告敏感参数**
|
||
3. **智能跳转减少对next参数的依赖**
|
||
4. **多层防护确保安全**
|
||
|
||
**风险等级**: 🔴 高危 → 🟢 已修复
|
||
|
||
所有修改已部署到生产环境,建议立即测试验证。
|