Skip to content

Commit

Permalink
lib,src,permission: port path.resolve to C++
Browse files Browse the repository at this point in the history
Co-Authored-By: Carlos Espa <cespatorres@gmail.com>
  • Loading branch information
RafaelGSS and Ceres6 committed Nov 22, 2023
1 parent 8e60189 commit b2649a3
Show file tree
Hide file tree
Showing 17 changed files with 395 additions and 31 deletions.
14 changes: 9 additions & 5 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -879,21 +879,25 @@ Environment::Environment(IsolateData* isolate_data,
// unless explicitly allowed by the user
options_->allow_native_addons = false;
flags_ = flags_ | EnvironmentFlags::kNoCreateInspector;
permission()->Apply({"*"}, permission::PermissionScope::kInspector);
permission()->Apply(this, {"*"}, permission::PermissionScope::kInspector);
if (!options_->allow_child_process) {
permission()->Apply({"*"}, permission::PermissionScope::kChildProcess);
permission()->Apply(
this, {"*"}, permission::PermissionScope::kChildProcess);
}
if (!options_->allow_worker_threads) {
permission()->Apply({"*"}, permission::PermissionScope::kWorkerThreads);
permission()->Apply(
this, {"*"}, permission::PermissionScope::kWorkerThreads);
}

if (!options_->allow_fs_read.empty()) {
permission()->Apply(options_->allow_fs_read,
permission()->Apply(this,
options_->allow_fs_read,
permission::PermissionScope::kFileSystemRead);
}

if (!options_->allow_fs_write.empty()) {
permission()->Apply(options_->allow_fs_write,
permission()->Apply(this,
options_->allow_fs_write,
permission::PermissionScope::kFileSystemWrite);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/permission/child_process_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace permission {

// Currently, ChildProcess manage a single state
// Once denied, it's always denied
void ChildProcessPermission::Apply(const std::vector<std::string>& allow,
void ChildProcessPermission::Apply(Environment* env,
const std::vector<std::string>& allow,
PermissionScope scope) {
deny_all_ = true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/permission/child_process_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace permission {

class ChildProcessPermission final : public PermissionBase {
public:
void Apply(const std::vector<std::string>& allow,
void Apply(Environment* env,
const std::vector<std::string>& allow,
PermissionScope scope) override;
bool is_granted(PermissionScope perm,
const std::string_view& param = "") const override;
Expand Down
5 changes: 3 additions & 2 deletions src/permission/fs_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ namespace permission {

// allow = '*'
// allow = '/tmp/,/home/example.js'
void FSPermission::Apply(const std::vector<std::string>& allow,
void FSPermission::Apply(Environment* env,
const std::vector<std::string>& allow,
PermissionScope scope) {
for (const std::string& res : allow) {
if (res == "*") {
Expand All @@ -130,7 +131,7 @@ void FSPermission::Apply(const std::vector<std::string>& allow,
}
return;
}
GrantAccess(scope, res);
GrantAccess(scope, PathResolve(env, {res}));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/permission/fs_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ namespace permission {

class FSPermission final : public PermissionBase {
public:
void Apply(const std::vector<std::string>& allow,
void Apply(Environment* env,
const std::vector<std::string>& allow,
PermissionScope scope) override;
bool is_granted(PermissionScope perm,
const std::string_view& param) const override;
Expand Down
3 changes: 2 additions & 1 deletion src/permission/inspector_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace permission {

// Currently, Inspector manage a single state
// Once denied, it's always denied
void InspectorPermission::Apply(const std::vector<std::string>& allow,
void InspectorPermission::Apply(Environment* env,
const std::vector<std::string>& allow,
PermissionScope scope) {
deny_all_ = true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/permission/inspector_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace permission {

class InspectorPermission final : public PermissionBase {
public:
void Apply(const std::vector<std::string>& allow,
void Apply(Environment* env,
const std::vector<std::string>& allow,
PermissionScope scope) override;
bool is_granted(PermissionScope perm,
const std::string_view& param = "") const override;
Expand Down
5 changes: 3 additions & 2 deletions src/permission/permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ void Permission::EnablePermissions() {
}
}

void Permission::Apply(const std::vector<std::string>& allow,
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(allow, scope);
permission->second->Apply(env, allow, scope);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/permission/permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class Permission {
const std::string_view& res);

// CLI Call
void Apply(const std::vector<std::string>& allow, PermissionScope scope);
void Apply(Environment* env,
const std::vector<std::string>& allow,
PermissionScope scope);
void EnablePermissions();

private:
Expand Down
5 changes: 4 additions & 1 deletion src/permission/permission_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace node {

class Environment;

namespace permission {

#define FILESYSTEM_PERMISSIONS(V) \
Expand Down Expand Up @@ -39,7 +41,8 @@ enum class PermissionScope {

class PermissionBase {
public:
virtual void Apply(const std::vector<std::string>& allow,
virtual void Apply(Environment* env,
const std::vector<std::string>& allow,
PermissionScope scope) = 0;
virtual bool is_granted(PermissionScope perm,
const std::string_view& param = "") const = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/permission/worker_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace permission {

// Currently, PolicyDenyWorker manage a single state
// Once denied, it's always denied
void WorkerPermission::Apply(const std::vector<std::string>& allow,
void WorkerPermission::Apply(Environment* env,
const std::vector<std::string>& allow,
PermissionScope scope) {
deny_all_ = true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/permission/worker_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace permission {

class WorkerPermission final : public PermissionBase {
public:
void Apply(const std::vector<std::string>& allow,
void Apply(Environment* env,
const std::vector<std::string>& allow,
PermissionScope scope) override;
bool is_granted(PermissionScope perm,
const std::string_view& param = "") const override;
Expand Down
Loading

0 comments on commit b2649a3

Please sign in to comment.