Skip to content

Commit 82d6b13

Browse files
RafaelGSSjuanarbol
authored andcommitted
permission: add debug log when inserting fs nodes
Signed-off-by: RafaelGSS <[email protected]> PR-URL: #48677 Reviewed-By: Paolo Insogna <[email protected]>
1 parent 45be29d commit 82d6b13

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/debug_utils.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ void NODE_EXTERN_PRIVATE FWrite(FILE* file, const std::string& str);
5050
V(NGTCP2_DEBUG) \
5151
V(SEA) \
5252
V(WASI) \
53-
V(MKSNAPSHOT)
53+
V(MKSNAPSHOT) \
54+
V(PERMISSION_MODEL)
5455

5556
enum class DebugCategory : unsigned int {
5657
#define V(name) name,

src/permission/fs_permission.cc

+47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "fs_permission.h"
22
#include "base_object-inl.h"
3+
#include "debug_utils-inl.h"
34
#include "util.h"
45
#include "v8.h"
56

@@ -72,6 +73,46 @@ namespace node {
7273

7374
namespace permission {
7475

76+
void PrintTree(FSPermission::RadixTree::Node* node, int spaces = 0) {
77+
std::string whitespace(spaces, ' ');
78+
79+
if (node == nullptr) {
80+
return;
81+
}
82+
if (node->wildcard_child != nullptr) {
83+
per_process::Debug(DebugCategory::PERMISSION_MODEL,
84+
"%s Wildcard: %s\n",
85+
whitespace,
86+
node->prefix);
87+
} else {
88+
per_process::Debug(DebugCategory::PERMISSION_MODEL,
89+
"%s Prefix: %s\n",
90+
whitespace,
91+
node->prefix);
92+
if (node->children.size()) {
93+
int child = 0;
94+
for (const auto pair : node->children) {
95+
++child;
96+
per_process::Debug(DebugCategory::PERMISSION_MODEL,
97+
"%s Child(%s): %s\n",
98+
whitespace,
99+
child,
100+
std::string(1, pair.first));
101+
PrintTree(pair.second, spaces + 2);
102+
}
103+
per_process::Debug(DebugCategory::PERMISSION_MODEL,
104+
"%s End of tree - child(%s)\n",
105+
whitespace,
106+
child);
107+
} else {
108+
per_process::Debug(DebugCategory::PERMISSION_MODEL,
109+
"%s End of tree: %s\n",
110+
whitespace,
111+
node->prefix);
112+
}
113+
}
114+
}
115+
75116
// allow = '*'
76117
// allow = '/tmp/,/home/example.js'
77118
void FSPermission::Apply(const std::string& allow, PermissionScope scope) {
@@ -175,6 +216,12 @@ void FSPermission::RadixTree::Insert(const std::string& path) {
175216
parent_node_prefix_len = i;
176217
}
177218
}
219+
220+
if (UNLIKELY(per_process::enabled_debug_list.enabled(
221+
DebugCategory::PERMISSION_MODEL))) {
222+
per_process::Debug(DebugCategory::PERMISSION_MODEL, "Inserting %s\n", path);
223+
PrintTree(root_node_);
224+
}
178225
}
179226

180227
} // namespace permission

src/permission/fs_permission.h

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class FSPermission final : public PermissionBase {
1818
void Apply(const std::string& allow, PermissionScope scope) override;
1919
bool is_granted(PermissionScope perm, const std::string_view& param) override;
2020

21-
// For debugging purposes, use the gist function to print the whole tree
22-
// https://gist.github.com/RafaelGSS/5b4f09c559a54f53f9b7c8c030744d19
2321
struct RadixTree {
2422
struct Node {
2523
std::string prefix;

0 commit comments

Comments
 (0)