//! V1-2.2 owner brief 2026-10-30: the drawing's root never decides whether the engine's markers //! show. Both composers appended the marker layer as the root's last child, so the layer //! inherited the root's `visibility` (the composition root is the engine's since the three-MEDIUM //! brief, `svg_marker_isolation`); a drawing `` whose children set `visible` back read //! clean before and showed its drawing without a single marker (security review, MEDIUM). The //! reader refuses a root `visibility` other than `visible` (`root_visibility`); the cleaning, //! the preview, the verdict and the PDF snapshot gate answer it; both composers refuse such a //! root. The other root properties are either removed by the reader already or affect the //! drawing and the markers alike (see `other_root_properties_*`). #![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] mod common; use std::collections::HashSet; use common::sheet_readers::{ OPEN, assert_refused_everywhere, canvas_outcome, preview, sheet, verdict_detail, }; use faunosteo_core::anatomy::compose::{ComposeError, compose_canvas, compose_highlight}; use faunosteo_core::anatomy::svg::clean::{CleaningOutcome, clean_svg}; use faunosteo_core::anatomy::svg::tree::parse_svg; use faunosteo_core::anatomy::svg::{Element, read_svg}; use faunosteo_core::domain::sheet::verdict::DrawingResult; use serde_json::json; const BODY: &str = ""; /// A drawing whose root opening tag carries `attrs`. fn rooted(attrs: &str, body: &str) -> String { let root = OPEN.replace("viewBox", &format!("{attrs} viewBox")); format!("{root}{body}") } /// The composed view's marker layer is its root's last child and the root lets it show. fn layer_shows(composed: &Element) -> bool { let layer = composed.element_children().last().unwrap(); let root_visible = composed .attr("visibility") .is_none_or(|v| v.trim().eq_ignore_ascii_case("visible")); layer.element_children().count() > 0 && root_visible } fn compose_both(tree: &Element) -> (Result, Result) { let sheet = sheet(); let ids: HashSet<&str> = ["hock"].into_iter().collect(); ( compose_canvas(&sheet, "main", tree), compose_highlight(&sheet, "main", tree.clone(), &ids, true), ) } #[test] fn a_drawing_without_root_visibility_shows_its_markers() { for root in [ "", "visibility=\"visible\"", "visibility=\"Visible\"", "visibility=\" visible\t\"", ] { let text = rooted(root, BODY); let read = read_svg(&text).unwrap_or_else(|e| panic!("{root}: {e:?}")); assert!(read.removed.is_empty(), "{root}"); let cleaning = clean_svg(text.as_bytes()); assert_eq!(cleaning.outcome, CleaningOutcome::Clean, "{root}"); // The verdict and the preview read the stored form, as uploaded. let stored = cleaning.stored.unwrap(); assert_eq!( verdict_detail(&stored), (false, DrawingResult::Ok, None), "{root}" ); assert_eq!( canvas_outcome(&preview(&stored), "detail")["ok"], json!(true), "{root}" ); let (canvas, highlight) = compose_both(&read.root); assert!(layer_shows(&canvas.unwrap()), "{root}"); assert!(layer_shows(&highlight.unwrap()), "{root}"); } } /// The reported drawing and every spelling of a non-visible root: refused by every reader. #[test] fn a_root_visibility_other_than_visible_is_refused_everywhere() { let values = [ "hidden", "collapse", "HIDDEN", "Hidden", " hidden ", "hidden\n", "hidden", "hidden", "inherit", "initial", "unset", "revert", "hidden !important", "visible hidden", "visibl", "", ]; let bodies = [ BODY, "", "", "", ]; for value in values { for body in bodies { assert_refused_everywhere( &rooted(&format!("visibility=\"{value}\""), body), "root_visibility", ); } } } /// The composers stay fail-closed on a root that did not come out of the reader. #[test] fn the_composers_never_give_the_layer_a_hidden_root() { for value in ["hidden", "collapse", "HIDDEN", "inherit"] { let tree = parse_svg(&rooted(&format!("visibility=\"{value}\""), BODY)).unwrap(); let (canvas, highlight) = compose_both(&tree); assert_eq!(canvas.unwrap_err(), ComposeError::RootVisibility, "{value}"); assert_eq!( highlight.unwrap_err(), ComposeError::RootVisibility, "{value}" ); } } /// The same property written another way never reaches the root: a root `style` or `class` /// and a `{BODY}"), ), "style", ), ( rooted( "", &format!("{BODY}"), ), "style", ), ]; for (text, removal) in &cleanable { let read = read_svg(text).unwrap(); assert!( read.removed.iter().any(|r| r == removal), "{text}: {:?}", read.removed ); let cleaning = clean_svg(text.as_bytes()); assert_eq!(cleaning.outcome, CleaningOutcome::Cleanable, "{text}"); let stored = cleaning.stored.unwrap(); let again = clean_svg(stored.as_bytes()); assert_eq!( (again.outcome, again.stored.as_deref()), (CleaningOutcome::Clean, Some(stored.as_str())), "the stored form is a fixpoint: {text}" ); let (canvas, highlight) = compose_both(&read_svg(&stored).unwrap().root); assert!( layer_shows(&canvas.unwrap()) && layer_shows(&highlight.unwrap()), "{text}" ); } let descendant = rooted( "", "", ); let read = read_svg(&descendant).unwrap(); assert!(read.removed.is_empty()); let (canvas, highlight) = compose_both(&read.root); assert!(layer_shows(&canvas.unwrap()) && layer_shows(&highlight.unwrap())); } /// The other root properties. Removed by the reader before V1-2.2: the paint servers and /// effects (`filter`, `mask`, `clip-path`), `transform`, `class`, `style`. Kept: `opacity`, /// `display` and the paint, which since the three-MEDIUM brief stay on the drawing's group and /// never reach a marker (`svg_marker_isolation`); every PDF marker still sets its fill. #[test] fn other_root_properties_are_removed_or_act_on_the_whole_view() { for name in ["filter", "mask", "clip-path"] { let text = rooted( &format!("{name}=\"url(#m)\""), &format!("{BODY}"), ); let read = read_svg(&text).unwrap(); assert!(read.removed.contains(&format!("svg@{name}")), "{name}"); assert!(read.root.attr(name).is_none(), "{name}"); } let read = read_svg(&rooted("transform=\"scale(0)\"", BODY)).unwrap(); assert_eq!(read.removed, ["svg@transform"]); for root in [ "opacity=\"0\"", "display=\"none\"", "fill=\"none\"", "fill-opacity=\"0\"", ] { let read = read_svg(&rooted(root, BODY)).unwrap_or_else(|e| panic!("{root}: {e:?}")); assert!(read.removed.is_empty(), "{root}"); let ids: HashSet<&str> = ["hock"].into_iter().collect(); let composed = compose_highlight(&sheet(), "main", read.root, &ids, true).unwrap(); for marker in composed .element_children() .last() .unwrap() .element_children() { assert!(marker.attr("fill").is_some() && marker.attr("fill-opacity").is_some()); } } } #[test] fn the_equine_drawing_has_no_root_visibility() { let horse = common::repo_file("supabase/seed/anatomical-sheets/equine/draft/assets/horse.svg"); let read = read_svg(&horse).unwrap(); assert!(read.root.attr("visibility").is_none() && read.removed.is_empty()); let (canvas, highlight) = compose_both(&read.root); assert!(canvas.is_ok() && highlight.is_ok()); }