- guard.js:流式输出护栏,跨 chunk 禁用词拦截并在句末截断超长回复 - history.js:tool 结果入历史前截断、历史按完整轮次裁剪,避免断链与上下文挤占 - reference.js:服务项目/协议改为可读文本渲染,替代裸 JSON 误导模型 - 临时脚本 test_restricted_backend.mjs 转为 test/restricted_fs_backend.test.js
95 lines
4.5 KiB
JavaScript
95 lines
4.5 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { CompositeBackend, FilesystemBackend } from "deepagents";
|
|
|
|
import { createRestrictedBackend } from "../agent/infra/restricted_fs_backend.js";
|
|
|
|
// 以 api/health 为沙箱根,避免写死本机绝对路径
|
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
const DATA_DIR = path.join(ROOT, "data");
|
|
const backend = createRestrictedBackend({ rootDir: ROOT, dataDir: DATA_DIR });
|
|
|
|
const isDenied = result => typeof result.error === "string" && result.error.includes("denied");
|
|
|
|
test("敏感文件读取被拒绝", async () => {
|
|
assert.ok(isDenied(await backend.read("conf.json")), "相对路径 conf.json 应被拒绝");
|
|
assert.ok(isDenied(await backend.read(path.join(ROOT, "conf.json"))), "沙箱内绝对路径 conf.json 应被拒绝");
|
|
assert.ok((await backend.read("/conf.json")).error, "根绝对路径 /conf.json 应被拒绝");
|
|
assert.ok(isDenied(await backend.read(".env")), ".env 应被拒绝");
|
|
assert.ok(isDenied(await backend.read(".env.local")), ".env.* 应被拒绝");
|
|
assert.ok(isDenied(await backend.read("data/users/u123/memory.txt")), "用户数据目录应被拒绝");
|
|
assert.ok(isDenied(await backend.read("logs/x.log")), "logs 目录应被拒绝");
|
|
});
|
|
|
|
test("路径穿越与沙箱外路径被拒绝", async () => {
|
|
assert.ok((await backend.read("../conf.json")).error, ".. 穿越应报错");
|
|
assert.ok((await backend.read(path.join(os.tmpdir(), "outside-sandbox.txt"))).error, "沙箱外绝对路径应报错");
|
|
});
|
|
|
|
test("正常资源仍可访问", async () => {
|
|
const agents = await backend.read("agent/bots/bot-escort/AGENTS.md");
|
|
assert.equal(agents.error, undefined, `AGENTS.md 应可读:${agents.error}`);
|
|
assert.match(agents.content, /用户记忆/);
|
|
|
|
const skill = await backend.read("agent/infra/skills/baidu-ai-map/SKILL.md");
|
|
assert.equal(skill.error, undefined, `SKILL.md 应可读:${skill.error}`);
|
|
assert.match(skill.content, /百度地图|Baidu|baidu/i);
|
|
});
|
|
|
|
test("ls / glob / grep 不泄露敏感文件", async () => {
|
|
const isConf = file => path.basename(file.replace(/[\\/]+$/, "")).toLowerCase() === "conf.json";
|
|
|
|
const ls = await backend.ls("/");
|
|
assert.ok(!ls.files.some(f => isConf(f.path)), "ls 不应列出 conf.json");
|
|
|
|
const glob = await backend.glob("**/conf*.json");
|
|
assert.ok(!glob.files.some(f => isConf(f.path)), "glob 不应返回 conf.json");
|
|
|
|
// conf.json 内存在 apiKey,若过滤失效此处会命中
|
|
const grep = await backend.grep("apiKey", "/", "conf.json");
|
|
assert.equal(grep.matches.filter(m => isConf(m.path)).length, 0, "grep 不应返回 conf.json 内容");
|
|
|
|
const normal = await backend.grep("escort", "agent/bots/bot-escort");
|
|
assert.ok(normal.matches.length > 0, "普通目录 grep 应正常工作");
|
|
});
|
|
|
|
test("拒绝写敏感文件,允许写普通文件", async () => {
|
|
assert.ok(isDenied(await backend.write("conf.json", "{}")), "写 conf.json 应被拒绝");
|
|
assert.ok(isDenied(await backend.write("data/users/u123/memory.txt", "x")), "写用户记忆应被拒绝");
|
|
assert.ok((await backend.write("../escape.txt", "x")).error, "写沙箱外路径应报错");
|
|
|
|
const name = `.restricted-backend-${process.pid}.txt`;
|
|
try {
|
|
const written = await backend.write(name, "hello");
|
|
assert.equal(written.error, undefined, `普通文件应可写:${written.error}`);
|
|
const read = await backend.read(name);
|
|
assert.equal(read.error, undefined, `普通文件应可读:${read.error}`);
|
|
assert.match(read.content, /hello/);
|
|
} finally {
|
|
await backend.delete(name);
|
|
}
|
|
});
|
|
|
|
test("CompositeBackend 的 /memories/ 挂载仍可用,默认路径仍受限", async () => {
|
|
const memoryRoot = fs.mkdtempSync(path.join(os.tmpdir(), "restricted-backend-memories-"));
|
|
const composite = new CompositeBackend(backend, {
|
|
"/memories/": new FilesystemBackend({ rootDir: memoryRoot, virtualMode: true }),
|
|
});
|
|
|
|
try {
|
|
const written = await composite.write("/memories/memory.txt", "测试记忆");
|
|
assert.equal(written.error, undefined, `/memories 应可写:${written.error}`);
|
|
const read = await composite.read("/memories/memory.txt");
|
|
assert.equal(read.error, undefined, `/memories 应可读:${read.error}`);
|
|
assert.equal(read.content, "测试记忆");
|
|
|
|
assert.ok((await composite.read("/conf.json")).error, "composite 默认路径仍应受限");
|
|
} finally {
|
|
fs.rmSync(memoryRoot, { recursive: true, force: true });
|
|
}
|
|
});
|