mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 08:15:29 +00:00
Add opt-in checked SVG image validation
This commit is contained in:
1 parent
83cb1133dd
commit
f32b47a1d4
12 files changed
+1861
-4
No files matched your search
Generated
+4
@@ -3092,14 +3092,18 @@ dependencies = [
|
||||
"craft-xml",
|
||||
"data-url",
|
||||
"flate2",
|
||||
"gif",
|
||||
"image-webp",
|
||||
"kurbo 0.13.1",
|
||||
"quick-xml 0.41.0",
|
||||
"resvg 0.48.1",
|
||||
"roxmltree 0.21.2",
|
||||
"thiserror 2.0.21",
|
||||
"toml",
|
||||
"usvg 0.48.1",
|
||||
"vectorcraft-geom",
|
||||
"vectorcraft-pathops",
|
||||
"zune-jpeg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
+12
-1
@@ -31,6 +31,13 @@ usvg = { version = "=0.48.1", default-features = false, optional = true }
|
||||
resvg = { version = "=0.48.1", default-features = false, features = ["text", "raster-images"], optional = true }
|
||||
|
||||
data-url = { version = "0.3.2", optional = true }
|
||||
# Renderer-specific validation uses the same resolved backends as resvg0.48.1.
|
||||
# zune-jpeg is used under its MIT licence option. The locked edge-alignment
|
||||
# regression must pass when changing this renderer/backend dependency group.
|
||||
zune-jpeg = { version = "=0.5.15", optional = true }
|
||||
gif = { version = "=0.14.2", optional = true }
|
||||
image-webp = { version = "=0.2.4", optional = true }
|
||||
|
||||
|
||||
[features]
|
||||
default = ["paths"]
|
||||
@@ -45,5 +52,9 @@ tree-text = ["tree", "usvg/text"]
|
||||
# The system's fonts in `usvg::fontdb`.
|
||||
tree-system-fonts = ["tree-text", "usvg/system-fonts"]
|
||||
# Bounded rendering of a parsed tree through the matching resvg/tiny-skia versions.
|
||||
tree-render = ["tree", "dep:resvg"]
|
||||
tree-render = ["tree", "dep:resvg", "dep:zune-jpeg", "dep:gif", "dep:image-webp"]
|
||||
tree-render-system-fonts = ["tree-render", "resvg/system-fonts"]
|
||||
|
||||
[dev-dependencies]
|
||||
# Already resolved in the workspace; test-only Cargo.lock edge inspection.
|
||||
toml = "=1.1.8"
|
||||
@@ -16,3 +16,58 @@ The shared adapter adds an aggregate 64 MiB stylesheet-text limit and idempotent
|
||||
|
||||
### Transform compatibility
|
||||
`transform_compat::parse_transform` preserves EffectCraft transform-list recovery and exact composition. It retains six numeric arguments per operation; scanning is not time/byte capped and matrix overflow behavior is unchanged. The strict geometry reader is unchanged. Source: Brandon Thomas, EffectCraft94e1c86d89a866771f80ad1610afda9aa12e85d8; bounded safe argument matching by Altynzhan Karybai,4035d181329e847d7a57bf3d059502ea2c91b560.
|
||||
|
||||
|
||||
### Opt-in validated embedded images
|
||||
|
||||
With `tree-render`, `tree::ValidatedImageResolver` owns one drawing's immutable
|
||||
`Limits`, original child-source work, data-URL normalization and renderer-specific
|
||||
raster validation. Existing `ImageResolver::new`/`resolver` and legacy parse entry
|
||||
points retain their behavior. Opt in explicitly:
|
||||
|
||||
```rust
|
||||
# #[cfg(feature = "tree-render")]
|
||||
# fn example() -> Result<(), craft_svg::tree::Error> {
|
||||
use craft_svg::tree::{self, Limits, ValidatedImageResolver, usvg};
|
||||
let images = ValidatedImageResolver::new(Limits::default());
|
||||
let checked = tree::preflight(
|
||||
"<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'/>",
|
||||
&images.limits(),
|
||||
)?;
|
||||
let drawing = images.parse_preflight(checked, usvg::Options::default())?;
|
||||
let usage = images.usage();
|
||||
// Importers disclose usage's invalid_* and *_limit_images; strict consumers refuse them.
|
||||
# let _ = (drawing, usage);
|
||||
# Ok(())
|
||||
# }
|
||||
```
|
||||
|
||||
The caller supplies fonts and a sufficient stack/worker for its depth policy.
|
||||
`parse_preflight` accepts only an opaque preflight made with exactly this context's
|
||||
limits. It consumes options and installs its own data-only resolver, including for
|
||||
all children; unrelated callbacks cannot fetch a linked file or bypass admission.
|
||||
The full expanded root height and each child's expanded height share one depth
|
||||
allowance. An overlapping root conversion on the same context is refused.
|
||||
|
||||
Unchanged XML converts from its original checked DOM. A modified data URL requires
|
||||
one checked replacement DOM. Invalid URLs become bounded resolver-visible image
|
||||
failures; hidden, unused and foreign elements do not acquire omission counts.
|
||||
Original UTF-8 child bytes are charged before XML conversion or shorter repairs,
|
||||
with per-document and aggregate bounds. Failed inflation/text work is not refunded;
|
||||
separate bounded retry-classification work never admits another child. Encoded
|
||||
raster callbacks spend at most `limits.bytes + limits.embedded_bytes` cumulatively,
|
||||
without retaining their sources. Pixel work and decoder allocation are admitted
|
||||
before decoding; attempted failures keep their charge. An oversized unattempted
|
||||
image does not prevent a later smaller image from fitting.
|
||||
|
||||
`resolve_text` accepts an already-decoded child SVG through the same admission
|
||||
context, rather than an unchecked DOM or a precharged-budget override. `usage()`
|
||||
reports omissions and work after conversion. The context is for one drawing, not a
|
||||
global rendering cache.
|
||||
|
||||
The renderer has no public decoded-pixel or decode-result hook. Validation therefore
|
||||
still decodes raster pixels before later renderer painting. The shared validator
|
||||
owns the backend dependencies; locked dependency-edge tests in both the shared
|
||||
workspace and consumers must keep them aligned with the selected resvg/usvg graph.
|
||||
This is a fail-fast maintenance check, not automatic access to private renderer
|
||||
code or a decode-once performance claim. Direct zune-jpeg use selects its MIT option.
|
||||
+20
-3
@@ -34,7 +34,13 @@ use crate::{guard, svgz};
|
||||
|
||||
pub mod document;
|
||||
pub mod draw;
|
||||
#[cfg(feature = "tree-render")]
|
||||
mod image_href;
|
||||
pub mod primitive;
|
||||
#[cfg(feature = "tree-render")]
|
||||
mod validated;
|
||||
#[cfg(feature = "tree-render")]
|
||||
pub use validated::ValidatedImageResolver;
|
||||
|
||||
/// What one import may use. The defaults suit a desktop app that parses on a thread with a
|
||||
/// large stack (see the module text).
|
||||
@@ -161,6 +167,7 @@ pub fn root_unitless_size(svg: &str, limits: &Limits) -> Result<Option<(f64, f64
|
||||
/// SVG text after shared byte, entity, attribute, XML and expansion-depth checks.
|
||||
/// Keep it opaque so consumers use the same checked tree and don't reparse XML locally.
|
||||
pub struct Preflight<'s> {
|
||||
limits: Limits,
|
||||
xml: roxmltree::Document<'s>,
|
||||
depth: usize,
|
||||
/// [`height`]: the nesting usvg converts, references followed.
|
||||
@@ -219,6 +226,8 @@ const UNSUPPORTED: &[&str] = &[
|
||||
|
||||
/// Checks already-decoded SVG text once, retaining the parsed XML for both depth policy and tree conversion.
|
||||
pub fn preflight<'s>(svg: &'s str, limits: &Limits) -> Result<Preflight<'s>, Error> {
|
||||
#[cfg(test)]
|
||||
PREFLIGHT_PASSES.with(|count| count.set(count.get().saturating_add(1)));
|
||||
let preflight_limits = crate::preflight::Limits {
|
||||
bytes: limits.bytes,
|
||||
depth: limits.depth,
|
||||
@@ -239,7 +248,7 @@ pub fn preflight<'s>(svg: &'s str, limits: &Limits) -> Result<Preflight<'s>, Err
|
||||
let Some(height) = height(&xml, limits.tree_depth)? else {
|
||||
return Err(Error::TooDeep(limits.tree_depth));
|
||||
};
|
||||
Ok(Preflight { xml, depth, height })
|
||||
Ok(Preflight { limits: *limits, xml, depth, height })
|
||||
}
|
||||
|
||||
/// Dimensions of an SVG viewport in SVG user units.
|
||||
@@ -432,9 +441,12 @@ struct Used {
|
||||
/// What resolving one drawing's embedded images used or refused.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct ImageUsage {
|
||||
/// Unique decoded raster pixels reserved before decode.
|
||||
/// Decoded raster pixel work reserved before decode. The legacy resolver
|
||||
/// shares a charge across repeated data; the validated context charges each
|
||||
/// attempted decode, including failures.
|
||||
pub raster_pixels: u64,
|
||||
/// Unpacked SVG bytes consumed by embedded SVG data URLs.
|
||||
/// Decoded SVG text work charged to embedded data URLs, including the
|
||||
/// remaining allowance consumed by a failed inflation attempt.
|
||||
pub embedded_svg_bytes: usize,
|
||||
/// Embedded SVG images parsed, including nested images.
|
||||
pub embedded_svg_count: usize,
|
||||
@@ -730,3 +742,8 @@ fn image_resolver_for(limits: Limits, used: Arc<Used>) -> usvg::ImageHrefResolve
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
static PREFLIGHT_PASSES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
@@ -29,6 +29,11 @@ pub use resvg;
|
||||
#[cfg(feature = "tree-render")]
|
||||
pub use resvg::tiny_skia;
|
||||
|
||||
#[cfg(feature = "tree-render")]
|
||||
mod raster;
|
||||
#[cfg(feature = "tree-render")]
|
||||
pub use raster::{RasterValidationFailure, validate_raster};
|
||||
|
||||
/// Filter primitives per filter. A result of every primitive is kept; real filters have 1 to 12
|
||||
/// (Inkscape's drop shadow has 5), and 64 leaves headroom for generated ones.
|
||||
const MAX_PRIMITIVES: usize = 64;
|
||||
@@ -546,3 +551,6 @@ fn deeper(cx: Cx) -> Result<(), Refusal> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "tree-render"))]
|
||||
mod decoder_edges;
|
||||
@@ -0,0 +1,127 @@
|
||||
//! Test-only resolved-edge checks. No Cargo subprocess or global version count.
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing)]
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct PackageId {
|
||||
name: String,
|
||||
version: String,
|
||||
source: Option<String>,
|
||||
}
|
||||
struct Package {
|
||||
id: PackageId,
|
||||
dependencies: Vec<String>,
|
||||
}
|
||||
pub(crate) struct LockGraph {
|
||||
packages: Vec<Package>,
|
||||
}
|
||||
impl LockGraph {
|
||||
pub(crate) fn parse(text: &str) -> Self {
|
||||
let lock: toml::Value = text.parse().unwrap();
|
||||
let packages = lock
|
||||
.get("package")
|
||||
.unwrap()
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|package| {
|
||||
let string = |key| package.get(key).unwrap().as_str().unwrap().to_owned();
|
||||
Package {
|
||||
id: PackageId {
|
||||
name: string("name"),
|
||||
version: string("version"),
|
||||
source: package.get("source").map(|v| v.as_str().unwrap().to_owned()),
|
||||
},
|
||||
dependencies: package
|
||||
.get("dependencies")
|
||||
.map(|d| d.as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_owned()).collect())
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Self { packages }
|
||||
}
|
||||
pub(crate) fn workspace_package(&self, name: &str) -> PackageId {
|
||||
let found: Vec<_> = self.packages.iter().filter(|p| p.id.name == name && p.id.source.is_none()).collect();
|
||||
assert_eq!(found.len(), 1, "one exact workspace package required: {name}");
|
||||
found[0].id.clone()
|
||||
}
|
||||
pub(crate) fn dependency(&self, from: &PackageId, name: &str) -> PackageId {
|
||||
let package = self.packages.iter().find(|p| &p.id == from).unwrap();
|
||||
let edges: Vec<_> = package.dependencies.iter().filter(|edge| edge.split_whitespace().next() == Some(name)).collect();
|
||||
assert_eq!(edges.len(), 1, "one {name} dependency edge required from {from:?}");
|
||||
let fields: Vec<_> = edges[0].split_whitespace().collect();
|
||||
let version = fields.get(1).copied();
|
||||
let source = fields.get(2).map(|s| s.trim_start_matches('(').trim_end_matches(')'));
|
||||
let found: Vec<_> = self
|
||||
.packages
|
||||
.iter()
|
||||
.filter(|p| p.id.name == name && version.is_none_or(|v| p.id.version == v) && source.is_none_or(|s| p.id.source.as_deref() == Some(s)))
|
||||
.collect();
|
||||
assert_eq!(found.len(), 1, "ambiguous or absent resolved edge {} from {from:?}", edges[0]);
|
||||
found[0].id.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn assert_renderer_edges(graph: &LockGraph, svg: &PackageId) {
|
||||
let renderer = graph.dependency(svg, "resvg");
|
||||
assert_eq!(graph.dependency(svg, "usvg"), graph.dependency(&renderer, "usvg"), "shared and renderer trees must be the same resolved package");
|
||||
for codec in ["gif", "image-webp", "zune-jpeg"] {
|
||||
assert_eq!(graph.dependency(svg, codec), graph.dependency(&renderer, codec), "validator and selected renderer differ for {codec}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validated_renderer_dependency_edges_are_the_same_resolved_packages() {
|
||||
let file = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../Cargo.lock");
|
||||
let graph = LockGraph::parse(&std::fs::read_to_string(file).unwrap());
|
||||
assert_renderer_edges(&graph, &graph.workspace_package("craft-svg"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dependency_lookup_follows_the_selected_edge_and_rejects_ambiguity() {
|
||||
let graph = LockGraph::parse(
|
||||
r#"
|
||||
[[package]]
|
||||
name = "consumer"
|
||||
version = "1"
|
||||
dependencies = ["codec 1 (registry+one)"]
|
||||
[[package]]
|
||||
name = "codec"
|
||||
version = "1"
|
||||
source = "registry+one"
|
||||
[[package]]
|
||||
name = "codec"
|
||||
version = "1"
|
||||
source = "registry+two"
|
||||
[[package]]
|
||||
name = "other"
|
||||
version = "1"
|
||||
dependencies = ["codec"]
|
||||
"#,
|
||||
);
|
||||
let selected = graph.dependency(&graph.workspace_package("consumer"), "codec");
|
||||
assert_eq!(selected.source.as_deref(), Some("registry+one"));
|
||||
assert!(std::panic::catch_unwind(|| graph.dependency(&graph.workspace_package("other"), "codec")).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_renderer_graph_rejects_a_split_validator_backend() {
|
||||
use std::fmt::Write;
|
||||
let make = |split: bool| {
|
||||
let mut text = String::from("[[package]]\nname='craft-svg'\nversion='1'\ndependencies=['resvg', 'usvg', 'gif', 'image-webp', '");
|
||||
text.push_str(if split { "zune-jpeg 2" } else { "zune-jpeg 1" });
|
||||
text.push_str(
|
||||
"']\n[[package]]\nname='resvg'\nversion='1'\nsource='registry+one'\ndependencies=['usvg', 'gif', 'image-webp', 'zune-jpeg 1']\n",
|
||||
);
|
||||
for name in ["usvg", "gif", "image-webp", "zune-jpeg"] {
|
||||
write!(&mut text, "[[package]]\nname='{name}'\nversion='1'\nsource='registry+one'\n").unwrap();
|
||||
}
|
||||
// An unrelated second codec version is valid; only the selected edges count.
|
||||
text.push_str("[[package]]\nname='zune-jpeg'\nversion='2'\nsource='registry+one'\n");
|
||||
LockGraph::parse(&text)
|
||||
};
|
||||
let matching = make(false);
|
||||
assert_renderer_edges(&matching, &matching.workspace_package("craft-svg"));
|
||||
let split = make(true);
|
||||
assert!(std::panic::catch_unwind(|| assert_renderer_edges(&split, &split.workspace_package("craft-svg"))).is_err());
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
//! Validate optional raster images with the same backends used by the SVG renderer.
|
||||
//!
|
||||
//! Header probing alone cannot detect damaged pixel data. Admit dimensions and
|
||||
//! reserve work before decoding: even a failed decode keeps its charge.
|
||||
|
||||
use std::io::Cursor;
|
||||
use std::num::NonZeroU64;
|
||||
|
||||
use crate::tree::{draw::tiny_skia, usvg};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum RasterValidationFailure {
|
||||
/// The renderer cannot decode the encoded image.
|
||||
Invalid,
|
||||
/// Header dimensions, cumulative work or decoder allocation exceed admission.
|
||||
Limit,
|
||||
}
|
||||
|
||||
/// Validate the renderer's first frame within per-image and remaining work limits.
|
||||
/// Failed attempted decodes keep their pixel charge. No decoded pixels are retained. SVG
|
||||
/// children use the same wrapped resolver and need no second recursive walk.
|
||||
pub fn validate_raster(kind: &usvg::ImageKind, max_pixels: u64, pixels_left: &mut u64) -> Result<(), RasterValidationFailure> {
|
||||
let data = match kind {
|
||||
usvg::ImageKind::PNG(data) | usvg::ImageKind::JPEG(data) | usvg::ImageKind::GIF(data) | usvg::ImageKind::WEBP(data) => data,
|
||||
usvg::ImageKind::SVG(_) => return Ok(()),
|
||||
};
|
||||
// The allocation probe includes oversized GIF/WebP frames, not merely their
|
||||
// canvas. Reserve before asking any rendering decoder to allocate pixels.
|
||||
let (width, height) = craft_image_codecs::probe_allocation_size(data).map_err(|_| RasterValidationFailure::Invalid)?;
|
||||
let pixels = area(width, height)?;
|
||||
if pixels > max_pixels {
|
||||
return Err(RasterValidationFailure::Limit);
|
||||
}
|
||||
*pixels_left = pixels_left.checked_sub(pixels).ok_or(RasterValidationFailure::Limit)?;
|
||||
match kind {
|
||||
usvg::ImageKind::PNG(data) => {
|
||||
let decoded = tiny_skia::Pixmap::decode_png(data).map_err(|_| RasterValidationFailure::Invalid)?;
|
||||
rgba_bytes(decoded.width(), decoded.height(), pixels)?;
|
||||
Ok(())
|
||||
}
|
||||
usvg::ImageKind::JPEG(data) => jpeg(data, pixels),
|
||||
usvg::ImageKind::GIF(data) => gif(data, pixels),
|
||||
usvg::ImageKind::WEBP(data) => webp(data, pixels),
|
||||
usvg::ImageKind::SVG(_) => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn area(width: u32, height: u32) -> Result<u64, RasterValidationFailure> {
|
||||
if width == 0 || height == 0 {
|
||||
return Err(RasterValidationFailure::Invalid);
|
||||
}
|
||||
u64::from(width).checked_mul(u64::from(height)).ok_or(RasterValidationFailure::Limit)
|
||||
}
|
||||
|
||||
/// Decoder headers must not request more output than the allocation probe paid for.
|
||||
fn rgba_bytes(width: u32, height: u32, admitted: u64) -> Result<usize, RasterValidationFailure> {
|
||||
let pixels = area(width, height)?;
|
||||
if pixels > admitted {
|
||||
return Err(RasterValidationFailure::Limit);
|
||||
}
|
||||
usize::try_from(pixels.checked_mul(4).ok_or(RasterValidationFailure::Limit)?).map_err(|_| RasterValidationFailure::Limit)
|
||||
}
|
||||
|
||||
fn buffer(length: usize) -> Result<Vec<u8>, RasterValidationFailure> {
|
||||
let mut output = Vec::new();
|
||||
output.try_reserve_exact(length).map_err(|_| RasterValidationFailure::Limit)?;
|
||||
output.resize(length, 0);
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn jpeg(data: &[u8], admitted: u64) -> Result<(), RasterValidationFailure> {
|
||||
use zune_jpeg::zune_core::colorspace::ColorSpace;
|
||||
use zune_jpeg::zune_core::options::DecoderOptions;
|
||||
|
||||
// Preserve the rendering decoder's default limits and RGBA conversion; a
|
||||
// general photo decoder may accept formats the SVG renderer cannot draw.
|
||||
let options = DecoderOptions::default().jpeg_set_out_colorspace(ColorSpace::RGBA);
|
||||
let mut decoder = zune_jpeg::JpegDecoder::new_with_options(Cursor::new(data), options);
|
||||
decoder.decode_headers().map_err(|_| RasterValidationFailure::Invalid)?;
|
||||
let info = decoder.info().ok_or(RasterValidationFailure::Invalid)?;
|
||||
let length = rgba_bytes(u32::from(info.width), u32::from(info.height), admitted)?;
|
||||
if decoder.output_colorspace() != Some(ColorSpace::RGBA) || decoder.output_buffer_size() != Some(length) {
|
||||
return Err(RasterValidationFailure::Invalid);
|
||||
}
|
||||
let mut output = buffer(length)?;
|
||||
decoder.decode_into(&mut output).map_err(|_| RasterValidationFailure::Invalid)
|
||||
}
|
||||
|
||||
fn gif(data: &[u8], admitted: u64) -> Result<(), RasterValidationFailure> {
|
||||
let bytes = admitted.checked_mul(4).ok_or(RasterValidationFailure::Limit)?;
|
||||
// Do not loosen the renderer's default 50 MB GIF frame limit. Smaller
|
||||
// imports also keep their own admitted allocation ceiling during decode.
|
||||
let limit = NonZeroU64::new(bytes.min(50_000_000)).ok_or(RasterValidationFailure::Invalid)?;
|
||||
let mut options = gif::DecodeOptions::new();
|
||||
options.set_color_output(gif::ColorOutput::RGBA);
|
||||
options.set_memory_limit(gif::MemoryLimit::Bytes(limit));
|
||||
let mut decoder = options.read_info(data).map_err(gif_error)?;
|
||||
let frame = decoder.read_next_frame().map_err(gif_error)?.ok_or(RasterValidationFailure::Invalid)?;
|
||||
let expected = rgba_bytes(u32::from(frame.width), u32::from(frame.height), admitted)?;
|
||||
if frame.buffer.len() != expected {
|
||||
return Err(RasterValidationFailure::Invalid);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn gif_error(error: gif::DecodingError) -> RasterValidationFailure {
|
||||
match error {
|
||||
gif::DecodingError::MemoryLimit | gif::DecodingError::OutOfMemory => RasterValidationFailure::Limit,
|
||||
_ => RasterValidationFailure::Invalid,
|
||||
}
|
||||
}
|
||||
|
||||
fn webp(data: &[u8], admitted: u64) -> Result<(), RasterValidationFailure> {
|
||||
let mut decoder = image_webp::WebPDecoder::new(Cursor::new(data)).map_err(webp_error)?;
|
||||
let (width, height) = decoder.dimensions();
|
||||
let rgba = rgba_bytes(width, height, admitted)?;
|
||||
let channels = if decoder.has_alpha() { 4 } else { 3 };
|
||||
let expected = usize::try_from(area(width, height)?.checked_mul(channels).ok_or(RasterValidationFailure::Limit)?)
|
||||
.map_err(|_| RasterValidationFailure::Limit)?;
|
||||
if expected > rgba {
|
||||
return Err(RasterValidationFailure::Limit);
|
||||
}
|
||||
if decoder.output_buffer_size() != Some(expected) {
|
||||
return Err(RasterValidationFailure::Invalid);
|
||||
}
|
||||
// A small valid image can have a compressed chunk larger than its pixels.
|
||||
// Bound decoder scratch without rejecting that ordinary encoding overhead.
|
||||
let admitted_bytes =
|
||||
usize::try_from(admitted.checked_mul(4).ok_or(RasterValidationFailure::Limit)?).map_err(|_| RasterValidationFailure::Limit)?;
|
||||
decoder.set_memory_limit(admitted_bytes.max(data.len()));
|
||||
let mut output = buffer(expected)?;
|
||||
decoder.read_image(&mut output).map_err(webp_error)
|
||||
}
|
||||
|
||||
fn webp_error(error: image_webp::DecodingError) -> RasterValidationFailure {
|
||||
match error {
|
||||
image_webp::DecodingError::MemoryLimitExceeded | image_webp::DecodingError::ImageTooLarge => RasterValidationFailure::Limit,
|
||||
_ => RasterValidationFailure::Invalid,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,428 @@
|
||||
//! Bounded data-URL normalization over the shared checked XML document.
|
||||
//! Only attribute values change; actual usvg callbacks decide image usage.
|
||||
use super::Error;
|
||||
#[cfg(test)]
|
||||
use super::Limits;
|
||||
use std::borrow::Cow;
|
||||
use std::ops::Range;
|
||||
|
||||
#[cfg(test)]
|
||||
const SVG_NS: &str = "http://www.w3.org/2000/svg";
|
||||
const XLINK_NS: &str = "http://www.w3.org/1999/xlink";
|
||||
const INVALID_IMAGE: &str = "data:image/png,%00";
|
||||
const IGNORED_DATA: &str = "data:application/octet-stream,%00";
|
||||
|
||||
struct Patch {
|
||||
range: Range<usize>,
|
||||
replacement: Cow<'static, str>,
|
||||
}
|
||||
|
||||
fn invalid(message: &str) -> Error {
|
||||
Error::Parse(message.into())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn repair<'a>(text: &'a str, limits: &Limits) -> Result<Cow<'a, str>, Error> {
|
||||
normalize(&super::preflight(text, limits)?)
|
||||
}
|
||||
|
||||
pub(super) fn normalize<'a>(checked: &super::Preflight<'a>) -> Result<Cow<'a, str>, Error> {
|
||||
let text = checked.xml.input_text();
|
||||
let limits = &checked.limits;
|
||||
let xml = &checked.xml;
|
||||
if !text.contains("href") {
|
||||
return Ok(Cow::Borrowed(text));
|
||||
}
|
||||
// Expanded values fit source + entity work. Twelve linear passes cover URL
|
||||
// scans/streaming decode, full attribute projection and worst-case XML escaping;
|
||||
// parameter compaction never allocates one record per original parameter.
|
||||
let mut work = text
|
||||
.len()
|
||||
.checked_add(limits.entity_bytes)
|
||||
.and_then(|n| n.checked_mul(12))
|
||||
.ok_or_else(|| invalid("SVG data-URL validation work exceeds its limit"))?;
|
||||
let max_patches = (limits.bytes / std::mem::size_of::<Patch>()).min(usize::try_from(limits.xml_nodes).unwrap_or(usize::MAX));
|
||||
let mut patches: Vec<Patch> = Vec::new();
|
||||
for node in xml.descendants().filter(|node| node.is_element()) {
|
||||
if !matches!(node.tag_name().name(), "image" | "feImage") {
|
||||
continue;
|
||||
}
|
||||
// Shared preflight inspects local image names in every namespace and
|
||||
// checks XLink before plain href. Normalize both representations, even
|
||||
// when one is inert or unselected. Only actual resolver callbacks count
|
||||
// omissions, so this does not turn hidden/unused resources into warnings.
|
||||
for href in node.attributes().filter(|a| a.name() == "href" && matches!(a.namespace(), None | Some(XLINK_NS))) {
|
||||
let Some(replacement) = data_url_replacement(href.value(), &mut work)? else { continue };
|
||||
let range = verified_value_range(text, node.range(), href.range(), href.name(), &mut work)?;
|
||||
if patches.len() == patches.capacity() {
|
||||
if patches.len() >= max_patches {
|
||||
return Err(invalid("SVG data-URL repair metadata exceeds its limit"));
|
||||
}
|
||||
let capacity = patches.capacity().max(8).saturating_mul(2).min(max_patches);
|
||||
patches
|
||||
.try_reserve_exact(capacity.saturating_sub(patches.len()))
|
||||
.map_err(|_| invalid("could not allocate bounded SVG data-URL repairs"))?;
|
||||
}
|
||||
patches.push(Patch { range, replacement });
|
||||
}
|
||||
}
|
||||
if patches.is_empty() {
|
||||
return Ok(Cow::Borrowed(text));
|
||||
}
|
||||
patches.sort_unstable_by_key(|patch| (patch.range.start, patch.range.end));
|
||||
patches.dedup_by_key(|patch| (patch.range.start, patch.range.end));
|
||||
let mut end = 0;
|
||||
let mut length = text.len();
|
||||
for patch in &patches {
|
||||
let range = &patch.range;
|
||||
if range.start < end {
|
||||
return Err(invalid("SVG data-URL source ranges overlap"));
|
||||
}
|
||||
end = range.end;
|
||||
length = length
|
||||
.checked_sub(range.len())
|
||||
.and_then(|n| n.checked_add(patch.replacement.len()))
|
||||
.ok_or_else(|| invalid("SVG data-URL repair size overflows"))?;
|
||||
}
|
||||
if length > limits.bytes {
|
||||
return Err(invalid("repaired SVG exceeds the text byte limit"));
|
||||
}
|
||||
let mut result = String::new();
|
||||
result.try_reserve_exact(length).map_err(|_| invalid("could not allocate bounded repaired SVG text"))?;
|
||||
let mut cursor = 0;
|
||||
for patch in patches {
|
||||
result.push_str(text.get(cursor..patch.range.start).ok_or_else(|| invalid("invalid SVG data-URL source range"))?);
|
||||
result.push_str(&patch.replacement);
|
||||
cursor = patch.range.end;
|
||||
}
|
||||
result.push_str(text.get(cursor..).ok_or_else(|| invalid("invalid SVG data-URL source range"))?);
|
||||
Ok(Cow::Owned(result))
|
||||
}
|
||||
|
||||
// The pinned MIME parser searches all earlier parameters for each new name.
|
||||
// Cap even quoted semicolons before invoking it: source-byte work is already
|
||||
// charged, so at most this constant times the bounded XML work reaches MIME
|
||||
// parsing. Bodies can remain large without acquiring MIME parameter records.
|
||||
const MAX_MIME_SEPARATORS: usize = 64;
|
||||
|
||||
fn mime_header_admitted(value: &str) -> bool {
|
||||
let trimmed = value.trim_start_matches(|ch| ch <= ' ');
|
||||
let mut bytes = trimmed.bytes();
|
||||
for expected in b"data:" {
|
||||
let Some(actual) = bytes.by_ref().find(|&byte| !matches!(byte, b'\t' | b'\n' | b'\r')) else { return true };
|
||||
if !actual.eq_ignore_ascii_case(expected) {
|
||||
return true; // Not a data URL: preserve external/fragment handling.
|
||||
}
|
||||
}
|
||||
let mut separators = 0;
|
||||
for byte in bytes {
|
||||
if matches!(byte, b',' | b'#') {
|
||||
break;
|
||||
}
|
||||
if byte == b';' {
|
||||
separators += 1;
|
||||
if separators > MAX_MIME_SEPARATORS {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// A long parameter list must not reach the quadratic MIME parser, but its
|
||||
/// essence, final base64 marker and body still determine the original image.
|
||||
/// Compact only that header, then use the same parser/streaming decoder. Escape
|
||||
/// the decoded attribute value once when projecting it back into XML.
|
||||
fn data_url_replacement(value: &str, work: &mut usize) -> Result<Option<Cow<'static, str>>, Error> {
|
||||
if mime_header_admitted(value) {
|
||||
return malformed_data_url_replacement(value, work).map(|replacement| replacement.map(Cow::Borrowed));
|
||||
}
|
||||
*work = work.checked_sub(value.len()).ok_or_else(|| invalid("SVG data-URL validation work exceeds its limit"))?;
|
||||
let normalized = value.trim_matches(|ch| ch <= ' ');
|
||||
let delimiter = normalized.find([',', '#']);
|
||||
let Some(comma) = delimiter.filter(|&at| normalized.as_bytes().get(at) == Some(&b',')) else {
|
||||
return missing_comma_replacement(value, work).map(|replacement| Some(Cow::Borrowed(replacement)));
|
||||
};
|
||||
let header = normalized.get(..comma).ok_or_else(|| invalid("invalid SVG data-URL header framing"))?;
|
||||
let essence = header.split_once(';').map_or(header, |(essence, _)| essence);
|
||||
// Retaining the final parameter lets the pinned parser decide whether its
|
||||
// suffix is a base64 marker, including its case/ASCII-whitespace rules.
|
||||
let suffix = header.rfind(';').and_then(|at| header.get(at..)).unwrap_or("");
|
||||
let body = normalized.get(comma..).ok_or_else(|| invalid("invalid SVG data-URL body framing"))?;
|
||||
let length = essence
|
||||
.len()
|
||||
.checked_add(suffix.len())
|
||||
.and_then(|n| n.checked_add(body.len()))
|
||||
.ok_or_else(|| invalid("SVG data-URL repair size overflows"))?;
|
||||
let mut compact = String::new();
|
||||
compact.try_reserve_exact(length).map_err(|_| invalid("could not allocate bounded SVG data-URL header"))?;
|
||||
compact.push_str(essence);
|
||||
compact.push_str(suffix);
|
||||
compact.push_str(body);
|
||||
if let Some(replacement) = malformed_data_url_replacement(&compact, work)? {
|
||||
return Ok(Some(Cow::Borrowed(replacement)));
|
||||
}
|
||||
let escaped_len = compact
|
||||
.chars()
|
||||
.try_fold(0usize, |n, ch| {
|
||||
n.checked_add(match ch {
|
||||
'&' | '\t' | '\n' | '\r' => 5,
|
||||
'\'' | '"' => 6,
|
||||
'<' | '>' => 4,
|
||||
_ => ch.len_utf8(),
|
||||
})
|
||||
})
|
||||
.ok_or_else(|| invalid("SVG data-URL repair size overflows"))?;
|
||||
*work = work.checked_sub(escaped_len).ok_or_else(|| invalid("SVG data-URL validation work exceeds its limit"))?;
|
||||
let mut escaped = String::new();
|
||||
escaped.try_reserve_exact(escaped_len).map_err(|_| invalid("could not allocate bounded SVG data-URL repair"))?;
|
||||
for ch in compact.chars() {
|
||||
match ch {
|
||||
'&' => escaped.push_str("&"),
|
||||
'\'' => escaped.push_str("'"),
|
||||
'"' => escaped.push_str("""),
|
||||
'<' => escaped.push_str("<"),
|
||||
'>' => escaped.push_str(">"),
|
||||
'\t' => escaped.push_str("	"),
|
||||
'\n' => escaped.push_str("
"),
|
||||
'\r' => escaped.push_str("
"),
|
||||
_ => escaped.push(ch),
|
||||
}
|
||||
}
|
||||
Ok(Some(Cow::Owned(escaped)))
|
||||
}
|
||||
|
||||
fn malformed_data_url_replacement(value: &str, work: &mut usize) -> Result<Option<&'static str>, Error> {
|
||||
malformed_data_url_replacement_with(value, work, data_url::DataUrl::process)
|
||||
}
|
||||
|
||||
fn malformed_data_url_replacement_with<'a>(
|
||||
value: &'a str,
|
||||
work: &mut usize,
|
||||
parse: impl FnOnce(&'a str) -> Result<data_url::DataUrl<'a>, data_url::DataUrlError>,
|
||||
) -> Result<Option<&'static str>, Error> {
|
||||
*work = work.checked_sub(value.len()).ok_or_else(|| invalid("SVG data-URL validation work exceeds its limit"))?;
|
||||
if !mime_header_admitted(value) {
|
||||
// Strip parameters before recovering MIME disposition; no original
|
||||
// parameter list reaches either our parser or shared whole-DOM preflight.
|
||||
return missing_comma_replacement(value, work).map(Some);
|
||||
}
|
||||
let url = match parse(value) {
|
||||
Ok(url) => url,
|
||||
Err(data_url::DataUrlError::NoComma) => return missing_comma_replacement(value, work).map(Some),
|
||||
Err(data_url::DataUrlError::NotADataUrl) => return Ok(None),
|
||||
};
|
||||
let mut exhausted = false;
|
||||
let decoded = url.decode(|bytes| {
|
||||
if let Some(left) = work.checked_sub(bytes.len()) {
|
||||
*work = left;
|
||||
Ok(())
|
||||
} else {
|
||||
exhausted = true;
|
||||
Err(())
|
||||
}
|
||||
});
|
||||
if exhausted {
|
||||
return Err(invalid("SVG data-URL validation work exceeds its limit"));
|
||||
}
|
||||
Ok(decoded.is_err().then(|| replacement_for_mime(url.mime_type())))
|
||||
}
|
||||
|
||||
/// Preflight must see a valid URL, while the resolver must keep its original
|
||||
/// disposition. Non-image MIME types are ignored even when their body is bad.
|
||||
fn replacement_for_mime(mime: &data_url::mime::Mime) -> &'static str {
|
||||
if mime.type_ == "image" || mime.matches("text", "plain") { INVALID_IMAGE } else { IGNORED_DATA }
|
||||
}
|
||||
|
||||
fn missing_comma_replacement(value: &str, work: &mut usize) -> Result<&'static str, Error> {
|
||||
// NoComma does not expose its MIME type. Ask the same parser about a header
|
||||
// with an empty body rather than guessing MIME defaults, case or parameters.
|
||||
// Match its URL C0 trimming; fragments and MIME parameters cannot affect
|
||||
// the image/text-plain essence. Do not allocate parameter bookkeeping for a
|
||||
// hostile missing-comma header just to recover that disposition.
|
||||
let normalized = value.trim_matches(|ch| ch <= ' ');
|
||||
let header = normalized.split_once('#').map_or(normalized, |(header, _)| header);
|
||||
let essence = header.split_once(';').map_or(header, |(essence, _)| essence);
|
||||
*work = work.checked_sub(essence.len()).ok_or_else(|| invalid("SVG data-URL validation work exceeds its limit"))?;
|
||||
let length = essence.len().checked_add(1).ok_or_else(|| invalid("SVG data-URL repair size overflows"))?;
|
||||
let mut candidate = String::new();
|
||||
candidate.try_reserve_exact(length).map_err(|_| invalid("could not allocate bounded SVG data-URL header"))?;
|
||||
candidate.push_str(essence);
|
||||
candidate.push(',');
|
||||
// This candidate has no MIME parameters, including for oversized original
|
||||
// lists. Its byte scan/allocation is charged above before the linear parse.
|
||||
if !mime_header_admitted(&candidate) {
|
||||
return Err(invalid("SVG data-URL MIME header exceeds its limit"));
|
||||
}
|
||||
let url = data_url::DataUrl::process(&candidate).map_err(|_| invalid("invalid SVG data-URL header framing"))?;
|
||||
Ok(replacement_for_mime(url.mime_type()))
|
||||
}
|
||||
|
||||
/// Use the complete source range rather than roxmltree's compact QName/equals
|
||||
/// offsets (u16/u8). Scan only this validated attribute, never a DTD declaration
|
||||
/// or another node. Charge the extra source scan before examining it.
|
||||
fn verified_value_range(text: &str, owner: Range<usize>, attribute: Range<usize>, local_name: &str, work: &mut usize) -> Result<Range<usize>, Error> {
|
||||
let error = || invalid("SVG data-URL attribute source positions cannot be represented safely");
|
||||
if attribute.start < owner.start || attribute.end > owner.end {
|
||||
return Err(error());
|
||||
}
|
||||
*work = work.checked_sub(attribute.len()).ok_or_else(|| invalid("SVG data-URL validation work exceeds its limit"))?;
|
||||
let source = text.get(attribute.clone()).ok_or_else(error)?;
|
||||
let name_end = source.find([' ', '\t', '\r', '\n', '=']).ok_or_else(error)?;
|
||||
let name = source.get(..name_end).ok_or_else(error)?;
|
||||
if name.rsplit(':').next() != Some(local_name) {
|
||||
return Err(error());
|
||||
}
|
||||
let after_name = source.get(name_end..).ok_or_else(error)?.trim_start_matches([' ', '\t', '\r', '\n']);
|
||||
let after_equals = after_name.strip_prefix('=').ok_or_else(error)?.trim_start_matches([' ', '\t', '\r', '\n']);
|
||||
let quote = after_equals.chars().next().ok_or_else(error)?;
|
||||
if !matches!(quote, '\'' | '"') || !after_equals.ends_with(quote) || after_equals.len() < 2 {
|
||||
return Err(error());
|
||||
}
|
||||
let start = attribute.end.checked_sub(after_equals.len()).and_then(|n| n.checked_add(1)).ok_or_else(error)?;
|
||||
let end = attribute.end.checked_sub(1).ok_or_else(error)?;
|
||||
if text.get(start..end).is_none_or(|value| value.contains(quote)) {
|
||||
return Err(error());
|
||||
}
|
||||
Ok(start..end)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn limits() -> Limits {
|
||||
Limits { bytes: 1 << 20, depth: 32, tree_depth: 32, entity_bytes: 65_536, ..Default::default() }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_image_urls_are_repaired_in_both_svg_namespaces_and_filter_resources() {
|
||||
for tag in ["image", "feImage"] {
|
||||
for text in [
|
||||
format!("<svg xmlns='{SVG_NS}'><{tag} href='data:image/png;base64,A'/></svg>"),
|
||||
format!("<svg><{tag} href='data:image/png;base64,A'/></svg>"),
|
||||
format!("<s:svg xmlns:s='{SVG_NS}' xmlns:link='{XLINK_NS}'><s:{tag} link:href='data:image/png;base64,A'/></s:svg>"),
|
||||
] {
|
||||
let repaired = repair(&text, &limits()).unwrap();
|
||||
assert!(matches!(repaired, Cow::Owned(_)));
|
||||
assert_eq!(repaired.matches(INVALID_IMAGE).count(), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unselected_href_and_foreign_local_image_names_are_normalized_for_shared_preflight() {
|
||||
for attrs in ["href='data:image/png,%00' x:href='data:image/png;base64,A'", "x:href='data:image/png;base64,A' href='data:image/png,%00'"] {
|
||||
let text = format!("<svg xmlns='{SVG_NS}' xmlns:x='{XLINK_NS}'><image {attrs}/></svg>");
|
||||
let repaired = repair(&text, &limits()).unwrap();
|
||||
assert!(matches!(repaired, Cow::Owned(_)));
|
||||
assert_eq!(repaired.matches(INVALID_IMAGE).count(), 2);
|
||||
assert!(!repaired.contains("base64,A"));
|
||||
}
|
||||
let text = format!(
|
||||
"<svg xmlns='{SVG_NS}' xmlns:x='{XLINK_NS}' xmlns:f='urn:foreign'><image x:href='data:image/png,%00' href='data:image/png;base64,A'/><f:image href='data:image/png;base64,A'/><image f:href='data:image/png;base64,A'/></svg>"
|
||||
);
|
||||
let repaired = repair(&text, &limits()).unwrap();
|
||||
assert_eq!(repaired.matches(INVALID_IMAGE).count(), 3);
|
||||
assert!(repaired.contains("f:href='data:image/png;base64,A'"), "foreign href attributes remain inert and unchanged");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn both_href_forms_on_hidden_unused_and_foreign_resources_do_not_preflight_as_omissions() {
|
||||
for tag in ["image", "feImage"] {
|
||||
let text = format!(
|
||||
"<svg xmlns='{SVG_NS}' xmlns:x='{XLINK_NS}' xmlns:f='urn:foreign'><defs><{tag} id='unused' href='data:image/png;base64,A' x:href='data:image/png;base64,A'/></defs><{tag} display='none' x:href='data:image/png;base64,A' href='data:image/png;base64,A'/><f:{tag} href='data:image/png;base64,A' x:href='data:image/png;base64,A'/></svg>"
|
||||
);
|
||||
let original = text.clone();
|
||||
let repaired = repair(&text, &limits()).unwrap();
|
||||
assert_eq!(text, original, "repair never mutates the original source");
|
||||
assert_eq!(repaired.matches(INVALID_IMAGE).count(), 6);
|
||||
let resolver = crate::tree::ImageResolver::new(limits());
|
||||
resolver.check_document(&repaired).unwrap();
|
||||
let usage = resolver.usage();
|
||||
assert_eq!(
|
||||
(usage.invalid_raster_images, usage.invalid_svg_images),
|
||||
(0, 0),
|
||||
"shared whole-document scanning cannot invent omissions before resolution"
|
||||
);
|
||||
assert!(matches!(repair(&repaired, &limits()).unwrap(), Cow::Borrowed(_)), "normalization is idempotent");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn entity_expanded_urls_patch_the_owning_attributes_without_rewriting_the_doctype() {
|
||||
let text = format!(
|
||||
"<!DOCTYPE svg [<!ENTITY bad 'data:image/png;base64,A'>]><svg xmlns='{SVG_NS}'><image href='&bad;'/><image href='data:image/png;base64,A'/></svg>"
|
||||
);
|
||||
let repaired = repair(&text, &limits()).unwrap();
|
||||
assert!(repaired.contains("<!ENTITY bad 'data:image/png;base64,A'>"));
|
||||
assert_eq!(repaired.matches(INVALID_IMAGE).count(), 2);
|
||||
assert!(!repaired.contains("href='&bad;'"));
|
||||
assert!(matches!(repair(&repaired, &limits()).unwrap(), Cow::Borrowed(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_data_and_external_urls_are_not_rewritten() {
|
||||
for value in ["data:image/png;base64,AA==", "data:image/svg+xml,%3Csvg%2F%3E", "file.png", "#local"] {
|
||||
let text = format!("<svg><image href='{value}'/></svg>");
|
||||
assert!(matches!(repair(&text, &limits()).unwrap(), Cow::Borrowed(_)));
|
||||
}
|
||||
let text = "<svg><image href='data:image/png;base64'/></svg>";
|
||||
assert!(repair(text, &limits()).unwrap().contains(INVALID_IMAGE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_non_image_urls_keep_ignored_replacements_even_without_a_comma() {
|
||||
for value in [
|
||||
"data:application/octet-stream;base64,A",
|
||||
"data:application/octet-stream;base64",
|
||||
"data:APPLICATION/OCTET-STREAM;BASE64,A",
|
||||
"data:text/css;base64,A",
|
||||
"data:audio/ogg;base64,A",
|
||||
"data:application/octet-stream;base64#fragment,not-the-delimiter",
|
||||
] {
|
||||
let text = format!("<svg><image href='{value}'/></svg>");
|
||||
let repaired = repair(&text, &limits()).unwrap();
|
||||
assert!(repaired.contains(IGNORED_DATA), "{value}: {repaired}");
|
||||
assert!(!repaired.contains(INVALID_IMAGE));
|
||||
assert!(matches!(repair(&repaired, &limits()).unwrap(), Cow::Borrowed(_)));
|
||||
let resolver = crate::tree::ImageResolver::new(limits());
|
||||
resolver.check_document(&repaired).unwrap();
|
||||
assert_eq!(resolver.usage().invalid_raster_images, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_and_default_plain_mime_failures_remain_disclosed() {
|
||||
for value in [
|
||||
"data:IMAGE/PNG;base64,A",
|
||||
"data:image/png;base64",
|
||||
"data:text/plain;base64,A",
|
||||
"data:;base64,A",
|
||||
"data:;base64",
|
||||
"data:not-a-mime;base64",
|
||||
] {
|
||||
let text = format!("<svg><image href='{value}'/></svg>");
|
||||
assert!(repair(&text, &limits()).unwrap().contains(INVALID_IMAGE), "{value}");
|
||||
}
|
||||
let value = "data:application/octet-stream;base64#fragment";
|
||||
let mut insufficient = value.len();
|
||||
assert!(malformed_data_url_replacement(value, &mut insufficient).is_err(), "the extra MIME-header parse has its own work charge");
|
||||
let mut sufficient = value.len() * 2;
|
||||
assert_eq!(malformed_data_url_replacement(value, &mut sufficient).unwrap(), Some(IGNORED_DATA));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_equals_spacing_is_repaired_and_output_growth_stays_bounded() {
|
||||
let text = format!("<svg><image href{}='data:image/png;base64,A'/></svg>", " ".repeat(300));
|
||||
let repaired = repair(&text, &limits()).unwrap();
|
||||
assert!(repaired.contains(INVALID_IMAGE));
|
||||
assert!(repaired.contains(&format!("href{}=", " ".repeat(300))));
|
||||
let text = "<svg><image href='data:;base64,A'/></svg>";
|
||||
assert!(matches!(repair(text, &Limits { bytes: text.len(), ..limits() }), Err(Error::Parse(message)) if message.contains("byte limit")));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "mime_header_tests.rs"]
|
||||
mod mime_header_tests;
|
||||
@@ -0,0 +1,158 @@
|
||||
use super::*;
|
||||
use std::fmt::Write;
|
||||
|
||||
fn parameters(count: usize) -> String {
|
||||
let mut text = String::new();
|
||||
for index in 0..count {
|
||||
write!(&mut text, ";p{index}=x").unwrap();
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hundred_thousand_unique_mime_parameters_are_refused_before_the_original_parser() {
|
||||
let params = parameters(100_000);
|
||||
for (essence, expected) in [
|
||||
("image/png", INVALID_IMAGE),
|
||||
("text/plain", INVALID_IMAGE),
|
||||
("", INVALID_IMAGE),
|
||||
("not-a-mime", INVALID_IMAGE),
|
||||
("application/octet-stream", IGNORED_DATA),
|
||||
("audio/ogg", IGNORED_DATA),
|
||||
] {
|
||||
for delimiter in [",AA==", "#fragment,AA==", ""] {
|
||||
let value = format!("data:{essence}{params};base64{delimiter}");
|
||||
let mut work = value.len() * 2;
|
||||
assert!(!mime_header_admitted(&value));
|
||||
let result =
|
||||
malformed_data_url_replacement_with(&value, &mut work, |_| panic!("the original parameter flood must not reach DataUrl::process"))
|
||||
.unwrap();
|
||||
assert_eq!(result, Some(expected), "{essence} {delimiter}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mime_parameter_admission_preserves_the_boundary_and_ignores_body_semicolons() {
|
||||
let accepted = format!("data:image/png{};base64,AA==", parameters(MAX_MIME_SEPARATORS - 1));
|
||||
assert!(mime_header_admitted(&accepted));
|
||||
let called = std::cell::Cell::new(false);
|
||||
let mut work = accepted.len() * 2;
|
||||
assert_eq!(
|
||||
malformed_data_url_replacement_with(&accepted, &mut work, |value| {
|
||||
called.set(true);
|
||||
data_url::DataUrl::process(value)
|
||||
})
|
||||
.unwrap(),
|
||||
None
|
||||
);
|
||||
assert!(called.get(), "an admitted original URL still reaches the real parser");
|
||||
let refused = format!("data:image/png{};base64,AA==", parameters(MAX_MIME_SEPARATORS));
|
||||
assert!(!mime_header_admitted(&refused));
|
||||
let mut work = refused.len() * 2;
|
||||
assert_eq!(malformed_data_url_replacement(&refused, &mut work).unwrap(), Some(INVALID_IMAGE));
|
||||
|
||||
for value in [format!("data:text/plain,{}", ";".repeat(100_000)), format!("external.png?{}", parameters(100_000))] {
|
||||
assert!(mime_header_admitted(&value), "payloads/external URLs are not MIME parameters");
|
||||
let mut work = value.len() * 2;
|
||||
assert_eq!(malformed_data_url_replacement(&value, &mut work).unwrap(), None);
|
||||
}
|
||||
let value = format!(" \td\ta\nt\ra:image/png{};base64,A", parameters(MAX_MIME_SEPARATORS));
|
||||
assert!(!mime_header_admitted(&value), "scheme normalization matches the pinned parser");
|
||||
let mut work = value.len() * 2;
|
||||
assert_eq!(malformed_data_url_replacement(&value, &mut work).unwrap(), Some(INVALID_IMAGE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compacted_parameter_lists_preserve_both_hrefs_and_foreign_resources_without_preflight_warnings() {
|
||||
let value = format!("data:image/png{};base64,AA==", parameters(MAX_MIME_SEPARATORS));
|
||||
let text = format!(
|
||||
"<svg xmlns='{SVG_NS}' xmlns:x='{XLINK_NS}' xmlns:f='urn:foreign'><defs><image id='unused' href='{value}' x:href='{value}'/></defs><image display='none' href='{value}' x:href='{value}'/><f:image href='{value}' x:href='{value}'/></svg>"
|
||||
);
|
||||
let limits = Limits { bytes: 1 << 20, ..Default::default() };
|
||||
let repaired = repair(&text, &limits).unwrap();
|
||||
assert_eq!(repaired.matches("data:image/png;base64,AA==").count(), 6);
|
||||
assert!(!repaired.contains(";p0="));
|
||||
let resolver = crate::tree::ImageResolver::new(limits);
|
||||
resolver.check_document(&repaired).unwrap();
|
||||
let usage = resolver.usage();
|
||||
assert_eq!((usage.invalid_raster_images, usage.invalid_svg_images), (0, 0));
|
||||
assert!(matches!(repair(&repaired, &Limits { bytes: 1 << 20, ..Default::default() }).unwrap(), Cow::Borrowed(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_and_quoted_parameters_share_the_same_conservative_boundary() {
|
||||
for (accepted, refused) in [
|
||||
(
|
||||
format!("data:image/png{},%00", ";p=x".repeat(MAX_MIME_SEPARATORS)),
|
||||
format!("data:image/png{},%00", ";p=x".repeat(MAX_MIME_SEPARATORS + 1)),
|
||||
),
|
||||
(
|
||||
format!("data:image/png;p=\"{}\",%00", ";".repeat(MAX_MIME_SEPARATORS - 1)),
|
||||
format!("data:image/png;p=\"{}\",%00", ";".repeat(MAX_MIME_SEPARATORS)),
|
||||
),
|
||||
] {
|
||||
assert!(mime_header_admitted(&accepted));
|
||||
let mut work = accepted.len() * 2;
|
||||
assert_eq!(malformed_data_url_replacement(&accepted, &mut work).unwrap(), None);
|
||||
assert!(!mime_header_admitted(&refused));
|
||||
let mut work = refused.len() * 2;
|
||||
assert_eq!(malformed_data_url_replacement(&refused, &mut work).unwrap(), Some(INVALID_IMAGE));
|
||||
}
|
||||
for value in ["external.png", "#local", "datx:image/png;param=x,body"] {
|
||||
let mut work = value.len() * 2;
|
||||
assert_eq!(malformed_data_url_replacement(value, &mut work).unwrap(), None);
|
||||
}
|
||||
let value = "data:image/png;base64";
|
||||
let mut work = value.len() * 2;
|
||||
assert_eq!(malformed_data_url_replacement(value, &mut work).unwrap(), Some(INVALID_IMAGE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parameter_flood_scans_and_essence_classification_spend_one_shared_work_ledger() {
|
||||
let value = format!("data:application/octet-stream{};base64,AA==", parameters(MAX_MIME_SEPARATORS));
|
||||
let essence_len = "data:application/octet-stream".len();
|
||||
let charge = value.len() + essence_len;
|
||||
let mut work = charge * 2;
|
||||
for remaining in [charge, 0] {
|
||||
assert_eq!(malformed_data_url_replacement_with(&value, &mut work, |_| panic!("not admitted")).unwrap(), Some(IGNORED_DATA));
|
||||
assert_eq!(work, remaining, "the original header scan and parameter-free fallback each retain their charge");
|
||||
}
|
||||
assert!(malformed_data_url_replacement_with(&value, &mut work, |_| panic!("no work remains")).is_err());
|
||||
assert_eq!(work, 0);
|
||||
let mut not_enough_for_essence = value.len();
|
||||
assert!(malformed_data_url_replacement_with(&value, &mut not_enough_for_essence, |_| panic!("not admitted")).is_err());
|
||||
assert_eq!(not_enough_for_essence, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_parameter_urls_preserve_bodies_and_base64_disposition() {
|
||||
let params = parameters(100_000);
|
||||
for (suffix, body, expected) in [
|
||||
(";base64", "AA==", vec![0]),
|
||||
("; BASE64 ", "AA==", vec![0]),
|
||||
(";p=last", "%00%26%27%22", vec![0, b'&', b'\'', b'"']),
|
||||
(";p=last", "&'"	

z", b"&\'\"z".to_vec()),
|
||||
] {
|
||||
let text = format!("<svg><image href='data:image/png{params}{suffix},{body}'/></svg>");
|
||||
let repaired = repair(&text, &Limits { bytes: 2 << 20, ..Default::default() }).unwrap();
|
||||
let xml = checked_xml(&repaired, &Limits { bytes: 2 << 20, ..Default::default() }).unwrap();
|
||||
let value = xml.descendants().find(|node| node.has_tag_name("image")).unwrap().attribute("href").unwrap();
|
||||
assert!(!value.contains(";p0="));
|
||||
assert_eq!(data_url::DataUrl::process(value).unwrap().decode_to_vec().unwrap().0, expected);
|
||||
assert!(matches!(repair(&repaired, &Limits { bytes: 2 << 20, ..Default::default() }).unwrap(), Cow::Borrowed(_)));
|
||||
}
|
||||
let malformed = format!("<svg><image href='data:image/png{params};base64,A'/></svg>");
|
||||
assert!(repair(&malformed, &Limits { bytes: 2 << 20, ..Default::default() }).unwrap().contains(INVALID_IMAGE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn header_compaction_has_a_shared_nonrefundable_work_charge() {
|
||||
let value = format!("data:image/png{};base64,AA==", parameters(100));
|
||||
let mut work = value.len();
|
||||
assert!(data_url_replacement(&value, &mut work).is_err());
|
||||
assert_eq!(work, 0, "the original parameter scan stays spent on refusal");
|
||||
let mut work = value.len() * 12;
|
||||
assert!(data_url_replacement(&value, &mut work).unwrap().is_some());
|
||||
assert!(work < value.len() * 12 - value.len(), "compact parse, streaming decode and XML projection are charged too");
|
||||
}
|
||||
@@ -0,0 +1,393 @@
|
||||
//! Opt-in complete image validation and checked-DOM normalization.
|
||||
//! Legacy ImageResolver entry points retain their existing behavior.
|
||||
use super::{Error, ImageUsage, Limits, Preflight, image_href, preflight, raster_format, usvg};
|
||||
use crate::svgz;
|
||||
use std::sync::{Arc, Mutex, PoisonError};
|
||||
|
||||
struct State {
|
||||
document_bytes: usize,
|
||||
pixel_limit: u64,
|
||||
pixels_left: u64,
|
||||
svg_byte_limit: usize,
|
||||
svg_bytes_left: usize,
|
||||
svg_failure_exhausted: bool,
|
||||
classification_bytes_left: usize,
|
||||
classification_content_left: usize,
|
||||
classification_text_bytes_left: usize,
|
||||
encoded_left: usize,
|
||||
invalid_raster: usize,
|
||||
invalid_svg: usize,
|
||||
raster_limits: usize,
|
||||
svg_limits: usize,
|
||||
svg_resource_limits: usize,
|
||||
root_active: bool,
|
||||
xml_depth: usize,
|
||||
depth: usize,
|
||||
svg_count: usize,
|
||||
svg_depth: usize,
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn new(limits: Limits) -> Self {
|
||||
Self {
|
||||
document_bytes: limits.bytes,
|
||||
pixel_limit: limits.embedded_raster_pixels,
|
||||
pixels_left: limits.embedded_raster_pixels,
|
||||
svg_byte_limit: limits.embedded_bytes,
|
||||
svg_bytes_left: limits.embedded_bytes,
|
||||
svg_failure_exhausted: false,
|
||||
classification_bytes_left: limits.embedded_bytes,
|
||||
classification_content_left: limits.embedded_bytes,
|
||||
classification_text_bytes_left: limits.embedded_bytes,
|
||||
encoded_left: limits.bytes.saturating_add(limits.embedded_bytes),
|
||||
invalid_raster: 0,
|
||||
invalid_svg: 0,
|
||||
raster_limits: 0,
|
||||
svg_limits: 0,
|
||||
svg_resource_limits: 0,
|
||||
root_active: false,
|
||||
xml_depth: 0,
|
||||
depth: 0,
|
||||
svg_count: 0,
|
||||
svg_depth: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn usage(&self) -> ImageUsage {
|
||||
ImageUsage {
|
||||
raster_pixels: self.pixel_limit.saturating_sub(self.pixels_left),
|
||||
embedded_svg_bytes: self.svg_byte_limit.saturating_sub(self.svg_bytes_left),
|
||||
embedded_svg_count: self.svg_count,
|
||||
embedded_svg_depth: self.svg_depth,
|
||||
invalid_raster_images: self.invalid_raster,
|
||||
raster_limit_images: self.raster_limits,
|
||||
invalid_svg_images: self.invalid_svg,
|
||||
svg_limit_images: self.svg_limits,
|
||||
svg_resource_limit_images: self.svg_resource_limits,
|
||||
}
|
||||
}
|
||||
|
||||
fn classify_after_svg_failure(&mut self, data: &[u8]) {
|
||||
if self.svg_resource_limits != 0 {
|
||||
self.svg_limits = self.svg_limits.saturating_add(1);
|
||||
self.svg_resource_limits = self.svg_resource_limits.saturating_add(1);
|
||||
return;
|
||||
}
|
||||
let compressed = data.starts_with(&[0x1f, 0x8b]);
|
||||
let content_left = self.classification_content_left;
|
||||
let limit = if compressed { content_left.min(self.classification_bytes_left) } else { content_left }.min(self.document_bytes);
|
||||
if compressed && limit == 0 {
|
||||
self.svg_limits = self.svg_limits.saturating_add(1);
|
||||
self.svg_resource_limits = self.svg_resource_limits.saturating_add(1);
|
||||
return;
|
||||
}
|
||||
let resource_refusal = match crate::svgz::inflate(data, limit) {
|
||||
Ok(bytes) => {
|
||||
if compressed {
|
||||
self.classification_bytes_left = self.classification_bytes_left.saturating_sub(bytes.len());
|
||||
}
|
||||
let Some(text_left) = self.classification_text_bytes_left.checked_sub(bytes.len()) else {
|
||||
self.svg_limits = self.svg_limits.saturating_add(1);
|
||||
self.svg_resource_limits = self.svg_resource_limits.saturating_add(1);
|
||||
return;
|
||||
};
|
||||
self.classification_text_bytes_left = text_left;
|
||||
match crate::svgz::decode(&bytes) {
|
||||
Ok(text) => text.len() > content_left || text.len() > self.document_bytes,
|
||||
Err(_) => {
|
||||
self.invalid_svg = self.invalid_svg.saturating_add(1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(crate::svgz::Error::TooLarge(_)) => {
|
||||
if compressed {
|
||||
self.classification_bytes_left = self.classification_bytes_left.saturating_sub(limit);
|
||||
}
|
||||
true
|
||||
}
|
||||
Err(error) => {
|
||||
// Nested data was fully inflated before refusal. Other failures
|
||||
// may have consumed the entire attempted inflation allowance.
|
||||
if compressed {
|
||||
let charged = match error {
|
||||
crate::svgz::Error::Nested(bytes) => bytes,
|
||||
_ => limit,
|
||||
};
|
||||
self.classification_bytes_left = self.classification_bytes_left.saturating_sub(charged);
|
||||
}
|
||||
self.invalid_svg = self.invalid_svg.saturating_add(1);
|
||||
return;
|
||||
}
|
||||
};
|
||||
self.svg_limits = self.svg_limits.saturating_add(1);
|
||||
if resource_refusal {
|
||||
self.svg_resource_limits = self.svg_resource_limits.saturating_add(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn raster(&mut self, kind: &usvg::ImageKind) -> bool {
|
||||
let encoded = match kind {
|
||||
usvg::ImageKind::SVG(_) => return true,
|
||||
usvg::ImageKind::PNG(data) | usvg::ImageKind::JPEG(data) | usvg::ImageKind::GIF(data) | usvg::ImageKind::WEBP(data) => data,
|
||||
};
|
||||
// usvg supplies a fresh Arc for every data-URL resolution, including
|
||||
// repeated <use> instances. Charge each actual validation, never retain
|
||||
// encoded buffers in an address-keyed memo that cannot hit this boundary.
|
||||
let Some(left) = self.encoded_left.checked_sub(encoded.len()) else {
|
||||
self.raster_limits = self.raster_limits.saturating_add(1);
|
||||
return false;
|
||||
};
|
||||
self.encoded_left = left;
|
||||
let result = super::draw::validate_raster(kind, self.pixel_limit, &mut self.pixels_left);
|
||||
match result {
|
||||
Ok(()) => true,
|
||||
Err(super::draw::RasterValidationFailure::Invalid) => {
|
||||
self.invalid_raster = self.invalid_raster.saturating_add(1);
|
||||
false
|
||||
}
|
||||
Err(super::draw::RasterValidationFailure::Limit) => {
|
||||
self.raster_limits = self.raster_limits.saturating_add(1);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One opt-in drawing's image work, normalization and exact rendering validation.
|
||||
///
|
||||
/// Unlike legacy ImageResolver, every attempted raster callback spends encoded
|
||||
/// and decoded work, even on failure. Rendering still decodes pixels afterwards:
|
||||
/// resvg does not expose a decoded-pixel injection or decode-result hook.
|
||||
/// All root and child conversions install this same resolver themselves.
|
||||
#[derive(Clone)]
|
||||
pub struct ValidatedImageResolver {
|
||||
limits: Limits,
|
||||
state: Arc<Mutex<State>>,
|
||||
}
|
||||
|
||||
impl ValidatedImageResolver {
|
||||
/// Create a separate budget for one drawing. Encoded raster work is bounded
|
||||
/// by root text bytes plus aggregate child SVG bytes, without retaining data.
|
||||
pub fn new(limits: Limits) -> Self {
|
||||
Self { limits, state: Arc::new(Mutex::new(State::new(limits))) }
|
||||
}
|
||||
|
||||
/// The immutable admission policy captured by this instance. Remaining work
|
||||
/// never changes these limits; use them for the matching opaque preflight.
|
||||
pub fn limits(&self) -> Limits {
|
||||
self.limits
|
||||
}
|
||||
|
||||
/// Snapshot admitted work and optional-image omissions.
|
||||
pub fn usage(&self) -> ImageUsage {
|
||||
self.state.lock().unwrap_or_else(PoisonError::into_inner).usage()
|
||||
}
|
||||
|
||||
/// Convert exactly the DOM checked under these limits, with this instance's
|
||||
/// resolver. A caller-supplied image callback cannot bypass image admission.
|
||||
/// The caller still supplies the stack/worker and font policy.
|
||||
pub fn parse_preflight(&self, checked: Preflight<'_>, mut options: usvg::Options<'_>) -> Result<usvg::Tree, Error> {
|
||||
if checked.limits != self.limits {
|
||||
return Err(Error::Parse("checked SVG limits differ from the validated image resolver".into()));
|
||||
}
|
||||
// A shallow source can expand through use/paint references. Reserve the
|
||||
// full checked converted height before any child callback, not source depth.
|
||||
let _scope = self.root_scope(checked.height)?;
|
||||
options.image_href_resolver = self.resolver();
|
||||
convert_checked(checked, &options)
|
||||
}
|
||||
|
||||
/// Resolve an already-decoded child SVG, charging original text before XML
|
||||
/// parsing or repair. This is not an unchecked/precharged admission hook.
|
||||
/// As with parse_preflight, unrelated image callbacks are replaced.
|
||||
pub fn resolve_text(&self, text: &str, mut options: usvg::Options<'_>) -> Result<Option<usvg::ImageKind>, Error> {
|
||||
let _scope = self.root_scope(0)?;
|
||||
options.image_href_resolver = self.resolver();
|
||||
Ok(self.embedded_text(text, &options))
|
||||
}
|
||||
|
||||
fn root_scope(&self, depth: usize) -> Result<Scope, Error> {
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if state.root_active {
|
||||
return Err(Error::Parse("the validated SVG resolver is already converting a drawing".into()));
|
||||
}
|
||||
let previous = state.xml_depth;
|
||||
state.root_active = true;
|
||||
state.xml_depth = depth;
|
||||
Ok(Scope { state: Arc::clone(&self.state), previous, child: false })
|
||||
}
|
||||
|
||||
fn resolver(&self) -> usvg::ImageHrefResolver<'static> {
|
||||
let this = self.clone();
|
||||
usvg::ImageHrefResolver {
|
||||
resolve_string: Box::new(|_, _| None),
|
||||
resolve_data: Box::new(move |mime, data, options| {
|
||||
let detected = raster_format(&data);
|
||||
let format = match mime {
|
||||
"image/jpg" | "image/jpeg" => (detected == Some(craft_image_codecs::Format::Jpeg)).then_some(craft_image_codecs::Format::Jpeg),
|
||||
"image/png" => (detected == Some(craft_image_codecs::Format::Png)).then_some(craft_image_codecs::Format::Png),
|
||||
"image/gif" => (detected == Some(craft_image_codecs::Format::Gif)).then_some(craft_image_codecs::Format::Gif),
|
||||
"image/webp" => (detected == Some(craft_image_codecs::Format::WebP)).then_some(craft_image_codecs::Format::WebP),
|
||||
"text/plain" => detected,
|
||||
_ => None,
|
||||
};
|
||||
if let Some(format) = format {
|
||||
let kind = match format {
|
||||
craft_image_codecs::Format::Png => usvg::ImageKind::PNG(data),
|
||||
craft_image_codecs::Format::Jpeg => usvg::ImageKind::JPEG(data),
|
||||
craft_image_codecs::Format::Gif => usvg::ImageKind::GIF(data),
|
||||
craft_image_codecs::Format::WebP => usvg::ImageKind::WEBP(data),
|
||||
_ => return None,
|
||||
};
|
||||
return this.state.lock().unwrap_or_else(PoisonError::into_inner).raster(&kind).then_some(kind);
|
||||
}
|
||||
if !matches!(mime, "image/svg+xml" | "text/plain") {
|
||||
if mime.starts_with("image/") {
|
||||
let mut state = this.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
state.invalid_raster = state.invalid_raster.saturating_add(1);
|
||||
}
|
||||
return None;
|
||||
}
|
||||
this.embedded_bytes(&data, options)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn can_start_child(&self) -> bool {
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if state.depth >= self.limits.embedded_depth {
|
||||
state.svg_limits = state.svg_limits.saturating_add(1);
|
||||
state.svg_resource_limits = state.svg_resource_limits.saturating_add(1);
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn embedded_bytes(&self, data: &[u8], options: &usvg::Options<'_>) -> Option<usvg::ImageKind> {
|
||||
if !self.can_start_child() {
|
||||
return None;
|
||||
}
|
||||
let room = {
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if state.svg_failure_exhausted {
|
||||
state.classify_after_svg_failure(data);
|
||||
return None;
|
||||
}
|
||||
state.svg_bytes_left
|
||||
};
|
||||
let text = match svgz::text(data, room.min(self.limits.bytes)) {
|
||||
Ok(text) if text.len() <= room => text,
|
||||
error => {
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
state.svg_bytes_left = 0;
|
||||
if matches!(error, Err(svgz::Error::TooLarge(_))) || error.is_ok() {
|
||||
state.svg_limits = state.svg_limits.saturating_add(1);
|
||||
state.svg_resource_limits = state.svg_resource_limits.saturating_add(1);
|
||||
} else {
|
||||
state.invalid_svg = state.invalid_svg.saturating_add(1);
|
||||
state.svg_failure_exhausted = true;
|
||||
state.classification_content_left = room;
|
||||
}
|
||||
return None;
|
||||
}
|
||||
};
|
||||
self.precharge_and_parse(&text, options)
|
||||
}
|
||||
|
||||
fn embedded_text(&self, text: &str, options: &usvg::Options<'_>) -> Option<usvg::ImageKind> {
|
||||
if !self.can_start_child() {
|
||||
return None;
|
||||
}
|
||||
self.precharge_and_parse(text, options)
|
||||
}
|
||||
|
||||
fn precharge_and_parse(&self, text: &str, options: &usvg::Options<'_>) -> Option<usvg::ImageKind> {
|
||||
{
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
let Some(left) = state.svg_bytes_left.checked_sub(text.len()) else {
|
||||
state.svg_bytes_left = 0;
|
||||
state.svg_limits = state.svg_limits.saturating_add(1);
|
||||
state.svg_resource_limits = state.svg_resource_limits.saturating_add(1);
|
||||
return None;
|
||||
};
|
||||
// Original UTF-8 bytes/work are never refunded when repair shortens
|
||||
// an attribute or conversion subsequently fails.
|
||||
state.svg_bytes_left = left;
|
||||
}
|
||||
let checked = match preflight(text, &self.limits) {
|
||||
Ok(checked) => checked,
|
||||
Err(error) => {
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
match error {
|
||||
Error::TooDeep(_) => state.svg_limits = state.svg_limits.saturating_add(1),
|
||||
Error::Text(svgz::Error::TooLarge(_)) => {
|
||||
state.svg_limits = state.svg_limits.saturating_add(1);
|
||||
state.svg_resource_limits = state.svg_resource_limits.saturating_add(1);
|
||||
}
|
||||
_ => state.invalid_svg = state.invalid_svg.saturating_add(1),
|
||||
}
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let scope = {
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
let Some(total) = state.xml_depth.checked_add(checked.height).filter(|depth| *depth <= self.limits.tree_depth) else {
|
||||
state.svg_limits = state.svg_limits.saturating_add(1);
|
||||
return None;
|
||||
};
|
||||
let previous = state.xml_depth;
|
||||
state.xml_depth = total;
|
||||
state.depth = state.depth.saturating_add(1);
|
||||
state.svg_depth = state.svg_depth.max(state.depth);
|
||||
Scope { state: Arc::clone(&self.state), previous, child: true }
|
||||
};
|
||||
let result = convert_checked(checked, options);
|
||||
drop(scope);
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
match result {
|
||||
Ok(tree) => {
|
||||
state.svg_count = state.svg_count.saturating_add(1);
|
||||
Some(usvg::ImageKind::SVG(tree))
|
||||
}
|
||||
Err(_) => {
|
||||
state.invalid_svg = state.invalid_svg.saturating_add(1);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Scope {
|
||||
state: Arc<Mutex<State>>,
|
||||
previous: usize,
|
||||
child: bool,
|
||||
}
|
||||
impl Drop for Scope {
|
||||
fn drop(&mut self) {
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
state.xml_depth = self.previous;
|
||||
if self.child {
|
||||
state.depth = state.depth.saturating_sub(1);
|
||||
} else {
|
||||
state.root_active = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_checked(checked: Preflight<'_>, options: &usvg::Options<'_>) -> Result<usvg::Tree, Error> {
|
||||
match image_href::normalize(&checked)? {
|
||||
std::borrow::Cow::Borrowed(_) => checked.parse(options),
|
||||
std::borrow::Cow::Owned(text) => {
|
||||
let limits = checked.limits;
|
||||
drop(checked);
|
||||
preflight(&text, &limits)?.parse(options)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod xml_tests;
|
||||
@@ -0,0 +1,340 @@
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing)]
|
||||
use super::*;
|
||||
use std::io::Write;
|
||||
const PIXELS: u64 = 24_000_000;
|
||||
const SVG_BYTES: usize = 16 << 20;
|
||||
|
||||
fn test_limits(pixels: u64) -> Limits {
|
||||
Limits {
|
||||
bytes: 1 << 30,
|
||||
depth: 513,
|
||||
tree_depth: 513,
|
||||
embedded_bytes: SVG_BYTES,
|
||||
embedded_depth: 32,
|
||||
embedded_raster_pixels: pixels,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
fn test_state(pixels: u64) -> State {
|
||||
State::new(test_limits(pixels))
|
||||
}
|
||||
fn wrapped_options(pixels: u64) -> (usvg::Options<'static>, Arc<Mutex<State>>, Arc<ValidatedImageResolver>) {
|
||||
let resolver = Arc::new(ValidatedImageResolver::new(test_limits(pixels)));
|
||||
let options = usvg::Options { image_href_resolver: resolver.resolver(), ..Default::default() };
|
||||
(options, Arc::clone(&resolver.state), resolver)
|
||||
}
|
||||
fn on_test_stack<T: Send>(run: impl FnOnce() -> Result<T, Error> + Send) -> Result<T, Error> {
|
||||
std::thread::scope(|scope| std::thread::Builder::new().stack_size(16 << 20).spawn_scoped(scope, run).unwrap().join().unwrap())
|
||||
}
|
||||
fn png() -> Vec<u8> {
|
||||
let image = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, [17, 83, 149].repeat(4)).unwrap();
|
||||
craft_image_codecs::encode(&image, craft_image_codecs::Format::Png, &Default::default()).unwrap()
|
||||
}
|
||||
fn data_url(mime: &str, bytes: &[u8]) -> String {
|
||||
use std::fmt::Write;
|
||||
let mut result = format!("data:{mime},");
|
||||
for byte in bytes {
|
||||
write!(&mut result, "%{byte:02X}").unwrap();
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn original_nested_xml_bytes_are_not_refunded_when_malformed_hrefs_shrink() {
|
||||
on_test_stack(|| {
|
||||
let (options, state, _resolver) = wrapped_options(PIXELS);
|
||||
let prefix = "<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><image width='1' height='1' href='data:image/png;base64,";
|
||||
let suffix = "!'/></svg>";
|
||||
let original_size = 3 << 20;
|
||||
let source = Arc::new([prefix.as_bytes(), &vec![b'A'; original_size - prefix.len() - suffix.len()], suffix.as_bytes()].concat());
|
||||
assert_eq!(source.len(), original_size);
|
||||
for accepted in 1..=5 {
|
||||
let kind = (options.image_href_resolver.resolve_data)("image/svg+xml", Arc::clone(&source), &options);
|
||||
assert!(matches!(kind, Some(usvg::ImageKind::SVG(_))), "source {accepted} fits the original-text allowance");
|
||||
drop(kind);
|
||||
let state = state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
assert_eq!(state.svg_bytes_left, SVG_BYTES - accepted * original_size);
|
||||
assert!(!state.svg_failure_exhausted);
|
||||
assert_eq!(state.classification_bytes_left, SVG_BYTES);
|
||||
let usage = state.usage();
|
||||
assert_eq!(usage.embedded_svg_bytes, accepted * original_size, "the short replacement cannot refund original XML work");
|
||||
}
|
||||
assert!((options.image_href_resolver.resolve_data)("image/svg+xml", Arc::clone(&source), &options).is_none());
|
||||
let state = state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
assert_eq!(state.svg_bytes_left, 0);
|
||||
assert_eq!((state.svg_limits, state.svg_resource_limits), (1, 1));
|
||||
assert!(!state.svg_failure_exhausted, "ordinary byte exhaustion cannot enter compatibility classification");
|
||||
assert_eq!(state.classification_bytes_left, SVG_BYTES);
|
||||
assert_eq!(state.usage().svg_resource_limit_images, 1);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_utf8_gzip_consumes_remaining_svg_work_before_the_next_child() {
|
||||
on_test_stack(|| {
|
||||
let (options, state, _resolver) = wrapped_options(PIXELS);
|
||||
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
encoder.write_all(&[0xff; 4096]).unwrap();
|
||||
let invalid = Arc::new(encoder.finish().unwrap());
|
||||
assert!((options.image_href_resolver.resolve_data)("image/svg+xml", invalid, &options).is_none());
|
||||
{
|
||||
let state = state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
assert_eq!(state.svg_bytes_left, 0, "a failed decode cannot retry the full work allowance");
|
||||
assert_eq!(state.invalid_svg, 1);
|
||||
assert!(state.svg_failure_exhausted);
|
||||
assert_eq!(state.svg_resource_limits, 0);
|
||||
assert_eq!(state.classification_bytes_left, SVG_BYTES);
|
||||
}
|
||||
let valid = Arc::new(b"<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><rect width='1' height='1'/></svg>".to_vec());
|
||||
let valid_size = valid.len();
|
||||
assert!((options.image_href_resolver.resolve_data)("image/svg+xml", valid, &options).is_none());
|
||||
let state = state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
assert_eq!((state.invalid_svg, state.svg_limits, state.svg_resource_limits), (1, 1, 0));
|
||||
assert_eq!(state.classification_bytes_left, SVG_BYTES, "plain input does not spend retry inflation work");
|
||||
let usage = state.usage();
|
||||
assert_eq!(usage.embedded_svg_bytes, SVG_BYTES);
|
||||
assert_eq!(usage.svg_resource_limit_images, 0, "a later small child is omitted, not a resource error");
|
||||
let charged = 2 * SVG_BYTES - state.svg_bytes_left - state.classification_bytes_left;
|
||||
assert_eq!(charged, SVG_BYTES);
|
||||
assert!(valid_size < SVG_BYTES);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn post_failure_classification_is_cumulative_and_never_admits_later_children() {
|
||||
on_test_stack(|| {
|
||||
let (options, state, _resolver) = wrapped_options(PIXELS);
|
||||
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
encoder.write_all(&[0xff; 256]).unwrap();
|
||||
assert!((options.image_href_resolver.resolve_data)("image/svg+xml", Arc::new(encoder.finish().unwrap()), &options).is_none());
|
||||
let text = b"<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'/>";
|
||||
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
encoder.write_all(text).unwrap();
|
||||
let valid = Arc::new(encoder.finish().unwrap());
|
||||
// Exercise the cumulative inflated-byte boundary with a small injected
|
||||
// second pool; public tests cover the full 16 MiB allowance.
|
||||
let classification_limit = text.len() * 2 - 1;
|
||||
state.lock().unwrap_or_else(PoisonError::into_inner).classification_bytes_left = classification_limit;
|
||||
assert!((options.image_href_resolver.resolve_data)("image/svg+xml", Arc::clone(&valid), &options).is_none());
|
||||
{
|
||||
let state = state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
assert_eq!(state.classification_bytes_left, text.len() - 1);
|
||||
assert_eq!((state.svg_limits, state.svg_resource_limits), (1, 0));
|
||||
}
|
||||
assert!((options.image_href_resolver.resolve_data)("image/svg+xml", Arc::clone(&valid), &options).is_none());
|
||||
let state = state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
assert_eq!(state.classification_bytes_left, 0);
|
||||
assert_eq!((state.svg_limits, state.svg_resource_limits), (2, 1));
|
||||
assert_eq!(state.svg_bytes_left, 0, "classification never refunds original failed work");
|
||||
assert!(state.svg_failure_exhausted);
|
||||
let charged = 2 * SVG_BYTES - state.svg_bytes_left - state.classification_bytes_left;
|
||||
assert_eq!(charged, 2 * SVG_BYTES, "original content plus aggregate retry inflation allowances remain bounded");
|
||||
assert_eq!(state.usage().embedded_svg_count, 0, "classified children must never reach the shared parser");
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retry_inflation_room_and_saved_decoded_content_room_remain_independent() {
|
||||
on_test_stack(|| {
|
||||
let mut latin1 = b"<?xml version='1.0' encoding='ISO-8859-1'?><svg xmlns='http://www.w3.org/2000/svg'><!--".to_vec();
|
||||
latin1.extend([0xe9; 100]);
|
||||
latin1.extend(b"--></svg>");
|
||||
let decoded_len = crate::svgz::decode(&latin1).unwrap().len();
|
||||
assert!(latin1.len() < 256 && decoded_len > 256 && decoded_len < 512);
|
||||
let gzip = |bytes: &[u8]| {
|
||||
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
encoder.write_all(bytes).unwrap();
|
||||
Arc::new(encoder.finish().unwrap())
|
||||
};
|
||||
for (content_room, expected_resource) in [(512usize, false), (256, true)] {
|
||||
let (options, state, _resolver) = wrapped_options(PIXELS);
|
||||
// Model preceding genuine content consumption before an unreadable
|
||||
// stream burns the rest. Classification must save this exact room.
|
||||
state.lock().unwrap_or_else(PoisonError::into_inner).svg_bytes_left = content_room;
|
||||
assert!((options.image_href_resolver.resolve_data)("image/svg+xml", gzip(&[0xff; 4]), &options).is_none());
|
||||
{
|
||||
let mut state = state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
assert_eq!(state.classification_content_left, content_room);
|
||||
assert_eq!(state.svg_bytes_left, 0);
|
||||
// Enough compressed retry work for the raw Latin-1 bytes, but
|
||||
// less than their decoded UTF-8 length, just like main's case.
|
||||
state.classification_bytes_left = latin1.len() + 1;
|
||||
}
|
||||
assert!((options.image_href_resolver.resolve_data)("image/svg+xml", gzip(&latin1), &options).is_none());
|
||||
let state = state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
assert_eq!(state.classification_bytes_left, 1, "retry work charges inflated bytes, not UTF-8 bytes");
|
||||
assert_eq!(state.svg_resource_limits, usize::from(expected_resource));
|
||||
assert_eq!(state.svg_bytes_left, 0, "classification never restores original content allowance");
|
||||
assert_eq!(state.usage().embedded_svg_count, 0, "no classified child is parsed or drawn");
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_raster_callbacks_release_sources_and_keep_every_decode_charge() {
|
||||
let mut broken = png();
|
||||
let idat = broken.windows(4).position(|bytes| bytes == b"IDAT").unwrap();
|
||||
broken[idat + 4] ^= 1;
|
||||
assert_eq!(craft_image_codecs::probe_allocation_size(&broken).unwrap(), (2, 2));
|
||||
let mut state = test_state(8);
|
||||
let encoded_before = state.encoded_left;
|
||||
for attempt in 1..=2 {
|
||||
// Match usvg's actual callback boundary: each href gets a new Arc.
|
||||
let encoded = Arc::new(broken.clone());
|
||||
let weak = Arc::downgrade(&encoded);
|
||||
let kind = usvg::ImageKind::PNG(Arc::clone(&encoded));
|
||||
assert!(!state.raster(&kind));
|
||||
assert_eq!(state.pixels_left, 8 - attempt * 4);
|
||||
assert_eq!(state.encoded_left, encoded_before - attempt as usize * encoded.len());
|
||||
assert_eq!(state.invalid_raster, attempt as usize);
|
||||
drop(kind);
|
||||
drop(encoded);
|
||||
assert!(weak.upgrade().is_none(), "validation must not retain failed callback buffers until parse completion");
|
||||
}
|
||||
assert!(!state.raster(&usvg::ImageKind::PNG(Arc::new(png()))));
|
||||
assert_eq!((state.invalid_raster, state.raster_limits), (2, 1));
|
||||
assert_eq!(state.usage().raster_pixels, 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_raster_callbacks_spend_exact_pixel_and_encoded_allowances_per_call() {
|
||||
let encoded = Arc::new(png());
|
||||
let kind = usvg::ImageKind::PNG(Arc::clone(&encoded));
|
||||
let mut state = test_state(8);
|
||||
state.encoded_left = encoded.len() * 2;
|
||||
assert!(state.raster(&kind));
|
||||
assert_eq!((state.pixels_left, state.encoded_left), (4, encoded.len()));
|
||||
assert!(state.raster(&kind), "even a directly reused Arc is a new actual validation call");
|
||||
assert_eq!((state.pixels_left, state.encoded_left), (0, 0));
|
||||
assert!(!state.raster(&kind));
|
||||
assert_eq!((state.invalid_raster, state.raster_limits), (0, 1));
|
||||
let usage = state.usage();
|
||||
assert_eq!((usage.raster_pixels, usage.raster_limit_images), (8, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raster_callbacks_own_per_call_pixel_charges_and_keep_mime_aliases_separate() {
|
||||
let png = Arc::new(png());
|
||||
let (options, state, resolver) = wrapped_options(4);
|
||||
assert!((options.image_href_resolver.resolve_data)("image/png", Arc::clone(&png), &options).is_some());
|
||||
assert!((options.image_href_resolver.resolve_data)("image/png", Arc::clone(&png), &options).is_none());
|
||||
assert_eq!((resolver.usage().raster_pixels, resolver.usage().invalid_raster_images, resolver.usage().raster_limit_images), (4, 0, 1));
|
||||
assert!((options.image_href_resolver.resolve_data)("image/jpeg", png, &options).is_none());
|
||||
let usage = state.lock().unwrap().usage();
|
||||
assert_eq!((usage.raster_pixels, usage.invalid_raster_images), (4, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoded_raster_refusal_is_per_image_and_does_not_consume_unattempted_work() {
|
||||
let bytes = png();
|
||||
let mut oversized = bytes.clone();
|
||||
oversized.extend([0; 32]);
|
||||
let mut state = test_state(4);
|
||||
state.encoded_left = bytes.len();
|
||||
assert!(!state.raster(&usvg::ImageKind::PNG(Arc::new(oversized))));
|
||||
assert_eq!((state.encoded_left, state.pixels_left), (bytes.len(), 4));
|
||||
assert!(state.raster(&usvg::ImageKind::PNG(Arc::new(bytes))));
|
||||
assert_eq!((state.encoded_left, state.pixels_left), (0, 0));
|
||||
let usage = state.usage();
|
||||
assert_eq!((usage.raster_pixels, usage.raster_limit_images, usage.invalid_raster_images), (4, 1, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encoded_raster_exhaustion_keeps_ordinary_drawing_and_reports_only_omitted_images() {
|
||||
let bytes = png();
|
||||
let href = data_url("image/png", &bytes);
|
||||
let text = format!(
|
||||
r##"<svg xmlns="http://www.w3.org/2000/svg" width="4" height="2"><rect width="4" height="2" fill="#00ff00"/><image width="2" height="2" href="{href}"/><image x="2" width="2" height="2" href="{href}"/></svg>"##
|
||||
);
|
||||
let resolver = ValidatedImageResolver::new(test_limits(8));
|
||||
resolver.state.lock().unwrap().encoded_left = bytes.len();
|
||||
let tree = resolver.parse_preflight(preflight(&text, &resolver.limits()).unwrap(), usvg::Options::default()).unwrap();
|
||||
let usage = resolver.usage();
|
||||
assert_eq!((usage.raster_pixels, usage.raster_limit_images, usage.invalid_raster_images), (4, 1, 0));
|
||||
let output = super::super::draw::render(&tree, usvg::Transform::identity(), 4, 2, 0, 1 << 20).unwrap();
|
||||
let background = output.pixel(3, 1).unwrap().demultiply();
|
||||
assert_eq!((background.red(), background.green(), background.blue(), background.alpha()), (0, 255, 0, 255));
|
||||
let pixel = output.pixel(0, 0).unwrap().demultiply();
|
||||
assert_eq!((pixel.red(), pixel.green(), pixel.blue(), pixel.alpha()), (17, 83, 149, 255));
|
||||
assert_eq!(resolver.state.lock().unwrap().encoded_left, 0);
|
||||
}
|
||||
|
||||
// Preserve upstream's text_rechecked boundary using the integrated real resolver.
|
||||
// Plain and gzip retries spend source bytes before decoding, independently of
|
||||
// inflation work and the drawing's genuine decoded-content capacity.
|
||||
#[test]
|
||||
fn plain_legacy_encoding_rechecks_have_a_separate_bounded_work_allowance() {
|
||||
on_test_stack(|| {
|
||||
let xml = b"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><svg><!--\xe9--></svg>";
|
||||
for compressed in [false, true] {
|
||||
let (options, state, _resolver) = wrapped_options(PIXELS);
|
||||
let encode = |bytes: &[u8]| {
|
||||
if compressed {
|
||||
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
encoder.write_all(bytes).unwrap();
|
||||
encoder.finish().unwrap()
|
||||
} else {
|
||||
bytes.to_vec()
|
||||
}
|
||||
};
|
||||
assert!((options.image_href_resolver.resolve_data)("image/svg+xml", Arc::new(encode(&[0xff])), &options).is_none());
|
||||
state.lock().unwrap_or_else(PoisonError::into_inner).classification_text_bytes_left = xml.len();
|
||||
assert!((options.image_href_resolver.resolve_data)("image/svg+xml", Arc::new(encode(xml)), &options).is_none());
|
||||
{
|
||||
let state = state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
assert_eq!(state.classification_text_bytes_left, 0);
|
||||
assert_eq!(state.svg_resource_limits, 0, "exactly fitting text recheck is permitted");
|
||||
assert_eq!(state.classification_bytes_left, SVG_BYTES - usize::from(compressed) * xml.len());
|
||||
}
|
||||
assert!((options.image_href_resolver.resolve_data)("image/svg+xml", Arc::new(encode(xml)), &options).is_none());
|
||||
let state = state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
assert_eq!(state.classification_text_bytes_left, 0);
|
||||
assert_eq!(state.svg_resource_limits, 1, "the next source cannot repeat text-conversion work");
|
||||
assert_eq!(state.invalid_svg, 1, "the original encoding error stays invalid");
|
||||
assert_eq!(state.svg_limits, 2);
|
||||
assert_eq!(state.usage().embedded_svg_count, 0, "rechecked children are never admitted");
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn embedded_gzip_obeys_document_bytes_before_aggregate_inflation_room() {
|
||||
on_test_stack(|| {
|
||||
let mut text = b"<svg xmlns='http://www.w3.org/2000/svg' width='1' height='1'><!--".to_vec();
|
||||
text.resize(256 - b"--></svg>".len(), b' ');
|
||||
text.extend_from_slice(b"--></svg>");
|
||||
assert_eq!(text.len(), 256);
|
||||
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
encoder.write_all(&text).unwrap();
|
||||
let encoded = Arc::new(encoder.finish().unwrap());
|
||||
for bytes in [text.len() - 1, text.len()] {
|
||||
let limits = Limits { bytes, embedded_bytes: 1024, ..test_limits(4) };
|
||||
assert!(limits.embedded_bytes > text.len());
|
||||
let resolver = ValidatedImageResolver::new(limits);
|
||||
let options = usvg::Options { image_href_resolver: resolver.resolver(), ..Default::default() };
|
||||
let image = (options.image_href_resolver.resolve_data)("image/svg+xml", Arc::clone(&encoded), &options);
|
||||
let usage = resolver.usage();
|
||||
if bytes == text.len() {
|
||||
assert!(matches!(image, Some(usvg::ImageKind::SVG(_))), "exact document boundary remains admissible");
|
||||
assert_eq!((usage.embedded_svg_count, usage.embedded_svg_bytes), (1, text.len()));
|
||||
assert_eq!((usage.svg_resource_limit_images, usage.invalid_svg_images), (0, 0));
|
||||
} else {
|
||||
assert!(image.is_none(), "aggregate room must not lift one document's inflate limit");
|
||||
assert_eq!((usage.embedded_svg_count, usage.svg_resource_limit_images, usage.invalid_svg_images), (0, 1, 0));
|
||||
assert_eq!(usage.embedded_svg_bytes, 1024, "refused work retains its aggregate charge");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
//! Public validated-context contracts; private byte/raster accounting lives in tests.rs.
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing)]
|
||||
use super::super::{Limits, PREFLIGHT_PASSES, preflight, usvg};
|
||||
use super::ValidatedImageResolver;
|
||||
use std::io::Write;
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
};
|
||||
|
||||
const NS: &str = "http://www.w3.org/2000/svg";
|
||||
|
||||
fn drawing(contents: &str) -> String {
|
||||
format!("<svg xmlns='{NS}' width='4' height='4'>{contents}</svg>")
|
||||
}
|
||||
|
||||
fn url(bytes: &[u8]) -> String {
|
||||
use std::fmt::Write;
|
||||
let mut value = String::from("data:image/svg+xml,");
|
||||
for byte in bytes {
|
||||
write!(&mut value, "%{byte:02X}").unwrap();
|
||||
}
|
||||
value
|
||||
}
|
||||
|
||||
fn gzip(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
|
||||
encoder.write_all(bytes).unwrap();
|
||||
encoder.finish().unwrap()
|
||||
}
|
||||
|
||||
fn image(href: &str) -> String {
|
||||
format!("<image width='4' height='4' href='{href}'/>")
|
||||
}
|
||||
|
||||
fn parse_count(text: &str, limits: Limits) -> (super::super::ImageUsage, usize) {
|
||||
PREFLIGHT_PASSES.with(|count| count.set(0));
|
||||
let context = ValidatedImageResolver::new(limits);
|
||||
let checked = preflight(text, &limits).unwrap();
|
||||
let tree = context.parse_preflight(checked, usvg::Options::default()).unwrap();
|
||||
assert!(!tree.root().children().is_empty(), "the visible drawing survives validation");
|
||||
(context.usage(), PREFLIGHT_PASSES.with(|count| count.get()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validated_root_reuses_checked_xml_and_rechecks_only_changed_attributes() {
|
||||
let ordinary = drawing("<rect width='4' height='4' fill='red'/>");
|
||||
let repaired = drawing("<rect width='4' height='4' fill='red'/><image width='1' height='1' href='data:image/png;base64,A'/>");
|
||||
let (usage, count) = parse_count(&ordinary, Limits::default());
|
||||
assert_eq!(count, 1);
|
||||
assert_eq!(usage.invalid_raster_images, 0);
|
||||
let (usage, count) = parse_count(&repaired, Limits::default());
|
||||
assert_eq!(count, 2);
|
||||
assert_eq!(usage.invalid_raster_images, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validated_plain_and_gzip_children_reuse_their_original_preflight() {
|
||||
for repaired in [false, true] {
|
||||
let mut contents = String::from("<rect width='4' height='4' fill='red'/>");
|
||||
if repaired {
|
||||
contents.push_str("<image width='1' height='1' href='data:image/png;base64,A'/>");
|
||||
}
|
||||
let child = drawing(&contents);
|
||||
for compressed in [false, true] {
|
||||
let encoded = if compressed { gzip(child.as_bytes()) } else { child.as_bytes().to_vec() };
|
||||
let root = drawing(&image(&url(&encoded)));
|
||||
let (usage, count) = parse_count(&root, Limits::default());
|
||||
assert_eq!(count, if repaired { 3 } else { 2 }, "compressed={compressed}, repaired={repaired}");
|
||||
assert_eq!(usage.embedded_svg_count, 1);
|
||||
assert_eq!(usage.embedded_svg_bytes, child.len());
|
||||
assert_eq!(usage.invalid_raster_images, usize::from(repaired));
|
||||
assert_eq!(usage.invalid_svg_images, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validated_original_xml_guards_precede_normalization_and_conversion() {
|
||||
let normal = drawing("<image href='data:image/png;base64,A'/>");
|
||||
let entity = format!("<!DOCTYPE svg [<!ENTITY a 'data:image/png;base64,A'>]>{}", drawing("<image width='1' height='1' href='&a;'/>"));
|
||||
let markup = format!("<!DOCTYPE svg [<!ENTITY a '<g/>'>]>{}", drawing("&a;"));
|
||||
let referenced = drawing("<defs><g id='a'><g id='b'><rect/></g></g></defs><use href='#a'/>");
|
||||
for (text, limits) in [
|
||||
(&normal, Limits { bytes: normal.len() - 1, ..Default::default() }),
|
||||
(&normal, Limits { attributes: 0, ..Default::default() }),
|
||||
(&normal, Limits { xml_nodes: 1, ..Default::default() }),
|
||||
(&entity, Limits { entity_bytes: 1, ..Default::default() }),
|
||||
(&markup, Limits { refuse_entity_markup: true, ..Default::default() }),
|
||||
(&referenced, Limits { tree_depth: 2, ..Default::default() }),
|
||||
] {
|
||||
assert!(preflight(text, &limits).is_err(), "original source must be refused before normalization: {text}");
|
||||
}
|
||||
let limits = Limits { entity_bytes: 1024, ..Default::default() };
|
||||
let context = ValidatedImageResolver::new(limits);
|
||||
let checked = preflight(&entity, &limits).unwrap();
|
||||
context.parse_preflight(checked, usvg::Options::default()).unwrap();
|
||||
assert_eq!(context.usage().invalid_raster_images, 1, "admitted entity data reaches omission accounting");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validated_context_requires_exact_preflight_limits() {
|
||||
let text = drawing("<rect width='1' height='1'/>");
|
||||
let limits = Limits { bytes: 4096, tree_depth: 16, embedded_bytes: 2048, ..Default::default() };
|
||||
let context = ValidatedImageResolver::new(limits);
|
||||
assert_eq!(context.limits(), limits);
|
||||
for changed in [
|
||||
Limits { bytes: 4095, ..limits },
|
||||
Limits { bytes: 4097, ..limits },
|
||||
Limits { tree_depth: 15, ..limits },
|
||||
Limits { embedded_bytes: 2049, ..limits },
|
||||
] {
|
||||
let checked = preflight(&text, &changed).unwrap();
|
||||
assert!(context.parse_preflight(checked, usvg::Options::default()).is_err());
|
||||
assert_eq!(context.usage(), Default::default(), "policy mismatch must not start conversion");
|
||||
}
|
||||
context.parse_preflight(preflight(&text, &limits).unwrap(), usvg::Options::default()).unwrap();
|
||||
}
|
||||
|
||||
fn hostile_options(calls: &Arc<AtomicUsize>) -> usvg::Options<'static> {
|
||||
let string_calls = Arc::clone(calls);
|
||||
let data_calls = Arc::clone(calls);
|
||||
usvg::Options {
|
||||
image_href_resolver: usvg::ImageHrefResolver {
|
||||
resolve_string: Box::new(move |_, _| {
|
||||
string_calls.fetch_add(1, Ordering::SeqCst);
|
||||
panic!("ambient string resolver must not run")
|
||||
}),
|
||||
resolve_data: Box::new(move |_, _, _| {
|
||||
data_calls.fetch_add(1, Ordering::SeqCst);
|
||||
panic!("caller data resolver must not bypass validation")
|
||||
}),
|
||||
},
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validated_root_and_nested_context_override_untrusted_image_callbacks() {
|
||||
let calls = Arc::new(AtomicUsize::new(0));
|
||||
let child = drawing(
|
||||
"<rect width='1' height='1'/><image href='file:///never-read-by-validation.png' width='1' height='1'/><image href='data:image/png,%00' width='1' height='1'/>",
|
||||
);
|
||||
let root = drawing(&format!("<rect width='1' height='1'/>{}", image(&url(child.as_bytes()))));
|
||||
let limits = Limits::default();
|
||||
let context = ValidatedImageResolver::new(limits);
|
||||
context.parse_preflight(preflight(&root, &limits).unwrap(), hostile_options(&calls)).unwrap();
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 0);
|
||||
assert_eq!(context.usage().embedded_svg_count, 1);
|
||||
assert_eq!(context.usage().invalid_raster_images, 1);
|
||||
let direct = ValidatedImageResolver::new(limits);
|
||||
assert!(matches!(direct.resolve_text(&child, hostile_options(&calls)).unwrap(), Some(usvg::ImageKind::SVG(_))));
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 0);
|
||||
assert_eq!(direct.usage().invalid_raster_images, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expanded_root_references_and_child_share_the_converted_depth_limit() {
|
||||
let child = drawing("<g><rect width='1' height='1'/></g>"); // height 3
|
||||
let href = url(child.as_bytes());
|
||||
let root = drawing(&format!("<defs><g id='a'><use href='#b'/></g><g id='b'>{}</g></defs><use href='#a'/>", image(&href))); // height 6
|
||||
assert!(preflight(&root, &Limits { tree_depth: 5, ..Default::default() }).is_err());
|
||||
assert!(preflight(&root, &Limits { tree_depth: 6, ..Default::default() }).is_ok());
|
||||
assert!(preflight(&child, &Limits { tree_depth: 3, ..Default::default() }).is_ok());
|
||||
for (depth, admitted) in [(8, false), (9, true)] {
|
||||
let limits = Limits { tree_depth: depth, ..Default::default() };
|
||||
let checked = preflight(&root, &limits).unwrap();
|
||||
assert!(checked.depth() < 6, "source nesting is shallower than expanded references");
|
||||
let context = ValidatedImageResolver::new(limits);
|
||||
context.parse_preflight(checked, usvg::Options::default()).unwrap();
|
||||
let usage = context.usage();
|
||||
assert_eq!(usage.embedded_svg_count, usize::from(admitted));
|
||||
assert_eq!(usage.svg_limit_images, usize::from(!admitted));
|
||||
assert_eq!(usage.invalid_svg_images, 0);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user