公网临时链接改为不透明短 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:
@@ -0,0 +1,34 @@
|
||||
// 单文件下载响应的公共实现:任务文件的浏览器下载(routes/files.js)与公网临时文件
|
||||
// (routes/public-files.js)共用,避免两处各写一套文件名/缓存策略。
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
|
||||
// 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 });
|
||||
}
|
||||
|
||||
module.exports = { downloadDisposition, sendDownload };
|
||||
@@ -0,0 +1,56 @@
|
||||
// 公网临时文件:短 ID 能力链接。
|
||||
//
|
||||
// 为什么不用签名 URL:外部服务(如智谱)那一跳只需要「随机不可猜 + 会过期」,而签名 URL 必须把
|
||||
// 会话名、文件名、过期时间、64 位签名全塞进 URL —— 又长又满是 %XX,模型抄进工具参数容易抄错,
|
||||
// 而且这条 URL 会留在会话历史里每轮重发,白烧 token。这里只发一个 22 字符的随机 ID:
|
||||
// http://<IP>:8090/p/9fQ2mZ7xK4tR1vBn
|
||||
//
|
||||
// 状态放本进程内存:重启即全部失效 —— 与签名方案语义一致(签名密钥本来就是进程内随机的),
|
||||
// 所以不是退步,而是把「不可猜」从 HMAC 换成了随机 ID 本身。
|
||||
//
|
||||
// 边界:只有内网签发入口(routes/public-link.js,不映射宿主机)能 mint;
|
||||
// 取用侧(routes/public-files.js)只按 ID 查表,查不到/过期一律 404。
|
||||
'use strict';
|
||||
|
||||
const crypto = require('crypto');
|
||||
|
||||
const DEFAULT_TTL_MS = 30 * 60 * 1000;
|
||||
const MAX_ENTRIES = 1000; // 兜底上限:内网签发,正常远达不到
|
||||
|
||||
const entries = new Map(); // id -> { sessionId, rel, expires }
|
||||
|
||||
function sweep(now) {
|
||||
for (const [id, e] of entries) {
|
||||
if (e.expires <= now) entries.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
function mint({ sessionId, rel, ttlMs }) {
|
||||
const now = Date.now();
|
||||
sweep(now);
|
||||
while (entries.size >= MAX_ENTRIES) {
|
||||
// 满了就丢最老的(Map 保持插入顺序)
|
||||
entries.delete(entries.keys().next().value);
|
||||
}
|
||||
const id = crypto.randomBytes(16).toString('base64url'); // 22 字符,URL 安全
|
||||
entries.set(id, {
|
||||
sessionId,
|
||||
rel,
|
||||
expires: now + (Number.isFinite(ttlMs) && ttlMs > 0 ? ttlMs : DEFAULT_TTL_MS),
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
// 命中且未过期才返回 {sessionId, rel, expires};过期即顺手删掉
|
||||
function resolve(id) {
|
||||
const key = String(id || '');
|
||||
const e = entries.get(key);
|
||||
if (!e) return null;
|
||||
if (e.expires <= Date.now()) {
|
||||
entries.delete(key);
|
||||
return null;
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
module.exports = { mint, resolve };
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
@@ -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');
|
||||
});
|
||||
|
||||
|
||||
@@ -31,6 +31,11 @@ app.use(express.json({ limit: '2mb' }));
|
||||
// 相比 nginx htpasswd 的 basic auth,cookie 在手机浏览器原生下载时也会自动携带,下载不再失败。
|
||||
const auth = createAuth(cfg);
|
||||
app.use(auth.router); // /login、/logout、/api/login 等免认证端点
|
||||
|
||||
// 公网临时文件的取用端点(GET /p/<id>):匿名可达,必须挂在 auth.middleware 之前。
|
||||
// 安全性来自“22 字符随机 ID + 服务端过期”,详见 routes/public-files.js 头注释。
|
||||
app.use('/p', require('./routes/public-files'));
|
||||
|
||||
app.use(auth.middleware); // 其余请求需登录态(cookie 或 Basic 头)
|
||||
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
Reference in New Issue
Block a user