summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/args/flags.rs12
-rw-r--r--runtime/ops/permissions.rs22
-rw-r--r--runtime/permissions.rs9
3 files changed, 19 insertions, 24 deletions
diff --git a/cli/args/flags.rs b/cli/args/flags.rs
index a4eca942f..5e5d80c4a 100644
--- a/cli/args/flags.rs
+++ b/cli/args/flags.rs
@@ -5,9 +5,11 @@ use clap::ArgMatches;
use clap::ColorChoice;
use clap::Command;
use clap::ValueHint;
+use deno_core::error::AnyError;
use deno_core::serde::Deserialize;
use deno_core::serde::Serialize;
use deno_core::url::Url;
+use deno_runtime::permissions::parse_sys_kind;
use deno_runtime::permissions::PermissionsOptions;
use log::debug;
use log::Level;
@@ -1842,15 +1844,9 @@ fn permission_args(app: Command) -> Command {
.help("Allow access to system info")
.validator(|keys| {
for key in keys.split(',') {
- match key {
- "hostname" | "osRelease" | "loadavg" | "networkInterfaces"
- | "systemMemoryInfo" | "getUid" | "getGid" => {}
- _ => {
- return Err(format!("unknown system info kind \"{}\"", key));
- }
- }
+ parse_sys_kind(key)?;
}
- Ok(())
+ Ok::<(), AnyError>(())
}),
)
.arg(
diff --git a/runtime/ops/permissions.rs b/runtime/ops/permissions.rs
index b79330f7f..5d6352744 100644
--- a/runtime/ops/permissions.rs
+++ b/runtime/ops/permissions.rs
@@ -1,8 +1,8 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+use crate::permissions::parse_sys_kind;
use crate::permissions::Permissions;
use deno_core::error::custom_error;
-use deno_core::error::type_error;
use deno_core::error::uri_error;
use deno_core::error::AnyError;
use deno_core::op;
@@ -50,7 +50,9 @@ pub fn op_query_permission(
.as_ref(),
),
"env" => permissions.env.query(args.variable.as_deref()),
- "sys" => permissions.sys.query(parse_sys_kind(args.kind.as_deref())?),
+ "sys" => permissions
+ .sys
+ .query(args.kind.as_deref().map(parse_sys_kind).transpose()?),
"run" => permissions.run.query(args.command.as_deref()),
"ffi" => permissions.ffi.query(args.path.as_deref().map(Path::new)),
"hrtime" => permissions.hrtime.query(),
@@ -84,7 +86,7 @@ pub fn op_revoke_permission(
"env" => permissions.env.revoke(args.variable.as_deref()),
"sys" => permissions
.sys
- .revoke(parse_sys_kind(args.kind.as_deref())?),
+ .revoke(args.kind.as_deref().map(parse_sys_kind).transpose()?),
"run" => permissions.run.revoke(args.command.as_deref()),
"ffi" => permissions.ffi.revoke(args.path.as_deref().map(Path::new)),
"hrtime" => permissions.hrtime.revoke(),
@@ -118,7 +120,7 @@ pub fn op_request_permission(
"env" => permissions.env.request(args.variable.as_deref()),
"sys" => permissions
.sys
- .request(parse_sys_kind(args.kind.as_deref())?),
+ .request(args.kind.as_deref().map(parse_sys_kind).transpose()?),
"run" => permissions.run.request(args.command.as_deref()),
"ffi" => permissions.ffi.request(args.path.as_deref().map(Path::new)),
"hrtime" => permissions.hrtime.request(),
@@ -141,15 +143,3 @@ fn parse_host(host_str: &str) -> Result<(String, Option<u16>), AnyError> {
let hostname = url.host_str().unwrap();
Ok((hostname.to_string(), url.port()))
}
-
-fn parse_sys_kind(kind: Option<&str>) -> Result<Option<&str>, AnyError> {
- if let Some(kind) = kind {
- match kind {
- "hostname" | "osRelease" | "loadavg" | "networkInterfaces"
- | "systemMemoryInfo" | "getUid" | "getGid" => Ok(Some(kind)),
- _ => Err(type_error(format!("unknown system info kind \"{}\"", kind))),
- }
- } else {
- Ok(kind)
- }
-}
diff --git a/runtime/permissions.rs b/runtime/permissions.rs
index 84ff286b4..1568410b3 100644
--- a/runtime/permissions.rs
+++ b/runtime/permissions.rs
@@ -3,6 +3,7 @@
use crate::colors;
use crate::fs_util::resolve_from_cwd;
use deno_core::error::custom_error;
+use deno_core::error::type_error;
use deno_core::error::uri_error;
use deno_core::error::AnyError;
#[cfg(test)]
@@ -304,6 +305,14 @@ impl ToString for RunDescriptor {
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct SysDescriptor(pub String);
+pub fn parse_sys_kind(kind: &str) -> Result<&str, AnyError> {
+ match kind {
+ "hostname" | "osRelease" | "loadavg" | "networkInterfaces"
+ | "systemMemoryInfo" | "getUid" | "getGid" => Ok(kind),
+ _ => Err(type_error(format!("unknown system info kind \"{}\"", kind))),
+ }
+}
+
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct FfiDescriptor(pub PathBuf);