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

BREAKING: disallow static import of local modules from remote modules #5050

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions cli/ops/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ fn op_fetch_source_files(
async move {
let resolved_specifier = ModuleSpecifier::resolve_url(&specifier)
.expect("Invalid specifier");
// TODO(bartlomieju): duplicated from `state.rs::ModuleLoader::load` - deduplicate
// Verify that remote file doesn't try to statically import local file.
if let Some(referrer) = ref_specifier_.as_ref() {
let referrer_url = referrer.as_url();
match referrer_url.scheme() {
"http" | "https" => {
Comment on lines +115 to +120
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cruft will be removed during TS compiler refactor

let specifier_url = resolved_specifier.as_url();
match specifier_url.scheme() {
"http" | "https" => {},
_ => {
let e = OpError::permission_denied("Remote module are not allowed to statically import local modules. Use dynamic import instead.".to_string());
return Err(e.into());
}
}
},
_ => {}
}
}
file_fetcher_
.fetch_source_file(&resolved_specifier, ref_specifier_)
.await
Expand Down
18 changes: 18 additions & 0 deletions cli/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,24 @@ impl ModuleLoader for State {
if let Err(e) = self.check_dyn_import(&module_specifier) {
return async move { Err(e.into()) }.boxed_local();
}
} else {
// Verify that remote file doesn't try to statically import local file.
if let Some(referrer) = maybe_referrer.as_ref() {
let referrer_url = referrer.as_url();
match referrer_url.scheme() {
"http" | "https" => {
let specifier_url = module_specifier.as_url();
match specifier_url.scheme() {
"http" | "https" => {}
_ => {
let e = OpError::permission_denied("Remote module are not allowed to statically import local modules. Use dynamic import instead.".to_string());
return async move { Err(e.into()) }.boxed_local();
}
}
}
_ => {}
}
}
}

let mut state = self.borrow_mut();
Expand Down
1 change: 1 addition & 0 deletions cli/tests/error_local_static_import_from_remote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "file:///some/dir/file.js";
2 changes: 2 additions & 0 deletions cli/tests/error_local_static_import_from_remote.js.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[WILDCARD]
Remote module are not allowed to statically import local modules. Use dynamic import instead.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of .js-only imports the error is very sparse and has no stack trace...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better than nothing

1 change: 1 addition & 0 deletions cli/tests/error_local_static_import_from_remote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "file:///some/dir/file.ts";
9 changes: 9 additions & 0 deletions cli/tests/error_local_static_import_from_remote.ts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[WILDCARD]
error: Uncaught PermissionDenied: Remote module are not allowed to statically import local modules. Use dynamic import instead.
at unwrapResponse ($deno$/ops/dispatch_json.ts:[WILDCARD])
at Object.sendAsync ($deno$/ops/dispatch_json.ts:[WILDCARD])
at async processImports ($deno$/compiler/imports.ts:[WILDCARD])
at async Object.processImports ($deno$/compiler/imports.ts:[WILDCARD])
at async compile ([WILDCARD]compiler.ts:[WILDCARD])
at async tsCompilerOnMessage ([WILDCARD]compiler.ts:[WILDCARD])
at async workerMessageRecvCallback ($deno$/runtime_worker.ts:[WILDCARD])
16 changes: 16 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,22 @@ itest!(error_type_definitions {
output: "error_type_definitions.ts.out",
});

itest!(error_local_static_import_from_remote_ts {
args: "run --reload http://localhost:4545/cli/tests/error_local_static_import_from_remote.ts",
check_stderr: true,
exit_code: 1,
http_server: true,
output: "error_local_static_import_from_remote.ts.out",
});

itest!(error_local_static_import_from_remote_js {
args: "run --reload http://localhost:4545/cli/tests/error_local_static_import_from_remote.js",
check_stderr: true,
exit_code: 1,
http_server: true,
output: "error_local_static_import_from_remote.js.out",
});

// TODO(bartlomieju) Re-enable
itest_ignore!(error_worker_dynamic {
args: "run --reload error_worker_dynamic.ts",
Expand Down