From b38bc9fc890398039f3d97b0710e965d000fa801 Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Sun, 25 Jun 2023 16:11:22 -0300 Subject: [PATCH] permission: handle end nodes with children cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/48531 Reviewed-By: Yagiz Nizipli Reviewed-By: Juan José Arboleda --- src/permission/fs_permission.cc | 1 - src/permission/fs_permission.h | 12 +++++++++--- test/parallel/test-permission-fs-wildcard.js | 7 +++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/permission/fs_permission.cc b/src/permission/fs_permission.cc index 0c844703b5a7ae..131300346d824a 100644 --- a/src/permission/fs_permission.cc +++ b/src/permission/fs_permission.cc @@ -132,7 +132,6 @@ bool FSPermission::RadixTree::Lookup(const std::string_view& s, if (current_node->children.size() == 0) { return when_empty_return; } - unsigned int parent_node_prefix_len = current_node->prefix.length(); const std::string path(s); auto path_len = path.length(); diff --git a/src/permission/fs_permission.h b/src/permission/fs_permission.h index 8d735a9095502e..50e6f8e34f93e8 100644 --- a/src/permission/fs_permission.h +++ b/src/permission/fs_permission.h @@ -25,13 +25,18 @@ class FSPermission final : public PermissionBase { std::string prefix; std::unordered_map children; Node* wildcard_child; + bool is_leaf; explicit Node(const std::string& pre) - : prefix(pre), wildcard_child(nullptr) {} + : prefix(pre), wildcard_child(nullptr), is_leaf(false) {} - Node() : wildcard_child(nullptr) {} + Node() : wildcard_child(nullptr), is_leaf(false) {} Node* CreateChild(std::string prefix) { + if (prefix.empty() && !is_leaf) { + is_leaf = true; + return this; + } char label = prefix[0]; Node* child = children[label]; @@ -56,6 +61,7 @@ class FSPermission final : public PermissionBase { return split_child->CreateChild(prefix.substr(i)); } } + child->is_leaf = true; return child->CreateChild(prefix.substr(i)); } @@ -114,7 +120,7 @@ class FSPermission final : public PermissionBase { if (children.size() == 0) { return true; } - return children['\0'] != nullptr; + return is_leaf; } }; diff --git a/test/parallel/test-permission-fs-wildcard.js b/test/parallel/test-permission-fs-wildcard.js index eb8d4ca53ced04..5b0dc411666013 100644 --- a/test/parallel/test-permission-fs-wildcard.js +++ b/test/parallel/test-permission-fs-wildcard.js @@ -59,6 +59,9 @@ if (common.isWindows) { '/slower', '/slown', '/home/foo/*', + '/files/index.js', + '/files/index.json', + '/files/i', ]; const { status, stderr } = spawnSync( process.execPath, @@ -74,6 +77,10 @@ if (common.isWindows) { assert.ok(process.permission.has('fs.read', '/home/foo')); assert.ok(process.permission.has('fs.read', '/home/foo/')); assert.ok(!process.permission.has('fs.read', '/home/fo')); + assert.ok(process.permission.has('fs.read', '/files/index.js')); + assert.ok(process.permission.has('fs.read', '/files/index.json')); + assert.ok(!process.permission.has('fs.read', '/files/index.j')); + assert.ok(process.permission.has('fs.read', '/files/i')); `, ] );