Getting a file into the page is a chain of browser contracts: cancel the right drag events, read from the right DataTransfer surface, distrust the file's self-reported type, and revoke every preview URL you mint. Each link passes a happy-path demo and fails only with nested drop zones, dropped folders, spoofed types, clipboard pastes, or long sessions.
Checklist (lead with the trap; test shapes and evidence framing in references/)
- Cancel dragover or the drop never fires — and guard the window. Cancel
dragoverto make a custom element a drop target, then canceldropto suppress the browser's default handling. Browsers generally do not require cancelingdragenter, even though the drag-and-drop specification describes that step; do not use it as the rationale for whetherdropcan fire. Miss the required cancellation and a file dropped on (or near) the zone can make the browser navigate to / open the file and throw away the page. Add a window-leveldragover+droppreventDefault(or scope-check the target) so a near-miss cannot blow away unsaved state. - Toggle the highlight on a dragenter/dragleave counter, not raw events.
dragenterfires on every child element anddragleavefires when the pointer crosses from the parent into a child, so a naive add/remove-class flickers over nested content. Keep an integer:++ondragenter,--ondragleave, add the highlight when it reaches 1, clear when it returns to 0. Toggle ondragenter, notdragover(dragoverrefires every few hundred ms and forces needless repaints). - Re-set dropEffect on every dragover.
dropEffectdescribes the desired effect for that one dispatch and reverts next tick; setdataTransfer.dropEffect = 'copy'inside thedragoverhandler each time or the cursor won't reflect the operation. UsedropEffect = 'none'to visibly reject a spot. - Read folders through items + webkitGetAsEntry, not files.
DataTransfer.filesis a flatFileListwith no directory support; onlyDataTransfer.items→DataTransferItem.webkitGetAsEntry()(orgetAsFileSystemHandle()) can recurse into a dropped directory, and both are non-standard — feature-detect ('getAsFileSystemHandle' in DataTransferItem.prototype) and fall back.itemsis only live inside thedrop/dragstarthandler, so capture entries synchronously before anyawait. For click-to-select folders use<input webkitdirectory multiple>; it can't also pick loose files and shows a browser trust prompt. - Treat accept and file.type as UX hints, never a type gate.
acceptonly filters the OS picker (MDN is explicit: it does not validate) and drag-drop/paste bypass it entirely.file.typeis guessed from the extension, is spoofable, and is often an empty string. For a real check, sniff magic bytes client-side (Blob.slice(0, N)→arrayBuffer/FileReader→ compare the signature) as a fast pre-filter, and re-validate on the server — that is the trust boundary. Header-only sniffing is still defeated by polyglots. Route active SVG/HTML rendering risk tofrontend-security-baseline; route frontend-owned server upload limits, multipart reconstruction, and relay policy tobff-proxy-security-contracts. - Extract pasted images from clipboardData.items and bound count/size. On the
pasteevent, iterateevent.clipboardData.items, take entries wherekind === 'file'(ortypestarts withimage/), and callgetAsFile(). A single paste can carry both text and an image, so decide precedence rather than assuming one. Enforcefiles.lengthandfile.sizelimits before you read —accept/multipledo not bound how many or how large. - One object URL per preview, and revoke it.
URL.createObjectURLmints a new blob URL every call and pins the blob in memory untilURL.revokeObjectURL; re-creating one per render leaks. Revoke when the preview is replaced and on unmount (React:useEffectcleanup) — but not inside theimgonloadhandler, which breaks right-click / open-in-new-tab. The browser only frees these on document unload, so long single-page sessions accumulate them.
Quick probes
Use as leads; confirm the source-to-sink path before filing.
rg -n "dragover|dragenter|dragleave|onDrop|dataTransfer" src/ app/ 2>/dev/null
rg -n "\.files\b|\.items\b|webkitGetAsEntry|getAsFileSystemHandle|webkitdirectory" src/ app/ 2>/dev/null
rg -n "accept=|\.type\b|magic|signature|FileReader|arrayBuffer\(" src/ app/ 2>/dev/null
rg -n "createObjectURL|revokeObjectURL|clipboardData|onPaste|'paste'" src/ app/ 2>/dev/null
Boundary with sibling skills
- Use file-ingest-contracts for the IN path: the drag-drop event contract,
DataTransferitems/files, directory/multiple, paste-image, and object-URL lifecycle for previews. - Use download-export-safety for the OUT path — generating downloads, Blob/anchor saves, clipboard writes, and export filenames. (
createObjectURLappears in both: ingest previews here, export downloads there.) - Use frontend-security-baseline when untrusted SVG/HTML is rendered as active browser content.
- Use bff-proxy-security-contracts when a frontend-owned server route parses or relays uploads and must bound files, fields, aggregate bytes, headers, redirects, or multipart reconstruction. Magic-byte sniffing on the client is only a pre-filter.
- Use i18n-copy-and-layout for displaying the ingested filename: truncation, bidi/RTL in names, and expansion.
PR-worthiness gate
File an ingest finding only when all hold:
- A real ingest surface exists (drop zone, file input, or paste target) that users reach.
- A specific contract is violated with a user-visible effect: highlight flicker over children, a drop that opens the file / navigates away, a folder drop that silently drops files, a type gate that trusts
accept/file.type, broken paste, or a preview that leaks or goes stale. - Current code has no guard or test for it.
- The fix is narrow: one drop handler, one counter, one
dropEffectline, one items/webkitGetAsEntryswitch, one magic-byte pre-check, or one revoke/cleanup.
Reject weak findings:
- A drop zone with no child elements does not need the counter.
createObjectURLwith a matching revoke on cleanup is a positive control, not a leak.- Missing magic-byte sniffing is not client-side "insecurity" by itself — the server is the trust boundary; file it as UX/robustness or route real content risk to frontend-security-baseline.
acceptwithout server validation is a note, not a client bug, unless the client code claims the type is now trusted.
Minimal useful PR: one failing check — e.g. a drop over a nested child keeps the highlight stable (counter), or a preview URL is revoked on replace/unmount.
Output shape
Return compact findings:
- Ingest contract: drag-event cancel/counter / dropEffect / items-vs-files / type-trust / paste / object-URL lifecycle.
- Evidence: file/line and the ingest-to-sink path.
- Risk: lost page state, flicker, dropped files, wrong-type acceptance, broken paste, retained blob memory / stale preview.
- Fix: smallest handler, counter, feature-detect, pre-check, or revoke.
- Verification: unit/DOM test or manual drop/paste/preview check that would catch the regression.
Sources
- MDN, Drag operations — canceling
dragovermakes a drop target;dropmust also be canceled; browsers diverge from the specification'sdragentercancellation step: https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/Drag_operations - MDN, DataTransfer.dropEffect — set it per
dragover;nonerejects a spot: https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/dropEffect - MDN, dragleave event and File drag and drop — child-element event noise and file-drop reading: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dragleave_event, https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/File_drag_and_drop
- MDN, DataTransferItem.webkitGetAsEntry — directories via
items; non-standard: https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry - MDN,
DataTransferItem.getAsFileSystemHandle()— file/directory handles for dragged items; feature-detect before use: https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/getAsFileSystemHandle - MDN, accept attribute — a hint for the picker, not validation; back with server-side checks: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/accept
- OWASP File Upload Cheat Sheet — do not trust
Content-Type; validate type/size server-side: https://cheatsheetseries.owasp.org/cheatsheets/File_Upload_Cheat_Sheet.html - MDN, Element paste event and ClipboardEvent.clipboardData —
clipboardData.items→getAsFile(): https://developer.mozilla.org/en-US/docs/Web/API/Element/paste_event, https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent/clipboardData - MDN, URL.createObjectURL and URL.revokeObjectURL — revoke to release; don't revoke too early: https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL_static, https://developer.mozilla.org/en-US/docs/Web/API/URL/revokeObjectURL_static