From c0a684c14ed70717e18b528bb8f366eb593636a7 Mon Sep 17 00:00:00 2001 From: Alexander Sage Date: Tue, 6 Sep 2022 09:30:42 -0700 Subject: [PATCH] fix(cli): Fix panic when providing invalid urls to --reload (#15784) --- cli/args/flags.rs | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 861cbb5fbf..afd505f449 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -1937,6 +1937,7 @@ fn reload_arg<'a>() -> Arg<'a> { Reloads specific modules", ) .value_hint(ValueHint::FilePath) + .validator(reload_arg_validate) } fn ca_file_arg<'a>() -> Arg<'a> { @@ -3046,6 +3047,16 @@ fn inspect_arg_validate(val: &str) -> Result<(), String> { } } +fn reload_arg_validate(urlstr: &str) -> Result<(), String> { + if urlstr.is_empty() { + return Err(String::from("Missing url. Check for extra commas.")); + } + match Url::from_str(urlstr) { + Ok(_) => Ok(()), + Err(e) => Err(e.to_string()), + } +} + fn watch_arg_parse( flags: &mut Flags, matches: &clap::ArgMatches, @@ -4280,6 +4291,78 @@ mod tests { assert!(r.is_err()); } + #[test] + fn reload_validator() { + let r = flags_from_vec(svec![ + "deno", + "run", + "--reload=http://deno.land/", + "script.ts" + ]); + assert!(r.is_ok(), "should accept valid urls"); + + let r = flags_from_vec(svec![ + "deno", + "run", + "--reload=http://deno.land/a,http://deno.land/b", + "script.ts" + ]); + assert!(r.is_ok(), "should accept accept multiple valid urls"); + + let r = flags_from_vec(svec![ + "deno", + "run", + "--reload=./relativeurl/", + "script.ts" + ]); + assert!(r.is_err(), "Should reject relative urls that start with ./"); + + let r = flags_from_vec(svec![ + "deno", + "run", + "--reload=relativeurl/", + "script.ts" + ]); + assert!(r.is_err(), "Should reject relative urls"); + + let r = + flags_from_vec(svec!["deno", "run", "--reload=/absolute", "script.ts"]); + assert!(r.is_err(), "Should reject absolute urls"); + + let r = flags_from_vec(svec!["deno", "run", "--reload=/", "script.ts"]); + assert!(r.is_err(), "Should reject absolute root url"); + + let r = flags_from_vec(svec!["deno", "run", "--reload=", "script.ts"]); + assert!(r.is_err(), "Should reject when nothing is provided"); + + let r = flags_from_vec(svec!["deno", "run", "--reload=,", "script.ts"]); + assert!(r.is_err(), "Should reject when a single comma is provided"); + + let r = flags_from_vec(svec![ + "deno", + "run", + "--reload=,http://deno.land/a", + "script.ts" + ]); + assert!(r.is_err(), "Should reject a leading comma"); + + let r = flags_from_vec(svec![ + "deno", + "run", + "--reload=http://deno.land/a,", + "script.ts" + ]); + assert!(r.is_err(), "Should reject a trailing comma"); + + let r = flags_from_vec(svec![ + "deno", + "run", + "--reload=http://deno.land/a,,http://deno.land/b", + "script.ts" + ]); + assert!(r.is_err(), "Should reject adjacent commas"); + } + #[test] fn bundle() { let r = flags_from_vec(svec!["deno", "bundle", "source.ts"]);