Initial import of streamline
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { Hono } from "hono";
|
||||
import { createWriteGate, isWriteGateExempt } from "../src/write_gate";
|
||||
|
||||
test("放行清单只认路径开头,相近的路径不沾光", () => {
|
||||
for (const p of ["/internal/tasks/x/update", "/openapi/security", "/openapi/v1/tasks", "/cli/v1/tasks", "/security", "/security/x/review/approve"]) {
|
||||
expect(isWriteGateExempt(p)).toBe(true);
|
||||
expect(isWriteGateExempt(`/api${p}`)).toBe(true);
|
||||
}
|
||||
for (const p of ["/tasks/x", "/import", "/securityx", "/openapi-lark-bots/x", "/openapi-key-requests", "/rsi-ship/x", "/internal"]) {
|
||||
expect(isWriteGateExempt(p)).toBe(false);
|
||||
expect(isWriteGateExempt(`/api${p}`)).toBe(false);
|
||||
}
|
||||
});
|
||||
|
||||
test("写请求没有登录身份 401,有身份放行;读请求和放行路径不看身份", async () => {
|
||||
const app = new Hono();
|
||||
app.use("*", createWriteGate(async (h) => (h.get("x-tt-session-v2") === "ok" ? "a@bytedance.com" : null)));
|
||||
app.all("*", (c) => c.text("reached"));
|
||||
|
||||
const anonymous = await app.request("/api/tasks/x", { method: "DELETE" });
|
||||
expect(anonymous.status).toBe(401);
|
||||
expect(await anonymous.json()).toMatchObject({ code: "no-sso" });
|
||||
|
||||
expect((await app.request("/api/tasks/x", { method: "DELETE", headers: { "x-tt-session-v2": "ok" } })).status).toBe(200);
|
||||
expect((await app.request("/api/tasks/x")).status).toBe(200);
|
||||
expect((await app.request("/api/security", { method: "POST" })).status).toBe(200);
|
||||
expect((await app.request("/api/internal/tasks/x/update", { method: "POST" })).status).toBe(200);
|
||||
});
|
||||
|
||||
// 放在子进程里跑:bun 的测试文件共用模块表,这里 import routes 会提前加载整棵控制面,别的测试文件
|
||||
// 再靠环境变量做的隔离(比如 alog_decode 的缓存目录)就失效了。setup_env 把库指向关着的端口,
|
||||
// fetch 也掐断,闸门一旦失效,请求也碰不到任何真实服务。
|
||||
const SCRIPT = `
|
||||
import "./setup_env";
|
||||
globalThis.fetch = async () => { throw new Error("blocked"); };
|
||||
console.warn = console.error = () => {};
|
||||
const { api } = await import("../src/routes");
|
||||
const { isWriteGateExempt } = await import("../src/write_gate");
|
||||
const seen = new Set();
|
||||
const gated = [];
|
||||
const leaked = [];
|
||||
for (const r of api.routes) {
|
||||
if (["ALL", "GET", "HEAD", "OPTIONS"].includes(r.method)) continue;
|
||||
const key = r.method + " " + r.path;
|
||||
if (seen.has(key) || isWriteGateExempt(r.path)) continue;
|
||||
seen.add(key);
|
||||
const url = r.path.replace(/:[a-zA-Z]+(\\{[^}]*\\})?/g, "no-such-id").replace(/\\*/g, "x");
|
||||
const res = await api.request(url, { method: r.method, headers: { "content-type": "application/json" }, body: "{}" });
|
||||
gated.push(key);
|
||||
if (res.status !== 401) leaked.push(key + " -> " + res.status);
|
||||
}
|
||||
console.log("RESULT " + JSON.stringify({ gated, leaked }));
|
||||
process.exit(0);
|
||||
`;
|
||||
|
||||
test("除了放行清单,每一条写路由没有登录身份都是 401,新加的路由也跑不掉", () => {
|
||||
const cache = mkdtempSync(path.join(tmpdir(), "write-gate-test-"));
|
||||
try {
|
||||
const run = Bun.spawnSync([process.execPath, "--eval", SCRIPT], {
|
||||
cwd: import.meta.dir,
|
||||
env: { ...process.env, STREAMLINE_ALOG_CACHE_DIR: cache },
|
||||
});
|
||||
const line = run.stdout.toString().split("\n").find((l) => l.startsWith("RESULT "));
|
||||
if (!line) throw new Error(`子进程没有跑完: ${run.stderr.toString().slice(-500)}`);
|
||||
const { gated, leaked } = JSON.parse(line.slice("RESULT ".length)) as { gated: string[]; leaked: string[] };
|
||||
expect(leaked).toEqual([]);
|
||||
// 这些是这次要堵的接口,清单被改宽时这里会先红。
|
||||
for (const key of [
|
||||
"POST /import", "POST /features", "POST /features/:id/port", "POST /prototypes", "POST /prototypes/:id/promote",
|
||||
"POST /verifications", "POST /verifications/:id/fix-batch", "DELETE /tasks/:id", "POST /tasks/:id/confirm",
|
||||
"POST /tasks/:id/review/approve", "PUT /nodes/:id/clusters", "POST /uploads/image", "POST /verify-check-presets",
|
||||
]) expect(gated).toContain(key);
|
||||
} finally {
|
||||
rmSync(cache, { recursive: true, force: true });
|
||||
}
|
||||
}, 60_000);
|
||||
Reference in New Issue
Block a user