SPB Git

Repository Public

attachments.ts 3.5 KB 65 lines typescript

1// Pièces jointes : types dangereux, aperçu en ligne permis, tailles.2const DANGEROUS_EXT = new Set([3  // exécutables et installeurs4  "exe", "scr", "com", "pif", "bat", "cmd", "msi", "msp", "mst", "cpl", "dll", "sys", "drv", "gadget", "app", "pkg", "dmg", "deb", "rpm", "apk", "ipa", "appx", "msix", "run", "bin", "elf",5  // scripts6  "js", "jse", "mjs", "vbs", "vbe", "vb", "wsf", "wsh", "wsc", "ps1", "psm1", "psd1", "sh", "bash", "zsh", "csh", "command", "py", "pyc", "pl", "rb", "php", "jar", "jnlp", "hta", "scpt", "applescript", "action", "workflow",7  // documents à macros8  "docm", "dotm", "xlsm", "xltm", "xlam", "xll", "pptm", "potm", "ppam", "ppsm", "sldm", "accde", "mde",9  // contenu actif interprété par un navigateur ou le système10  "html", "htm", "xhtml", "xht", "shtml", "mht", "mhtml", "svg", "svgz", "xml", "xsl", "swf",11  // raccourcis et conteneurs d'exécution12  "lnk", "url", "scf", "inf", "reg", "iso", "img", "vhd", "vhdx", "chm", "hlp", "one", "onetoc2", "library-ms", "settingcontent-ms", "website", "webloc", "desktop",13])14const DANGEROUS_TYPES = /^(application\/(x-msdownload|x-msdos-program|x-dosexec|x-executable|x-sh|x-shellscript|javascript|x-javascript|ecmascript|java-archive|x-ms-shortcut|hta|vnd\.ms-.*macroenabled.*|x-apple-diskimage|vnd\.microsoft\.portable-executable)|text\/(html|javascript|x-shellscript|xml)|image\/svg\+xml|application\/xhtml\+xml)$/i1516export function extensionOf(name: string): string {17  const base = name.trim().replace(/[\s.]+$/, "")18  const i = base.lastIndexOf(".")19  return i <= 0 ? "" : base.slice(i + 1).toLowerCase()20}2122export type AttachmentRisk = "dangerous-type" | "double-extension" | null2324/** Faut-il avertir avant d'ouvrir cette pièce jointe ? */25export function attachmentRisk(name: string | null | undefined, type: string | null | undefined): AttachmentRisk {26  const n = (name ?? "").trim()27  const ext = extensionOf(n)28  if (ext && DANGEROUS_EXT.has(ext)) {29    // « facture.pdf.exe » : l'extension visible n'est pas la vraie30    const inner = extensionOf(n.slice(0, n.length - ext.length - 1))31    if (inner && /^(pdf|docx?|xlsx?|pptx?|txt|jpe?g|png|gif|zip|csv|rtf|mp[34])$/.test(inner)) return "double-extension"32    return "dangerous-type"33  }34  if (type && DANGEROUS_TYPES.test(type.trim())) return "dangerous-type"35  // nom contenant un caractère d'inversion d'écriture (RLO) qui déguise l'extension36  if (/[\u202a-\u202e\u2066-\u2069]/.test(n)) return "double-extension"37  return null38}3940const INLINE_IMAGE = /^image\/(png|jpeg|gif|webp)$/i41/** Seuls ces types peuvent être affichés dans la page (la passerelle refuse `inline=1` pour le reste). */42export function canPreviewInline(type: string | null | undefined): boolean {43  return !!type && INLINE_IMAGE.test(type.trim())44}4546export function formatSize(bytes: number, locale = "fr"): string {47  if (!Number.isFinite(bytes) || bytes < 0) return ""48  const units = locale.startsWith("fr") ? ["o", "ko", "Mo", "Go", "To"] : ["B", "KB", "MB", "GB", "TB"]49  let v = bytes50  let i = 051  while (v >= 1000 && i < units.length - 1) {52    v /= 100053    i++54  }55  const s = i === 0 ? String(Math.round(v)) : v < 10 ? v.toFixed(1) : String(Math.round(v))56  return `${locale.startsWith("fr") ? s.replace(".", ",") : s}\u00a0${units[i]}`57}5859export const MAX_UPLOAD_BYTES = 50 * 1024 * 10246061export function safeFileName(name: string | null | undefined, fallback = "fichier"): string {62  const n = (name ?? "").replace(/[\u0000-\u001f\u007f/\\:*?"<>|\u202a-\u202e\u2066-\u2069]/g, "_").trim()63  return n.slice(0, 150) || fallback64}65
Files main
Menu

Theme