DEV Community

Cover image for One Copied Helper Broke File Pickers on 85 Pages — the Live FileList Race
Shayan Ali
Shayan Ali

Posted on

One Copied Helper Broke File Pickers on 85 Pages — the Live FileList Race

The file picker did nothing. Drag-and-drop worked fine, on the same pages, through the same helper. That asymmetry was the whole clue.

The reports came in one tool at a time. Click the upload area, pick a file, nothing happens. No error, no console noise, no half-loaded state — just nothing. And on the very same pages, dragging the same file onto the same upload area worked perfectly. Same helper, same pages, two behaviors. When the same code fails one way and works another, the difference between the two paths is the bug.

The helper

ToolNest is 146 free tools, all client-side, and the upload tools share one small helper, copied into each page. It wires a drop zone and a hidden file input to the same callback:

function wireDropzone(dz, input, onFiles) {
  dz.addEventListener("click", function() { input.click(); });
  dz.addEventListener("drop", function(e) {
    if (e.dataTransfer.files.length) onFiles(e.dataTransfer.files);
  });
  input.addEventListener("change", function() {
    var picked = Array.prototype.slice.call(input.files);
    input.value = "";
    if (picked.length) onFiles(picked);
  });
}
Enter fullscreen mode Exit fullscreen mode

That is the fixed version, verbatim from the live source. The change handler snapshots the files into a plain array before clearing the input. The old version did it the other way round: it passed the live input.files straight to the handler, and then cleared input.value — and the handler was async.

Why that order breaks everything

FileList is a live object. It is not a snapshot of the files you picked; it is a view onto the input's current selection. When you set input.value = "", the browser resets the selection — and every reference to that FileList, including the one you just handed to your handler, now reports a length of zero. The files are not deleted. The view is just empty, because the selection it views no longer exists.

Now add async. The handler awaits something — a library loading, a file being read — and the await yields control. The clear has already run synchronously, right after the handler was invoked. When the handler resumes and reaches for files[0], the list is empty. No exception is thrown. An empty list is a perfectly valid list, so the handler runs its "no files" path and does nothing, silently. That silence is why the bug survived: nothing in the console, nothing on the page, just a picker that appears dead.

And that is exactly why drag-and-drop kept working. The drop branch passes e.dataTransfer.files — a FileList from the drop event, not from the input. Clearing input.value does not touch it, because it was never the input's list. Two branches, two different FileList provenances, one shared clear. The picker failed; the drop sailed through. If you only ever tested drag-and-drop, the bug was invisible.

How it was found

It was not found by a page-load check. Every affected page loaded fine, returned 200, rendered correctly, and showed a working-looking upload area. It was found by an audit that operated every control with real inputs: pick a real file through the picker, watch what happens, assert the outcome. The picker path failed on every upload tool that used the helper. The drop path worked on all of them. That split pointed straight at the helper, and the helper pointed straight at the ordering.

The fix was one line in each copy — snapshot before clearing — applied across all 85 definitions of the helper in the tree. Verification was the same method as discovery: drive the picker with real files on every upload tool, confirm the files arrive, and grep the whole tree to confirm zero remaining live-FileList passes. The locale pages needed no fix — the helper appears in only four text-to-speech pages there, already in the fixed pattern and never called, and the 104 locale tool pages have no file inputs at all.

The rule, generalized

Snapshot before you clear. Any time a handler is async, treat every live DOM collection it touches as borrowed, not owned: FileList, HTMLCollection, NodeList from the live query methods. Copy what you need into a plain array first, synchronously, and then do whatever cleanup you like:

// Snapshot first — a plain array is yours, a FileList is on loan.
var picked = Array.prototype.slice.call(input.files);
input.value = "";
if (picked.length) onFiles(picked);
Enter fullscreen mode Exit fullscreen mode

[...input.files] does the same job. The reverse direction — building a FileList to hand back to an input — goes through DataTransfer, which is the only sanctioned way to construct one. But for reading, the array snapshot is the whole fix.

The deeper lesson

Eighty-five copies of one helper means eighty-five copies of one bug. A helper that lives in one place, imported once, turns this story into a one-line fix with one verification. A helper copied into every page turns it into a fleet-wide patch with a fleet-wide audit. Copy-paste is a distribution mechanism for defects, and the more useful the helper, the wider the distribution.

The other lesson is about what counts as a check. A page that loads, renders, and shows the right title can still have a dead upload button, a calculator that never computes, or a search that matches nothing. The audit that found this bug — and a second one the same day, a search that matched whole queries as single substrings and returned nothing for multi-word phrases — operated every button, input, and form with real inputs and asserted the real outcome. Page-load checks are not an audit. Operating the thing is the audit.

The tools are at ToolNest, and the page where the helper above lives is the PDF merger — picker, drop zone, and all. If you have a file input anywhere in your own code, go read its change handler. If it passes input.files to anything async and clears the input afterwards, you have this bug. Snapshot first.

Top comments (0)