Skip to content

fs: check every path in recursive operations - #67932

Open
ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:fs-recursive-permission-checks
Open

ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:fs-recursive-permission-checks

Conversation

@ShogunPanda

Copy link
Copy Markdown
Contributor

When the permission model is enabled, some recursive node:fs operations only checked the root path and then walked the tree natively, so their behavior was inconsistent with the equivalent per-path APIs and with fs.cp():

  • fs.cpSync() (recursive, no filter) now uses the per-entry JavaScript copy path when the permission model is enabled, so every source read and destination write is checked, mirroring fs.cp().
  • Recursive fs.rmSync() now checks every descendant before removing the tree, and denies the removal if the tree cannot be fully walked.
  • Recursive fs.mkdir(), fs.mkdirSync() and fs.promises.mkdir() now check each missing parent directory before creating it, not only the final path.

fs.rm() and fs.promises.rm() are not changed: they already remove entries one by one through the regular per-path fs operations, which are checked individually. A test case for fs.promises.rm() is included.

Behavior with the permission model disabled is unchanged (the native fast paths are kept).

New tests cover exact grants on paths that do not exist at startup, entries created after startup, relative paths after process.chdir(), a directory tree that cannot be fully walked, and wildcard-grant controls.

Release notes

With --permission, recursive fs.cpSync(), fs.rmSync() and fs.mkdir()/fs.mkdirSync() now throw ERR_ACCESS_DENIED when a descendant, or a parent directory that would be created, is outside the granted paths. Recursive fs.rmSync() also throws ERR_ACCESS_DENIED when it cannot walk the whole tree to check it.

When the permission model is enabled, fs.cpSync() now uses the
per-entry JavaScript copy path instead of the native recursive copy,
so every source and destination path is checked, matching fs.cp().

Recursive fs.rmSync() now checks every descendant before removing the
tree, and denies the removal if the tree cannot be fully walked.
Recursive fs.mkdir(), fs.mkdirSync() and fs.promises.mkdir() check
each missing parent directory before creating it.

Signed-off-by: Paolo Insogna <paolo@cowtech.it>
Assisted-By: OpenAI:GPT-6-Sol <openai/gpt-6-sol>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/security-wg

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run. labels Dec 1, 2026
@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.52459% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.58%. Comparing base (d4318d9) to head (62b5a72).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/node_file.cc 87.93% 3 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67932      +/-   ##
==========================================
- Coverage   90.59%   90.58%   -0.02%     
==========================================
  Files         802      802              
  Lines      286656   286714      +58     
  Branches    55383    55394      +11     
==========================================
+ Hits       259710   259721      +11     
- Misses      17058    17094      +36     
- Partials     9888     9899      +11     
Files with missing lines Coverage Δ
lib/internal/fs/cp/cp-sync.js 71.98% <100.00%> (ø)
src/node_file.cc 76.93% <87.93%> (+0.20%) ⬆️

... and 34 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.

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

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. 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.

2 participants