You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: use single statSync in handleFileDownload to avoid TOCTOU crash - #479
handleFileDownload ran fs.existsSync and then two fs.statSync calls on the same path. If the file was removed between the check and the second statSync, an unhandled ENOENT was thrown and the request crashed. It also made three blocking filesystem calls for every download.
Fix
Replaced the three calls with a single fs.statSync inside a try/catch. A failure now returns 404 File not found.
The resulting Stats object is reused for both the isFile() check and Content-Length.
Testing
tsc --noEmit passes for the changed file.
Downloading an existing file works as before; a missing path or a directory returns 404.
Summary by CodeRabbit
Bug Fixes
File downloads now return a not-found response when the requested path cannot be accessed or does not point to a file. This provides consistent handling for unavailable download paths and prevents those cases from being treated as valid file downloads.
Downloads now return 404 when a file is missing or removed mid-request, instead of risking an unhandled error. The change is narrow and matches its stated intent, so merge risk is minimal.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: replacing the duplicate synchronous stat checks in handleFileDownload with a single statSync to avoid the TOCTOU crash. The raw summary confirms this single-statSync c…
Description check
The description follows the required structure in substance. It links the issue (Closes #471), and it explains the problem and the fix. It also includes a Testing section with what was verified. It om…
Linked Issues check
Issue #471 requires that handleFileDownload in src/server/signalling/handlers/fileShare.ts call fs.statSync once inside a try/catch, return 404 when the stat call fails, reuse the Stats object for the…
Out of Scope Changes check
The whole-PR diff against the merge base changes only src/server/signalling/handlers/fileShare.ts, with a hunk of +8/-2 lines inside handleFileDownload. Every changed line implements the stat-call con…
Full details: Title check
Explanation
The title names the main change: replacing the duplicate synchronous stat checks in handleFileDownload with a single statSync to avoid the TOCTOU crash. The raw summary confirms this single-statSync change in fileShare.ts, and the objectives describe the same change.
Full details: Description check
Explanation
The description follows the required structure in substance. It links the issue (Closes #471), and it explains the problem and the fix. It also includes a Testing section with what was verified. It omits the template's Screenshots section, the Functional Verification checklist, the Additional Notes section, and the Checklist. These omissions are acceptable for a low-effort backend change with no UI impact, so the description is mostly complete.
Full details: Linked Issues check
Explanation
Issue #471 requires that handleFileDownload in src/server/signalling/handlers/fileShare.ts call fs.statSync once inside a try/catch, return 404 when the stat call fails, reuse the Stats object for the isFile() check and file size, and return 404 when the path is not a file. The diff at the reviewed head meets each requirement. The fs.existsSync call and the second fs.statSync call are removed. A single fs.statSync call is wrapped in try/catch, and the catch returns json(res, 404, { error: "File not found" }). The stat.isFile() check returns the same 404. The existing stat object continues to supply the file size for Content-Length, because the diff leaves that code unchanged. The issue does not request automated tests, and the PR does not add any. No coding requirement from #471 remains unmet.
Full details: Out of Scope Changes check
Explanation
The whole-PR diff against the merge base changes only src/server/signalling/handlers/fileShare.ts, with a hunk of +8/-2 lines inside handleFileDownload. Every changed line implements the stat-call consolidation required by #471. No unrelated changes, refactors, or documentation edits appear in the diff. The changes are within scope.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
A rabbit checks the file path once One stat result guides the way If the path is missing, 404 If it is not a file, 404 The download handler hops along With fewer checks in its day
Reviewing files that changed from the base of the PR and between a31bc71 and add145f.
Files selected for processing: 1
src/server/signalling/handlers/fileShare.ts
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.
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
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.
Closes #471
Problem
handleFileDownloadranfs.existsSyncand then twofs.statSynccalls on the same path. If the file was removed between the check and the secondstatSync, an unhandledENOENTwas thrown and the request crashed. It also made three blocking filesystem calls for every download.Fix
fs.statSyncinside atry/catch. A failure now returns404 File not found.Statsobject is reused for both theisFile()check andContent-Length.Testing
tsc --noEmitpasses for the changed file.Summary by CodeRabbit