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.

@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 91.93548% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.58%. Comparing base (d4318d9) to head (c554efa).
⚠️ Report is 55 commits behind head on main.

Files with missing lines Patch % Lines
src/node_file.cc 91.52% 2 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67932      +/-   ##
==========================================
- Coverage   90.59%   90.58%   -0.02%     
==========================================
  Files         802      802              
  Lines      286656   287061     +405     
  Branches    55383    55443      +60     
==========================================
+ Hits       259710   260043     +333     
- Misses      17058    17119      +61     
- 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% <91.52%> (+0.21%) ⬆️

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

@ShogunPanda
ShogunPanda force-pushed the fs-recursive-permission-checks branch from 62b5a72 to 357b0dd Compare December 3, 2026 08:02
@ShogunPanda ShogunPanda 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 Dec 3, 2026

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
mcollina requested a review from RafaelGSS December 3, 2026 15:06
@mcollina

mcollina commented Dec 3, 2026

Copy link
Copy Markdown
Member

@ShogunPanda codecov reports that some lines needs more testing, can you add?

@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 Dec 3, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

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.

Assisted-By: OpenAI GPT-6 Sol <openai/gpt-6-sol>
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
@ShogunPanda
ShogunPanda force-pushed the fs-recursive-permission-checks branch from 357b0dd to c554efa Compare December 3, 2026 18:28

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina 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 Dec 4, 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 Dec 4, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@RafaelGSS RafaelGSS left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add --permission-audit variants of these tests?

FYI isEnabled() is also true in audit mode

Comment thread src/node_file.cc
@@ -1834,6 +1834,33 @@ static void ClearReadOnlyAttributeW(const std::filesystem::path& path,
}
#endif

static std::optional<std::string> DeniedRmPath(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This denies under --permission-audit. warning_only() is only checked for the per-entry case, so a tree that can't be walked still throws

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.

4 participants