diff --git a/src/permission/fs_permission.cc b/src/permission/fs_permission.cc index 0baf1291a966..4767d617f847 100644 --- a/src/permission/fs_permission.cc +++ b/src/permission/fs_permission.cc @@ -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* 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); } } diff --git a/test/parallel/test-permission-drop-fs-path-type-change.js b/test/parallel/test-permission-drop-fs-path-type-change.js new file mode 100644 index 000000000000..ff4d2146c5e1 --- /dev/null +++ b/test/parallel/test-permission-drop-fs-path-type-change.js @@ -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', + }); + `); +}