Skip to content
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
34 changes: 21 additions & 13 deletions src/permission/fs_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,29 @@ void FSPermission::Drop(Environment* env,
}

void FSPermission::RevokeAccess(PermissionScope perm, const std::string& res) {
const std::string path = WildcardIfDir(res);
// A grant is stored with a trailing wildcard when the path was a directory
// at grant time. The path may have been created, removed or replaced since
// then, so match the grant regardless of what the path currently is.
const std::string wildcard = res.back() == node::kPathSeparator
? res + "*"
: res + node::kPathSeparator + "*";
const auto matches = [&](const std::string& granted) {
return granted == res || granted == wildcard;
};

std::vector<std::string>* granted_paths;
if (perm == PermissionScope::kFileSystemRead) {
auto it =
std::find(granted_paths_in_.begin(), granted_paths_in_.end(), path);
if (it != granted_paths_in_.end()) {
granted_paths_in_.erase(it);
RebuildTree(PermissionScope::kFileSystemRead);
}
granted_paths = &granted_paths_in_;
} else if (perm == PermissionScope::kFileSystemWrite) {
auto it =
std::find(granted_paths_out_.begin(), granted_paths_out_.end(), path);
if (it != granted_paths_out_.end()) {
granted_paths_out_.erase(it);
RebuildTree(PermissionScope::kFileSystemWrite);
}
granted_paths = &granted_paths_out_;
} else {
return;
}

const size_t size = granted_paths->size();
std::erase_if(*granted_paths, matches);
if (granted_paths->size() != size) {
RebuildTree(perm);
}
}

Expand Down
79 changes: 79 additions & 0 deletions test/parallel/test-permission-drop-fs-path-type-change.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict';

require('../common');

const { spawnSync } = require('child_process');
const assert = require('assert');
const { isMainThread } = require('worker_threads');

if (!isMainThread) {
process.exit(0);
}
const tmpdir = require('../common/tmpdir');
const fs = require('fs');
const path = require('path');

tmpdir.refresh();

function runWithGrants(target, script) {
const child = spawnSync(process.execPath, [
'--permission',
`--allow-fs-read=${target}`,
`--allow-fs-write=${target}`,
'-e',
`
const assert = require('assert');
const fs = require('fs');
const target = ${JSON.stringify(target)};
${script}
`,
]);

if (child.status !== 0) {
console.error('stdout:', child.stdout?.toString());
console.error('stderr:', child.stderr?.toString());
}
assert.strictEqual(child.status, 0);
}

// The path does not exist when it is granted and is created as a directory
// before the drop.
{
const target = path.join(tmpdir.path, 'created-later');

runWithGrants(target, `
fs.mkdirSync(target);

process.permission.drop('fs.read', target);
assert.ok(!process.permission.has('fs.read', target));
assert.throws(() => fs.readdirSync(target), { code: 'ERR_ACCESS_DENIED' });
assert.ok(process.permission.has('fs.write', target));

process.permission.drop('fs.write', target);
assert.ok(!process.permission.has('fs.write', target));
assert.throws(() => fs.rmdirSync(target), { code: 'ERR_ACCESS_DENIED' });
`);
}

// The path is a directory when it is granted and is replaced by a file
// before the drop.
{
const target = path.join(tmpdir.path, 'replaced-by-file');
fs.mkdirSync(target);

runWithGrants(target, `
fs.rmdirSync(target);
fs.writeFileSync(target, 'content');

process.permission.drop('fs.read', target);
assert.ok(!process.permission.has('fs.read', target));
assert.throws(() => fs.readFileSync(target), { code: 'ERR_ACCESS_DENIED' });
assert.ok(process.permission.has('fs.write', target));

process.permission.drop('fs.write', target);
assert.ok(!process.permission.has('fs.write', target));
assert.throws(() => fs.writeFileSync(target, 'other'), {
code: 'ERR_ACCESS_DENIED',
});
`);
}
Loading