Skip to content

Commit a506755

Browse files
authored
permission: drop fs grants regardless of the path's current type
Revoking a grant re-checked whether the path is a directory, so a path that changed type after startup no longer matched the stored grant and the drop was a silent no-op. Match both the plain and the wildcard form. Signed-off-by: RafaelGSS <rafael.nunu@hotmail.com> PR-URL: #66766 Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
1 parent e8c279d commit a506755

2 files changed

Lines changed: 80 additions & 15 deletions

File tree

‎src/permission/fs_permission.cc‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,21 @@ void FSPermission::Drop(Environment* env,
176176
}
177177

178178
void FSPermission::RevokeAccess(PermissionScope perm, const std::string& res) {
179-
const std::string path = WildcardIfDir(res);
180-
if (perm == PermissionScope::kFileSystemRead) {
181-
auto it =
182-
std::find(granted_paths_in_.begin(), granted_paths_in_.end(), path);
183-
if (it != granted_paths_in_.end()) {
184-
granted_paths_in_.erase(it);
185-
RebuildTree(PermissionScope::kFileSystemRead);
186-
}
187-
} else if (perm == PermissionScope::kFileSystemWrite) {
188-
auto it =
189-
std::find(granted_paths_out_.begin(), granted_paths_out_.end(), path);
190-
if (it != granted_paths_out_.end()) {
191-
granted_paths_out_.erase(it);
192-
RebuildTree(PermissionScope::kFileSystemWrite);
193-
}
179+
if (perm != PermissionScope::kFileSystemRead &&
180+
perm != PermissionScope::kFileSystemWrite) {
181+
return;
182+
}
183+
std::string wildcard = res;
184+
if (wildcard.back() != node::kPathSeparator) {
185+
wildcard += node::kPathSeparator;
186+
}
187+
wildcard += '*';
188+
auto& paths = perm == PermissionScope::kFileSystemRead ? granted_paths_in_
189+
: granted_paths_out_;
190+
if (std::erase_if(paths, [&](const std::string& p) {
191+
return p == res || p == wildcard;
192+
}) > 0) {
193+
RebuildTree(perm);
194194
}
195195
}
196196

‎test/parallel/test-permission-drop-fs-granted-path.js‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,71 @@ for (const files of [
167167
assert.strictEqual(child.status, 0);
168168
}
169169

170+
// Grant a path that does not exist yet, then create it as a directory
171+
// dropping it still revokes the grant
172+
{
173+
const later = path.join(tmpdir.path, 'later-dir');
174+
const child = spawnSync(process.execPath, [
175+
'--permission',
176+
`--allow-fs-read=${later}`,
177+
`--allow-fs-write=${later}`,
178+
'-e',
179+
`
180+
const assert = require('assert');
181+
const fs = require('fs');
182+
const later = ${JSON.stringify(later)};
183+
184+
fs.mkdirSync(later);
185+
for (const scope of ['fs.read', 'fs.write']) {
186+
assert.ok(process.permission.has(scope, later));
187+
188+
process.permission.drop(scope, later);
189+
190+
assert.ok(!process.permission.has(scope, later));
191+
assert.ok(!process.permission.has(scope, later + '/file.txt'));
192+
}
193+
`,
194+
]);
195+
if (child.status !== 0) {
196+
console.error('Case 6 stderr:', child.stderr?.toString());
197+
}
198+
assert.strictEqual(child.status, 0);
199+
}
200+
201+
// Grant a directory, then remove it - dropping it still revokes the grant
202+
{
203+
const gone = path.join(tmpdir.path, 'gone-dir');
204+
fs.mkdirSync(gone);
205+
const child = spawnSync(process.execPath, [
206+
'--permission',
207+
`--allow-fs-read=${gone}`,
208+
`--allow-fs-read=${dir}`,
209+
`--allow-fs-write=${gone}`,
210+
`--allow-fs-write=${dir}`,
211+
'-e',
212+
`
213+
const assert = require('assert');
214+
const fs = require('fs');
215+
const gone = ${JSON.stringify(gone)};
216+
const dir = ${JSON.stringify(dir)};
217+
218+
fs.rmdirSync(gone);
219+
for (const scope of ['fs.read', 'fs.write']) {
220+
assert.ok(process.permission.has(scope, gone));
221+
222+
process.permission.drop(scope, gone);
223+
224+
assert.ok(!process.permission.has(scope, gone));
225+
assert.ok(process.permission.has(scope, dir + '/item1.txt'));
226+
}
227+
`,
228+
]);
229+
if (child.status !== 0) {
230+
console.error('Case 7 stderr:', child.stderr?.toString());
231+
}
232+
assert.strictEqual(child.status, 0);
233+
}
234+
170235
// Drop entire scope without reference - revokes everything
171236
{
172237
const child = spawnSync(process.execPath, [

0 commit comments

Comments
 (0)