Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --allow-import flag #25469

Merged
merged 43 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
fbb40ad
feat: Add `--allow-imports` flag
bartlomieju Sep 5, 2024
8170f32
actually make it work for dynamic imports
bartlomieju Sep 5, 2024
9f947cc
merge in main
dsherret Sep 17, 2024
ef0ebc9
Merge branch 'main' into allow_imports
dsherret Sep 19, 2024
d040407
updates
dsherret Sep 19, 2024
ba90b1a
Merge branch 'main' into allow_imports
dsherret Sep 21, 2024
9d42419
Starting to use permissions container more
dsherret Sep 22, 2024
eaff962
Starting to use permissions container more
dsherret Sep 22, 2024
05a39e9
Saving for revert
dsherret Sep 22, 2024
f99c39c
Revert "Saving for revert"
dsherret Sep 22, 2024
282dab6
add permissions for check and cache. Allow reading from the file syst…
dsherret Sep 22, 2024
479d046
Start adding tests and fix it not working for run
dsherret Sep 22, 2024
2b58d88
fix up flags
dsherret Sep 22, 2024
b0c74e0
add 0.0.0.0
dsherret Sep 22, 2024
9e48c18
test jsr_host_from_url
dsherret Sep 22, 2024
240e2c0
add gist.githubusercontent.com
dsherret Sep 22, 2024
3f88938
update test because localhost is allowed by default (though maybe it …
dsherret Sep 22, 2024
43f4624
improve naming
dsherret Sep 22, 2024
4e6b5d2
Merge branch 'main' into allow_imports
dsherret Sep 23, 2024
5630383
Clippy
dsherret Sep 24, 2024
d90489f
Merge branch 'main' into allow_imports
dsherret Sep 24, 2024
b458c3d
Fixing up more tests
dsherret Sep 24, 2024
3927928
Merge branch 'main' into allow_imports
dsherret Sep 25, 2024
1787bf7
Checking in before revert
dsherret Sep 25, 2024
9e53639
Fix worker permissions and update expectations
dsherret Sep 25, 2024
b326096
finish
dsherret Sep 25, 2024
134718f
clippy
dsherret Sep 25, 2024
a232f95
Start removing localhost as allowed by default
dsherret Sep 25, 2024
abb5de4
Merge branch 'main' into allow_imports
dsherret Sep 25, 2024
f1282e5
fix run tests
dsherret Sep 25, 2024
9a164ec
fix lsp caching
dsherret Sep 25, 2024
1124b45
Update and fix more tests
dsherret Sep 25, 2024
2dca777
more test fixes
dsherret Sep 25, 2024
f0d1c05
more fixes
dsherret Sep 25, 2024
d315bf2
Do not load non-jsr remote modules in `deno publish`
dsherret Sep 25, 2024
02120ac
fix publish tests
dsherret Sep 25, 2024
65e1e96
add tests for doc
dsherret Sep 25, 2024
4facd74
fix remaining tests
dsherret Sep 25, 2024
f4b41c5
self review
dsherret Sep 25, 2024
41c63f2
improve help output
dsherret Sep 25, 2024
79b9992
fix check_specifiers test
dsherret Sep 26, 2024
d7e1744
review and update linux test
dsherret Sep 26, 2024
c66b814
fix failing windows test
dsherret Sep 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "Saving for revert"
This reverts commit 05a39e9.
  • Loading branch information
dsherret committed Sep 22, 2024
commit f99c39c18aac067f43988bca2ca3d0fe3151f700
7 changes: 1 addition & 6 deletions cli/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ use deno_runtime::deno_node::DenoFsNodeResolverEnv;
use deno_runtime::deno_node::NodeResolver;
use deno_runtime::deno_permissions::Permissions;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::deno_permissions::PermissionsContainerKind;
use deno_runtime::deno_tls::rustls::RootCertStore;
use deno_runtime::deno_tls::RootCertStoreProvider;
use deno_runtime::deno_web::BlobStore;
Expand Down Expand Up @@ -771,11 +770,7 @@ impl CliFactory {
desc_parser.as_ref(),
&self.cli_options()?.permissions_options(),
)?;
Ok(PermissionsContainer::new(
desc_parser,
permissions,
PermissionsContainerKind::Root,
))
Ok(PermissionsContainer::new(desc_parser, permissions))
})
}

