fix(anatomy): V1-2.2 reserve the PDF halo id in the reader and composers

A drawing defining filter#osteo-halo replaced the PDF halo (a transparent
filter erased the marker); an element holding the id before the first
defs won its resolution. The reader refuses the id in any letter case
(reserved_id), both composers refuse such a tree, the halo is always the
engine's. Exact name only: osteo-overlays stays.
This commit is contained in:
bchanot committed 2026-10-30 17:30:48 +01:00
1 parent 2f1f0764bf
commit 08efd9a792
13 files changed
+781 -34

No files matched your search

+21 -16
View File
@@ -10,11 +10,15 @@
//! 2026-10-29), so a view never resolves into another view's drawing or the page, and the page
//! never into a drawing. The reader already refused references to ids a drawing does not
//! define. Stored bytes are never rewritten; the PDF view is rasterised as a document of its
//! own and keeps the drawing's ids.
//! own and keeps the drawing's ids, so it never holds one the composition writes
//! (`svg::refs::RESERVED_IDS`, the halo filter): the reader refuses such a drawing and both
//! composers refuse such a tree.
use std::collections::HashSet;
use super::layer::{LayerError, find_view, render_layer, view_box_of};
pub use super::svg::refs::HALO_FILTER_ID;
use super::svg::refs::holds_reserved_id;
use super::svg::sanitize::{local_reference_text, scope_url_targets, url_targets, urls_are_local};
use super::svg::serialize::is_xml_text;
use super::svg::{Element, Node};
@@ -22,7 +26,6 @@ use crate::domain::sheet::AnatomicalSheetDocument;
pub const HIGHLIGHT_FILL: &str = "#b85a3f";
pub const HIGHLIGHT_FILL_OPACITY: &str = "0.85";
pub const HALO_FILTER_ID: &str = "osteo-halo";
const CANVAS_FALLBACK_VIEW_BOX: &str = "0 0 100 100";
/// Why a view cannot be composed.
@@ -36,6 +39,11 @@ pub enum ComposeError {
/// not read as a local reference (`sanitize::value_acceptable`), so only an element tree
/// that did not come out of `read_svg` reaches it.
UnscopedReference,
/// The drawing holds an id the composition writes (`svg::refs::RESERVED_IDS`): in the PDF
/// view its element would replace the halo filter or win its resolution; neither form
/// composes it. Fail-closed: the reader refuses it (`reserved_id`), so only a tree that did
/// not come out of `read_svg` reaches it, a composed view given back included.
ReservedId,
}
impl From<LayerError> for ComposeError {
@@ -71,6 +79,9 @@ pub fn compose_canvas(
asset: &Element,
) -> Result<Element, ComposeError> {
let view = find_view(document, view_id).ok_or(ComposeError::UnknownView)?;
if holds_reserved_id(asset) {
return Err(ComposeError::ReservedId);
}
let mut root = Element {
qname: "svg".to_owned(),
namespace: asset.namespace.clone(),
@@ -147,7 +158,8 @@ fn scope_ids(element: &mut Element, scope: &str) {
}
/// The PDF view of `view_id`: `asset` (sanitised, consumed) framed on the view, the markers
/// of `annotation_ids` only, recoloured, with the halo filter when `halo`.
/// of `annotation_ids` only, recoloured, with the halo filter when `halo`. The only element
/// of the result holding a reserved id is the composition's own.
pub fn compose_highlight(
document: &AnatomicalSheetDocument,
view_id: &str,
@@ -156,13 +168,16 @@ pub fn compose_highlight(
halo: bool,
) -> Result<Element, ComposeError> {
let view = find_view(document, view_id).ok_or(ComposeError::UnknownView)?;
if holds_reserved_id(&asset) {
return Err(ComposeError::ReservedId);
}
if let Some(view_box) = view_box_of(view) {
asset.set_attr("viewBox", view_box);
}
asset.remove_attr("width");
asset.remove_attr("height");
if halo {
ensure_halo_filter(&mut asset);
add_halo_filter(&mut asset);
}
let layer = render_layer(document, view_id, &HashSet::new())?;
let layer = highlight_layer(layer, annotation_ids, halo);
@@ -198,24 +213,14 @@ fn highlight_element(mut element: Element, halo: bool) -> Element {
element
}
/// One halo filter per root: in the first `<defs>` of the drawing, else in a new `<defs>`
/// placed first; nothing when a `filter#osteo-halo` already exists.
fn ensure_halo_filter(root: &mut Element) {
if has_halo_filter(root) {
return;
}
/// The halo filter in the first `<defs>` of the drawing, else in a new `<defs>` placed first.
fn add_halo_filter(root: &mut Element) {
if !append_to_first_defs(root) {
let defs = Element::svg("defs", Vec::new(), vec![Node::Element(halo_filter())]);
root.children.insert(0, Node::Element(defs));
}
}
fn has_halo_filter(element: &Element) -> bool {
element.element_children().any(|c| {
(c.qname == "filter" && c.attr("id") == Some(HALO_FILTER_ID)) || has_halo_filter(c)
})
}
/// Appends the halo to the first `<defs>` below `element` (document order).
fn append_to_first_defs(element: &mut Element) -> bool {
for child in &mut element.children {
+5 -3
View File
@@ -5,7 +5,8 @@
//! (`malformed`), or any removal of active or external content, or a processing
//! instruction (`svg_active_content`, the names in `active`, a PI as `?<target>`), or, once
//! no active content is found, a reference to an id the drawing does not define, in the
//! file or after a removal (`dangling_reference`); no cleaning is offered;
//! file or after a removal (`dangling_reference`), then an id the composition writes
//! (`reserved_id`, `refs::RESERVED_IDS`); no cleaning is offered;
//! - cleanable: only other removals (editor metadata and namespaces, `data-*`, `<style>`,
//! `class` or `style` on the root, ids outside the grammar, unsupported elements, long
//! titles or descriptions): the admin confirms them;
@@ -23,7 +24,7 @@ use serde::Serialize;
use ts_rs::TS;
use super::prescan::SVG_CAPS;
use super::refs::check_targets;
use super::refs::{check_reserved_ids, check_targets};
use super::serialize::{serialize, stored_form};
use super::{processing_instructions, read_svg, read_svg_for_cleaning};
use crate::domain::sheet::verdict::sha256_hex;
@@ -99,7 +100,8 @@ pub fn clean_svg(bytes: &[u8]) -> SvgCleaning {
if !active.is_empty() {
return rejected(ACTIVE_CONTENT, read.removed, active);
}
if let Err(rejection) = check_targets(&read.root) {
if let Err(rejection) = check_targets(&read.root).and_then(|()| check_reserved_ids(&read.root))
{
return rejected(rejection.reason(), read.removed, read.active);
}
let preview = serialize(&read.root);
+5 -3
View File
@@ -13,18 +13,20 @@ pub use sanitize::Sanitized;
pub use tree::{Element, Node, SvgRejection};
/// Bounds, parses, sanitises and checks the references of an SVG text: no cycle, within the
/// expansion budget, each naming an id the drawing defines.
/// expansion budget, each naming an id the drawing defines; and no id the composition writes.
pub fn read_svg(text: &str) -> Result<Sanitized, SvgRejection> {
let root = tree::parse_svg(text)?;
let sanitized = sanitize::sanitize(root)?;
refs::check_references(&sanitized.root)?;
refs::check_targets(&sanitized.root)?;
refs::check_reserved_ids(&sanitized.root)?;
Ok(sanitized)
}
/// `read_svg` as the cleaning reads a file picked for upload: an oversized `title` or `desc`
/// is a removal, not a refusal (`sanitize::sanitize_for_cleaning`); the targets are checked by
/// the cleaning after it has classified active content (`clean::clean_svg`).
/// is a removal, not a refusal (`sanitize::sanitize_for_cleaning`); the targets and the
/// reserved ids are checked by the cleaning after it has classified active content
/// (`clean::clean_svg`).
pub fn read_svg_for_cleaning(text: &str) -> Result<Sanitized, SvgRejection> {
let root = tree::parse_svg(text)?;
let sanitized = sanitize::sanitize_for_cleaning(root)?;
+34
View File
@@ -8,6 +8,7 @@
//! chains are not bounded by the depth cap) and saturating, with an early exit.
//! `check_targets` refuses a reference to an id the drawing does not define (owner decision
//! 2026-10-29): once inlined in a page, `#id` would resolve against another view or the page.
//! `check_reserved_ids` refuses an id the composition writes (owner brief 2026-10-30).
use std::collections::{HashMap, HashSet};
@@ -17,6 +18,39 @@ use super::tree::{Element, Node, SvgRejection};
/// Expanded elements a drawing may instantiate (the product's drawing: a few thousand).
pub const MAX_EXPANDED_ELEMENTS: u64 = 50_000;
/// The PDF highlight's filter (`compose::compose_highlight`), written into the drawing's own
/// document: the PDF view keeps the drawing's ids.
pub const HALO_FILTER_ID: &str = "osteo-halo";
/// Every id the composition writes next to the drawing's ids. A drawing defining one would
/// replace the engine's element (a `filter#osteo-halo` of its own) or win its resolution (any
/// element holding it before the engine's). Exact names, never a prefix: the historical
/// drawings carry `osteo-overlays`.
pub const RESERVED_IDS: [&str; 1] = [HALO_FILTER_ID];
/// `id` is a reserved id in any letter case. The reader keeps ASCII ids only
/// (`sanitize::is_reference_name`), so no other spelling of one reaches the tree.
pub fn is_reserved_id(id: &str) -> bool {
RESERVED_IDS
.iter()
.any(|reserved| id.eq_ignore_ascii_case(reserved))
}
/// No element of `root`, the root included, holds a reserved id.
pub fn check_reserved_ids(root: &Element) -> Result<(), SvgRejection> {
if holds_reserved_id(root) {
Err(SvgRejection::ReservedId)
} else {
Ok(())
}
}
/// `element` or one of its descendants holds a reserved id (the composers' fail-closed check).
pub fn holds_reserved_id(element: &Element) -> bool {
element.attr("id").is_some_and(is_reserved_id)
|| element.element_children().any(holds_reserved_id)
}
struct Flat {
edges: Vec<usize>,
}
+4
View File
@@ -108,6 +108,9 @@ pub enum SvgRejection {
/// inlined in a page it would resolve against another view or the page (owner decision
/// 2026-10-29).
DanglingReference,
/// An id the composition writes into the drawing's own document (`refs::RESERVED_IDS`):
/// the drawing's element would stand in for the engine's (V1-2.2, owner brief 2026-10-30).
ReservedId,
}
impl SvgRejection {
@@ -123,6 +126,7 @@ impl SvgRejection {
Self::TooComplex => "too_complex",
Self::ReferenceCycle => "reference_cycle",
Self::DanglingReference => "dangling_reference",
Self::ReservedId => "reserved_id",
}
}
}
+1
View File
@@ -478,6 +478,7 @@ fn canvas_of<'a>(
ComposeError::UnknownView => Failure::new("unknown_view", view_id),
ComposeError::InvalidCharacter => Failure::new("invalid_character", view_id),
ComposeError::UnscopedReference => Failure::new("svg_rejected", "unscoped_reference"),
ComposeError::ReservedId => Failure::new("svg_rejected", "reserved_id"),
})?;
let svg = serialize(&canvas);
if svg.len() > MAX_COMPOSED_BYTES {