From fba743ef0b89cd0f6340a3473d80df7b955d04e0 Mon Sep 17 00:00:00 2001 From: Finn Bear Date: Fri, 23 Sep 2022 17:25:05 -0700 Subject: [PATCH] Don't let surf panic on invalid URIs (#1205) --- lib/src/fnc/http.rs | 5 +++-- lib/src/fnc/util/http/mod.rs | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/src/fnc/http.rs b/lib/src/fnc/http.rs index a6528d58..ddfd25c1 100644 --- a/lib/src/fnc/http.rs +++ b/lib/src/fnc/http.rs @@ -34,11 +34,12 @@ pub async fn delete((_, _): (Value, Option)) -> Result { fn try_as_uri(fn_name: &str, value: Value) -> Result { match value { - Value::Strand(uri) => Ok(uri), + // Avoid surf crate panic by pre-checking URI. + Value::Strand(uri) if crate::fnc::util::http::uri_is_valid(&uri) => Ok(uri), _ => Err(Error::InvalidArguments { name: fn_name.to_owned(), // Assumption is that URI is first argument. - message: String::from("The first argument should be a string."), + message: String::from("The first argument should be a string containing a valid URI."), }), } } diff --git a/lib/src/fnc/util/http/mod.rs b/lib/src/fnc/util/http/mod.rs index e31242f2..3950cfff 100644 --- a/lib/src/fnc/util/http/mod.rs +++ b/lib/src/fnc/util/http/mod.rs @@ -6,6 +6,10 @@ use crate::sql::value::Value; use surf::Client; use surf::Config; +pub(crate) fn uri_is_valid(uri: &str) -> bool { + surf::Url::parse(uri).is_ok() +} + pub async fn head(uri: Strand, opts: impl Into) -> Result { // Set a default client with no timeout let cli: Client = Config::new().set_timeout(None).try_into().unwrap();