Repository navigation
Conversation
|
How is rimraf to know that this is accidental cwd removal, and not intentional cwd removal? I suppose it's fair to just mimic what Node does, which we can do by just not resolving the paths in the first place before passing them to the implementation. However, "mimic what node does" gets tricky when looking at the other implementations. For example:
Deleting whitespace-only paths must continue to be supported, how else would you delete a file named So, I think there's some fixing to be done here, but this PR goes too far. I'll take a look in the coming days. In all cases, rimraf should either mimic Node's behavior, or we should send a PR to node to fix |
|
Thank you for the detailed analysis! You're absolutely right that:
I'll leave this for you to handle since you have the full context on what rimraf's behavior should be. Happy to help if you'd like me to work on a revised approach after you've had a chance to investigate. |
Based on maintainer feedback: - Whitespace-only paths like ' ' or '\t' are valid filenames - Only reject empty string '' to prevent cwd deletion - Updated tests to verify whitespace paths resolve correctly Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
5cb3ec8 to
6d77072
Compare
|
Thanks for the detailed feedback @isaacs! You're absolutely right - whitespace paths are valid filenames. I've updated the PR to:
The behavior now matches Node's approach more closely - only the literal empty string gets special handling. |
|
Once nodejs/node#61958 is fixed, I'll update rimraf to behave accordingly in all implementations. |
|
Thanks for filing the upstream issue. Since you'll be updating rimraf after nodejs/node#61958 is resolved, should I close this PR? Happy to do so if it's just adding noise. |
|
Yeah, I'm going to put up a PR to mirror the node behavior, and we can land it once that is finalized in Node, just to be sure it's the right approach. Thanks for bringing this up :) |
When removing any path that has `..` or `.` as path portions, call `path.normalize` on the path before attempting to process it. When removing the path `''`, throw a `stat ENOENT` error. When removing `.`, replace with `process.cwd()` and proceed as normal. Also, add support for deleting `file:` URLs and Buffer paths, which are normalized to strings for the benefit of older Node versions. Glob patters must still be normal `strings`. This mirrors the behavior of node, once nodejs/node#61968 lands. Closes: #342 Fixes: #326 Re: nodejs/node#61958 Credit: @abhu85, @RajeshKumar11, @isaacs
When removing any path that has `..` or `.` as path portions, call `path.normalize` on the path before attempting to process it. When removing the path `''`, throw a `stat ENOENT` error. When removing `.`, replace with `process.cwd()` and proceed as normal. Also, add support for deleting `file:` URLs and Buffer paths, which are normalized to strings for the benefit of older Node versions. Glob patterns must still be normal `strings`. This mirrors the behavior of node, once nodejs/node#61968 lands. Closes: #342 Fixes: #326 Re: nodejs/node#61958 Credit: @abhu85, @RajeshKumar11, @isaacs
When removing any path that has `..` or `.` as path portions, call `path.normalize` on the path before attempting to process it. When removing the path `''`, throw a `stat ENOENT` error. When removing `.`, replace with `process.cwd()` and proceed as normal. Also, add support for deleting `file:` URLs and Buffer paths, which are normalized to `string` for the benefit of older Node versions. Glob patterns must still be normal `string` values. This mirrors the behavior of node, once nodejs/node#61968 lands. Closes: #342 Fixes: #326 Re: nodejs/node#61958 Credit: @abhu85, @RajeshKumar11, @isaacs
When removing any path that has `..` or `.` as path portions, call `path.normalize` on the path before attempting to process it. When removing the path `''`, throw a `stat ENOENT` error. When removing `.`, replace with `process.cwd()` and proceed as normal. Also, add support for deleting `file:` URLs and Buffer paths, which are normalized to `string` for the benefit of older Node versions. Glob patterns must still be normal `string` values. This mirrors the behavior of node, once nodejs/node#61968 lands. Closes: #342 Fixes: #326 Re: nodejs/node#61958 Credit: @abhu85, @RajeshKumar11, @isaacs
Summary
Fixes #326
When an empty string is passed to rimraf (e.g.,
rimrafSync('')),path.resolve('')returns the current working directory, causing rimraf to delete all files in cwd. This is extremely dangerous behavior that has caused real data loss for users.The Problem
The issue is that
resolve('')returnsprocess.cwd(), so the empty string silently becomes a valid path pointing to the current directory.The Fix
This PR adds validation to reject empty strings and whitespace-only paths before they are resolved, throwing a clear
ERR_INVALID_ARG_VALUEerror:Changes
src/path-arg.ts: Add empty/whitespace path validation beforeresolve()calltest/path-arg.ts: Add tests for empty string, spaces-only, and tabs/newlinesTest Results
All 31 path-arg tests pass (6 new tests added):