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.
This commit is contained in:
bchanot committed 2026-11-01 12:40:14 +01:00
1 parent 8dd2d78d38
commit 03be75beed
6 files changed
+126 -29

No files matched your search

+6 -2
View File
@@ -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;
+70 -14
View File
@@ -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<Bounds> {
/// 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));