diff options
Diffstat (limited to 'cli/file_fetcher.rs')
-rw-r--r-- | cli/file_fetcher.rs | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs index 3e3c29002..0e69560ef 100644 --- a/cli/file_fetcher.rs +++ b/cli/file_fetcher.rs @@ -213,7 +213,12 @@ impl SourceFileFetcher { self: &Self, module_url: &Url, ) -> Result<SourceFile, ErrBox> { - let filepath = module_url.to_file_path().expect("File URL expected"); + let filepath = module_url.to_file_path().map_err(|()| { + ErrBox::from(DenoError::new( + ErrorKind::InvalidPath, + "File URL contains invalid path".to_owned(), + )) + })?; let source_code = match fs::read(filepath.clone()) { Ok(c) => c, @@ -717,6 +722,20 @@ mod tests { } #[test] + fn test_fetch_local_file_no_panic() { + let (_temp_dir, fetcher) = test_setup(); + if cfg!(windows) { + // Should fail: missing drive letter. + let u = Url::parse("file:///etc/passwd").unwrap(); + fetcher.fetch_local_file(&u).unwrap_err(); + } else { + // Should fail: local network paths are not supported on unix. + let u = Url::parse("file://server/etc/passwd").unwrap(); + fetcher.fetch_local_file(&u).unwrap_err(); + } + } + + #[test] fn test_get_source_code_1() { let (temp_dir, fetcher) = test_setup(); // http_util::fetch_sync_string requires tokio |