Skip to content

fix: use single statSync in handleFileDownload to avoid TOCTOU crash - #479

Merged
PinJinx merged 1 commit into
AOSSIE-Org:mainfrom
RITVIKKAMASETTY:fix/download-single-statsync
Nov 21, 2026
Merged

PinJinx merged 1 commit into
AOSSIE-Org:mainfrom
RITVIKKAMASETTY:fix/download-single-statsync

Conversation

@RITVIKKAMASETTY

@RITVIKKAMASETTY RITVIKKAMASETTY commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Closes #471

Problem

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.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The path-download handler now calls fs.statSync once inside a try/catch. It returns 404 if the call fails or the path is not a file.

Changes

File download handling

Layer / File(s) Summary
File metadata lookup
src/server/signalling/handlers/fileShare.ts
The handler calls fs.statSync once and returns 404 if the call fails or the path is not a file.

Estimated code review effort: 2 (Simple) | ~5 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested labels: Typescript Lang

Suggested reviewers: pinjinx

Merge Risk: Minimal · up to add14

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed 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 Passed 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.

Share

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

Recent review info

Run configuration

Commits

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.

@PinJinx
PinJinx merged commit 04bcd81 into AOSSIE-Org:main Nov 21, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: handleFileDownload calls fs.statSync twice on the same path — TOCTOU race and redundant syscall

2 participants