feat(sandbox): 不可信字节在沙箱子进程里解析(撤销权限、超时与内存强杀);种子、文件预览、字幕解析迁入

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
rootandClaude Opus 5.5 committed 2026-10-03 01:44:31 +08:00
1 parent 1a76d2d700
commit eeec91e892
21 files changed
+632 -178

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
// 不可信字节的解析入口(docs/adr/ADR-005-sandbox.md):缺省在进程内解析;服务端启动时用 setTorrentParser 换成沙箱子进程实现。
// adapters 不依赖服务端,测试与单独使用时照常进程内解析。
import { BencodeError, parseTorrent, type TorrentMeta } from "./qbittorrent/torrent.ts";
export type TorrentParser = (bytes: Uint8Array) => Promise<TorrentMeta>;
let parser: TorrentParser = async (bytes) => parseTorrent(bytes);
export function setTorrentParser(p: TorrentParser): void {
parser = p;
}
/** 解析 .torrent;不是有效种子抛 BencodeError */
export function parseTorrentIsolated(bytes: Uint8Array): Promise<TorrentMeta> {
return parser(bytes);
}
/** 校验是 .torrent 并取 v1 infohash 与名称;不是种子返回 null(取代进程内的 inspectTorrent) */
export async function inspectTorrentIsolated(
bytes: Uint8Array,
): Promise<{ infoHash: string; name: string | null } | null> {
try {
const m = await parser(bytes);
return { infoHash: m.infohashV1 ?? m.hash, name: m.name || null };
} catch (e) {
if (e instanceof BencodeError) return null;
throw e;
}
}