Skip to content

Commit 30b7133

Browse files
daeyeonMoLow
authored andcommittedJul 6, 2023
src: clarify the parameter name in Permission::Apply
This fixes confusing parameter names. They are references to set allow-permission. Signed-off-by: Daeyeon Jeong <daeyeon.dev@gmail.com> PR-URL: #47874 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
1 parent 49875f0 commit 30b7133

7 files changed

+222
-1
lines changed
 
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "child_process_permission.h"
2+
3+
#include <string>
4+
#include <vector>
5+
6+
namespace node {
7+
8+
namespace permission {
9+
10+
// Currently, ChildProcess manage a single state
11+
// Once denied, it's always denied
12+
void ChildProcessPermission::Apply(const std::string& allow,
13+
PermissionScope scope) {
14+
deny_all_ = true;
15+
}
16+
17+
bool ChildProcessPermission::is_granted(PermissionScope perm,
18+
const std::string_view& param) {
19+
return deny_all_ == false;
20+
}
21+
22+
} // namespace permission
23+
} // namespace node
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef SRC_PERMISSION_CHILD_PROCESS_PERMISSION_H_
2+
#define SRC_PERMISSION_CHILD_PROCESS_PERMISSION_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include <vector>
7+
#include "permission/permission_base.h"
8+
9+
namespace node {
10+
11+
namespace permission {
12+
13+
class ChildProcessPermission final : public PermissionBase {
14+
public:
15+
void Apply(const std::string& allow, PermissionScope scope) override;
16+
bool is_granted(PermissionScope perm,
17+
const std::string_view& param = "") override;
18+
19+
private:
20+
bool deny_all_;
21+
};
22+
23+
} // namespace permission
24+
25+
} // namespace node
26+
27+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
28+
#endif // SRC_PERMISSION_CHILD_PROCESS_PERMISSION_H_

