WIP: form attachments (e-mail only) — core, endpoint, Graph large upload
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
0ba0014ed5
commit
6003b3d7d7
@@ -0,0 +1,149 @@
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
import type { ClientIpOptions } from '../../shared/rateLimit.js'
|
||||
import type { FormFileType } from './fileTypes.js'
|
||||
|
||||
import { FORM_FILE_TYPES, MB, RISKY_FILE_EXTENSIONS } from './fileTypes.js'
|
||||
|
||||
/** A file handed to `attachments.scan` — in memory, after every other check passed. */
|
||||
export type FormFileScanInput = {
|
||||
data: Uint8Array
|
||||
/** The field key of the File field it came from. */
|
||||
field: string
|
||||
/** The cleaned name the attachment will carry. */
|
||||
name: string
|
||||
size: number
|
||||
/** Type key, e.g. 'pdf'. */
|
||||
type: string
|
||||
}
|
||||
|
||||
export type FormFileScanResult = { ok: boolean; reason?: string }
|
||||
|
||||
/**
|
||||
* `ipalKit({ forms: { attachments } })` — files on panel forms. They are
|
||||
* NEVER stored: a file is checked, attached to the company e-mail, and dropped.
|
||||
*/
|
||||
export type FormAttachmentsOption = {
|
||||
/**
|
||||
* Extensions from the refused list (RISKY_FILE_EXTENSIONS: programs, macro-enabled
|
||||
* Office files, archives, HTML/SVG…) this project accepts after all, e.g. `['zip']`.
|
||||
* The extension still has to belong to a file type of the field (`fileTypes`).
|
||||
*/
|
||||
allowRisky?: string[]
|
||||
/**
|
||||
* Add a file type (`{ dwg: { label, extensions, mimeType, detect } }`), replace
|
||||
* a built-in one, or remove one with `false`.
|
||||
*/
|
||||
fileTypes?: Record<string, false | FormFileType>
|
||||
/** File submissions handled at the same time, per instance. More get "try again". Default 2; 0 = no guard. */
|
||||
maxConcurrent?: number
|
||||
/** Ceiling for a field's "Maximum files". Default 3. */
|
||||
maxFiles?: number
|
||||
/** Ceiling for a field's "Maximum size (MB)". Default 10. */
|
||||
maxSizeMB?: number
|
||||
/** Ceiling for all files of one submission, in MB. Default 10. */
|
||||
maxTotalMB?: number
|
||||
/**
|
||||
* The line added to the e-mail that carries a file ("…was not checked for
|
||||
* viruses"). `false` leaves it out; a string or a function replaces it.
|
||||
*/
|
||||
notice?: ((locale: string | undefined) => string) | false | string
|
||||
/** Path of the submit endpoint under the API route. Default '/ipal/forms/submit'. */
|
||||
path?: string
|
||||
/**
|
||||
* Antivirus hook. The plugin has NO malware scanner; plug one in here
|
||||
* (ClamAV, a scanning API). Return `{ ok: false }` to refuse the file.
|
||||
* A thrown error refuses the file too.
|
||||
*/
|
||||
scan?: (file: FormFileScanInput) => FormFileScanResult | Promise<FormFileScanResult>
|
||||
/**
|
||||
* The submit endpoint's gates — the same options createSubmitAction takes,
|
||||
* so both paths behave alike. `allowedOrigins`: other origins that may post
|
||||
* to it (a front end on another domain); the site's own origin always may.
|
||||
*/
|
||||
submit?: FormSubmitEndpointOptions
|
||||
/** Refuse PDFs that declare JavaScript, launch actions or embedded files (a byte search). Default false. */
|
||||
strictPdf?: boolean
|
||||
}
|
||||
|
||||
export type FormSubmitEndpointOptions = {
|
||||
allowedOrigins?: string[]
|
||||
clientIp?: ClientIpOptions
|
||||
/** Honeypot field name. Default HONEYPOT_FIELD. */
|
||||
honeypotField?: string
|
||||
/** Max submissions per IP per minute. Default 5; 0 turns it off. */
|
||||
maxPerMinute?: number
|
||||
turnstileHostnames?: string[]
|
||||
turnstileOnConfigError?: 'allow' | 'reject'
|
||||
}
|
||||
|
||||
export type ResolvedFormAttachments = {
|
||||
maxConcurrent: number
|
||||
maxFiles: number
|
||||
maxSizeMB: number
|
||||
maxTotalMB: number
|
||||
notice: FormAttachmentsOption['notice']
|
||||
path: string
|
||||
risky: ReadonlySet<string>
|
||||
scan?: FormAttachmentsOption['scan']
|
||||
strictPdf: boolean
|
||||
submit: FormSubmitEndpointOptions
|
||||
types: Record<string, FormFileType>
|
||||
}
|
||||
|
||||
export const FORM_SUBMIT_PATH = '/ipal/forms/submit'
|
||||
|
||||
const positive = (value: number | undefined, fallback: number) => (typeof value === 'number' && value > 0 ? value : fallback)
|
||||
|
||||
/** Defaults applied; `true` = everything default. */
|
||||
export function resolveFormAttachments(option: FormAttachmentsOption | true): ResolvedFormAttachments {
|
||||
const o = option === true ? {} : option
|
||||
const types: Record<string, FormFileType> = { ...FORM_FILE_TYPES }
|
||||
for (const [key, type] of Object.entries(o.fileTypes ?? {})) {
|
||||
if (type === false) {delete types[key]}
|
||||
else {types[key] = { ...type, extensions: type.extensions.map((e) => e.replace(/^\./, '').toLowerCase()) }}
|
||||
}
|
||||
const allowed = new Set((o.allowRisky ?? []).map((e) => e.replace(/^\./, '').toLowerCase()))
|
||||
return {
|
||||
maxConcurrent: typeof o.maxConcurrent === 'number' && o.maxConcurrent >= 0 ? o.maxConcurrent : 2,
|
||||
maxFiles: Math.floor(positive(o.maxFiles, 3)),
|
||||
maxSizeMB: positive(o.maxSizeMB, 10),
|
||||
maxTotalMB: positive(o.maxTotalMB, positive(o.maxSizeMB, 10)),
|
||||
notice: o.notice,
|
||||
path: o.path ? `/${o.path.replace(/^\/+/, '')}` : FORM_SUBMIT_PATH,
|
||||
risky: new Set(RISKY_FILE_EXTENSIONS.filter((e) => !allowed.has(e))),
|
||||
scan: o.scan,
|
||||
strictPdf: o.strictPdf === true,
|
||||
submit: o.submit ?? {},
|
||||
types,
|
||||
}
|
||||
}
|
||||
|
||||
/** The resolved option of this Payload instance, or null when `forms.attachments` is off. */
|
||||
export function getFormAttachments(payload: BasePayload): null | ResolvedFormAttachments {
|
||||
const value = (payload.config?.custom as { ipal?: { formAttachments?: ResolvedFormAttachments } } | undefined)?.ipal?.formAttachments
|
||||
return value ?? null
|
||||
}
|
||||
|
||||
/** A File field as the panel stores it. */
|
||||
export type FileFieldConfig = {
|
||||
allowedTypes?: null | string[]
|
||||
maxFiles?: null | number
|
||||
maxSizeMB?: null | number
|
||||
name?: null | string
|
||||
}
|
||||
|
||||
/**
|
||||
* The limits that really apply to a File field: the editor's values, capped by
|
||||
* the plugin option and by what the e-mail transport can carry (`transportMaxBytes`).
|
||||
*/
|
||||
export function fileFieldLimits(field: FileFieldConfig, options: ResolvedFormAttachments, transportMaxBytes?: null | number) {
|
||||
const sizeMB = Math.min(positive(field.maxSizeMB ?? undefined, options.maxSizeMB), options.maxSizeMB)
|
||||
const cap = typeof transportMaxBytes === 'number' ? transportMaxBytes : Infinity
|
||||
return {
|
||||
allowedTypes: (field.allowedTypes ?? []).filter((key) => options.types[key]),
|
||||
maxBytes: Math.floor(Math.min(sizeMB * MB, cap)),
|
||||
maxFiles: Math.max(1, Math.min(Math.floor(positive(field.maxFiles ?? undefined, 1)), options.maxFiles)),
|
||||
maxTotalBytes: Math.floor(Math.min(options.maxTotalMB * MB, cap)),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user