SPB Git

Repository Public

attachments.test.ts 7.8 KB 119 lines typescript

1// SP Mail Next — essais unitaires des pièces jointes : normalisation, vignette sûre, glyphes, aperçu, filtres → paramètres, limites du ZIP, nom de l'archive.2import assert from "node:assert/strict"3import { test } from "node:test"4import {5  ATTACHMENT_KINDS, GALLERY_PAGE, ZIP_MAX_BYTES, ZIP_MAX_ITEMS, attachmentKey, decodeTextPreview, emptyGalleryFilter, extensionLabel, filenameFromDisposition, galleryParams, inlineUrl, isFiltered, isGatewayUrl, isInlineSafe,6  kindGlyph, normalizeAttachment, previewMode, previewOf, sizeBounds, zipBody, zipProblem,7} from "./attachments"89const MB = "0b6c1f1e-2f0a-4c57-9a55-0a4a5f0f6f10"10const raw = (over: Record<string, unknown> = {}) => ({11  mailbox_id: MB, email_id: "e1", thread_id: "t1", blob_id: "b1", part_id: "2", name: "photo.png", type: "IMAGE/PNG", size: 1234, kind: "image", from: [{ email: "a@example.com", name: "Ada" }, { nope: 1 }],12  subject: "Objet", received_at: "2026-11-01T12:00:00Z", download_url: `/api/jmap/${MB}/download/acc/b1/photo.png?accept=image%2Fpng`, inline_ok: true, ...over,13})1415test("normalizeAttachment : forme du serveur, type en minuscules, entrées inutilisables écartées", () => {16  const a = normalizeAttachment(raw())!17  assert.equal(a.type, "image/png")18  assert.equal(a.inline_ok, true)19  assert.deepEqual(a.from, [{ email: "a@example.com", name: "Ada" }])20  assert.equal(attachmentKey(a), `${MB}:e1:b1`)21  assert.equal(normalizeAttachment(raw({ blob_id: "" })), null)22  assert.equal(normalizeAttachment(null), null)23  assert.equal(normalizeAttachment(raw({ kind: "virus" }))!.kind, "other")24  assert.equal(normalizeAttachment(raw({ name: "", size: -4 }))!.name, "attachment")25  assert.equal(normalizeAttachment(raw({ size: -4 }))!.size, 0)26  // adresse hors de la passerelle : refusée (jamais d'origine tierce, de schéma actif ni de remontée de chemin)27  for (const url of ["https://evil.example/x?accept=a", "javascript:alert(1)", "//evil.example/api/jmap/x", `/api/jmap/${MB}/download/../../admin?accept=a`, "/api/other"]) assert.equal(normalizeAttachment(raw({ download_url: url })), null, url)28})2930test("vignette : seulement les images matricielles sûres", () => {31  for (const t of ["image/png", "image/jpeg", "image/gif", "image/webp", " IMAGE/PNG "]) assert.equal(isInlineSafe(t), true, t)32  for (const t of ["image/svg+xml", "text/html", "application/pdf", "image/png; charset=x", "image/bmp", ""]) assert.equal(isInlineSafe(t), false, t)33  assert.equal(inlineUrl(normalizeAttachment(raw())!), `/api/jmap/${MB}/download/acc/b1/photo.png?accept=image%2Fpng&inline=1`)34  // le serveur dirait « inline_ok » pour un SVG : le type l'emporte35  const svg = normalizeAttachment(raw({ type: "image/svg+xml", name: "x.svg", inline_ok: true }))!36  assert.equal(svg.inline_ok, false)37  assert.equal(inlineUrl(svg), null)38  assert.equal(inlineUrl(normalizeAttachment(raw({ inline_ok: false }))!), null)39  assert.equal(isGatewayUrl(`/api/jmap/${MB}/download/a/b/n.txt?accept=text%2Fplain`), true)40})4142test("famille → glyphe, extension de la tuile", () => {43  for (const k of ATTACHMENT_KINDS) assert.equal(typeof kindGlyph(k), "string")44  assert.equal(kindGlyph("image"), "image")45  assert.equal(kindGlyph("archive"), "zip")46  assert.equal(kindGlyph("spreadsheet"), "grid")47  assert.equal(kindGlyph("nope" as never), "file")48  assert.equal(extensionLabel("Rapport final.PDF"), "PDF")49  assert.equal(extensionLabel("archive.tar.gz"), "GZ")50  assert.equal(extensionLabel("sans-extension"), "")51  assert.equal(extensionLabel("x.extensiontroplongue"), "")52})5354test("previewMode : image, PDF, texte ; jamais HTML ni SVG", () => {55  assert.equal(previewMode("image/png", "a.png"), "image")56  assert.equal(previewMode("application/pdf", "a.pdf"), "pdf")57  assert.equal(previewMode("text/plain", "notes.txt"), "text")58  assert.equal(previewMode("text/csv", "a.csv"), "text")59  assert.equal(previewMode("application/json", "a.json"), "text")60  assert.equal(previewMode("application/octet-stream", "journal.log"), "text")61  assert.equal(previewMode("application/octet-stream", "a.bin"), "none")62  for (const [t, n] of [["text/html", "a.html"], ["image/svg+xml", "a.svg"], ["text/plain", "piege.html"], ["text/plain", "piege.svg"], ["application/xhtml+xml", "a"], ["text/xml", "a.xml"], ["text/javascript", "a.txt"], ["application/zip", "a.zip"], ["image/bmp", "a.bmp"]]) {63    assert.equal(previewMode(t, n), "none", `${t} ${n}`)64  }65  const p = previewOf(normalizeAttachment(raw())!)66  assert.equal(p.mode, "image")67  assert.equal(p.inlineUrl?.endsWith("&inline=1"), true)68  assert.equal(p.from, "Ada")69  assert.equal(previewOf(normalizeAttachment(raw({ type: "application/pdf", name: "a.pdf", inline_ok: false }))!).inlineUrl, null)70})7172test("decodeTextPreview : borne, troncature, octets invalides", () => {73  const enc = new TextEncoder()74  assert.deepEqual(decodeTextPreview(enc.encode("été"), 4), { text: "été", truncated: false })75  assert.deepEqual(decodeTextPreview(enc.encode("abcdef"), 6, 4), { text: "abcd", truncated: true })76  assert.equal(decodeTextPreview(enc.encode("abc"), 10_000, 4).truncated, true) // le fichier est plus long que ce qui a été lu77  assert.equal(decodeTextPreview(new Uint8Array([0xff, 0x41, 0x00]), 3).text, "�A�")78  assert.equal(decodeTextPreview(enc.encode("bom"), 6).text, "bom")79})8081test("filtres → paramètres", () => {82  const f = emptyGalleryFilter()83  assert.equal(isFiltered(f), false)84  assert.deepEqual(galleryParams(f, "America/Toronto"), { mailboxes: undefined, kind: undefined, q: "", from: "", after: undefined, limit: GALLERY_PAGE, cursor: undefined, tz: "America/Toronto" })85  const g = { q: " facture ", kinds: ["pdf" as const, "image" as const, "pdf" as const], size: "large" as const, from: " ada@example.com ", date: "30d" as const, mailbox: MB }86  assert.equal(isFiltered(g), true)87  assert.deepEqual(galleryParams(g, "UTC", "CUR", 10), { mailboxes: MB, kind: "pdf,image", min_size: 1048576, max_size: 10485759, q: "facture", from: "ada@example.com", after: "30d", limit: 10, cursor: "CUR", tz: "UTC" })88  assert.deepEqual(sizeBounds("any"), {})89  assert.deepEqual(sizeBounds("small"), { max_size: 102399 })90  assert.deepEqual(sizeBounds("medium"), { min_size: 102400, max_size: 1048575 })91  assert.deepEqual(sizeBounds("huge"), { min_size: 10485760 })92  for (const k of ["q", "from"] as const) assert.equal(isFiltered({ ...f, [k]: "x" }), true)93})9495test("ZIP : limites de la sélection, corps de la demande", () => {96  assert.equal(zipProblem([]), "empty")97  assert.equal(zipProblem([{ size: 1 }]), null)98  assert.equal(zipProblem(Array.from({ length: ZIP_MAX_ITEMS }, () => ({ size: 1 }))), null)99  assert.equal(zipProblem(Array.from({ length: ZIP_MAX_ITEMS + 1 }, () => ({ size: 1 }))), "too_many")100  assert.equal(zipProblem([{ size: ZIP_MAX_BYTES }]), null)101  assert.equal(zipProblem([{ size: ZIP_MAX_BYTES }, { size: 1 }]), "too_large")102  const a = normalizeAttachment(raw())!103  assert.deepEqual(zipBody([a]), { items: [{ mailbox_id: MB, email_id: "e1", blob_id: "b1", name: "photo.png" }] })104  assert.equal(zipBody([a], "x".repeat(300)).filename?.length, 100)105})106107test("nom de l'archive d'après Content-Disposition", () => {108  assert.equal(filenameFromDisposition("attachment; filename*=UTF-8''pi%C3%A8ces-jointes.zip", "x.zip"), "pièces-jointes.zip")109  assert.equal(filenameFromDisposition('attachment; filename="a b.zip"', "x.zip"), "a b.zip")110  assert.equal(filenameFromDisposition("attachment; filename=plain.zip", "x.zip"), "plain.zip")111  assert.equal(filenameFromDisposition("attachment; filename*=UTF-8''..%2F..%2Fetc%2Fpasswd", "x.zip"), "passwd")112  assert.equal(filenameFromDisposition("attachment; filename*=UTF-8''%E0%A4%A", "x.zip"), "x.zip")113  assert.equal(filenameFromDisposition(null, "x.zip"), "x.zip")114})115116test("PDF : jamais d'aperçu dans la page, le mode reste « pdf » (téléchargement expliqué)", () => {117  assert.equal(previewMode("application/pdf", "rapport.pdf"), "pdf")118})119
Files main
Menu

Theme