Skip to content

Commit

Permalink
permission: handle end nodes with children cases
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Jun 23, 2023
1 parent 198affc commit d301f50
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/permission/fs_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,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();
Expand Down
9 changes: 8 additions & 1 deletion src/permission/fs_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "v8.h"

#include <iostream>
#include <unordered_map>
#include "permission/permission_base.h"
#include "util.h"
Expand All @@ -25,13 +26,18 @@ class FSPermission final : public PermissionBase {
std::string prefix;
std::unordered_map<char, Node*> children;
Node* wildcard_child;
bool is_leaf;

explicit Node(const std::string& pre)
: prefix(pre), wildcard_child(nullptr) {}

Node() : wildcard_child(nullptr) {}

Node* CreateChild(std::string prefix) {
if (prefix.length() == 0 && is_leaf == false) {
is_leaf = true;
return this;
}
char label = prefix[0];

Node* child = children[label];
Expand All @@ -56,6 +62,7 @@ class FSPermission final : public PermissionBase {
return split_child->CreateChild(prefix.substr(i));
}
}
child->is_leaf = true;
return child->CreateChild(prefix.substr(i));
}

Expand Down Expand Up @@ -114,7 +121,7 @@ class FSPermission final : public PermissionBase {
if (children.size() == 0) {
return true;
}
return children['\0'] != nullptr;
return is_leaf;
}
};

Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-permission-fs-wildcard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'));
`,
]
);
Expand Down

0 comments on commit d301f50

Please sign in to comment.