import { describe, it, expect } from "vitest"; import { stripHtml } from "@/lib/notifications/strip-html"; /** * The five plain-text senders used to strip tags with a local single-pass * regex replace (CodeQL: incomplete multi-character sanitization). The * hardened shared helper re-applies the strip until the string stops * changing, so a tag re-formed by an earlier removal cannot survive. These * cases pin the invariant that matters — no tag-shaped sequence in any * output — and identity on plain input. */ describe("stripHtml", () => { it("leaves plain input unchanged", () => { expect(stripHtml("Blood pressure logged: 120/80")).toBe( "Blood pressure logged: 120/80", ); expect(stripHtml("")).toBe(""); }); it("strips simple tags exactly like the old single pass", () => { expect(stripHtml("urgent reading")).toBe("urgent reading"); expect(stripHtml("line
break")).toBe("linebreak"); expect(stripHtml("a < b and b > a")).toBe("a a"); }); it("removes the classic nested-tag bypass shapes", () => { expect(stripHtml("<script>alert(1)")).toBe("script>alert(1)"); expect(stripHtml("<script>")).toBe("script>"); expect(stripHtml("<<script>>payload")).toBe("script>>payload"); }); it("never lets a tag-shaped sequence survive any adversarial input", () => { const hostile = [ "<script>alert(1)</script>", "ipt>alert(1)ipt>", "<".repeat(64) + "b>".repeat(64) + "script>alert(1)", '', "e>f>g>", ]; for (const input of hostile) { expect(stripHtml(input), input).not.toMatch(/<[^>]*>/); } }); it("handles unterminated brackets without stripping legitimate text", () => { // No closing `>` anywhere after the `<` — nothing tag-shaped to remove. expect(stripHtml("