|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { normalizeSessionIconInput, parseSessionIcon } from "./session-icon.js"; |
| 3 | + |
| 4 | +describe("session icons", () => { |
| 5 | + it("parses named and single-grapheme emoji forms", () => { |
| 6 | + expect(parseSessionIcon("name:lobster")).toEqual({ kind: "named", name: "lobster" }); |
| 7 | + expect(parseSessionIcon("🦞")).toEqual({ kind: "emoji", emoji: "🦞" }); |
| 8 | + expect(parseSessionIcon("👩🏽💻")).toEqual({ kind: "emoji", emoji: "👩🏽💻" }); |
| 9 | + expect(parseSessionIcon("name:Nope")).toBeNull(); |
| 10 | + expect(parseSessionIcon("🦞🚀")).toBeNull(); |
| 11 | + expect(parseSessionIcon("A")).toBeNull(); |
| 12 | + }); |
| 13 | + |
| 14 | + it("trims inputs and canonicalizes safe SVG markup", () => { |
| 15 | + expect( |
| 16 | + normalizeSessionIconInput( |
| 17 | + " svg:<svg viewBox='0 0 24 24'><g transform='translate(1 2)'><path d='M1 2' fill='currentColor'/></g></svg> ", |
| 18 | + ), |
| 19 | + ).toEqual({ |
| 20 | + ok: true, |
| 21 | + value: |
| 22 | + 'svg:<svg viewBox="0 0 24 24"><g transform="translate(1 2)"><path d="M1 2" fill="currentColor"/></g></svg>', |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + it.each([ |
| 27 | + "svg:<svg><script>alert(1)</script></svg>", |
| 28 | + 'svg:<svg onload="alert(1)"></svg>', |
| 29 | + 'svg:<svg xmlns:xlink="http://www.w3.org/1999/xlink"><path xlink:href="#x"/></svg>', |
| 30 | + 'svg:<svg><path fill="url(#paint)"/></svg>', |
| 31 | + "svg:<!DOCTYPE svg><svg></svg>", |
| 32 | + "svg:<svg></svg><svg></svg>", |
| 33 | + 'svg:<svg><use href="#x"/></svg>', |
| 34 | + 'svg:<svg><path style="fill:red"/></svg>', |
| 35 | + ])("rejects hostile SVG: %s", (value) => { |
| 36 | + expect(normalizeSessionIconInput(value).ok).toBe(false); |
| 37 | + }); |
| 38 | + |
| 39 | + it("rejects SVG icons over 4096 bytes", () => { |
| 40 | + const value = `svg:<svg><title>${"x".repeat(4096)}</title></svg>`; |
| 41 | + expect(normalizeSessionIconInput(value)).toMatchObject({ ok: false }); |
| 42 | + }); |
| 43 | +}); |
0 commit comments