Skip to content
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

fs: check subdir correctly in cpSync #55033

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3192,8 +3192,12 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
}

std::string dest_path_str = dest_path.string();
std::string src_path_str = src_path.string();
if (!src_path_str.ends_with(std::filesystem::path::preferred_separator)) {
src_path_str += std::filesystem::path::preferred_separator;
}
// Check if dest_path is a subdirectory of src_path.
if (src_is_dir && dest_path_str.starts_with(src_path.string())) {
if (src_is_dir && dest_path_str.starts_with(src_path_str)) {
std::string message = "Cannot copy " + src_path.string() +
" to a subdirectory of self " + dest_path.string();
return THROW_ERR_FS_CP_EINVAL(env, message.c_str());
Expand Down
43 changes: 41 additions & 2 deletions test/parallel/test-fs-cp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import tmpdir from '../common/tmpdir.js';
tmpdir.refresh();

let dirc = 0;
function nextdir() {
return tmpdir.resolve(`copy_${++dirc}`);
function nextdir(dirname) {
return tmpdir.resolve(dirname || `copy_${++dirc}`);
}

// Synchronous implementation of copy.
Expand Down Expand Up @@ -312,6 +312,45 @@ function nextdir() {
);
}

// It must not throw error if attempt is made to copy to dest
// directory with same prefix as src directory
// regression test for https://github.com/nodejs/node/issues/54285
{
const src = nextdir('prefix');
const dest = nextdir('prefix-a');
mkdirSync(src);
mkdirSync(dest);
cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true }));
}

// It must not throw error if attempt is made to copy to dest
// directory if the parent of dest has same prefix as src directory
// regression test for https://github.com/nodejs/node/issues/54285
{
const src = nextdir('aa');
const destParent = nextdir('aaa');
const dest = nextdir('aaa/aabb');
mkdirSync(src);
mkdirSync(destParent);
mkdirSync(dest);
cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true }));
}

// It throws error if attempt is made to copy src to dest
// when src is parent directory of the parent of dest
{
const src = nextdir('a');
const destParent = nextdir('a/b');
const dest = nextdir('a/b/c');
mkdirSync(src);
mkdirSync(destParent);
mkdirSync(dest);
assert.throws(
() => cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true })),
{ code: 'ERR_FS_CP_EINVAL' },
);
}

// It throws error if attempt is made to copy to subdirectory of self.
{
const src = './test/fixtures/copy/kitchen-sink';
Expand Down
Loading