diff options
Diffstat (limited to 'cli/permissions.rs')
-rw-r--r-- | cli/permissions.rs | 51 |
1 files changed, 29 insertions, 22 deletions
diff --git a/cli/permissions.rs b/cli/permissions.rs index 2179ed33f..a56754f18 100644 --- a/cli/permissions.rs +++ b/cli/permissions.rs @@ -97,7 +97,7 @@ impl Default for PermissionState { } #[derive(Clone, Debug, Default)] -pub struct DenoPermissions { +pub struct Permissions { // Keep in sync with cli/js/permissions.ts pub allow_read: PermissionState, pub read_whitelist: HashSet<PathBuf>, @@ -111,8 +111,15 @@ pub struct DenoPermissions { pub allow_hrtime: PermissionState, } -impl DenoPermissions { +impl Permissions { pub fn from_flags(flags: &Flags) -> Self { + // assert each whitelist path is absolute, since the cwd may change. + for path in &flags.read_whitelist { + assert!(path.has_root()); + } + for path in &flags.write_whitelist { + assert!(path.has_root()); + } Self { allow_read: PermissionState::from(flags.allow_read), read_whitelist: flags.read_whitelist.iter().cloned().collect(), @@ -392,7 +399,7 @@ mod tests { PathBuf::from("/b/c"), ]; - let perms = DenoPermissions::from_flags(&Flags { + let perms = Permissions::from_flags(&Flags { read_whitelist: whitelist.clone(), write_whitelist: whitelist, ..Default::default() @@ -439,7 +446,7 @@ mod tests { #[test] fn test_check_net() { - let perms = DenoPermissions::from_flags(&Flags { + let perms = Permissions::from_flags(&Flags { net_whitelist: svec![ "localhost", "deno.land", @@ -523,13 +530,13 @@ mod tests { #[test] fn test_permissions_request_run() { let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); - let mut perms0 = DenoPermissions::from_flags(&Flags { + let mut perms0 = Permissions::from_flags(&Flags { ..Default::default() }); set_prompt_result(true); assert_eq!(perms0.request_run(), PermissionState::Allow); - let mut perms1 = DenoPermissions::from_flags(&Flags { + let mut perms1 = Permissions::from_flags(&Flags { ..Default::default() }); set_prompt_result(false); @@ -541,7 +548,7 @@ mod tests { fn test_permissions_request_read() { let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); let whitelist = vec![PathBuf::from("/foo/bar")]; - let mut perms0 = DenoPermissions::from_flags(&Flags { + let mut perms0 = Permissions::from_flags(&Flags { read_whitelist: whitelist.clone(), ..Default::default() }); @@ -553,7 +560,7 @@ mod tests { PermissionState::Allow ); - let mut perms1 = DenoPermissions::from_flags(&Flags { + let mut perms1 = Permissions::from_flags(&Flags { read_whitelist: whitelist.clone(), ..Default::default() }); @@ -563,7 +570,7 @@ mod tests { PermissionState::Allow ); - let mut perms2 = DenoPermissions::from_flags(&Flags { + let mut perms2 = Permissions::from_flags(&Flags { read_whitelist: whitelist, ..Default::default() }); @@ -579,7 +586,7 @@ mod tests { fn test_permissions_request_write() { let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); let whitelist = vec![PathBuf::from("/foo/bar")]; - let mut perms0 = DenoPermissions::from_flags(&Flags { + let mut perms0 = Permissions::from_flags(&Flags { write_whitelist: whitelist.clone(), ..Default::default() }); @@ -591,7 +598,7 @@ mod tests { PermissionState::Allow ); - let mut perms1 = DenoPermissions::from_flags(&Flags { + let mut perms1 = Permissions::from_flags(&Flags { write_whitelist: whitelist.clone(), ..Default::default() }); @@ -601,7 +608,7 @@ mod tests { PermissionState::Allow ); - let mut perms2 = DenoPermissions::from_flags(&Flags { + let mut perms2 = Permissions::from_flags(&Flags { write_whitelist: whitelist, ..Default::default() }); @@ -618,7 +625,7 @@ mod tests { let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); let whitelist = svec!["localhost:8080"]; - let mut perms0 = DenoPermissions::from_flags(&Flags { + let mut perms0 = Permissions::from_flags(&Flags { net_whitelist: whitelist.clone(), ..Default::default() }); @@ -632,7 +639,7 @@ mod tests { PermissionState::Allow ); - let mut perms1 = DenoPermissions::from_flags(&Flags { + let mut perms1 = Permissions::from_flags(&Flags { net_whitelist: whitelist.clone(), ..Default::default() }); @@ -644,7 +651,7 @@ mod tests { PermissionState::Allow ); - let mut perms2 = DenoPermissions::from_flags(&Flags { + let mut perms2 = Permissions::from_flags(&Flags { net_whitelist: whitelist.clone(), ..Default::default() }); @@ -656,7 +663,7 @@ mod tests { PermissionState::Deny ); - let mut perms3 = DenoPermissions::from_flags(&Flags { + let mut perms3 = Permissions::from_flags(&Flags { net_whitelist: whitelist, ..Default::default() }); @@ -668,13 +675,13 @@ mod tests { #[test] fn test_permissions_request_env() { let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); - let mut perms0 = DenoPermissions::from_flags(&Flags { + let mut perms0 = Permissions::from_flags(&Flags { ..Default::default() }); set_prompt_result(true); assert_eq!(perms0.request_env(), PermissionState::Allow); - let mut perms1 = DenoPermissions::from_flags(&Flags { + let mut perms1 = Permissions::from_flags(&Flags { ..Default::default() }); set_prompt_result(false); @@ -685,13 +692,13 @@ mod tests { #[test] fn test_permissions_request_plugin() { let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); - let mut perms0 = DenoPermissions::from_flags(&Flags { + let mut perms0 = Permissions::from_flags(&Flags { ..Default::default() }); set_prompt_result(true); assert_eq!(perms0.request_plugin(), PermissionState::Allow); - let mut perms1 = DenoPermissions::from_flags(&Flags { + let mut perms1 = Permissions::from_flags(&Flags { ..Default::default() }); set_prompt_result(false); @@ -702,13 +709,13 @@ mod tests { #[test] fn test_permissions_request_hrtime() { let guard = PERMISSION_PROMPT_GUARD.lock().unwrap(); - let mut perms0 = DenoPermissions::from_flags(&Flags { + let mut perms0 = Permissions::from_flags(&Flags { ..Default::default() }); set_prompt_result(true); assert_eq!(perms0.request_hrtime(), PermissionState::Allow); - let mut perms1 = DenoPermissions::from_flags(&Flags { + let mut perms1 = Permissions::from_flags(&Flags { ..Default::default() }); set_prompt_result(false); |