公网临时链接改为不透明短 ID(/p/<22字符>),不再暴露文件名/会话名/签名

- public-files.js(新): 内存表 id → (会话, 相对路径, 过期时间);mint 顺手清过期项并兜底上限
- routes/public-files.js(新): GET /p/<id> 匿名取用(查表 → resolveInTask 复核 → sendDownload),未命中/过期一律 404
- routes/public-link.js: 不再签 HMAC,改为 mint 短 ID,返回 http://<IP>:8090/p/<id>(约 50 字符、纯 ASCII)
  动因:原 URL 把文件名写了两遍再挂 64 位签名,共 300+ 字符;它会被模型抄进工具参数、并长期留在会话历史每轮重发
- download-send.js(新): 从 files.js 抽出 sendDownload/downloadDisposition,供浏览器下载与公网取用共用
- server.js: /p 挂在 auth.middleware 之前(匿名可达);浏览器下载那套签名链接保持不动
- docs: DEPLOY.md 4.1 / ARCHITECTURE.md(文件树、路由表、约束)/ README.md 同步
This commit is contained in:
2026-09-15 21:27:43 +08:00
parent e3d553c1af
commit 3f4ab51ea5
9 changed files with 170 additions and 55 deletions
+2 -26
View File
@@ -117,32 +117,8 @@ router.get('/:id/files/download-token', function (req, res) {
}
});
// Content-Disposition 手工构造,绕开 content-disposition 包的 ASCII 兜底:
// 该包会把中文名逐字替换成 ?(???.pdf),且 filename= 后紧跟 filename*= 会让
// 老 AOSP DownloadManager 的正则解析失败而改从 URL 猜名(download.pdf)。
// 这里传统 filename= 直接放百分号编码的 UTF-8:朴素解析器解码后能得到真名,
// 标准客户端照旧优先 filename*(RFC 5987)。
function downloadDisposition(name) {
const legacy = encodeURIComponent(name);
// RFC 5987 attr-char 不含 ' ( ) ! *,ext-value 里需额外编码
const ext = legacy.replace(/['()!*]/g, function (c) {
return '%' + c.charCodeAt(0).toString(16).toUpperCase();
});
return 'attachment; filename="' + legacy + '"; filename*=UTF-8\'\'' + ext;
}
// 下载统一响应:禁用一切缓存协商。
// 默认的 ETag/Last-Modified + Cache-Control: public, max-age=0 会诱导移动端浏览器
// 先发条件请求拿 304 空体,下载管理器随后卡在“等待中”(桌面 Safari/curl 不受影响)。
// 不能用 res.download:它会用 content-disposition 包覆盖上面的文件名策略;
// res.sendFile 不设置 Content-Disposition,Content-Type 按扩展名自动出,Range 由 send 支持。
function sendDownload(res, abs) {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
res.setHeader('Content-Disposition', downloadDisposition(path.basename(abs)));
res.sendFile(abs, { lastModified: false, etag: false, maxAge: 0 });
}
// Content-Disposition / 下载响应统一实现在 ../download-send.js(公网临时文件取用也用它)
const { downloadDisposition, sendDownload } = require('../download-send');
// 下载核心(支持子目录路径,前端用 ?path=xxx 传入相对路径)
function serveDownload(req, res) {
+38
View File
@@ -0,0 +1,38 @@
// 公网临时文件的取用端点:GET /p/<id>(id 由 routes/public-link.js 在内网现签)。
//
// 这一跳的访问者是**外部服务**(如智谱服务器来拉参考图),它不带 cookie、不带 Authorization,
// 所以必须匿名可达。安全性完全来自:
// 1. id 是 22 字符随机串(crypto.randomBytes(16) base64url),不可枚举;
// 2. 服务端按时过期(public_link.ttl_ms),过期即 404;
// 3. 只吐"签发时已校验过、且落在该会话任务目录内"的文件,这里再用 resolveInTask 复核一次。
//
// 挂在 auth.middleware **之前**(server.js),所以登录与否都能访问;未命中一律 404,不吐内部信息。
'use strict';
const fs = require('fs');
const express = require('express');
const engine = require('../engine');
const publicFiles = require('../public-files');
const { resolveInTask } = require('../tools/executor');
const { sendDownload } = require('../download-send');
const log = require('../log');
const router = express.Router();
router.get('/:id', function (req, res) {
const entry = publicFiles.resolve(req.params.id);
if (!entry) return res.status(404).type('text/plain').send('not found or expired\n');
let abs;
try {
abs = resolveInTask(engine.taskDirOf(entry.sessionId), entry.rel);
if (!fs.statSync(abs).isFile()) throw new Error('not a file');
} catch (_) {
return res.status(404).type('text/plain').send('not found\n');
}
log.info('public-file', entry.sessionId + ' 外部取用:' + entry.rel);
sendDownload(res, abs);
});
module.exports = router;
+10 -13
View File
@@ -2,9 +2,10 @@
//
// 用途:把任务目录里的文件变成一条短命的公网 URL,交给外部服务去取。第一个消费者是
// MCP video_gen:智谱服务器生成视频前要自己来拉参考图,它不带 cookie、不带 Authorization,
// 所以必须给它一条「不可猜 + 会过期」的链接 —— 这正是 files.js 一直在用的签名下载链接
// (auth.js 的 isSignedDownload 已放行带 ?t= 的 /files/download* 请求)。
// 这里解决的唯一问题:**沙箱里的 AI 怎么拿到一条签名**(签发接口要登录,executor 里没有凭据)。
// 所以必须给它一条「不可猜 + 会过期」的链接。
// 产出的形态是一个不透明短 ID(见 public-files.js 为什么不用签名 URL):
// http://<IP>:8090/p/9fQ2mZ7xK4tR1vBn
// 这解决的是:**沙箱里的 AI 怎么拿到一条公网链接**(签发接口要登录,executor 里没有凭据)。
//
// 为什么单独一个端口、且不映射到宿主机:
// 这条接口没法用凭据保护(AI 的命令带不了凭据),那就干脆让外部**连都连不上**。
@@ -12,8 +13,8 @@
//
// 边界(改这里务必保持):
// 1. 只接受绝对路径,且必须落在 workspace_root 内;再反查会话表确认会话真实存在。
// 2. 取 realpath 复核仍在任务根内:挡住"先 ln -s 一个外泄目标,再签它的链接"。
// 3. 只签"已存在且是文件"的路径;签出来的链接由 files.js 的 serveDownload 再验一遍。
// 2. 取 realpath 复核仍在任务根内:挡住“先 ln -s 一个外泄目标,再签它的链接”。
// 3. 只签“已存在且是文件”的路径;取用侧(routes/public-files.js)会再复核一次目录边界。
// 4. 返回 text/plain 而不是 JSON:AI 用 `curl -s` 拿到的 stdout 就是现成 URL,免解析。
'use strict';
@@ -21,7 +22,7 @@ const path = require('path');
const fs = require('fs');
const express = require('express');
const engine = require('../engine');
const { signDownload } = require('../download-token');
const publicFiles = require('../public-files');
const log = require('../log');
const router = express.Router();
@@ -62,14 +63,10 @@ router.get('/public-link', function (req, res) {
return bad(res, 400, 'path 经符号链接指向了任务根之外,拒绝签发');
}
const expires = Date.now() + pl.ttl_ms;
const t = signDownload(sid, fileRel, expires);
const url = pl.base_url + '/api/sessions/' + encodeURIComponent(sid)
+ '/files/download-name/' + encodeURIComponent(path.basename(fileRel))
+ '?path=' + encodeURIComponent(fileRel)
+ '&expires=' + expires + '&t=' + t;
const id = publicFiles.mint({ sessionId: sid, rel: fileRel, ttlMs: pl.ttl_ms });
const url = pl.base_url + '/p/' + id;
log.info('public-link', sid + ' 签发公网链接(' + Math.round(pl.ttl_ms / 60000) + ' 分钟):' + fileRel);
log.info('public-link', sid + ' 签发公网链接(' + Math.round(pl.ttl_ms / 60000) + ' 分钟):' + fileRel + ' → /p/' + id);
res.type('text/plain').send(url + '\n');
});