Skip to content

fs: fix TypeError in glob when directory access is denied #58674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Sylphy-0xd3ac
Copy link

@Sylphy-0xd3ac Sylphy-0xd3ac commented Jun 11, 2025

When a directory cannot be read due to permission issues, the async version of fs.glob() returns null from readdir(), while the sync version returns an empty array. This causes a TypeError when trying to access the 'length' property of null.

Fix by making the async readdir() method return an empty array on error, consistent with the sync version behavior.

Fixes: #58670
Fixes: #58276

@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels Jun 11, 2025
@juanarbol
Copy link
Member

This is nice! Thanks! Would you mind adding a test case? Just make sure that it will never return null

@Sylphy-0xd3ac
Copy link
Author

Sylphy-0xd3ac commented Jun 12, 2025

@juanarbol Done. Added test case

Copy link
Member

@juanarbol juanarbol left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks!

@juanarbol juanarbol added the request-ci Add this label to start a Jenkins CI on a PR. label Jun 13, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 13, 2025
@nodejs-github-bot
Copy link
Collaborator

Copy link

codecov bot commented Jun 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.08%. Comparing base (faada65) to head (0a4f0f9).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #58674      +/-   ##
==========================================
- Coverage   90.09%   90.08%   -0.01%     
==========================================
  Files         640      640              
  Lines      188271   188271              
  Branches    36923    36916       -7     
==========================================
- Hits       169625   169607      -18     
- Misses      11386    11405      +19     
+ Partials     7260     7259       -1     
Files with missing lines Coverage Δ
lib/internal/fs/glob.js 91.98% <100.00%> (ø)

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

@Sylphy-0xd3ac Sylphy-0xd3ac force-pushed the main branch 2 times, most recently from 6b33002 to 7ae4597 Compare June 14, 2025 08:44
@Sylphy-0xd3ac
Copy link
Author

Delete the test-fs-glob-readdir-error-handling.mjs file as its functionality is now covered by the test-fs-glob.mjs

@Ethan-Arrowood Ethan-Arrowood added the request-ci Add this label to start a Jenkins CI on a PR. label Jun 16, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 16, 2025
@nodejs-github-bot
Copy link
Collaborator

@Sylphy-0xd3ac Sylphy-0xd3ac force-pushed the main branch 2 times, most recently from db318e6 to 8c18cc5 Compare June 24, 2025 04:36
Sylphy-0xd3ac pushed a commit to Sylphy-0xd3ac/node that referenced this pull request Jun 24, 2025
When a directory cannot be read due to permission issues, the async
version of fs.glob() returns null from readdir(), while the sync
version returns an empty array. This causes a TypeError when trying
to access the 'length' property of null.

PR-URL: nodejs#58674
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ethan-Arrowood <[email protected]>
Reviewed-By: Juan José <[email protected]>
@Sylphy-0xd3ac
Copy link
Author

Add PR-URL and Reviewed-By fields to the commit message

Sylphy-0xd3ac pushed a commit to Sylphy-0xd3ac/node that referenced this pull request Jun 24, 2025
When a directory cannot be read due to permission issues, the async
version of fs.glob() returns null from readdir(), while the sync
version returns an empty array. This causes a TypeError when trying
to access the 'length' property of null.

PR-URL: nodejs#58674
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ethan-Arrowood <[email protected]>
Reviewed-By: Juan José <[email protected]>
Sylphy-0xd3ac pushed a commit to Sylphy-0xd3ac/node that referenced this pull request Jun 24, 2025
When a directory cannot be read due to permission issues, the async
version of fs.glob() returns null from readdir(), while the sync
version returns an empty array. This causes a TypeError when trying
to access the 'length' property of null.

PR-URL: nodejs#58674
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ethan-Arrowood <[email protected]>
Reviewed-By: Juan José <[email protected]>
When a directory cannot be read due to permission issues, the async
version of fs.glob() returns null from readdir(), while the sync
version returns an empty array. This causes a TypeError when trying
to access the 'length' property of null.

PR-URL: nodejs#58674
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ethan-Arrowood <[email protected]>
Reviewed-By: Juan José <[email protected]>
@Ethan-Arrowood
Copy link
Contributor

I think you need to stop force-pushing so it can land. Another contributor can correct me if I'm wrong though

@Sylphy-0xd3ac
Copy link
Author

Sorry for the force-push. Could you please help re-approve this PR so it can land? Thank you

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 the fs subsystem / file system. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fs.glob and fs/promises.glob breaks when read directory that permission is not granted fs/promises.glob breaks on symlinks, unlike fs.globSync
5 participants