Files
novalon-website/src/lib/client-ip.test.ts
T
zhangxiang a366bd1400 fix(qa): 第五轮系统性质量保障缺陷修复 + 单元/集成测试
安全/鉴权:登出清服务端 httpOnly 令牌、登录 IP 限流前置 bcrypt、停用账号
令牌在 6 个内联 role handler 失效、admin/items 存在性 oracle 认证前置、
同意偏好 sanitize fail-closed、api-crypto 按 HTTP 方法判体。

CMS:workflow 状态更新+审计收进交互式 $transaction(失败即回滚)、编辑器
richtext 走 textarea 防换行损毁、array/object 子字段按作用域读写防污染、
about/contact/erp-upgrade 补 ISR revalidate 与路由映射。

UI/a11y:AnimatedCounter 入视口前显起点、CLS 累计上报、MetricCard 动画入
effect 并卸载取消 rAF、后台状态四态映射/搜索防抖+序号守卫/媒体单 input/
吞错改横幅/表单 label-aria 关联。

新增对应判别性单测与 pagination/rate-limit/client-ip/sanitize/validate-content-data
等模块及 tests-integration 真库集成层。
2026-09-28 10:48:07 +08:00

57 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect } from "@jest/globals";
import { getClientIp } from "./client-ip";
function headersOf(init: Record<string, string>): Headers {
return new Headers(init);
}
describe("getClientIp", () => {
it("优先取 X-Real-IP(Nginx 用 $remote_addr 覆写,客户端无法伪造)", () => {
const headers = headersOf({
"x-real-ip": "203.0.113.9",
"x-forwarded-for": "1.1.1.1",
});
expect(getClientIp(headers)).toBe("203.0.113.9");
});
it("N-25 攻击面:伪造最左 XFF 不得改变限流桶(无 X-Real-IP 时取链尾)", () => {
const headers = headersOf({
"x-forwarded-for": "1.1.1.1, 2.2.2.2, 198.51.100.7",
});
expect(getClientIp(headers)).toBe("198.51.100.7");
});
it("逐请求轮换 XFF 时,旧实现会得到 3 个不同桶、新实现稳定为一个", () => {
const spoofed = ["1.1.1.1", "2.2.2.2", "3.3.3.3"].map((ip) =>
headersOf({ "x-forwarded-for": `${ip}, 198.51.100.7` }),
);
const buckets = new Set(spoofed.map((h) => getClientIp(h)));
expect(buckets.size).toBe(1);
expect([...buckets][0]).toBe("198.51.100.7");
});
it("只有一跳时取该跳", () => {
expect(getClientIp(headersOf({ "x-forwarded-for": "198.51.100.7" }))).toBe(
"198.51.100.7",
);
});
it("空白值等同缺失(trim 后为空 ⇒ 不成为限流键)", () => {
expect(
getClientIp(headersOf({ "x-forwarded-for": "198.51.100.7, , " })),
).toBe("198.51.100.7");
expect(getClientIp(headersOf({ "x-real-ip": " " }))).toBe("unknown");
expect(getClientIp(headersOf({ "x-real-ip": "" }))).toBe("unknown");
expect(getClientIp(headersOf({ "x-forwarded-for": " , " }))).toBe(
"unknown",
);
});
it("两个头都缺失时为 unknown", () => {
expect(getClientIp(headersOf({}))).toBe("unknown");
});
});