Fix race condition in getFileIDs that crashes Node.js process on multi-file uploads#33
Open
jnavarrete-ep wants to merge 1 commit intoSendSafely:masterfrom
Open
Conversation
encryptAndUploadFiles with >=2 file paths can crash the Node.js process
with an unhandled TypeError "Cannot read properties of undefined (reading
'name')" at lib/SendSafely.js:2902. Three compounding bugs in getFileIDs
together produce the crash:
1. Index/array mismatch. The non-legacy branch pushed to files in
fs.stat completion order, then called handleResponse(i, ..., files)
where i is the input index. When completion order != input order,
files[i] was undefined. Fixed by assigning files[i] = file.
2. Broken sequential chain. Inside _loop, the line p = _p wrote to an
undeclared identifier. In sloppy mode this silently created a global;
the outer for loop's p never updated, so every iteration ran in
parallel against the initial Promise.resolve(). Fixed by having _loop
return its chained promise and having the for loop reassign p.
3. Unhandled throw. The TypeError escaped via a .then() callback chain
that is not caught upstream, so consumer sendsafely.error and
file.upload.error handlers never saw it. Added a defensive guard in
handleResponse that raises the existing server.error event instead
of throwing when files[index] is undefined.
No public API, event names, or parameter shapes change. The legacy
{size, data} upload path is untouched. Repo has no test suite; verified
manually by calling encryptAndUploadFiles with multiple file paths.
Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SendSafely#encryptAndUploadFileswith ≥2 file-path arguments can crash theNode.js process with an unhandled
TypeError: Cannot read properties of undefined (reading 'name')atlib/SendSafely.js:2902. This PR fixes threecompounding bugs in
getFileIDsthat together produce the crash.Stack trace observed in production (SDK 3.0.3, Node 22.21.1)
Root cause
filesinfs.statcompletion order, then callshandleResponse(i, ..., files, ...)where
iis the input index. When completion order ≠ input order,files[i]isundefined._loop, the linep = _pwrites to anundeclared identifier. In sloppy mode this silently creates a global; the
outer
forloop'spnever updates, so all iterations run in parallelagainst the initial
Promise.resolve(). This maximises the likelihood ofbug Remove duplicate event listener for message event #1 manifesting.
TypeErrorescapes via a.then()callback chainthat isn't caught upstream, so the consumer's
sendsafely.errorandfile.upload.errorhandlers never see it and the process exits.Changes
files[i] = fileinstead offiles.push(file),guaranteeing
files[i]is populated beforehandleResponsereads it.handleResponsegains a defensive guard: iffiles[index]is ever undefined,raise
server.errorinstead of throwing._loopnow returns its promise and theforloop reassignspto thereturn value. Iterations chain sequentially as originally intended.
Risk
Low. No public API changes. The legacy
{size, data}upload path is untouched.No timing-sensitive behaviour changes beyond making iterations actually
sequential (which was already the author's stated intent).
Tests
The repository has no existing test suite. Verified manually by calling
encryptAndUploadFileswith multiple file paths before and after the patch.Before: intermittent process crash. After: all files upload successfully.