| import { sanitizeHtml, sanitizeSvg } from "./Sanitize"; |
|
|
| |
| jest.mock("@/Core/DynamicImports", () => ({ |
| dynamicImports: { |
| dompurify: { |
| get module() { |
| |
| return Promise.resolve({ |
| sanitize: (input: string, config?: any) => { |
| |
| |
| if (typeof input !== "string") return ""; |
|
|
| let sanitized = input.replace( |
| /<script\b[^>]*>[\s\S]*?<\/script>/gi, |
| "" |
| ); |
| sanitized = sanitized.replace(/ on\w+="[^"]*"/gi, ""); |
|
|
| |
| if (config?.USE_PROFILES?.svg) { |
| return sanitized; |
| } |
|
|
| |
| |
| return sanitized; |
| }, |
| }); |
| }, |
| }, |
| }, |
| })); |
|
|
| describe("Sanitization Functions", () => { |
| describe("sanitizeHtml", () => { |
| it("should remove script tags from HTML", async () => { |
| const unsafeHtml = |
| '<p>Hello <script>alert("XSS")</script> World</p>'; |
| const sanitized = await sanitizeHtml(unsafeHtml); |
| expect(sanitized).not.toContain("<script>"); |
| expect(sanitized).toBe("<p>Hello World</p>"); |
| }); |
|
|
| it("should remove event handlers like onclick", async () => { |
| const unsafeHtml = "<b onclick=\"alert('XSS')\">Click me</b>"; |
| const sanitized = await sanitizeHtml(unsafeHtml); |
| expect(sanitized).not.toContain("onclick"); |
| expect(sanitized).toBe("<b>Click me</b>"); |
| }); |
|
|
| it("should allow safe HTML tags like <p>, <b>, and <a>", async () => { |
| const safeHtml = |
| '<p>This is <b>bold</b> and includes a <a href="#">link</a>.</p>'; |
| const sanitized = await sanitizeHtml(safeHtml); |
| |
| |
| expect(sanitized).toBe(safeHtml); |
| }); |
|
|
| it("should handle null and undefined input gracefully", async () => { |
| expect(await sanitizeHtml(null)).toBe(""); |
| expect(await sanitizeHtml(undefined)).toBe(""); |
| }); |
| }); |
|
|
| describe("sanitizeSvg", () => { |
| it("should remove script tags from SVG content", async () => { |
| const unsafeSvg = '<svg><script>alert("XSS")</script><rect/></svg>'; |
| const sanitized = await sanitizeSvg(unsafeSvg); |
| expect(sanitized).not.toContain("<script>"); |
| expect(sanitized).toBe("<svg><rect/></svg>"); |
| }); |
|
|
| it("should remove event handlers like onload from SVG elements", async () => { |
| const unsafeSvg = "<svg onload=\"alert('XSS')\"><circle/></svg>"; |
| const sanitized = await sanitizeSvg(unsafeSvg); |
| expect(sanitized).not.toContain("onload"); |
| expect(sanitized).toBe("<svg><circle/></svg>"); |
| }); |
|
|
| it("should allow SVG-specific tags like <use> and attributes like xlink:href", async () => { |
| |
| |
| const safeSvg = |
| '<svg><defs><symbol id="shape"/></defs><use xlink:href="#shape"></use></svg>'; |
| const sanitized = await sanitizeSvg(safeSvg); |
| expect(sanitized).toContain("<use"); |
| }); |
| }); |
| }); |
|
|