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:
co-authored by
Claude Fable 5.2
parent
b6f9f34705
commit
97202ee03e
@@ -22,10 +22,19 @@ export type FormFileFailure = {
|
||||
field: string
|
||||
/** The file's name as the visitor knows it (cleaned of control characters). */
|
||||
file?: string
|
||||
kind: 'file_count' | 'file_too_large' | 'file_type' | 'required' | 'unknown_fields'
|
||||
/**
|
||||
* - `file_too_large` — over the limit (`max`, MB)
|
||||
* - `file_type` — a format the field does not accept (`types`)
|
||||
* - `file_refused` — the right extension but not acceptable: the content does not match it, macros, a
|
||||
* program, an archive, a doubled extension, or the project's scanner said no
|
||||
* - `file_count` — more files than the field takes (`max`)
|
||||
*/
|
||||
kind: 'file_count' | 'file_refused' | 'file_too_large' | 'file_type' | 'required' | 'unknown_fields'
|
||||
/** The limit: MB for file_too_large, a count for file_count. */
|
||||
max?: number
|
||||
/** "PDF, DOC, DOCX" — for file_type. */
|
||||
/** True when the field is not required — the visitor may send the form without the file. */
|
||||
optional?: boolean
|
||||
/** "PDF, DOC, DOCX" — the formats the field accepts. */
|
||||
types?: string
|
||||
}
|
||||
|
||||
@@ -79,34 +88,35 @@ export function createFileGate({
|
||||
const file = displayName(rawName)
|
||||
const count = (counts.get(fieldName) ?? 0) + 1
|
||||
counts.set(fieldName, count)
|
||||
if (count > limits.maxFiles) {return { failure: { field: fieldName, kind: 'file_count', max: limits.maxFiles } }}
|
||||
const optional = !field.required
|
||||
if (count > limits.maxFiles) {return { failure: { field: fieldName, kind: 'file_count', max: limits.maxFiles, optional } }}
|
||||
|
||||
const types = describeFileTypes(limits.allowedTypes, options.types).labels
|
||||
const typeFailure: FormFileFailure = { field: fieldName, file, kind: 'file_type', types }
|
||||
const refusedFailure: FormFileFailure = { field: fieldName, file, kind: 'file_refused', optional, types }
|
||||
const match = matchFileType(rawName, limits.allowedTypes, options.types, options.risky)
|
||||
if ('refused' in match) {return { failure: typeFailure }}
|
||||
if ('refused' in match) {return { failure: match.refused === 'type' ? { ...refusedFailure, kind: 'file_type' } : refusedFailure }}
|
||||
|
||||
const maxBytes = Math.max(0, Math.min(limits.maxBytes, limits.maxTotalBytes - total))
|
||||
const tooLarge = (): FormFileFailure => ({ field: fieldName, file, kind: 'file_too_large', max: limitInMB(limits.maxBytes) })
|
||||
const tooLarge = (): FormFileFailure => ({ field: fieldName, file, kind: 'file_too_large', max: limitInMB(limits.maxBytes), optional })
|
||||
let checked = false
|
||||
return {
|
||||
slot: {
|
||||
checkStart: (head) => {
|
||||
if (checked || head.length < Math.min(SIGNATURE_BYTES, 4)) {return null}
|
||||
checked = true
|
||||
return looksExecutable(head) || signatureMatches(match.key, head.subarray(0, SIGNATURE_BYTES)) === false ? typeFailure : null
|
||||
return looksExecutable(head) || signatureMatches(match.key, head.subarray(0, SIGNATURE_BYTES)) === false ? refusedFailure : null
|
||||
},
|
||||
maxBytes,
|
||||
tooLarge,
|
||||
},
|
||||
token: { field: fieldName, file, key: match.key, maxBytes, rawName, tooLarge, typeFailure },
|
||||
token: { field: fieldName, file, key: match.key, maxBytes, rawName, refusedFailure, tooLarge },
|
||||
}
|
||||
}
|
||||
|
||||
const finish = async (token: BeginToken, data: Uint8Array): Promise<FormFileFailure | null> => {
|
||||
if (data.length > token.maxBytes) {return token.tooLarge()}
|
||||
const type = options.types[token.key]
|
||||
if ((await checkFileContent(token.key, type, data, { strictPdf: options.strictPdf })) !== null) {return token.typeFailure}
|
||||
if ((await checkFileContent(token.key, type, data, { strictPdf: options.strictPdf })) !== null) {return token.refusedFailure}
|
||||
const file: AcceptedFormFile = {
|
||||
name: safeFileName(token.rawName, type.extensions[0]),
|
||||
type: token.key,
|
||||
@@ -118,9 +128,9 @@ export function createFileGate({
|
||||
if (options.scan) {
|
||||
try {
|
||||
const verdict = await options.scan({ name: file.name, type: file.type, data, field: file.field, size: file.size })
|
||||
if (!verdict?.ok) {return token.typeFailure}
|
||||
if (!verdict?.ok) {return token.refusedFailure}
|
||||
} catch {
|
||||
return token.typeFailure
|
||||
return token.refusedFailure
|
||||
}
|
||||
}
|
||||
total += data.length
|
||||
@@ -145,8 +155,8 @@ type BeginToken = {
|
||||
key: string
|
||||
maxBytes: number
|
||||
rawName: string
|
||||
refusedFailure: FormFileFailure
|
||||
tooLarge: () => FormFileFailure
|
||||
typeFailure: FormFileFailure
|
||||
}
|
||||
|
||||
export type FileGate = ReturnType<typeof createFileGate>
|
||||
|
||||
Reference in New Issue
Block a user