Form files: the visitor is told when the problem is the file — why and what to do; UTF-8 file names

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-13 16:51:05 +01:00
co-authored by Claude Fable 5.2
parent b6f9f34705
commit 97202ee03e
18 changed files with 335 additions and 128 deletions
+9 -6
View File
@@ -145,26 +145,29 @@ export type FileRefusal =
/**
* Name check, before any byte of the file: the last extension must belong to
* one of the allowed types, and NO extension of the name may be a risky one
* ("cv.pdf.exe", "cv.exe.pdf") unless the project allowed it.
* one of the allowed types (else `type`: simply another format), and NO
* extension of the name may be a risky one ("cv.pdf.exe", "cv.exe.pdf") unless
* the project allowed it, nor a second known one ("cv.pdf.docx") — else `refused`.
*/
export function matchFileType(
name: string,
allowed: readonly string[],
types: Record<string, FormFileType>,
risky: ReadonlySet<string>,
): { key: string; type: FormFileType } | { refused: 'name' } {
): { key: string; type: FormFileType } | { refused: 'refused' | 'type' } {
const extensions = fileExtensions(name.split(/[/\\]/).pop() ?? '')
const last = extensions.at(-1)
if (!last || extensions.some((e) => risky.has(e))) {return { refused: 'name' }}
// A program, an archive, a macro-enabled file — refused as such, not as "another format".
if (extensions.some((e) => risky.has(e))) {return { refused: 'refused' }}
if (!last) {return { refused: 'type' }}
// "cv.pdf.docx": an extra extension that names another known type hides what the file is.
const known = new Set(Object.values(types).flatMap((t) => t.extensions))
if (extensions.slice(0, -1).some((e) => known.has(e))) {return { refused: 'name' }}
if (extensions.slice(0, -1).some((e) => known.has(e))) {return { refused: 'refused' }}
for (const key of allowed) {
const type = types[key]
if (type?.extensions.includes(last)) {return { type, key }}
}
return { refused: 'name' }
return { refused: 'type' }
}
/** Whole-file check, once the file is in memory. Returns the refusal, or null when it passes. */