‎src/permission/fs_permission.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace permission {
1616

1717
class FSPermission final : public PermissionBase {
1818
public:
19-
void Apply(const std::string& deny, PermissionScope scope) override;
19+
void Apply(const std::string& allow, PermissionScope scope) override;
2020
bool is_granted(PermissionScope perm, const std::string_view& param) override;
2121

2222
// For debugging purposes, use the gist function to print the whole tree

‎src/permission/permission.h

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#ifndef SRC_PERMISSION_PERMISSION_H_
2+
#define SRC_PERMISSION_PERMISSION_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include "debug_utils.h"
7+
#include "node_options.h"
8+
#include "permission/child_process_permission.h"
9+
#include "permission/fs_permission.h"
10+
#include "permission/permission_base.h"
11+
#include "permission/worker_permission.h"
12+
#include "v8.h"
13+
14+
#include <string_view>
15+
#include <unordered_map>
16+
17+
namespace node {
18+
19+
class Environment;
20+
21+
namespace permission {
22+
23+
#define THROW_IF_INSUFFICIENT_PERMISSIONS(env, perm_, resource_, ...) \
24+
do { \
25+
if (UNLIKELY(!(env)->permission()->is_granted(perm_, resource_))) { \
26+
node::permission::Permission::ThrowAccessDenied( \
27+
(env), perm_, resource_); \
28+
return __VA_ARGS__; \
29+
} \
30+
} while (0)
31+
32+
class Permission {
33+
public:
34+
Permission();
35+
36+
FORCE_INLINE bool is_granted(const PermissionScope permission,
37+
const std::string_view& res = "") const {
38+
if (LIKELY(!enabled_)) return true;
39+
return is_scope_granted(permission, res);
40+
}
41+
42+
static PermissionScope StringToPermission(const std::string& perm);
43+
static const char* PermissionToString(PermissionScope perm);
44+
static void ThrowAccessDenied(Environment* env,
45+
PermissionScope perm,
46+
const std::string_view& res);
47+
48+
// CLI Call
49+
void Apply(const std::string& allow, PermissionScope scope);
50+
void EnablePermissions();
51+
52+
private:
53+
COLD_NOINLINE bool is_scope_granted(const PermissionScope permission,
54+
const std::string_view& res = "") const {
55+
auto perm_node = nodes_.find(permission);
56+
if (perm_node != nodes_.end()) {
57+
return perm_node->second->is_granted(permission, res);
58+
}
59+
return false;
60+
}
61+
62+
std::unordered_map<PermissionScope, std::shared_ptr<PermissionBase>> nodes_;
63+
bool enabled_;
64+
};
65+
66+
} // namespace permission
67+
68+
} // namespace node
69+
70+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
71+
#endif // SRC_PERMISSION_PERMISSION_H_

‎src/permission/permission_base.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef SRC_PERMISSION_PERMISSION_BASE_H_
2+
#define SRC_PERMISSION_PERMISSION_BASE_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include <map>
7+
#include <string>
8+
#include <string_view>
9+
#include "v8.h"
10+
11+
namespace node {
12+
13+
namespace permission {
14+
15+
#define FILESYSTEM_PERMISSIONS(V) \
16+
V(FileSystem, "fs", PermissionsRoot) \
17+
V(FileSystemRead, "fs.read", FileSystem) \
18+
V(FileSystemWrite, "fs.write", FileSystem)
19+
20+
#define CHILD_PROCESS_PERMISSIONS(V) V(ChildProcess, "child", PermissionsRoot)
21+
22+
#define WORKER_THREADS_PERMISSIONS(V) \
23+
V(WorkerThreads, "worker", PermissionsRoot)
24+
25+
#define PERMISSIONS(V) \
26+
FILESYSTEM_PERMISSIONS(V) \
27+
CHILD_PROCESS_PERMISSIONS(V) \
28+
WORKER_THREADS_PERMISSIONS(V)
29+
30+
#define V(name, _, __) k##name,
31+
enum class PermissionScope {
32+
kPermissionsRoot = -1,
33+
PERMISSIONS(V) kPermissionsCount
34+
};
35+
#undef V
36+
37+
class PermissionBase {
38+
public:
39+
virtual void Apply(const std::string& allow, PermissionScope scope) = 0;
40+
virtual bool is_granted(PermissionScope perm,
41+
const std::string_view& param = "") = 0;
42+
};
43+
44+
} // namespace permission
45+
46+
} // namespace node
47+
48+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
49+
#endif // SRC_PERMISSION_PERMISSION_BASE_H_

‎src/permission/worker_permission.cc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "permission/worker_permission.h"
2+
3+
#include <string>
4+
#include <vector>
5+
6+
namespace node {
7+
8+
namespace permission {
9+
10+
// Currently, PolicyDenyWorker manage a single state
11+
// Once denied, it's always denied
12+
void WorkerPermission::Apply(const std::string& allow, PermissionScope scope) {
13+
deny_all_ = true;
14+
}
15+
16+
bool WorkerPermission::is_granted(PermissionScope perm,
17+
const std::string_view& param) {
18+
return deny_all_ == false;
19+
}
20+
21+
} // namespace permission
22+
} // namespace node

‎src/permission/worker_permission.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef SRC_PERMISSION_WORKER_PERMISSION_H_
2+
#define SRC_PERMISSION_WORKER_PERMISSION_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include <vector>
7+
#include "permission/permission_base.h"
8+
9+
namespace node {
10+
11+
namespace permission {
12+
13+
class WorkerPermission final : public PermissionBase {
14+
public:
15+
void Apply(const std::string& allow, PermissionScope scope) override;
16+
bool is_granted(PermissionScope perm,
17+
const std::string_view& param = "") override;
18+
19+
private:
20+
bool deny_all_;
21+
};
22+
23+
} // namespace permission
24+
25+
} // namespace node
26+
27+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
28+
#endif // SRC_PERMISSION_WORKER_PERMISSION_H_

0 commit comments

Comments
 (0)
Please sign in to comment.