Skip to content

Commit c5c6965

Browse files
authored
src,permission: add support to permission.has(addon)
PR-URL: nodejs#58951 Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Ilyas Shabi <[email protected]>
1 parent 7be2528 commit c5c6965

File tree

9 files changed

+74
-2
lines changed

9 files changed

+74
-2
lines changed

node.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
'src/permission/wasi_permission.cc',
165165
'src/permission/worker_permission.cc',
166166
'src/permission/net_permission.cc',
167+
'src/permission/addon_permission.cc',
167168
'src/pipe_wrap.cc',
168169
'src/process_wrap.cc',
169170
'src/signal_wrap.cc',
@@ -294,6 +295,7 @@
294295
'src/permission/wasi_permission.h',
295296
'src/permission/worker_permission.h',
296297
'src/permission/net_permission.h',
298+
'src/permission/addon_permission.h',
297299
'src/pipe_wrap.h',
298300
'src/req_wrap.h',
299301
'src/req_wrap-inl.h',

src/env.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ Environment::Environment(IsolateData* isolate_data,
913913
// unless explicitly allowed by the user
914914
if (!options_->allow_addons) {
915915
options_->allow_native_addons = false;
916+
permission()->Apply(this, {"*"}, permission::PermissionScope::kAddon);
916917
}
917918
flags_ = flags_ | EnvironmentFlags::kNoCreateInspector;
918919
permission()->Apply(this, {"*"}, permission::PermissionScope::kInspector);

src/permission/addon_permission.cc

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

src/permission/addon_permission.h

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

src/permission/permission.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Permission::Permission() : enabled_(false) {
8585
std::make_shared<InspectorPermission>();
8686
std::shared_ptr<PermissionBase> wasi = std::make_shared<WASIPermission>();
8787
std::shared_ptr<PermissionBase> net = std::make_shared<NetPermission>();
88+
std::shared_ptr<PermissionBase> addon = std::make_shared<AddonPermission>();
8889
#define V(Name, _, __, ___) \
8990
nodes_.insert(std::make_pair(PermissionScope::k##Name, fs));
9091
FILESYSTEM_PERMISSIONS(V)
@@ -109,6 +110,10 @@ Permission::Permission() : enabled_(false) {
109110
nodes_.insert(std::make_pair(PermissionScope::k##Name, net));
110111
NET_PERMISSIONS(V)
111112
#undef V
113+
#define V(Name, _, __, ___) \
114+
nodes_.insert(std::make_pair(PermissionScope::k##Name, addon));
115+
ADDON_PERMISSIONS(V)
116+
#undef V
112117
}
113118

114119
const char* GetErrorFlagSuggestion(node::permission::PermissionScope perm) {

src/permission/permission.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "debug_utils.h"
77
#include "node_options.h"
8+
#include "permission/addon_permission.h"
89
#include "permission/child_process_permission.h"
910
#include "permission/fs_permission.h"
1011
#include "permission/inspector_permission.h"

src/permission/permission_base.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ namespace permission {
3131

3232
#define NET_PERMISSIONS(V) V(Net, "net", PermissionsRoot, "--allow-net")
3333

34+
#define ADDON_PERMISSIONS(V) \
35+
V(Addon, "addon", PermissionsRoot, "--allow-addons")
36+
3437
#define PERMISSIONS(V) \
3538
FILESYSTEM_PERMISSIONS(V) \
3639
CHILD_PROCESS_PERMISSIONS(V) \
3740
WASI_PERMISSIONS(V) \
3841
WORKER_THREADS_PERMISSIONS(V) \
3942
INSPECTOR_PERMISSIONS(V) \
40-
NET_PERMISSIONS(V)
43+
NET_PERMISSIONS(V) \
44+
ADDON_PERMISSIONS(V)
4145

4246
#define V(name, _, __, ___) k##name,
4347
enum class PermissionScope {

test/parallel/test-permission-allow-addons-cli.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ const loadFixture = createRequire(fixtures.path('node_modules'));
1919
const msg = loadFixture('pkgexports/no-addons');
2020
assert.strictEqual(msg, 'using native addons');
2121
}
22+
23+
{
24+
assert.ok(process.permission.has('addon'));
25+
}

test/parallel/test-permission-has.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ const assert = require('assert');
3434
assert.ok(!process.permission.has('worker'));
3535
assert.ok(!process.permission.has('inspector'));
3636
assert.ok(!process.permission.has('net'));
37-
// TODO(rafaelgss): add addon
37+
assert.ok(!process.permission.has('addon'));
3838
}

0 commit comments

Comments
 (0)