176 lines
7.5 KiB
TypeScript
176 lines
7.5 KiB
TypeScript
import { describe, expect, test, beforeEach, afterEach } from "bun:test";
|
|
import { mkdtempSync, rmSync, existsSync, readFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import {
|
|
DEFAULT_OUTPUT_BYTES_LIMIT,
|
|
protectLargeOutput,
|
|
printJson,
|
|
redactSensitiveText,
|
|
formatSourceLabel,
|
|
formatExpiresAt,
|
|
} from "./printer.js";
|
|
|
|
describe("cli/printer - 上下文防爆与敏感脱敏", () => {
|
|
describe("redactSensitiveText 脱敏处理", () => {
|
|
test("脱敏 URL Query 中的 accessToken / token / returnToken", () => {
|
|
const input = "https://app.ekuaibao.com/api/test?accessToken=sec_123456&corpId=corpxx&returnToken=ret_789";
|
|
const redacted = redactSensitiveText(input);
|
|
expect(redacted).not.toContain("sec_123456");
|
|
expect(redacted).not.toContain("ret_789");
|
|
expect(redacted).toContain("accessToken=<redacted>");
|
|
expect(redacted).toContain("returnToken=<redacted>");
|
|
expect(redacted).toContain("corpId=corpxx");
|
|
});
|
|
|
|
test("脱敏对象存储签名 URL 中的临时凭证", () => {
|
|
const input = "https://files.example/a.png?Expires=1&OSSAccessKeyId=key&Signature=sig&security-token=secret";
|
|
const redacted = redactSensitiveText(input);
|
|
expect(redacted).toContain("OSSAccessKeyId=<redacted>");
|
|
expect(redacted).toContain("Signature=<redacted>");
|
|
expect(redacted).toContain("security-token=<redacted>");
|
|
expect(redacted).not.toContain("secret");
|
|
});
|
|
|
|
test("脱敏 Authorization Bearer token", () => {
|
|
const input = "Request headers: Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.xyz";
|
|
const redacted = redactSensitiveText(input);
|
|
expect(redacted).not.toContain("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.xyz");
|
|
expect(redacted).toContain("Bearer <redacted>");
|
|
});
|
|
|
|
test("脱敏 password、secret、appSecurity 等键值", () => {
|
|
const input = '{"username": "admin", "password": "super_secret_password_123", "appSecurity": "sec_key"}';
|
|
const redacted = redactSensitiveText(input);
|
|
expect(redacted).not.toContain("super_secret_password_123");
|
|
expect(redacted).not.toContain("sec_key");
|
|
expect(redacted).toContain('"password": "<redacted>"');
|
|
expect(redacted).toContain('"appSecurity": "<redacted>"');
|
|
});
|
|
});
|
|
|
|
describe("protectLargeOutput 与 printJson 体积熔断保护", () => {
|
|
let tempDir: string;
|
|
|
|
beforeEach(() => {
|
|
tempDir = mkdtempSync(join(tmpdir(), "hose-printer-test-"));
|
|
});
|
|
|
|
afterEach(() => {
|
|
try {
|
|
rmSync(tempDir, { recursive: true, force: true });
|
|
} catch {
|
|
// ignore
|
|
}
|
|
});
|
|
|
|
test("正常体积的数据可以直接输出 JSON", () => {
|
|
const normalData = { hello: "world", numbers: [1, 2, 3] };
|
|
expect(() => protectLargeOutput(normalData)).not.toThrow();
|
|
const output = printJson(normalData);
|
|
expect(JSON.parse(output)).toEqual(normalData);
|
|
});
|
|
|
|
test("超过 200KB 的巨型数据默认抛出熔断错误", () => {
|
|
// 构造超过 200KB 的大数组
|
|
const bigArray = Array.from({ length: 1500 }, (_, i) => ({
|
|
id: `id-${i}`,
|
|
description: "这是一个用于测试大报文熔断拦截的测试长字段内容,模拟企业级海量单据与工作流节点拓扑",
|
|
nested: { count: i, payload: "x".repeat(120) },
|
|
}));
|
|
|
|
expect(() => protectLargeOutput(bigArray)).toThrow(
|
|
/输出体积过大.*超过安全阈值.*--output-file.*--full/
|
|
);
|
|
|
|
expect(() => printJson(bigArray)).toThrow(/输出体积过大/);
|
|
});
|
|
|
|
test("显式指定 allowFull 时允许全量输出大报文", () => {
|
|
const bigArray = Array.from({ length: 1500 }, (_, i) => ({
|
|
id: `id-${i}`,
|
|
payload: "y".repeat(150),
|
|
}));
|
|
|
|
expect(() => protectLargeOutput(bigArray, { allowFull: true })).not.toThrow();
|
|
const output = printJson(bigArray, { allowFull: true });
|
|
expect(output.length).toBeGreaterThan(DEFAULT_OUTPUT_BYTES_LIMIT);
|
|
});
|
|
|
|
test("指定 outputFile 时大报文直接落盘并返回精简摘要", () => {
|
|
const targetFile = join(tempDir, "exported-bills.json");
|
|
const bigArray = Array.from({ length: 1500 }, (_, i) => ({
|
|
id: `id-${i}`,
|
|
payload: "z".repeat(150),
|
|
}));
|
|
|
|
const resultJson = printJson(bigArray, { outputFile: targetFile });
|
|
const summary = JSON.parse(resultJson);
|
|
|
|
expect(summary.outputFile).toBe(targetFile);
|
|
expect(summary.bytes).toBeGreaterThan(DEFAULT_OUTPUT_BYTES_LIMIT);
|
|
expect(existsSync(targetFile)).toBe(true);
|
|
|
|
const savedContent = JSON.parse(readFileSync(targetFile, "utf-8"));
|
|
expect(savedContent).toHaveLength(1500);
|
|
expect(savedContent[0].id).toBe("id-0");
|
|
});
|
|
});
|
|
|
|
describe("formatSourceLabel 4大凭证类别规范格式化", () => {
|
|
test("企业开放接口 (OpenAPI 密钥)", () => {
|
|
expect(formatSourceLabel("openapi")).toBe("企业开放接口 (OpenAPI 密钥)");
|
|
expect(formatSourceLabel("account", true)).toBe("企业开放接口 (OpenAPI 密钥)");
|
|
expect(formatSourceLabel("link", true)).toBe("企业开放接口 (OpenAPI 密钥)");
|
|
});
|
|
|
|
test("个人账号凭证 (手机号)", () => {
|
|
expect(formatSourceLabel("account")).toBe("个人账号凭证 (手机号)");
|
|
});
|
|
|
|
test("合思协助平台授权链接 (协助链接)", () => {
|
|
expect(formatSourceLabel("link")).toBe("合思协助平台授权链接 (协助链接)");
|
|
expect(formatSourceLabel("assist")).toBe("合思协助平台授权链接 (协助链接)");
|
|
});
|
|
|
|
test("网页临时抓包凭证 (Cookie)", () => {
|
|
expect(formatSourceLabel("curl")).toBe("网页临时抓包凭证 (Cookie)");
|
|
expect(formatSourceLabel("browser")).toBe("网页临时抓包凭证 (Cookie)");
|
|
expect(formatSourceLabel("f12")).toBe("网页临时抓包凭证 (Cookie)");
|
|
});
|
|
|
|
test("兜底处理", () => {
|
|
expect(formatSourceLabel("manual")).toBe("手动配置");
|
|
expect(formatSourceLabel("other")).toBe("其他 (other)");
|
|
expect(formatSourceLabel(undefined)).toBe("未知来源");
|
|
});
|
|
});
|
|
|
|
describe("formatExpiresAt 过期状态精准描述", () => {
|
|
test("长期有效凭据", () => {
|
|
expect(formatExpiresAt(undefined, true, "openapi")).toBe("长期有效 (OpenAPI 密钥)");
|
|
expect(formatExpiresAt(0, false)).toBe("长期有效 / 服务端未显式限制");
|
|
});
|
|
|
|
test("已过期但具备自动续期能力的凭据", () => {
|
|
const pastTime = Date.now() - 3600000;
|
|
expect(formatExpiresAt(pastTime, true, "account")).toContain("(🔄 随主账号自动换票)");
|
|
expect(formatExpiresAt(pastTime, true, "openapi")).toContain("(🔄 OpenAPI 密钥自动续期)");
|
|
expect(formatExpiresAt(pastTime, true)).toContain("(🔄 自动续期)");
|
|
});
|
|
|
|
test("已过期且不可续期的凭据精准告警", () => {
|
|
const pastTime = Date.now() - 3600000;
|
|
expect(formatExpiresAt(pastTime, false, "link")).toContain("(⚠️ 协助授权已失效)");
|
|
expect(formatExpiresAt(pastTime, false, "curl")).toContain("(⚠️ Cookie 已失效)");
|
|
expect(formatExpiresAt(pastTime, false, "manual")).toContain("(⚠️ 已过期)");
|
|
});
|
|
|
|
test("未来有效期的剩余提示", () => {
|
|
const futureMins = Date.now() + 30 * 60000;
|
|
expect(formatExpiresAt(futureMins, true, "account")).toContain("支持主账号自动换票");
|
|
expect(formatExpiresAt(futureMins, true, "openapi")).toContain("支持 OpenAPI 密钥自动续期");
|
|
});
|
|
});
|
|
});
|