公网临时链接图片改 inline 返回:点开直接看图,不再一律下载
/p/<id> 原先不分类型一律 attachment(为安卓封装壳的 DownloadManager 兜底),图片外链点开只会进下载器;read_file 读图也走这个端点,用户点开来只能下载、看不到图。 - download-send.js 抽出 contentDisposition(type),新增 inlineImageType / sendInlineImage - 图片显式给 image/* + X-Content-Type-Options: nosniff + inline;缓存仍 no-store(/p/ 是短命 bearer 链) - 按扩展名判定:png/jpg/jpeg/webp/gif/bmp/tif/tiff/avif/ico/svg,不读文件头做嗅探 - SVG 额外加 CSP: sandbox(匿名同源端点,直接打开不能执行内嵌脚本;img 引用不受影响) - inline 走 dotfiles:allow,保证 read_file 的 .imgcache/ 压缩产物能取到 - 其余类型(PDF/Office/zip/HTML 等)保持 attachment,下载语义与安卓兜底不变 - README「公网临时链接」补响应头说明
This commit is contained in:
@@ -360,6 +360,7 @@ http://<host>/p/<id>
|
||||
- 只由 core 进程内的工具签发,签发前校验文件在任务目录内且 `realpath` 未越界。
|
||||
- 默认 30 分钟过期,可配置 1 分钟到 24 小时。
|
||||
- 链接可重复使用,直到过期;服务重启后全部失效。
|
||||
- 响应头按类型分开:图片(`png/jpg/webp/gif/bmp/tif/avif/ico/svg`)返回 `inline` + 对应 `image/*`,点开链接直接看图;其余类型返回 `attachment`,交给浏览器/WebView 下载(SVG 额外带 `Content-Security-Policy: sandbox`,禁止内嵌脚本)。
|
||||
- 任何人拿到链接即可匿名访问,不要对含密钥的文件签发。
|
||||
|
||||
### HTTPS
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// 单文件下载响应的公共实现:任务文件的浏览器下载(routes/files.js)与公网临时文件
|
||||
// 单文件下载 / 内联响应的公共实现:任务文件的浏览器下载(routes/files.js)、公网临时文件
|
||||
// (routes/public-files.js)共用,避免两处各写一套文件名/缓存策略。
|
||||
'use strict';
|
||||
|
||||
@@ -9,13 +9,37 @@ const path = require('path');
|
||||
// 老 AOSP DownloadManager 的正则解析失败而改从 URL 猜名(download.pdf)。
|
||||
// 这里传统 filename= 直接放百分号编码的 UTF-8:朴素解析器解码后能得到真名,
|
||||
// 标准客户端照旧优先 filename*(RFC 5987)。
|
||||
function downloadDisposition(name) {
|
||||
function contentDisposition(name, type) {
|
||||
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;
|
||||
return type + '; filename="' + legacy + '"; filename*=UTF-8\'\'' + ext;
|
||||
}
|
||||
|
||||
function downloadDisposition(name) {
|
||||
return contentDisposition(name, 'attachment');
|
||||
}
|
||||
|
||||
// 内联图片判定:只看扩展名,不读文件头——公网端点不做内容嗅探,也不该为了“猜类型”整包读文件。
|
||||
// svg 也算:浏览器只有拿到 image/svg+xml 才会渲染而不是下载(脚本风险由下面的 CSP sandbox 兜住)。
|
||||
const INLINE_IMAGE_TYPES = new Map([
|
||||
['.png', 'image/png'],
|
||||
['.jpg', 'image/jpeg'],
|
||||
['.jpeg', 'image/jpeg'],
|
||||
['.webp', 'image/webp'],
|
||||
['.gif', 'image/gif'],
|
||||
['.bmp', 'image/bmp'],
|
||||
['.tif', 'image/tiff'],
|
||||
['.tiff', 'image/tiff'],
|
||||
['.avif', 'image/avif'],
|
||||
['.ico', 'image/x-icon'],
|
||||
['.svg', 'image/svg+xml'],
|
||||
]);
|
||||
|
||||
function inlineImageType(abs) {
|
||||
return INLINE_IMAGE_TYPES.get(path.extname(String(abs || '')).toLowerCase()) || null;
|
||||
}
|
||||
|
||||
// 下载统一响应:禁用一切缓存协商。
|
||||
@@ -23,12 +47,32 @@ function downloadDisposition(name) {
|
||||
// 先发条件请求拿 304 空体,下载管理器随后卡在“等待中”(桌面 Safari/curl 不受影响)。
|
||||
// 不能用 res.download:它会用 content-disposition 包覆盖上面的文件名策略;
|
||||
// res.sendFile 不设置 Content-Disposition,Content-Type 按扩展名自动出,Range 由 send 支持。
|
||||
function sendDownload(res, abs) {
|
||||
function noStore(res) {
|
||||
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
|
||||
res.setHeader('Pragma', 'no-cache');
|
||||
res.setHeader('Expires', '0');
|
||||
}
|
||||
|
||||
function sendDownload(res, abs) {
|
||||
noStore(res);
|
||||
res.setHeader('Content-Disposition', downloadDisposition(path.basename(abs)));
|
||||
res.sendFile(abs, { lastModified: false, etag: false, maxAge: 0 });
|
||||
}
|
||||
|
||||
module.exports = { downloadDisposition, sendDownload };
|
||||
// 内联图片响应(公网链 GET /p/<id> 的图片分支):点开链接直接显示,不再被当成附件下载。
|
||||
// 安卓封装壳 / WebView 对 attachment 一律交系统下载器,图片外链因此“只下载、不打开”;
|
||||
// inline + 明确的 image/* 才走渲染路径。缓存仍 no-store:/p/<id> 是短命 bearer 链,
|
||||
// 不该被中间缓存留副本(与下载同语义)。
|
||||
function sendInlineImage(res, abs, type) {
|
||||
noStore(res);
|
||||
res.setHeader('Content-Type', type);
|
||||
res.setHeader('X-Content-Type-Options', 'nosniff');
|
||||
res.setHeader('Content-Disposition', contentDisposition(path.basename(abs), 'inline'));
|
||||
// SVG 是唯一能带脚本的“图片”,而 /p/ 是匿名同源端点:直接打开会以本应用身份执行脚本。
|
||||
// sandbox 不给 allow-scripts,退化成不可执行、无同源权限的文档;<img> 引用本就不执行脚本,不受影响。
|
||||
if (type === 'image/svg+xml') res.setHeader('Content-Security-Policy', 'sandbox');
|
||||
// dotfiles 显式放行:read_file 的压缩产物在 .imgcache/(点目录),默认策略对其他调用方不保证。
|
||||
res.sendFile(abs, { dotfiles: 'allow', lastModified: false, etag: false, maxAge: 0 });
|
||||
}
|
||||
|
||||
module.exports = { contentDisposition, downloadDisposition, sendDownload, sendInlineImage, inlineImageType };
|
||||
|
||||
@@ -7,6 +7,10 @@
|
||||
// 3. 只吐"签发时已校验过、且落在该会话任务目录内"的文件,这里再用 resolveInTask 复核一次。
|
||||
//
|
||||
// 挂在 auth.middleware **之前**(server.js),所以登录与否都能访问;未命中一律 404,不吐内部信息。
|
||||
//
|
||||
// 响应头按扩展名分两路:图片(png/jpg/webp/gif/.../svg)走 inline + 明确 image/*,
|
||||
// 用户或外部服务点开链接就能直接看图;其余一律 attachment,交给下载器
|
||||
// (安卓封装壳的 DownloadManager 只认 attachment,下载语义不能改)。
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
@@ -14,7 +18,7 @@ const express = require('express');
|
||||
const engine = require('../engine');
|
||||
const publicFiles = require('../public-files');
|
||||
const { resolveInTask } = require('../tools/executor');
|
||||
const { sendDownload } = require('../download-send');
|
||||
const { sendDownload, sendInlineImage, inlineImageType } = require('../download-send');
|
||||
const log = require('../log');
|
||||
|
||||
const router = express.Router();
|
||||
@@ -31,7 +35,12 @@ router.get('/:id', function (req, res) {
|
||||
return res.status(404).type('text/plain').send('not found\n');
|
||||
}
|
||||
|
||||
log.info('public-file', entry.sessionId + ' 外部取用:' + entry.rel);
|
||||
// 图片单独走 inline:用户点开 /p/<id> 是要看图,不是要下载文件。安卓封装壳 / WebView
|
||||
// 对 Content-Disposition: attachment 一律交系统下载器,图片外链因此“只下载、不打开”。
|
||||
// 其余类型保持原下载语义(PDF/Office 等交给下载器或外部服务自己处理)。
|
||||
const imgType = inlineImageType(abs);
|
||||
log.info('public-file', entry.sessionId + ' 外部取用' + (imgType ? '(图片,inline)' : '(附件)') + ':' + entry.rel);
|
||||
if (imgType) return sendInlineImage(res, abs, imgType);
|
||||
sendDownload(res, abs);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user