Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/nodejs/src/permission/permission.h
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/addon_permission.h" 9 #include "permission/child_process_permission.h" 10 #include "permission/fs_permission.h" 11 #include "permission/inspector_permission.h" 12 #include "permission/permission_base.h" 13 #include "permission/wasi_permission.h" 14 #include "permission/worker_permission.h" 15 #include "v8.h" 16 17 #include <string_view> 18 #include <unordered_map> 19 20 namespace node { 21 22 class Environment; 23 24 namespace fs { 25 class FSReqBase; 26 } 27 28 namespace permission { 29 30 #define THROW_IF_INSUFFICIENT_PERMISSIONS(env, perm, resource, ...) \ 31 do { \ 32 node::Environment* env__ = (env); \ 33 const node::permission::PermissionScope perm__ = (perm); \ 34 const auto resource__ = (resource); \ 35 if (!env__->permission()->is_granted(env__, perm__, resource__)) \ 36 [[unlikely]] { \ 37 node::permission::Permission::ThrowAccessDenied( \ 38 env__, perm__, resource__); \ 39 return __VA_ARGS__; \ 40 } \ 41 } while (0) 42 43 #define ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( \ 44 env, wrap, perm, resource, ...) \ 45 do { \ 46 node::Environment* env__ = (env); \ 47 const node::permission::PermissionScope perm__ = (perm); \ 48 const auto resource__ = (resource); \ 49 if (!env__->permission()->is_granted(env__, perm__, resource__)) \ 50 [[unlikely]] { \ 51 node::permission::Permission::AsyncThrowAccessDenied( \ 52 env__, (wrap), perm__, resource__); \ 53 return __VA_ARGS__; \ 54 } \ 55 } while (0) 56 57 #define ERR_ACCESS_DENIED_IF_INSUFFICIENT_PERMISSIONS( \ 58 env, perm, resource, args, ...) \ 59 do { \ 60 node::Environment* env__ = (env); \ 61 const node::permission::PermissionScope perm__ = (perm); \ 62 const auto resource__ = (resource); \ 63 if (!env__->permission()->is_granted(env__, perm__, resource__)) \ 64 [[unlikely]] { \ 65 Local<Value> err_access; \ 66 if (node::permission::CreateAccessDeniedError(env__, perm__, resource__) \ 67 .ToLocal(&err_access)) { \ 68 args.GetReturnValue().Set(err_access); \ 69 } else { \ 70 args.GetReturnValue().Set(UV_EACCES); \ 71 } \ 72 return __VA_ARGS__; \ 73 } \ 74 } while (0) 75 76 #define SET_INSUFFICIENT_PERMISSION_ERROR_CALLBACK(scope) \ 77 void InsufficientPermissionError(std::string_view resource) { \ 78 v8::HandleScope handle_scope(env()->isolate()); \ 79 v8::Context::Scope context_scope(env()->context()); \ 80 v8::Local<v8::Value> arg; \ 81 if (!permission::CreateAccessDeniedError(env(), (scope), resource) \ 82 .ToLocal(&arg)) { \ 83 } \ 84 MakeCallback(env()->oncomplete_string(), 1, &arg); \ 85 } 86 87 class Permission { 88 public: 89 Permission(); 90 91 FORCE_INLINE bool is_granted(Environment* env, 92 const PermissionScope permission, 93 const std::string_view& res = "") const { 94 if (!enabled_) [[likely]] { 95 return true; 96 } 97 return is_scope_granted(env, permission, res); 98 } 99 100 FORCE_INLINE bool enabled() const { return enabled_; } 101 102 static PermissionScope StringToPermission(const std::string& perm); 103 static const char* PermissionToString(PermissionScope perm); 104 static void ThrowAccessDenied(Environment* env, 105 PermissionScope perm, 106 const std::string_view& res); 107 static void AsyncThrowAccessDenied(Environment* env, 108 fs::FSReqBase* req_wrap, 109 PermissionScope perm, 110 const std::string_view& res); 111 112 // CLI Call 113 void Apply(Environment* env, 114 const std::vector<std::string>& allow, 115 PermissionScope scope); 116 void EnablePermissions(); 117 118 private: 119 COLD_NOINLINE bool is_scope_granted(Environment* env, 120 const PermissionScope permission, 121 const std::string_view& res = "") const { 122 auto perm_node = nodes_.find(permission); 123 if (perm_node != nodes_.end()) { 124 return perm_node->second->is_granted(env, permission, res); 125 } 126 return false; 127 } 128 129 std::unordered_map<PermissionScope, std::shared_ptr<PermissionBase>> nodes_; 130 bool enabled_; 131 }; 132 133 } // namespace permission 134 135 } // namespace node 136 137 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 138 #endif // SRC_PERMISSION_PERMISSION_H_