Expand Down
2 changes: 0 additions & 2 deletions cli/lsp/testing/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use deno_core::unsync::spawn_blocking;
use deno_core::ModuleSpecifier;
use deno_runtime::deno_permissions::Permissions;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::deno_permissions::PermissionsContainerKind;
use deno_runtime::tokio_util::create_and_run_current_thread;
use indexmap::IndexMap;
use std::borrow::Cow;
Expand Down Expand Up @@ -277,7 +276,6 @@ impl TestRun {
let permissions_container = PermissionsContainer::new(
permission_desc_parser.clone(),
permissions.clone(),
PermissionsContainerKind::Root,
);
let worker_sender = test_event_sender_factory.worker();
let fail_fast_tracker = fail_fast_tracker.clone();
Expand Down
7 changes: 1 addition & 6 deletions cli/ops/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use deno_runtime::deno_permissions::create_child_permissions;
use deno_runtime::deno_permissions::ChildPermissionsArg;
use deno_runtime::deno_permissions::PermissionDescriptorParser;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::deno_permissions::PermissionsContainerKind;
use tokio::sync::mpsc::UnboundedSender;
use uuid::Uuid;

Expand Down Expand Up @@ -73,11 +72,7 @@ pub fn op_pledge_test_permissions(
&mut parent_permissions,
args,
)?;
PermissionsContainer::new(
permission_desc_parser,
perms,
PermissionsContainerKind::Root,
)
PermissionsContainer::new(permission_desc_parser, perms)
};
let parent_permissions = parent_permissions.clone();

Expand Down
7 changes: 1 addition & 6 deletions cli/ops/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use deno_runtime::deno_permissions::create_child_permissions;
use deno_runtime::deno_permissions::ChildPermissionsArg;
use deno_runtime::deno_permissions::PermissionDescriptorParser;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::deno_permissions::PermissionsContainerKind;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;
Expand Down Expand Up @@ -68,11 +67,7 @@ pub fn op_pledge_test_permissions(
&mut parent_permissions,
args,
)?;
PermissionsContainer::new(
permission_desc_parser,
perms,
PermissionsContainerKind::Root,
)
PermissionsContainer::new(permission_desc_parser, perms)
};
let parent_permissions = parent_permissions.clone();

