Skip to content

Commit

Permalink
fix: report canonicalized module path (#560)
Browse files Browse the repository at this point in the history
In 31c0292 (#557), I improved the error message printed when the
module is outside of the root dir to include the file path of the
imported module. That's not enough to troubleshoot the problem,
because we print the non-canonical path, which is potentially different
from the string we use to check if the module path is within the root
dir.

In this commit, I refactored the method to make the code easier to
follow, and improved the error message to print the canonicalized
paths of the module and the root dir.

Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos authored Jul 8, 2024
1 parent 575acd8 commit 67fa72b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
84 changes: 42 additions & 42 deletions runtime/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,52 +72,52 @@ impl ModuleLoader for ZinniaModuleLoader {
async move {
let spec_str = module_specifier.as_str();

let code = {
let is_module_local = match module_specifier.to_file_path() {
Err(()) => false,
Ok(module_path) => {
match &module_root {
None => true,
Some(root) => module_path
// Resolve any symlinks inside the path to prevent modules from escaping our sandbox
.canonicalize()
// Check that the module path is inside the module root directory
.map(|p| p.starts_with(root))
.unwrap_or(false),
}
},
};
if is_module_local {
read_file_to_string(module_specifier.to_file_path().unwrap()).await?
} else if spec_str == "https://deno.land/[email protected]/testing/asserts.ts" || spec_str == "https://deno.land/[email protected]/testing/asserts.ts" {
let details = || {
let mut msg = format!("\nModule URL: {spec_str}");
if let Some(referrer) = &maybe_referrer {
msg.push_str("\nImported from: ");
msg.push_str(referrer.as_str());
}
msg
};

if spec_str == "https://deno.land/[email protected]/testing/asserts.ts" || spec_str == "https://deno.land/[email protected]/testing/asserts.ts" {
return Err(anyhow!("Zinnia bundles Deno asserts as 'zinnia:assert`. Please update your imports accordingly.{}", details()));
}

if module_specifier.scheme() != "file" {
return Err(anyhow!(
"Unsupported scheme: {}. Zinnia can import local modules only.{}",
module_specifier.scheme(),
details()
))
}

let module_path = module_specifier.to_file_path().map_err(|_|
anyhow!("Module specifier cannot be converted to a filepath.{}", details())
)?;

// Check that the module path is inside the module root directory
if let Some(canonical_root) = &module_root {
// Resolve any symlinks inside the path to prevent modules from escaping our sandbox
let canonical_module = module_path.canonicalize().map_err(|err|
anyhow!("Cannot canonicalize module path: {err}.{}", details())
)?;

if !canonical_module.starts_with(canonical_root) {
return Err(anyhow!(
"Zinnia bundles Deno asserts as 'zinnia:assert`. Please update your imports accordingly.\nModule URL: {spec_str}\nImported from: {}",
maybe_referrer.map(|u| u.to_string()).unwrap_or("(none)".into())
"Cannot import files outside of the module root directory.\n\
Root directory (canonical): {}\n\
Module file path (canonical): {}\
{}",
canonical_root.display(),
canonical_module.display(),
details()
));
} else {
let mut msg = if module_specifier.scheme() == "file" && module_root.is_some() {
let module_desc = match module_specifier.to_file_path() {
Ok(m) => format!("Module file path: {}", m.display()),
Err(_) => "(Cannot convert the module specifier to a file path)".to_string(),
};
format!(
"Cannot import files outside of module root directory {}\n{}",
module_root.unwrap().display(),
module_desc
)
} else {
"Zinnia supports importing from relative paths only. ".to_string()
};
msg.push_str("\nModule URL: ");
msg.push_str(spec_str);
if let Some(referrer) = &maybe_referrer {
msg.push_str("\nImported from: ");
msg.push_str(referrer.as_str());
}
return Err(anyhow!(msg));
}
};

let code = read_file_to_string(module_path).await?;
let module = ModuleSource::new(ModuleType::JavaScript, code.into(), &module_specifier);
Ok(module)
}.boxed_local()
Expand Down Expand Up @@ -196,7 +196,7 @@ mod tests {
Err(err) => {
let msg = format!("{err}");
assert!(
msg.contains("Cannot import files outside of module root directory"),
msg.contains("Cannot import files outside of the module root directory"),
"Expected import to fail with the sandboxing error, it failed with a different error instead:\n{}",
msg,
);
Expand Down
2 changes: 1 addition & 1 deletion runtime/tests/js/module_loader_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ test("can import files outside the main module directory", async () => {

test("cannot import files over http", async () => {
let err = await assertRejects(() => import("https://deno.land/[email protected]/version.ts"));
assertMatch(err.message, /Zinnia supports importing from relative paths only/);
assertMatch(err.message, /Zinnia can import local modules only/);
});

0 comments on commit 67fa72b

Please sign in to comment.