66#include " node_errors.h"
77#include " node_external_reference.h"
88#include " node_file.h"
9+ #include " node_process-inl.h"
910
1011#include " v8.h"
1112
@@ -70,7 +71,7 @@ PermissionScope Permission::StringToPermission(const std::string& perm) {
7071}
7172#undef V
7273
73- Permission::Permission () : enabled_(false ) {
74+ Permission::Permission () : enabled_(false ), warning_only_( false ) {
7475 std::shared_ptr<PermissionBase> fs = std::make_shared<FSPermission>();
7576 std::shared_ptr<PermissionBase> child_p =
7677 std::make_shared<ChildProcessPermission>();
@@ -149,24 +150,45 @@ MaybeLocal<Value> CreateAccessDeniedError(Environment* env,
149150void Permission::ThrowAccessDenied (Environment* env,
150151 PermissionScope perm,
151152 const std::string_view& res) {
152- Local<Value> err;
153- if (CreateAccessDeniedError (env, perm, res).ToLocal (&err)) {
154- env->isolate ()->ThrowException (err);
153+ // If permission is set to "audit" only. We should not throw, but
154+ // emit warning whenever a permission is "bypassed"
155+ if (!env->permission ()->warning_only ()) {
156+ Local<Value> err;
157+ if (CreateAccessDeniedError (env, perm, res).ToLocal (&err)) {
158+ env->isolate ()->ThrowException (err);
159+ }
160+ // If ToLocal returned false, then v8 will have scheduled a
161+ // superseding error to be thrown.
162+ return ;
155163 }
156- // If ToLocal returned false, then v8 will have scheduled a
157- // superseding error to be thrown.
164+ std::string_view perm_str = Permission::PermissionToString (perm);
165+ ProcessEmitWarningSync (
166+ env,
167+ " ERR_ACCESS_DENIED suppressed. Permission: %s, Resource: %s" ,
168+ perm_str,
169+ res);
158170}
159171
160172void Permission::AsyncThrowAccessDenied (Environment* env,
161173 fs::FSReqBase* req_wrap,
162174 PermissionScope perm,
163175 const std::string_view& res) {
164- Local<Value> err;
165- if (CreateAccessDeniedError (env, perm, res).ToLocal (&err)) {
166- return req_wrap->Reject (err);
176+ if (env->permission ()->warning_only ()) {
177+ Local<Value> err;
178+ if (CreateAccessDeniedError (env, perm, res).ToLocal (&err)) {
179+ return req_wrap->Reject (err);
180+ }
181+ // If ToLocal returned false, then v8 will have scheduled a
182+ // superseding error to be thrown.
183+ return ;
167184 }
168- // If ToLocal returned false, then v8 will have scheduled a
169- // superseding error to be thrown.
185+ std::string_view perm_str = Permission::PermissionToString (perm);
186+ // TODO: handle warning error
187+ ProcessEmitWarning (
188+ env,
189+ " ERR_ACCESS_DENIED suppressed. Permission: %s, Resource: %s" ,
190+ perm_str,
191+ res);
170192}
171193
172194void Permission::EnablePermissions () {
@@ -175,6 +197,12 @@ void Permission::EnablePermissions() {
175197 }
176198}
177199
200+ void Permission::EnableWarningOnly () {
201+ if (!warning_only_) {
202+ warning_only_ = true ;
203+ }
204+ }
205+
178206void Permission::Apply (Environment* env,
179207 const std::vector<std::string>& allow,
180208 PermissionScope scope) {
0 commit comments