From 03be75beed31b4a641f1f5ee5f4fb1b5b8ee13ff Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 1 Nov 2026 12:40:14 +0100 Subject: [PATCH] fix(anatomy): V1-2.2 refuse arc radii browsers read as zero Security review of the interior rule: an arc radius below f32 precision was scaled by the core to a half disc the browser never fills. Nonzero radii below 2^-20 make the path unreadable; arcs add their f32 drift to the chord deviation; tracing stops past the vertex cap; a path without outline is refused. --- apps/app/e2e/fixtures/interior-probe.json | 11 ++- crates/core/src/anatomy/frame.rs | 8 +- crates/core/src/anatomy/path.rs | 84 ++++++++++++++++---- crates/core/tests/interior_probe.rs | 16 +++- crates/core/tests/sheet_marker_visibility.rs | 6 +- docs/development/RUST.md | 30 +++++-- 6 files changed, 126 insertions(+), 29 deletions(-) diff --git a/apps/app/e2e/fixtures/interior-probe.json b/apps/app/e2e/fixtures/interior-probe.json index dbf0c42e..47124597 100644 --- a/apps/app/e2e/fixtures/interior-probe.json +++ b/apps/app/e2e/fixtures/interior-probe.json @@ -129,7 +129,7 @@ "widest": { "x": 65.0, "y": 65.0, - "r": 1.0 + "r": 0.999 }, "svg": "Jarret / Jarret" }, @@ -158,6 +158,15 @@ "r": 1.905 }, "svg": "Jarret / Jarret" + }, + { + "name": "arc-radius-1e-49", + "accepted": false, + "codes": [ + "annotation.geometry.out_of_frame" + ], + "widest": null, + "svg": "Jarret / Jarret" } ], "equine": [ diff --git a/crates/core/src/anatomy/frame.rs b/crates/core/src/anatomy/frame.rs index bc37701d..a2c676b1 100644 --- a/crates/core/src/anatomy/frame.rs +++ b/crates/core/src/anatomy/frame.rs @@ -228,11 +228,15 @@ pub fn annotation_placement( } /// Whether the fill of a polygon or a path holds a disc of `MIN_VISIBLE_PX` at `scale` CSS px -/// per unit; a circle's disc is its own (path data the bounds read whole always has an outline). +/// per unit; a circle's disc is its own. A path without an outline is refused (the bounds and +/// the outline share one reader, so it does not happen). fn interior_placement(geometry: &Geometry, scale: f64, budget: &mut Budget) -> Placement { let tolerance = INTERIOR_TOLERANCE_PX / scale; let Some(outline) = geometry_outline(geometry, tolerance) else { - return Placement::InFrame; + return match geometry { + Geometry::Circle(_) => Placement::InFrame, + _ => Placement::InteriorUnmeasured, + }; }; if !budget.affords(&outline) { return Placement::InteriorUnmeasured; diff --git a/crates/core/src/anatomy/path.rs b/crates/core/src/anatomy/path.rs index a50dfb6c..bd3ba598 100644 --- a/crates/core/src/anatomy/path.rs +++ b/crates/core/src/anatomy/path.rs @@ -14,6 +14,11 @@ //! The same reading gives a path's fill outline (`path_outline`, owner brief 2026-11-01, zone //! without interior): each subpath as a polyline, curves and arcs cut into chords, with the //! largest distance between a chord and its curve (`Outline::deviation`). +//! +//! An arc radius other than zero is at least `MIN_ARC_RADIUS` (security review of the interior +//! rule, 2026-11-01): browsers read it in `f32`, where `1e-49` is a zero radius (a straight line) +//! and a radius below about `1e-19` squares to nothing (no arc drawn), while `f64` scales it up +//! to the half ellipse through the endpoints, an interior the browser never fills. /// The smallest box holding a set of points. #[derive(Debug, Clone, Copy, PartialEq)] @@ -67,6 +72,12 @@ pub fn path_bounds(d: &str) -> Option { /// The largest number of chords a curve or an arc is cut into: past it the deviation grows /// instead (the measure stays conservative). pub const MAX_CURVE_CHORDS: usize = 64; +/// The smallest arc radius read, zero aside: `2⁻²⁰`, the reciprocal of the largest coordinate, +/// so the radii scaled up to reach the endpoint (at most `2⁸²` squared) stay finite in `f32`. +pub const MIN_ARC_RADIUS: f64 = 1.0 / 1_048_576.0; +/// The relative `f32` error of a browser's arc arithmetic, added to an arc's chord deviation +/// times its scaled radius: a bound, not a measure (the centre may lie far outside the frame). +const ARC_F32_DRIFT: f64 = 1.0 / 8_388_608.0; /// A fill outline: its subpaths as polylines (each closed by the fill), the largest distance /// between a chord and the curve it stands for, and whether more than the asked number of @@ -322,12 +333,13 @@ impl Flattening { } /// The chords for a curve whose second derivative stays within `bend`: a chord over a - /// parameter step `h` departs from the curve by at most `bend × h² / 8`. - fn chords(&mut self, bend: f64) -> usize { + /// parameter step `h` departs from the curve by at most `bend × h² / 8`, plus `drift`. + fn chords(&mut self, bend: f64, drift: f64) -> usize { let wanted = (bend / (8.0 * self.tolerance)).sqrt().ceil(); let count = wanted.clamp(1.0, MAX_CURVE_CHORDS as f64) as usize; let step = 1.0 / count as f64; - self.outline.deviation = self.outline.deviation.max(bend * step * step / 8.0); + let deviation = bend * step * step / 8.0 + drift; + self.outline.deviation = self.outline.deviation.max(deviation); count } } @@ -349,15 +361,17 @@ impl Pen { /// The points of a curve between its ends, `count` chords over `t` in (0, 1). fn trace(&mut self, count: usize, at: impl Fn(f64) -> (f64, f64)) { - if let Some(flat) = &mut self.flat { + if let Some(flat) = self.flat.as_mut().filter(|flat| !flat.outline.overflow) { for i in 1..count { flat.push(at(i as f64 / count as f64)); } } } - fn chords(&mut self, bend: f64) -> usize { - self.flat.as_mut().map_or(1, |flat| flat.chords(bend)) + fn chords(&mut self, bend: f64, drift: f64) -> usize { + self.flat + .as_mut() + .map_or(1, |flat| flat.chords(bend, drift)) } /// A drawing command after a closepath starts a new subpath at the closed one's start. @@ -418,7 +432,7 @@ impl Pen { _ => { let mut arc = cursor.arc_parameters()?; arc.to = point(arc.to.0, arc.to.1); - self.arc(&arc); + self.arc(&arc)?; } } self.cubic_control = cubic; @@ -464,7 +478,7 @@ impl Pen { let bend = 6.0 * distance(second_difference(from, c1, c2)) .max(distance(second_difference(c1, c2, to))); - let count = self.chords(bend); + let count = self.chords(bend, 0.0); self.trace(count, |t| { ( cubic_at(from.0, c1.0, c2.0, to.0, t), @@ -490,7 +504,7 @@ impl Pen { quadratic_at(from.1, control.1, to.1, t), )); } - let count = self.chords(2.0 * distance(second_difference(from, control, to))); + let count = self.chords(2.0 * distance(second_difference(from, control, to)), 0.0); self.trace(count, |t| { ( quadratic_at(from.0, control.0, to.0, t), @@ -502,14 +516,19 @@ impl Pen { } /// SVG 1.1 F.6: an arc to the current point draws nothing, a zero radius draws a line; - /// otherwise the ellipse's extremes inside the sweep count. - fn arc(&mut self, arc: &Arc) { + /// otherwise the ellipse's extremes inside the sweep count. A radius below `MIN_ARC_RADIUS`, + /// zero aside, is not read. + fn arc(&mut self, arc: &Arc) -> Option<()> { + let tiny = |r: f64| r != 0.0 && r.abs() < MIN_ARC_RADIUS; + if tiny(arc.rx) || tiny(arc.ry) { + return None; + } if arc.to == self.at { - return; + return Some(()); } if arc.rx == 0.0 || arc.ry == 0.0 { self.line(arc.to); - return; + return Some(()); } let ellipse = Ellipse::through(self.at, arc); for angle in ellipse.extreme_angles() { @@ -519,9 +538,14 @@ impl Pen { } // Over the sweep as `t` goes from 0 to 1 the second derivative stays within the larger // radius times the sweep squared. - let count = self.chords(ellipse.rx.max(ellipse.ry) * ellipse.sweep * ellipse.sweep); + let radius = ellipse.rx.max(ellipse.ry); + let count = self.chords( + radius * ellipse.sweep * ellipse.sweep, + radius * ARC_F32_DRIFT, + ); self.trace(count, |t| ellipse.at(ellipse.start + ellipse.sweep * t)); self.line(arc.to); + Some(()) } } @@ -880,6 +904,38 @@ mod tests { assert_eq!(lone.subpaths.len(), 1, "a lone moveto fills nothing"); } + /// A radius below `2⁻²⁰` other than zero is not read: a browser reads it as zero or draws + /// nothing, where `f64` would scale it up to a half ellipse. + #[test] + fn a_tiny_arc_radius_is_not_read() { + for d in [ + "M40 50A1e-49 1e-49 0 0 1 90 50Z", + "M40 50A1e-40 1e-40 0 0 1 90 50Z", + "M40 50A1e-20 1 0 0 1 90 50Z", + "M40 50A1 9.5e-7 0 0 1 90 50Z", + ] { + assert_eq!(path_bounds(d), None, "{d}"); + assert_eq!(path_outline(d, 0.05, 4096), None, "{d}"); + } + let floor = format!("M40 50A{MIN_ARC_RADIUS} {MIN_ARC_RADIUS} 0 0 1 90 50Z"); + assert!(path_bounds(&floor).is_some()); + assert!(path_bounds("M40 50A0 1e-49 0 0 1 90 50Z").is_none()); + assert!( + path_bounds("M40 50A0 0 0 0 1 90 50Z").is_some(), + "zero: a line" + ); + } + + /// An arc's centre may lie far outside the frame: the browser's `f32` error on its radius is + /// added to the deviation (`2²⁰ × 2⁻²³` = 0.125 here), never left out. + #[test] + fn a_large_arc_radius_adds_its_f32_drift() { + let outline = path_outline("M0 0A1048576 1048576 0 0 1 1 0Z", 0.05, 4096).unwrap(); + assert!(outline.deviation >= 0.125, "{}", outline.deviation); + let small = path_outline("M0 0A1 1 0 0 1 1 0Z", 0.05, 4096).unwrap(); + assert!(small.deviation <= 0.05 + 1e-6, "{}", small.deviation); + } + #[test] fn the_outline_stops_at_its_vertex_cap() { let d = format!("M0 0{}", "l1 1".repeat(9)); diff --git a/crates/core/tests/interior_probe.rs b/crates/core/tests/interior_probe.rs index 0af1f045..7dcdb96f 100644 --- a/crates/core/tests/interior_probe.rs +++ b/crates/core/tests/interior_probe.rs @@ -20,11 +20,15 @@ use serde_json::{Value, json}; const FIXTURE: &str = "apps/app/e2e/fixtures/interior-probe.json"; const THIN: &str = "annotation.geometry.interior_too_small"; +const UNREADABLE: &str = "annotation.geometry.out_of_frame"; const PHONE: &str = "0 0 286 310"; -/// The widest disc of a shape, measured to a thousandth of a unit: centre and radius. +/// The widest disc of a shape, measured to a thousandth of a unit: centre and radius; none for +/// path data the core does not read. fn widest(geometry: &Geometry) -> Value { - let outline = geometry_outline(geometry, 0.001).unwrap(); + let Some(outline) = geometry_outline(geometry, 0.001) else { + return Value::Null; + }; let search = Search { floor: 0.0, enough: f64::INFINITY, @@ -185,6 +189,12 @@ fn cases() -> Vec { polygon(&[[40.0, 45.716], [46.6, 45.716], [43.3, 40.0]]), plain, ), + // A radius browsers read as zero (`f32`), where `f64` would scale it to a half disc. + case( + "arc-radius-1e-49", + path("M40 50A1e-49 1e-49 0 0 1 90 50Z"), + plain, + ), ] } @@ -211,7 +221,7 @@ fn equine() -> Vec { fn the_interior_probe_is_current() { let cases = cases(); for c in &cases { - let refused = c["codes"] == json!([THIN]); + let refused = c["codes"] == json!([THIN]) || c["codes"] == json!([UNREADABLE]); assert!(c["accepted"] == json!(true) || refused, "{}", c["name"]); } let probe = json!({ diff --git a/crates/core/tests/sheet_marker_visibility.rs b/crates/core/tests/sheet_marker_visibility.rs index 5817fb5b..cf484e41 100644 --- a/crates/core/tests/sheet_marker_visibility.rs +++ b/crates/core/tests/sheet_marker_visibility.rs @@ -204,7 +204,7 @@ fn the_equine_threshold_falls_on_the_caudal_zone_then_the_junctions() { found }; let pair = |id: &str, code: &str| (id.to_owned(), code.to_owned()); - assert!(small(1.23).is_empty()); + assert!(small(1.235).is_empty(), "4.009 px"); assert_eq!(small(1.24), [pair("caudal", THIN)]); let (_, failures) = judged(&text, &[(HORSE_ID, &scaled_horse(1.24))]); assert_eq!( @@ -401,6 +401,10 @@ fn path_numbers_past_the_frame_magnitude_are_refused() { "M1 1A1e39 1e39 0 0 1 3 1L3 3L1 3Z", "M1 1A1 1 1e39 0 1 3 1L3 3L1 3Z", "M1 1L3 1L3 3L-1e39 3Z", + // An arc radius below 2^-20 other than zero (security review of the interior rule): + // a browser reads it as zero or draws nothing, `f64` would scale it to a half disc. + "M1 1A1e-49 1e-49 0 0 1 3 1Z", + "M1 1A1e-20 1e-20 0 0 1 3 1Z", ] { assert_eq!(main(d), [OUT_OF_FRAME], "{d}"); } diff --git a/docs/development/RUST.md b/docs/development/RUST.md index 5d599b2e..9f46b418 100644 --- a/docs/development/RUST.md +++ b/docs/development/RUST.md @@ -4341,7 +4341,8 @@ rule is a scale floor against the bypass, never a review of shapes (the zones ke security review measured its consequence (MEDIUM, open, owner decision): a zone path whose interior has zero winding (an outline traced twice in opposite directions) or a near-collinear polygon passes the 4 px rule, paints no interior and is hit only on its 2-unit stroke (0.28 CSS px on the equine phone scale), in the three engines. Closing it needs a -filled-area measure (nonzero rule) or a « zone without interior » issue. +filled-area measure (nonzero rule) or a « zone without interior » issue. Closed by the interior rule (owner brief of +2026-11-01, §12.14). ### 12.14 The last MEDIUM: zone without interior (owner, 2026-11-01) @@ -4381,12 +4382,12 @@ validated equine; below, a tap target smaller than the points'. | `path` unit tests | 300 random cubics, quadratics and arcs: every curve point within the recorded deviation of its chords, the deviation within 0.05 unless capped (arcs whose radii the endpoints scale up); closepath restarts at the subpath start; vertex cap | Browsers (`apps/app/e2e/ui/interior-clicks.spec.ts`, the six click projects: Chromium 1280, 375 and 320 touch, Firefox -1280, WebKit 1280 and 375 touch; 90 passed, and `canvas-clicks.spec.ts` 99 unchanged). The probe -(`e2e/fixtures/interior-probe.json`, written and drift-checked by `tests/interior_probe.rs`) gives 14 shapes in the +1280, WebKit 1280 and 375 touch; 96 passed, and `canvas-clicks.spec.ts` 99 unchanged). The probe +(`e2e/fixtures/interior-probe.json`, written and drift-checked by `tests/interior_probe.rs`) gives 15 shapes in the preview's 286 × 310 box (one unit per CSS px) with the core's verdict and widest disc. For each, the browser's own fill (`pointer-events="fill"`, stroke out) is sampled every 0.25 px and its widest disc taken by an exact distance transform: -the core never claims more than the browser fills (at most 0.5 px under it); the 8 refused fill no 4 px disc in any -engine; the 6 accepted take a real click or tap at the core's centre and the hit test at 32 points of 90 % of its radius, +the core never claims more than the browser fills (at most 0.5 px under it); the 9 refused fill no 4 px disc in any +engine (the ninth, an arc radius of `1e-49`, fills nothing at all); the 6 accepted take a real click or tap at the core's centre and the hit test at 32 points of 90 % of its radius, fill only and as shipped. A drawing root `fill-rule="evenodd"` leaves the pentagram filled at its centre (isolation). Then the equine shapes in the canvas: the core's disc hit test-clear (samples under a point's disc aside, at most a quarter), and a real press where the fill lies farthest from every disc records the zone. @@ -4403,9 +4404,22 @@ interior of a zone, takes its taps; no rule refuses it. The human review identif is published; no proof here claims that every marker is individually selectable when two overlap entirely. The arc rotation reduced in `f32` by browsers (sub-pixel, §12.13) stays bounded by the `2²⁰` number bound, its tests kept. -Mutations (`.claude/audits/2026-11-01-v1-2-2-mutations-interior.py`): 24 killed, Z22 (the floor prune removed) survives -as equivalent, a cell that cannot reach the floor never holding an accepted disc; S1, S8 and F16 of the previous scripts, -whose anchors moved, re-anchored and killed. Module 579 180 B gzip -9 (+4 946 B, budget 600 000, margin 20 820 B). +Fresh reviews on `8dd2d78`. Security PASS with one MEDIUM, fixed: an arc radius below `f32` precision (`1e-49`) was read by +the core as the half disc its endpoints scale it to, while browsers read it as zero (a straight line) or, below about +`1e-19`, square it to nothing; the core accepted a shape the browser never fills. An arc radius other than zero is now at +least `path::MIN_ARC_RADIUS` `2⁻²⁰` (the scaled radii stay finite in `f32`), else the path is unreadable +(`out_of_frame`): `a_tiny_arc_radius_is_not_read`, `path_numbers_past_the_frame_magnitude_are_refused`, probe +`arc-radius-1e-49`. Its LOWs, fixed: an arc adds its scaled radius × `2⁻²³` to the deviation (the browser's `f32` arc +arithmetic, a bound, not a measure: `a_large_arc_radius_adds_its_f32_drift`); the flattening stops tracing once over the +vertex cap; a path without an outline is refused, not accepted (unreachable, the bounds and the outline sharing one +reader). Informational: verdict and preview may differ by a libm ulp between native and wasm on a shape tuned to the +threshold; the verdict has no production caller yet (V1-2.3). Verifier ECARTS(3), all documentation: this section's +pointer from §12.13, the × 1.235 assertion, the audit tools committed. + +Mutations (`.claude/audits/2026-11-01-v1-2-2-mutations-interior.py`): 26 killed, 2 equivalent: Z22 (the floor prune +removed; a cell that cannot reach the floor never holds an accepted disc) and Z25 (a path without an outline accepted; +unreachable). S1, S8 and F16 of the previous scripts, whose anchors moved, re-anchored and killed. Module 579 357 B gzip -9 +(native build; +5 124 B, budget 600 000, margin 20 643 B). Ignored cargo tests: the three are one test, `pdf_serialize::l_report_with_real_rasters_costs_what_it_costs`, counted once per cargo run whose features build `pdf_serialize` (workspace, `cli`, `ts,pdf-read,pdf-write`). A timing print for