Files
ny-exam-platform/docs/security/hardening.md
T
nxxy335 a3be81b261 fix(security): 修复上传名单文件可被 URL 直连访问(nginx 下可 getshell)
严重漏洞:roster_import 使用 $_FILES 的原始扩展名拼 roster_YYYYMMDD_HHMMSS.<ext>
落在 Web 根目录内的 data/uploads/,既无扩展名白名单(可上传 .php),文件名又
可按秒枚举;而 bootstrap 只写了 data/.htaccess,nginx 不读 .htaccess,
生产环境(nginx + PHP-FPM)下可直接远程代码执行。

修复(api.php roster_import 四层防护):
- 扩展名白名单:仅 xlsx / xlsm / csv / txt
- 随机文件名:bin2hex(random_bytes(16)),杜绝枚举
- 解析完成后 @unlink 立即删除,Web 目录下不留存任何上传文件
- 20MB 大小限制 + UPLOAD_ERR 检查

加固(bootstrap.php):data 及 cache/uploads/snapshots 每个目录都写
.htaccess(Deny from all)+ index.html 占位;README 与文档补充 nginx
location ^~ /data/ { deny all; return 404; } 规则。

测试:新增 e2e tools/test-upload-guard.js(11 断言,覆盖 .php/.phtml 被拒、
无残留、合法 csv 导入成功且即删、无可枚举文件名),并入 tests/run-all.sh。
全套 90 项通过。

docs: 新增 VitePress 文档站(18 页)
- 使用指南 12 页:简介/部署/快速上手/系统设置/名单管理/统计口径/试卷设置/
  快照趋势异常/导出打印/数据看板/随机抽取/常见问题
- 安全 2 页:安全总览(含本次漏洞复盘)与部署加固清单(P0-P2)
- 开发 4 页:架构与目录/接口一览/SQLite 存储/测试
- 配图迁移至 docs/public/images/;.gitignore 忽略文档构建产物
2026-09-10 22:42:13 +08:00

157 lines
3.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 部署加固清单
按 **P0 → P2** 顺序执行。P0 不做等于把配置和名单暴露在公网上。
## P0:屏蔽数据目录(必做)
`data/` 位于 Web 根目录内,里面有 `ny.db`(配置 / 名单 / 卷王账号密码)、
`config.json`、`roster.json`、令牌缓存与快照。**必须禁止直接访问。**
### nginx(生产环境默认,必做)
```nginx
location ^~ /data/ {
deny all;
return 404;
}
```
若站点部署在子路径下(例如 `/index/`),改为对应的前缀:
```nginx
location ^~ /index/data/ {
deny all;
return 404;
}
```
```bash
nginx -t && nginx -s reload
```
验证(应返回 404 或 403,而不是文件内容):
```bash
curl -I https://你的域名/data/config.json
```
### Apache
仓库已自动生成 `.htaccess`。仍需确认:
```apache
<Directory "/path/to/ny-exam-platform/data">
Require all denied
</Directory>
```
且虚拟主机的 `AllowOverride` 不为 `None`。
### PHP 内置服务器(开发环境)
**内置服务器既不解析 `.htaccess`,也没有 location 规则**,开发时不要把 8080 端口暴露到公网。
## P0:排查是否已被上传过 Webshell
如果你运行过修复前的版本,请立即检查:
```bash
ls -la data/uploads/
```
- 正常情况:目录为空(新版本解析后立即删除);
- 若存在 `.php` / `.phtml` / `.phar` / `.htaccess` 之外的可疑文件,立即删除;
- 检查 nginx access 日志中是否有对 `/data/uploads/*.php` 的 `POST` 请求;
- 如确认被利用,请**轮换卷王后台密码**并重新部署干净的目录。
## P1:全站 HTTPS
```nginx
server {
listen 80;
server_name sj.example.com;
return 301 https://$host$request_uri;
}
```
登录密码经 RSA 加密后再提交,但令牌 Cookie 在 HTTP 下可被中间人截获。
## P1:限制上传体积
与后端 20 MB 校验保持一致:
```nginx
client_max_body_size 20m;
```
PHP 侧:
```ini
upload_max_filesize = 20M
post_max_size = 24M
```
## P1:关闭信息泄漏
```nginx
server_tokens off;
```
```ini
expose_php = Off
display_errors = Off
log_errors = On
```
## P1:PHP-FPM 最小权限
- PHP-FPM 进程用户(如 `www`)只对 `data/` 有写权限,其它目录只读;
- 禁止在 Web 根目录放 `.git`:
```nginx
location ~ /\.git {
deny all;
return 404;
}
```
## P2:来源 IP 限制(内网场景)
```nginx
allow 10.0.0.0/8;
allow 192.168.0.0/16;
deny all;
```
## P2:定期备份
备份 `data/` 目录即可,包含:
| 文件 | 内容 |
| --- | --- |
| `data/ny.db` | 配置、名单、每卷配置(SQLite) |
| `data/config.json` | 配置(文件备份) |
| `data/roster.json` | 名单(文件备份) |
| `data/snapshots/` | 成绩快照 |
## P2:改掉卷王默认口令
卷王后台默认 `admin / 123456` 请务必修改,并在平台 **设置** 中同步更新。
## 加固自检清单
- [ ] `curl -I https://域名/data/config.json` 返回 404 / 403
- [ ] `curl -I https://域名/data/ny.db` 返回 404 / 403
- [ ] 上传一个 `.php` 后缀文件被拒绝
- [ ] 正常导入 `.xlsx` 后 `data/uploads/` 为空
- [ ] 全站 HTTPS,HTTP 自动跳转
- [ ] `server_tokens off`、`expose_php = Off`
- [ ] `data/` 可写,其它目录只读
- [ ] 卷王后台已改默认密码
- [ ] `data/` 已纳入定期备份
## 相关
- [安全总览](./index.md)
- [环境要求与部署](../guide/install.md)