{"record":{"id":"d7dd11d2a127e378","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-characters-in-file-name","errorCode":"error-invalid-characters-in-file-name","errorMessage":"error-invalid-characters-in-file-name","messagePattern":"error-invalid-characters-in-file-name","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/media/file/functions/sanitizeFileName.ts","lineNumber":15,"sourceCode":"import path from 'node:path';\n\nexport function sanitizeFileName(fileName: string) {\n\tconst base = path.basename(fileName);\n\n\tif (base !== fileName) {\n\t\tthrow new Error('error-invalid-file-name');\n\t}\n\n\tif (base === '.' || base.startsWith('..')) {\n\t\tthrow new Error('error-invalid-file-name');\n\t}\n\n\tif (!/^[a-zA-Z0-9._-]+$/.test(base)) {\n\t\tthrow new Error('error-invalid-characters-in-file-name');\n\t}\n\n\treturn base;\n}\n","sourceCodeStart":1,"sourceCodeEnd":20,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/media/file/functions/sanitizeFileName.ts#L1-L20","documentation":"Final branch of sanitizeFileName (sanitizeFileName.ts:14-16): the (separator-free) name must match /^[a-zA-Z0-9._-]+$/ or it is rejected with error-invalid-characters-in-file-name. Allowed: ASCII letters, digits, dot, underscore, hyphen. Anything else - spaces, unicode letters, '$', '#', parentheses, backslashes - fails. The FileSystem store applies this on every write/read/stat/unlink.","triggerScenarios":"File names with spaces ('my sound.mp3'), special characters ('sound$.mp3', 'file(1).pdf'), unicode/emoji names, or Windows backslash names ('..\\\\passwd') reaching a FileSystem store operation because the caller bypassed the normal client-side name sanitization.","commonSituations":"Upgrades that hardened name rules: uploads stored before the guard now fail on download with legacy names; custom REST scripts and apps-engine apps writing raw names; assets (sounds, emojis) installed with human-readable names containing spaces; non-English file names with accented characters.","solutions":["Sanitize names before upload: strip/replace characters outside [a-zA-Z0-9._-] (e.g. replace with '-')","For legacy records failing on access, rewrite the stored file name in the Uploads/FileStore data to a conforming one (and rename on disk)","Route uploads through Rocket.Chat's own upload flow, which sanitizes the name client-side","Add a client-side regex pre-check identical to the server's to give immediate feedback"],"exampleFix":"// before: uploading 'relatório final (v2).pdf' -> error-invalid-characters-in-file-name\n// after\nconst safe = file.name.replace(/[^a-zA-Z0-9._-]/g, '-').replace(/^\\.+/g, '');\nuploadWithSafeName(file, safe);","handlingStrategy":"validation","validationCode":"const FILE_NAME_RE = /^[a-zA-Z0-9._-]+$/;\nexport const sanitizeName = (name: string): string =>\n  path.basename(name).replace(/[^a-zA-Z0-9._-]/g, '-').replace(/^\\.+/g, '') || 'file';\n\nif (!FILE_NAME_RE.test(name)) name = sanitizeName(name);","typeGuard":"const isSafeStoreFileName = (name: string): name is string =>\n  /^[a-zA-Z0-9._-]+$/.test(name) && name !== '.' && !name.startsWith('..');","tryCatchPattern":"try {\n  store.createWriteStream({ name, ... });\n} catch (error: any) {\n  if (error.message === 'error-invalid-characters-in-file-name') {\n    return retryWith(path.basename(name).replace(/[^a-zA-Z0-9._-]/g, '-'));\n  }\n  throw error;\n}","preventionTips":["Apply the same charset filter client-side for instant feedback","Avoid spaces/unicode in generated download names","Audit legacy upload records after hardening upgrades"],"tags":["file-upload","filename","validation","filesystem-store","security"],"backgroundTag":"invalid-filename-characters","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}