From 41fd5f22ff332ed5a6a3620f4025b44c26dc46ea Mon Sep 17 00:00:00 2001 From: Kim Minh Kaplan Date: Wed, 4 Oct 2023 18:11:17 +0000 Subject: [PATCH] Do not panic when trying to parse a color that is not ASCII. --- imageflow_helpers/src/colors.rs | 3 +++ imageflow_riapi/src/ir4/parsing.rs | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/imageflow_helpers/src/colors.rs b/imageflow_helpers/src/colors.rs index accd0f39f..d31a12da0 100644 --- a/imageflow_helpers/src/colors.rs +++ b/imageflow_helpers/src/colors.rs @@ -28,6 +28,9 @@ fn parse_rgba_slices(r: &str, g: &str, b: &str, a :&str) -> Result std::result::Result { + if ! value.is_ascii() { + return Err(ParseColorError::FormatIncorrect("CSS colors must be in ASCII only")); + } let value = match &value[0..1] { "#" => &value[1..], _ => value diff --git a/imageflow_riapi/src/ir4/parsing.rs b/imageflow_riapi/src/ir4/parsing.rs index cf9c94578..83045668b 100644 --- a/imageflow_riapi/src/ir4/parsing.rs +++ b/imageflow_riapi/src/ir4/parsing.rs @@ -424,7 +424,7 @@ impl Instructions{ i.cropyunits = p.parse_f64("cropyunits"); i.quality = p.parse_i32("quality").or_else(||p.parse_i32("jpeg.quality")); i.zoom = p.parse_f64("zoom").or_else(|| p.parse_f64("dpr")); - i.bgcolor_srgb = p.parse_color_srgb("bgcolor").or_else(||p.parse_color_srgb("bgcolor")); + i.bgcolor_srgb = p.parse_color_srgb("bgcolor"); i.jpeg_subsampling = p.parse_subsampling("subsampling"); i.webp_quality = p.parse_f64("webp.quality"); @@ -1098,6 +1098,7 @@ fn test_url_parsing() { t("s.grayscale=Y", Instructions{s_grayscale: Some(GrayscaleAlgorithm::Y), ..Default::default()}, vec![]); t("s.grayscale=Bt709", Instructions{s_grayscale: Some(GrayscaleAlgorithm::Bt709), ..Default::default()}, vec![]); + t("bgcolor=é", Default::default(), vec![ParseWarning::ValueInvalid(("bgcolor".into(), "é".into())), ParseWarning::KeyNotSupported(("bgcolor".into(), "é".into()))]); t("bgcolor=red", Instructions { bgcolor_srgb: Some(Color32(0xffff0000)), ..Default::default() }, vec![]); t("bgcolor=f00", Instructions { bgcolor_srgb: Some(Color32(0xffff0000)), ..Default::default() }, vec![]); t("bgcolor=f00f", Instructions { bgcolor_srgb: Some(Color32(0xffff0000)), ..Default::default() }, vec![]);