Expand Down
7 changes: 1 addition & 6 deletions cli/standalone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use deno_runtime::deno_node::create_host_defined_options;
use deno_runtime::deno_node::NodeResolver;
use deno_runtime::deno_permissions::Permissions;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::deno_permissions::PermissionsContainerKind;
use deno_runtime::deno_tls::rustls::RootCertStore;
use deno_runtime::deno_tls::RootCertStoreProvider;
use deno_runtime::deno_web::BlobStore;
Expand Down Expand Up @@ -686,11 +685,7 @@ pub async fn run(
Arc::new(RuntimePermissionDescriptorParser::new(fs.clone()));
let permissions =
Permissions::from_options(desc_parser.as_ref(), &permissions)?;
PermissionsContainer::new(
desc_parser,
permissions,
PermissionsContainerKind::Root,
)
PermissionsContainer::new(desc_parser, permissions)
};
let feature_checker = Arc::new({
let mut checker = FeatureChecker::default();
Expand Down
2 changes: 0 additions & 2 deletions cli/tools/bench/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use deno_core::ModuleSpecifier;
use deno_core::PollEventLoopOptions;
use deno_runtime::deno_permissions::Permissions;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::deno_permissions::PermissionsContainerKind;
use deno_runtime::permissions::RuntimePermissionDescriptorParser;
use deno_runtime::tokio_util::create_and_run_current_thread;
use deno_runtime::WorkerExecutionMode;
Expand Down Expand Up @@ -279,7 +278,6 @@ async fn bench_specifiers(
let permissions_container = PermissionsContainer::new(
permissions_desc_parser.clone(),
permissions.clone(),
PermissionsContainerKind::Root,
);
let sender = sender.clone();
let options = option_for_handles.clone();
Expand Down
7 changes: 2 additions & 5 deletions cli/tools/jupyter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use deno_core::url::Url;
use deno_runtime::deno_io::Stdio;
use deno_runtime::deno_io::StdioPipe;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::deno_permissions::PermissionsContainerKind;
use deno_runtime::WorkerExecutionMode;
use deno_terminal::colors;
use jupyter_runtime::jupyter::ConnectionInfo;
Expand Down Expand Up @@ -65,10 +64,8 @@ pub async fn kernel(
resolve_url_or_path("./$deno$jupyter.ts", cli_options.initial_cwd())
.unwrap();
// TODO(bartlomieju): should we run with all permissions?
let permissions = PermissionsContainer::allow_all(
factory.permission_desc_parser()?.clone(),
PermissionsContainerKind::Root,
);
let permissions =
PermissionsContainer::allow_all(factory.permission_desc_parser()?.clone());
let npm_resolver = factory.npm_resolver().await?.clone();
let resolver = factory.resolver().await?.clone();
let worker_factory = factory.create_cli_main_worker_factory().await?;
Expand Down
2 changes: 0 additions & 2 deletions cli/tools/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ use deno_runtime::deno_io::Stdio;
use deno_runtime::deno_io::StdioPipe;
use deno_runtime::deno_permissions::Permissions;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::deno_permissions::PermissionsContainerKind;
use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::permissions::RuntimePermissionDescriptorParser;
use deno_runtime::tokio_util::create_and_run_current_thread;
Expand Down Expand Up @@ -1223,7 +1222,6 @@ async fn test_specifiers(
let permissions_container = PermissionsContainer::new(
permission_desc_parser.clone(),
permissions.clone(),
PermissionsContainerKind::Root,
);
let worker_sender = test_event_sender_factory.worker();
let fail_fast_tracker = fail_fast_tracker.clone();
Expand Down
4 changes: 0 additions & 4 deletions cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use deno_runtime::deno_node;
use deno_runtime::deno_node::NodeExtInitServices;
use deno_runtime::deno_node::NodeResolver;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::deno_permissions::PermissionsContainerKind;
use deno_runtime::deno_tls::RootCertStoreProvider;
use deno_runtime::deno_web::BlobStore;
use deno_runtime::fmt_errors::format_js_error;
Expand Down Expand Up @@ -535,7 +534,6 @@ impl CliMainWorkerFactory {
shared.module_loader_factory.create_for_main(
PermissionsContainer::allow_all(
self.shared.permission_desc_parser.clone(),
PermissionsContainerKind::Root,
),
permissions.clone(),
);
Expand Down Expand Up @@ -845,15 +843,13 @@ mod tests {
use deno_core::resolve_path;
use deno_fs::RealFs;
use deno_runtime::deno_permissions::Permissions;
use deno_runtime::deno_permissions::PermissionsContainerKind;

fn create_test_worker() -> MainWorker {
let main_module =
resolve_path("./hello.js", &std::env::current_dir().unwrap()).unwrap();
let permissions = PermissionsContainer::new(
Arc::new(RuntimePermissionDescriptorParser::new(Arc::new(RealFs))),
Permissions::none_without_prompt(),
PermissionsContainerKind::Root,
);

let options = WorkerOptions {
Expand Down
8 changes: 3 additions & 5 deletions runtime/examples/extension/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use deno_core::op2;
use deno_core::FsModuleLoader;
use deno_core::ModuleSpecifier;
use deno_fs::RealFs;
use deno_permissions::PermissionsContainerKind;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::permissions::RuntimePermissionDescriptorParser;
use deno_runtime::worker::MainWorker;
Expand All @@ -38,10 +37,9 @@ async fn main() -> Result<(), AnyError> {
eprintln!("Running {main_module}...");
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
PermissionsContainer::allow_all(
Arc::new(RuntimePermissionDescriptorParser::new(Arc::new(RealFs))),
PermissionsContainerKind::Root,
),
PermissionsContainer::allow_all(Arc::new(
RuntimePermissionDescriptorParser::new(Arc::new(RealFs)),
)),
WorkerOptions {
module_loader: Rc::new(FsModuleLoader),
extensions: vec![hello_runtime::init_ops_and_esm()],
Expand Down
7 changes: 1 addition & 6 deletions runtime/ops/worker_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use deno_permissions::create_child_permissions;
use deno_permissions::ChildPermissionsArg;
use deno_permissions::PermissionDescriptorParser;
use deno_permissions::PermissionsContainer;
use deno_permissions::PermissionsContainerKind;
use deno_web::deserialize_js_transferables;
use deno_web::JsMessageData;
use log::debug;
Expand Down Expand Up @@ -167,11 +166,7 @@ fn op_create_worker(
&mut parent_permissions,
child_permissions_arg,
)?;
PermissionsContainer::new(
permission_desc_parser,
perms,
PermissionsContainerKind::Child,
)
PermissionsContainer::new(permission_desc_parser, perms)
} else {
parent_permissions.clone()
};
Expand Down
50 changes: 7 additions & 43 deletions runtime/permissions/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2078,12 +2078,6 @@ pub fn specifier_to_file_path(
}
}

#[derive(Debug, Clone, Copy)]
pub enum PermissionsContainerKind {
Root,
Child,
}

/// Wrapper struct for `Permissions` that can be shared across threads.
///
/// We need a way to have internal mutability for permissions as they might get
Expand All @@ -2098,41 +2092,33 @@ pub struct PermissionsContainer {
// so that the code is not so verbose elsewhere.
pub descriptor_parser: Arc<dyn PermissionDescriptorParser>,
pub inner: Arc<Mutex<Permissions>>,
pub kind: PermissionsContainerKind,
}

impl PermissionsContainer {
pub fn new(
descriptor_parser: Arc<dyn PermissionDescriptorParser>,
perms: Permissions,
kind: PermissionsContainerKind,
) -> Self {
Self {
descriptor_parser,
inner: Arc::new(Mutex::new(perms)),
kind,
}
}

pub fn allow_all(
descriptor_parser: Arc<dyn PermissionDescriptorParser>,
kind: PermissionsContainerKind,
) -> Self {
Self::new(descriptor_parser, Permissions::allow_all(), kind)
Self::new(descriptor_parser, Permissions::allow_all())
}

#[inline(always)]
pub fn check_specifier(
&self,
specifier: &ModuleSpecifier,
) -> Result<(), AnyError> {
let mut inner = self.inner.lock();
match specifier.scheme() {
"file" => {
// grant for the root?
if self.kind == PermissionsContainerKind::Root {
return Ok(());
}
let mut inner = self.inner.lock();
if inner.read.is_allow_all() {
return Ok(()); // avoid allocations below
}
Expand All @@ -2154,14 +2140,9 @@ impl PermissionsContainer {
"data" => Ok(()),
"blob" => Ok(()),
_ => {
let mut inner = self.inner.lock();
if inner.net.is_allow_all() && inner.import.is_allow_all() {
return Ok(()); // avoid allocations below
}
let desc = self
.descriptor_parser
.parse_net_descriptor_from_url(specifier)?;
// todo(THIS PR): only query if in net, but prompt for allow import
inner.net.check(&desc, Some("import()"))?;
inner
.import
Expand Down Expand Up @@ -3131,11 +3112,7 @@ mod tests {
},
)
.unwrap();
let mut perms = PermissionsContainer::new(
Arc::new(parser),
perms,
PermissionsContainerKind::Root,
);
let mut perms = PermissionsContainer::new(Arc::new(parser), perms);

let cases = [
// Inside of /a/specific and /a/specific/dir/name
Expand Down Expand Up @@ -3332,11 +3309,7 @@ mod tests {
},
)
.unwrap();
let mut perms = PermissionsContainer::new(
Arc::new(parser),
perms,
PermissionsContainerKind::Root,
);
let mut perms = PermissionsContainer::new(Arc::new(parser), perms);

let url_tests = vec![
// Any protocol + port for localhost should be ok, since we don't specify
Expand Down Expand Up @@ -3392,7 +3365,7 @@ mod tests {
svec!["/a"]
};
let parser = TestPermissionDescriptorParser;
let perms = Permissions::from_options(
let mut perms = Permissions::from_options(
&parser,
&PermissionsOptions {
allow_read: Some(read_allowlist),
Expand All @@ -3401,11 +3374,7 @@ mod tests {
},
)
.unwrap();
let perms = PermissionsContainer::new(
Arc::new(parser),
perms,
PermissionsContainerKind::Root,
);
let mut perms = PermissionsContainer::new(Arc::new(parser), perms);

let mut fixtures = vec![
(
Expand Down Expand Up @@ -3453,7 +3422,6 @@ mod tests {
let perms = PermissionsContainer::new(
Arc::new(TestPermissionDescriptorParser),
perms,
PermissionsContainerKind::Root,
);

let mut test_cases = vec![];
Expand Down Expand Up @@ -4043,11 +4011,7 @@ mod tests {
},
)
.unwrap();
let mut perms = PermissionsContainer::new(
Arc::new(parser),
perms,
PermissionsContainerKind::Root,
);
let mut perms = PermissionsContainer::new(Arc::new(parser), perms);
let cases = [
("allowed.domain.", true),
("1.1.1.1", true),
Expand Down