diff --git a/crates/engine/src/cmds_path.rs b/crates/engine/src/cmds_path.rs index 79a481f2..24a01c68 100644 --- a/crates/engine/src/cmds_path.rs +++ b/crates/engine/src/cmds_path.rs @@ -305,7 +305,7 @@ fn extend(s: &mut Session, p: &Value) -> Result { if d.len() > 4_000_000 { return Err(EngineError::Param("param 'd' is too long".into())); } - let piece = anicraft_geom::svg::parse_path_data(d).map_err(|e| EngineError::Param(format!("param 'd': {e}")))?; + let piece = anicraft_svg::parse_path_data(d).map_err(|e| EngineError::Param(format!("param 'd': {e}")))?; let at_start = match opt_str(p, "end")?.unwrap_or("end") { "end" => false, "start" => true, diff --git a/crates/svg/src/path_data.rs b/crates/svg/src/path_data.rs index 4b577206..cb083fa0 100644 --- a/crates/svg/src/path_data.rs +++ b/crates/svg/src/path_data.rs @@ -16,6 +16,13 @@ fn v(p: Point) -> Vec2 { /// Parses SVG path data into a [`Path`]: icon outlines, `path.d` commands and SVG import. pub fn parse_path_data(d: &str) -> Result { + // The shared geometry parser permits an implicit origin; SVG and command input must + // begin with moveto rather than manufacturing an anchor for malformed drawing commands. + if let Some(first) = d.bytes().find(|byte| !matches!(byte, b' ' | b'\t' | b'\r' | b'\n')) + && !matches!(first, b'M' | b'm') + { + return Err("path data must start with moveto".into()); + } let subpaths = parse_path(d).map_err(|e| e.to_string())?; let mut path = Path::new(); for sub in subpaths { @@ -68,6 +75,16 @@ mod tests { p.anchors().into_iter().map(|(_, a)| a).collect() } + #[test] + fn nonempty_paths_require_an_initial_moveto() { + for data in ["L10 10", "H10", "V10", "C0 0 1 1 2 2", "Q1 1 2 2", "Z", " l10 10"] { + assert!(parse_path_data(data).is_err(), "accepted drawing command without moveto: {data}"); + } + for data in ["", " \t\r\n", "m1 2 l3 4", " M0 0 L1 1"] { + assert!(parse_path_data(data).is_ok(), "rejected valid path: {data:?}"); + } + } + #[test] fn malformed_tokens_do_not_import_a_truncated_path() { for data in ["@", "M0 0 L1 1 @", "M0 0 L1 1 1e", "M0 0 L1 1 1e9999"] { diff --git a/crates/svg/src/tests.rs b/crates/svg/src/tests.rs index aa17d7f5..c21a7fb4 100644 --- a/crates/svg/src/tests.rs +++ b/crates/svg/src/tests.rs @@ -258,13 +258,13 @@ fn hostile_markup_is_refused_before_parsing() { prev = name; } bomb.push_str("]>&h;"); - assert!(matches!(import_with_fonts(bomb.as_bytes(), &fonts), Err(SvgError::Preflight(_)))); + assert!(matches!(import_with_fonts(bomb.as_bytes(), &fonts), Err(SvgError::Preflight(craft_svg::preflight::Error::Entities)))); // Groups nested far past any drawing. let deep = format!("{}{}", "".repeat(5000), "".repeat(5000)); - assert!(matches!(import_with_fonts(deep.as_bytes(), &fonts), Err(SvgError::Preflight(_)))); + assert!(matches!(import_with_fonts(deep.as_bytes(), &fonts), Err(SvgError::Preflight(craft_svg::preflight::Error::TooDeep(_))))); // An unpacked SVGZ past the size limit. let huge = craft_svg::svgz::compress(&vec![b' '; (64 << 20) + 1]); - assert!(matches!(import_with_fonts(&huge, &fonts), Err(SvgError::Preflight(_)))); + assert!(matches!(import_with_fonts(&huge, &fonts), Err(SvgError::Preflight(craft_svg::preflight::Error::Text(craft_svg::svgz::Error::TooLarge(_)))))); } #[test]