fix(cli): avoid crash on import of invalid module names (#19523)

Fixes https://github.com/denoland/deno/issues/17748
Closes #17770

Co-authored-by: Anton Bershanskiy
<bershanskiy@users.noreply.github.com>
Co-authored-by: David Sherret <dsherret@users.noreply.github.com>
This commit is contained in:
Bartek Iwańczuk 2023-06-15 20:36:33 +02:00 committed by GitHub
parent fa63fd4610
commit 0733943fe7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 1 deletions

View file

@ -363,7 +363,23 @@ impl CliNpmRegistryApiInner {
}
fn get_package_url(&self, name: &str) -> Url {
self.base_url.join(name).unwrap()
// list of all characters used in npm packages:
// !, ', (, ), *, -, ., /, [0-9], @, [A-Za-z], _, ~
const ASCII_SET: percent_encoding::AsciiSet =
percent_encoding::NON_ALPHANUMERIC
.remove(b'!')
.remove(b'\'')
.remove(b'(')
.remove(b')')
.remove(b'*')
.remove(b'-')
.remove(b'.')
.remove(b'/')
.remove(b'@')
.remove(b'_')
.remove(b'~');
let name = percent_encoding::utf8_percent_encode(name, &ASCII_SET);
self.base_url.join(&name.to_string()).unwrap()
}
fn get_package_file_cache_path(&self, name: &str) -> PathBuf {

View file

@ -183,6 +183,13 @@ itest!(dynamic_import_reload_same_package {
http_server: true,
});
itest!(dynamic_import_invalid_package_name {
args: "run -A --reload npm/dynamic_import_invalid_package_name/main.ts",
output: "npm/dynamic_import_invalid_package_name/main.out",
envs: env_vars_for_npm_tests(),
http_server: true,
});
itest!(env_var_re_export_dev {
args: "run --allow-read --allow-env --quiet npm/env_var_re_export/main.js",
output_str: Some("dev\n"),

View file

@ -0,0 +1,6 @@
Download http://localhost:4545/npm/registry/ws%3A
FAILED
TypeError: npm package 'ws:' does not exist.
at async file:///[WILDCARD]/dynamic_import_invalid_package_name/main.ts:2:3 {
code: "ERR_MODULE_NOT_FOUND"
}

View file

@ -0,0 +1,6 @@
try {
await import(`npm:${"ws:"}`);
} catch (err) {
console.log("FAILED");
console.log(err);
}