-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
/
Copy pathpermission.cc
200 lines (173 loc) Β· 6.42 KB
/
permission.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "permission.h"
#include "base_object-inl.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
#include "node.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_file.h"
#include "v8.h"
#include <memory>
#include <string>
#include <vector>
namespace node {
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::IntegrityLevel;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Value;
namespace permission {
namespace {
// permission.has('fs.in', '/tmp/')
// permission.has('fs.in')
static void Has(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK(args[0]->IsString());
String::Utf8Value utf8_deny_scope(env->isolate(), args[0]);
if (*utf8_deny_scope == nullptr) {
return;
}
const std::string deny_scope = *utf8_deny_scope;
PermissionScope scope = Permission::StringToPermission(deny_scope);
if (scope == PermissionScope::kPermissionsRoot) {
return args.GetReturnValue().Set(false);
}
if (args.Length() > 1 && !args[1]->IsUndefined()) {
String::Utf8Value utf8_arg(env->isolate(), args[1]);
if (*utf8_arg == nullptr) {
return;
}
return args.GetReturnValue().Set(
env->permission()->is_granted(env, scope, *utf8_arg));
}
return args.GetReturnValue().Set(env->permission()->is_granted(env, scope));
}
} // namespace
#define V(Name, label, _, __) \
if (perm == PermissionScope::k##Name) return #Name;
const char* Permission::PermissionToString(const PermissionScope perm) {
PERMISSIONS(V)
return nullptr;
}
#undef V
#define V(Name, label, _, __) \
if (perm == label) return PermissionScope::k##Name;
PermissionScope Permission::StringToPermission(const std::string& perm) {
PERMISSIONS(V)
return PermissionScope::kPermissionsRoot;
}
#undef V
Permission::Permission() : enabled_(false) {
std::shared_ptr<PermissionBase> fs = std::make_shared<FSPermission>();
std::shared_ptr<PermissionBase> child_p =
std::make_shared<ChildProcessPermission>();
std::shared_ptr<PermissionBase> worker_t =
std::make_shared<WorkerPermission>();
std::shared_ptr<PermissionBase> inspector =
std::make_shared<InspectorPermission>();
std::shared_ptr<PermissionBase> wasi = std::make_shared<WASIPermission>();
#define V(Name, _, __, ___) \
nodes_.insert(std::make_pair(PermissionScope::k##Name, fs));
FILESYSTEM_PERMISSIONS(V)
#undef V
#define V(Name, _, __, ___) \
nodes_.insert(std::make_pair(PermissionScope::k##Name, child_p));
CHILD_PROCESS_PERMISSIONS(V)
#undef V
#define V(Name, _, __, ___) \
nodes_.insert(std::make_pair(PermissionScope::k##Name, worker_t));
WORKER_THREADS_PERMISSIONS(V)
#undef V
#define V(Name, _, __, ___) \
nodes_.insert(std::make_pair(PermissionScope::k##Name, inspector));
INSPECTOR_PERMISSIONS(V)
#undef V
#define V(Name, _, __, ___) \
nodes_.insert(std::make_pair(PermissionScope::k##Name, wasi));
WASI_PERMISSIONS(V)
#undef V
}
const char* GetErrorFlagSuggestion(node::permission::PermissionScope perm) {
switch (perm) {
#define V(Name, _, __, Flag) \
case node::permission::PermissionScope::k##Name: \
return Flag[0] != '\0' ? "Use " Flag " to manage permissions." : "";
PERMISSIONS(V)
#undef V
default:
return "";
}
}
MaybeLocal<Value> CreateAccessDeniedError(Environment* env,
PermissionScope perm,
const std::string_view& res) {
const char* suggestion = GetErrorFlagSuggestion(perm);
Local<Object> err = ERR_ACCESS_DENIED(
env->isolate(), "Access to this API has been restricted. %s", suggestion);
Local<Value> perm_string;
Local<Value> resource_string;
std::string_view perm_str = Permission::PermissionToString(perm);
if (!ToV8Value(env->context(), perm_str, env->isolate())
.ToLocal(&perm_string) ||
!ToV8Value(env->context(), res, env->isolate())
.ToLocal(&resource_string) ||
err->Set(env->context(), env->permission_string(), perm_string)
.IsNothing() ||
err->Set(env->context(), env->resource_string(), resource_string)
.IsNothing()) {
return MaybeLocal<Value>();
}
return err;
}
void Permission::ThrowAccessDenied(Environment* env,
PermissionScope perm,
const std::string_view& res) {
Local<Value> err;
if (CreateAccessDeniedError(env, perm, res).ToLocal(&err)) {
env->isolate()->ThrowException(err);
}
// If ToLocal returned false, then v8 will have scheduled a
// superseding error to be thrown.
}
void Permission::AsyncThrowAccessDenied(Environment* env,
fs::FSReqBase* req_wrap,
PermissionScope perm,
const std::string_view& res) {
Local<Value> err;
if (CreateAccessDeniedError(env, perm, res).ToLocal(&err)) {
return req_wrap->Reject(err);
}
// If ToLocal returned false, then v8 will have scheduled a
// superseding error to be thrown.
}
void Permission::EnablePermissions() {
if (!enabled_) {
enabled_ = true;
}
}
void Permission::Apply(Environment* env,
const std::vector<std::string>& allow,
PermissionScope scope) {
auto permission = nodes_.find(scope);
if (permission != nodes_.end()) {
permission->second->Apply(env, allow, scope);
}
}
void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
SetMethodNoSideEffect(context, target, "has", Has);
target->SetIntegrityLevel(context, IntegrityLevel::kFrozen).FromJust();
}
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Has);
}
} // namespace permission
} // namespace node
NODE_BINDING_CONTEXT_AWARE_INTERNAL(permission, node::permission::Initialize)
NODE_BINDING_EXTERNAL_REFERENCE(permission,
node::permission::RegisterExternalReferences)