|
| 1 | +/** |
| 2 | + * url-safety 单元测试(CR PR#345 要求补的覆盖)。 |
| 3 | + * |
| 4 | + * sanitizeMediaUrl 现在做两件事: |
| 5 | + * 1. 协议白名单:只放 http/https + 站内相对路径,拒 javascript:/data:/协议相对 |
| 6 | + * 2. http -> https 自动升级,顺手清显式 :80(http 默认端口在 https 下会挂 TLS) |
| 7 | + * |
| 8 | + * sanitizeExternalUrl 走的是 link 白名单(多个 mailto:),不在本次 PR 改动范围, |
| 9 | + * 但顺手补几条 smoke test 锁住边界。 |
| 10 | + */ |
| 11 | +import { describe, expect, test } from "vitest"; |
| 12 | +import { sanitizeMediaUrl, sanitizeExternalUrl } from "../lib/url-safety"; |
| 13 | + |
| 14 | +describe("sanitizeMediaUrl", () => { |
| 15 | + test("https 原样返回(normalizer 可能加 trailing slash,URL.toString 已稳定)", () => { |
| 16 | + expect(sanitizeMediaUrl("https://example.com/x.jpg")).toBe( |
| 17 | + "https://example.com/x.jpg", |
| 18 | + ); |
| 19 | + }); |
| 20 | + |
| 21 | + test("http:// 自动升级到 https://", () => { |
| 22 | + expect(sanitizeMediaUrl("http://example.com/x.jpg")).toBe( |
| 23 | + "https://example.com/x.jpg", |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + test("HTTP:// 大小写不敏感升级", () => { |
| 28 | + expect(sanitizeMediaUrl("HTTP://example.com/x.jpg")).toBe( |
| 29 | + "https://example.com/x.jpg", |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + test("显式 :80 端口在升级时清空(防止 https 拿 80 走 TLS)", () => { |
| 34 | + expect(sanitizeMediaUrl("http://example.com:80/x.jpg")).toBe( |
| 35 | + "https://example.com/x.jpg", |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + test("非 80 的显式端口保留(用户可能跑了 https on 8443 这种)", () => { |
| 40 | + expect(sanitizeMediaUrl("http://example.com:8080/x.jpg")).toBe( |
| 41 | + "https://example.com:8080/x.jpg", |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + test("升级保留 path / query / hash", () => { |
| 46 | + expect( |
| 47 | + sanitizeMediaUrl( |
| 48 | + "http://mmbiz.qpic.cn/sz_mmbiz_jpg/abc/0?wx_fmt=jpeg&tp=webp#x", |
| 49 | + ), |
| 50 | + ).toBe("https://mmbiz.qpic.cn/sz_mmbiz_jpg/abc/0?wx_fmt=jpeg&tp=webp#x"); |
| 51 | + }); |
| 52 | + |
| 53 | + test("站内相对路径原样返回,不走 URL parser", () => { |
| 54 | + expect(sanitizeMediaUrl("/logo.png")).toBe("/logo.png"); |
| 55 | + expect(sanitizeMediaUrl("/event/cover.webp?v=1")).toBe( |
| 56 | + "/event/cover.webp?v=1", |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + test("协议相对 URL 被拒(//evil.com 会继承当前页协议跳到攻击者域)", () => { |
| 61 | + expect(sanitizeMediaUrl("//evil.com/x.jpg")).toBeNull(); |
| 62 | + }); |
| 63 | + |
| 64 | + test("javascript: / data: / vbscript: 被拒", () => { |
| 65 | + expect(sanitizeMediaUrl("javascript:alert(1)")).toBeNull(); |
| 66 | + expect(sanitizeMediaUrl("data:image/png;base64,AAA")).toBeNull(); |
| 67 | + expect(sanitizeMediaUrl("vbscript:msgbox(1)")).toBeNull(); |
| 68 | + }); |
| 69 | + |
| 70 | + test("mailto: 在媒体场景被拒(不在 SAFE_MEDIA_PROTOCOLS)", () => { |
| 71 | + expect(sanitizeMediaUrl("mailto:a@b.com")).toBeNull(); |
| 72 | + }); |
| 73 | + |
| 74 | + test("空 / null / undefined / 仅空白 → null", () => { |
| 75 | + expect(sanitizeMediaUrl(null)).toBeNull(); |
| 76 | + expect(sanitizeMediaUrl(undefined)).toBeNull(); |
| 77 | + expect(sanitizeMediaUrl("")).toBeNull(); |
| 78 | + expect(sanitizeMediaUrl(" ")).toBeNull(); |
| 79 | + }); |
| 80 | + |
| 81 | + test("升级行为幂等:已是 https 不改", () => { |
| 82 | + const out1 = sanitizeMediaUrl("https://example.com/x.jpg"); |
| 83 | + const out2 = sanitizeMediaUrl(out1!); |
| 84 | + expect(out2).toBe(out1); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe("sanitizeExternalUrl", () => { |
| 89 | + test("mailto 允许(媒体场景拒、链接场景允许,区分两个白名单)", () => { |
| 90 | + expect(sanitizeExternalUrl("mailto:a@b.com")).toBe("mailto:a@b.com"); |
| 91 | + }); |
| 92 | + |
| 93 | + test("协议相对 URL 被拒(同 media)", () => { |
| 94 | + expect(sanitizeExternalUrl("//evil.com/x")).toBeNull(); |
| 95 | + }); |
| 96 | + |
| 97 | + test("站内相对路径原样返回", () => { |
| 98 | + expect(sanitizeExternalUrl("/about")).toBe("/about"); |
| 99 | + }); |
| 100 | +}); |
0 commit comments