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: recursion should bail on permission error #31505

Closed
wants to merge 7 commits into from
Closed
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
20 changes: 13 additions & 7 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1226,11 +1226,17 @@ int MKDirpSync(uv_loop_t* loop,
int err = uv_fs_mkdir(loop, req, next_path.c_str(), mode, nullptr);
while (true) {
switch (err) {
// Note: uv_fs_req_cleanup in terminal paths will be called by
// ~FSReqWrapSync():
case 0:
if (continuation_data.paths().size() == 0) {
return 0;
}
break;
case UV_EACCES:
case UV_EPERM: {
return err;
}
case UV_ENOENT: {
std::string dirname = next_path.substr(0,
next_path.find_last_of(kPathSeparator));
Expand All @@ -1243,9 +1249,6 @@ int MKDirpSync(uv_loop_t* loop,
}
break;
}
case UV_EPERM: {
return err;
}
default:
uv_fs_req_cleanup(req);
int orig_err = err;
Expand Down Expand Up @@ -1293,6 +1296,8 @@ int MKDirpAsync(uv_loop_t* loop,

while (true) {
switch (err) {
// Note: uv_fs_req_cleanup in terminal paths will be called by
// FSReqAfterScope::~FSReqAfterScope()
case 0: {
if (req_wrap->continuation_data()->paths().size() == 0) {
req_wrap->continuation_data()->Done(0);
Expand All @@ -1303,6 +1308,11 @@ int MKDirpAsync(uv_loop_t* loop,
}
break;
}
case UV_EACCES:
case UV_EPERM: {
req_wrap->continuation_data()->Done(err);
break;
}
case UV_ENOENT: {
std::string dirname = path.substr(0,
path.find_last_of(kPathSeparator));
Expand All @@ -1318,10 +1328,6 @@ int MKDirpAsync(uv_loop_t* loop,
req_wrap->continuation_data()->mode(), nullptr);
break;
}
case UV_EPERM: {
req_wrap->continuation_data()->Done(err);
break;
}
default:
uv_fs_req_cleanup(req);
// Stash err for use in the callback.
Expand Down
71 changes: 71 additions & 0 deletions test/parallel/test-fs-mkdir-recursive-eaccess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';

// Test that mkdir with recursive option returns appropriate error
// when executed on folder it does not have permission to access.
// Ref: https://github.com/nodejs/node/issues/31481

const common = require('../common');

if (!common.isWindows && process.getuid() === 0)
common.skip('as this test should not be run as `root`');

if (common.isIBMi)
common.skip('IBMi has a different access permission mechanism');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const assert = require('assert');
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

let n = 0;

function makeDirectoryReadOnly(dir) {
let accessErrorCode = 'EACCES';
if (common.isWindows) {
accessErrorCode = 'EPERM';
execSync(`icacls ${dir} /inheritance:r`);
execSync(`icacls ${dir} /deny "everyone":W`);
} else {
fs.chmodSync(dir, '444');
}
return accessErrorCode;
}

function makeDirectoryWritable(dir) {
if (common.isWindows) {
execSync(`icacls ${dir} /grant "everyone":W`);
}
}

// Synchronous API should return an EACCESS error with path populated.
{
const dir = path.join(tmpdir.path, `mkdirp_${n++}`);
fs.mkdirSync(dir);
const codeExpected = makeDirectoryReadOnly(dir);
let err = null;
try {
fs.mkdirSync(path.join(dir, '/foo'), { recursive: true });
} catch (_err) {
err = _err;
}
makeDirectoryWritable(dir);
assert(err);
assert.strictEqual(err.code, codeExpected);
assert(err.path);
}

// Asynchronous API should return an EACCESS error with path populated.
{
const dir = path.join(tmpdir.path, `mkdirp_${n++}`);
fs.mkdirSync(dir);
const codeExpected = makeDirectoryReadOnly(dir);
fs.mkdir(path.join(dir, '/bar'), { recursive: true }, (err) => {
makeDirectoryWritable(dir);
assert(err);
assert.strictEqual(err.code, codeExpected);
assert(err.path);
});
}