Skip to content

fs: reject a mode above INT32_MAX instead of aborting - #67498

Open
yohannesjoya wants to merge 1 commit into
nodejs:mainfrom
yohannesjoya:fs-mode-int32
Open

yohannesjoya wants to merge 1 commit into
nodejs:mainfrom
yohannesjoya:fs-mode-int32

Conversation

@yohannesjoya

Copy link
Copy Markdown
Contributor

A mode between 2³¹ and 2³²−1 passes parseFileMode() (it validates as Uint32) but the C++ bindings CHECK for an Int32, so the process aborts with Assertion failed: args[2]->IsInt32(). This affects fs.open(), fs.openSync(), fs.promises.open(), fs.chmod(), fs.mkdir() and the stream constructors. Reproduced on main with fs.openSync('f', 'w', 0x80000000).

This validates the mode as a non-negative Int32, so the caller gets a RangeError with the real limit. ReadStream/WriteStream validate options.mode in the constructor so the error is thrown where the stream is created. Two existing tests that asserted the old <= 4294967295 message are updated, and a new test covers every affected API; it aborts on main and passes with this change.

Credit: @semimikoh traced this to #52050 in the issue, and @wdskuki's #62538 had the same validateInt32 idea and the stream constructor change (carried here with Co-authored-by). The issue was closed by the stale bot; could a collaborator reopen it?

Fixes: #62516
Refs: #62538

AI disclosure: I used a coding agent to help find the affected call sites and draft the test. I reproduced the abort on main, reviewed the change line by line, and ran the fs test suite locally.

parseFileMode() accepted any 32-bit unsigned integer, but the native
bindings read the mode as a signed 32-bit integer and CHECK that it is
one. A mode between 2**31 and 2**32 - 1 therefore passed validation and
then aborted the process from fs.open(), fs.chmod(), fs.mkdir() and
their sync and promise variants. Validate the mode as a non-negative
Int32 instead so that the caller gets a RangeError.

ReadStream and WriteStream now validate options.mode in the constructor
too, so an invalid mode throws where the stream is created instead of
failing inside the open callback.

Fixes: nodejs#62516
Refs: nodejs#62538
Co-authored-by: wei_ds <wei_ds@126.com>
Signed-off-by: Yohannes Desta <yohannesdestagebru10@gmail.com>
@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run. labels Nov 15, 2026
@codecov

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.67%. Comparing base (c99ebf2) to head (f960b73).
⚠️ Report is 163 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/fs/streams.js 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67498      +/-   ##
==========================================
- Coverage   92.99%   90.67%   -2.32%     
==========================================
  Files         428      799     +371     
  Lines      195633   280245   +84612     
  Branches    30281    54115   +23834     
==========================================
+ Hits       181937   254125   +72188     
- Misses      13369    16238    +2869     
- Partials      327     9882    +9555     
Files with missing lines Coverage Δ
lib/internal/validators.js 97.86% <100.00%> (+10.10%) ⬆️
lib/internal/fs/streams.js 94.80% <66.66%> (+2.20%) ⬆️

... and 553 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jasnell jasnell added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 15, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 16, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Passing large value for mode option in fs.createWriteStream results in crash

3 participants