mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 08:15:29 +00:00
Merge pull request #2041 from Terra854/codex/svg-integrity-hooks-review21
Share checked SVG image validation and DOM reuse
This commit is contained in:
17 files changed
+2425
-77
No files matched your search
Generated
+3
@@ -3177,11 +3177,14 @@ 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",
|
||||
|
||||
@@ -46,6 +46,45 @@ pub(crate) struct JpegMeta {
|
||||
pub held_coefficients: u64,
|
||||
}
|
||||
|
||||
/// Validate a JPEG by decoding the 8-bit RGBA output used by renderers.
|
||||
///
|
||||
/// This deliberately uses the existing zune backend's default decode policy.
|
||||
/// Unlike general JPEG import it does not fall back to additional JPEG modes,
|
||||
/// apply orientation, or retain pixels. The caller supplies the admitted pixel count.
|
||||
pub fn validate_jpeg_rgba8(bytes: &[u8], max_pixels: u64) -> Result<(), CodecError> {
|
||||
validate_jpeg_rgba8_with_panic_policy(bytes, max_pixels, cfg!(panic = "abort"))
|
||||
}
|
||||
|
||||
fn validate_jpeg_rgba8_with_panic_policy(bytes: &[u8], max_pixels: u64, panic_aborts: bool) -> Result<(), CodecError> {
|
||||
// The decoder can assert while upsampling these frames. On abort builds the
|
||||
// unwind guard below cannot turn that assertion into an image omission.
|
||||
if panic_aborts && first_component_subsampled(bytes) {
|
||||
return Err(CodecError::malformed(F, "JPEG first component is subsampled below the frame maximum"));
|
||||
}
|
||||
let decode = || {
|
||||
let options = DecoderOptions::default().jpeg_set_out_colorspace(ColorSpace::RGBA);
|
||||
let mut decoder = zune_jpeg::JpegDecoder::new_with_options(std::io::Cursor::new(bytes), options);
|
||||
decoder.decode_headers().map_err(|error| CodecError::malformed(F, format_args!("{error:?}")))?;
|
||||
let info = decoder.info().ok_or_else(|| CodecError::malformed(F, "missing frame dimensions"))?;
|
||||
let pixels = u64::from(info.width) * u64::from(info.height);
|
||||
if pixels == 0 {
|
||||
return Err(CodecError::malformed(F, "empty frame"));
|
||||
}
|
||||
if pixels > max_pixels {
|
||||
return Err(CodecError::LimitExceeded("JPEG exceeds the admitted pixel count".into()));
|
||||
}
|
||||
let length = usize::try_from(pixels * 4).map_err(|_| CodecError::LimitExceeded("JPEG RGBA output is too large".into()))?;
|
||||
if decoder.output_colorspace() != Some(ColorSpace::RGBA) || decoder.output_buffer_size() != Some(length) {
|
||||
return Err(CodecError::malformed(F, "unexpected RGBA output"));
|
||||
}
|
||||
let mut output = Vec::new();
|
||||
output.try_reserve_exact(length).map_err(|_| CodecError::LimitExceeded("could not allocate JPEG RGBA output".into()))?;
|
||||
output.resize(length, 0);
|
||||
decoder.decode_into(&mut output).map_err(|error| CodecError::malformed(F, format_args!("{error:?}")))
|
||||
};
|
||||
std::panic::catch_unwind(decode).map_err(|_| CodecError::malformed(F, "JPEG decoder failed on this frame"))?
|
||||
}
|
||||
|
||||
/// The first frame of `b` as [`crate::JpegInfo`], from its markers alone (see [`crate::jpeg_info`]).
|
||||
pub(crate) fn info(b: &[u8]) -> Option<crate::JpegInfo> {
|
||||
let m = scan_metadata(b);
|
||||
@@ -417,10 +456,17 @@ fn first_component_subsampled(b: &[u8]) -> bool {
|
||||
return false;
|
||||
}
|
||||
let mut i = 2;
|
||||
while let (Some(&0xFF), Some(&m)) = (b.get(i), b.get(i + 1)) {
|
||||
while let Some(&prefix) = b.get(i) {
|
||||
// The decoder searches forward between segments. Stray bytes and
|
||||
// stuffed FF 00 pairs must not hide a frame from the abort-build guard.
|
||||
if prefix != 0xFF {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
let Some(&m) = b.get(i + 1) else { return false };
|
||||
match m {
|
||||
0xFF => i += 1,
|
||||
0x01 | 0xD0..=0xD8 => i += 2,
|
||||
0x00 | 0x01 | 0xD0..=0xD8 => i += 2,
|
||||
0xD9 | 0xDA => return false,
|
||||
_ => {
|
||||
let Some(len) = b.get(i + 2..i + 4).map(|l| usize::from(u16::from_be_bytes([l[0], l[1]]))) else { return false };
|
||||
@@ -1314,25 +1360,61 @@ mod tests {
|
||||
(v * 65535.0 / 4095.0).round() as u16
|
||||
}
|
||||
|
||||
fn progressive_frame_header(comps: &[(u8, u8)]) -> Vec<u8> {
|
||||
let mut s = vec![0xFF, 0xD8, 0xFF, 0xE0, 0, 4, 0, 0, 0xFF, 0xC2, 0, (8 + 3 * comps.len()) as u8, 8, 0, 8, 0, 8, comps.len() as u8];
|
||||
for (i, &(h, v)) in comps.iter().enumerate() {
|
||||
s.extend([i as u8 + 1, (h << 4) | v, 0]);
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn first_component_subsampling() {
|
||||
let sof = |comps: &[(u8, u8)]| {
|
||||
let mut s = vec![0xFF, 0xD8, 0xFF, 0xE0, 0, 4, 0, 0, 0xFF, 0xC2, 0, (8 + 3 * comps.len()) as u8, 8, 0, 8, 0, 8, comps.len() as u8];
|
||||
for (i, &(h, v)) in comps.iter().enumerate() {
|
||||
s.extend([i as u8 + 1, (h << 4) | v, 0]);
|
||||
}
|
||||
s
|
||||
};
|
||||
assert!(first_component_subsampled(&sof(&[(1, 1), (1, 1), (1, 1), (2, 2)])));
|
||||
assert!(first_component_subsampled(&sof(&[(1, 2), (1, 2), (2, 1)])));
|
||||
assert!(!first_component_subsampled(&sof(&[(2, 2), (1, 1), (1, 1)])));
|
||||
assert!(!first_component_subsampled(&sof(&[(1, 1); 4])));
|
||||
assert!(!first_component_subsampled(&sof(&[])));
|
||||
assert!(first_component_subsampled(&progressive_frame_header(&[(1, 1), (1, 1), (1, 1), (2, 2)])));
|
||||
assert!(first_component_subsampled(&progressive_frame_header(&[(1, 2), (1, 2), (2, 1)])));
|
||||
assert!(!first_component_subsampled(&progressive_frame_header(&[(2, 2), (1, 1), (1, 1)])));
|
||||
assert!(!first_component_subsampled(&progressive_frame_header(&[(1, 1); 4])));
|
||||
assert!(!first_component_subsampled(&progressive_frame_header(&[])));
|
||||
assert!(!first_component_subsampled(&[0xFF, 0xD8, 0xFF, 0xDA, 0, 2]));
|
||||
assert!(!first_component_subsampled(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 20, 8]));
|
||||
assert!(!first_component_subsampled(b"not a jpeg"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renderer_validation_refuses_unsafe_sampling_under_abort_policy() {
|
||||
// The frame header alone exercises refusal before any pixel decode.
|
||||
let header = progressive_frame_header(&[(1, 1), (1, 1), (1, 1), (2, 2)]);
|
||||
let refuses_sampling = |result| {
|
||||
matches!(
|
||||
result,
|
||||
Err(CodecError::Malformed { format: Format::Jpeg, message })
|
||||
if message == "JPEG first component is subsampled below the frame maximum"
|
||||
)
|
||||
};
|
||||
assert!(refuses_sampling(validate_jpeg_rgba8_with_panic_policy(&header, 64, true)));
|
||||
assert!(!refuses_sampling(validate_jpeg_rgba8_with_panic_policy(&header, 64, false)));
|
||||
|
||||
let mut ordinary = Vec::new();
|
||||
jpeg_encoder::Encoder::new(&mut ordinary, 90).encode(&[128], 1, 1, jpeg_encoder::ColorType::Luma).unwrap();
|
||||
validate_jpeg_rgba8_with_panic_policy(&ordinary, 1, true).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jpeg_sampling_guard_finds_frame_after_marker_padding() {
|
||||
let header = progressive_frame_header(&[(1, 1), (1, 1), (1, 1), (2, 2)]);
|
||||
// Exercise the shared header guard, without a pixel payload.
|
||||
for split in [2, 8] {
|
||||
let (before, after) = header.split_at(split);
|
||||
for padding in [&[0][..], &[0x7F, 0][..], &[0xFF, 0][..], &[0xFF][..]] {
|
||||
let padded = [before, padding, after].concat();
|
||||
assert!(first_component_subsampled(&padded), "offset {split}, padding {padding:?}");
|
||||
let got = validate_jpeg_rgba8_with_panic_policy(&padded, 64, true);
|
||||
assert!(matches!(got, Err(CodecError::Malformed { message, .. }) if message.contains("frame maximum")));
|
||||
}
|
||||
}
|
||||
assert!(!first_component_subsampled(&[0xFF, 0xD8, 0, 0xFF]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn routing() {
|
||||
assert!(!uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0]));
|
||||
|
||||
@@ -45,6 +45,7 @@ pub use crate::cicp::ContentLightLevel;
|
||||
pub use crate::codecs::avif::{HDR_REFERENCE_WHITE_NITS, HdrTransfer, encode_avif_hdr, encode_avif_with_speed};
|
||||
pub use crate::codecs::exr_cryptomatte::{cryptomatte_id, cryptomatte_key, cryptomatte_preview_color};
|
||||
pub use crate::codecs::gif_frames::{AnimationFrame, MAX_GIF_FRAMES};
|
||||
pub use crate::codecs::jpeg::validate_jpeg_rgba8;
|
||||
pub use crate::codecs::png::{
|
||||
PngDecodeRows, PngExtras, PngHeader, PngPalette, PngRowSink, PngRows, SharedBytes, encode_indexed as encode_png_indexed,
|
||||
encode_pq as encode_png_pq, palette as png_palette, stereo as png_stereo,
|
||||
|
||||
+10
-1
@@ -31,6 +31,11 @@ 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.
|
||||
# JPEG validation uses the existing backend in craft-image-codecs.
|
||||
gif = { version = "=0.14.2", optional = true }
|
||||
image-webp = { version = "=0.2.4", optional = true }
|
||||
|
||||
|
||||
[features]
|
||||
default = ["paths"]
|
||||
@@ -45,5 +50,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: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,59 @@ 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
|
||||
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.
|
||||
Expanded root ancestor frames (checked height minus the callback leaf) and each
|
||||
child's complete expanded height share one depth allowance. The callback leaf is
|
||||
not counted twice; source nesting alone cannot hide expanded reference chains.
|
||||
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. The normal path counts only resolved images. If a repair cannot fit,
|
||||
the checked original can still convert when all its MIME headers meet the work
|
||||
limit. In that case malformed image declarations are counted conservatively.
|
||||
Original UTF-8 child bytes are charged before XML conversion or shorter repairs,
|
||||
with per-document and aggregate bounds. A malformed plain encoding spends its
|
||||
input bytes and leaves the remaining work for siblings. Failed inflation can
|
||||
exhaust its attempted allowance, and the separate bounded retry classification
|
||||
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 validator uses the existing JPEG backend in craft-image-codecs. The lockfile
|
||||
test checks that it and the other validation backends match the selected resvg
|
||||
dependency edges. resvg decodes the image again when it paints.
|
||||
@@ -8,7 +8,7 @@
|
||||
//! - `paths` (feature `paths`, on by default): the geometry of a document as Bézier paths in
|
||||
//! millimetres, for cutters, plotters and CAD import.
|
||||
//! - [`color`]: CSS colours.
|
||||
//! - [`write`]: a writer for SVG documents, their path data and numbers.
|
||||
//! - [`mod@write`]: a writer for SVG documents, their path data and numbers.
|
||||
//! - `tree` (feature `tree`): the full parser. usvg at the one version the Craft apps share,
|
||||
//! re-exported, behind an entry point that runs the checks above before it parses.
|
||||
//!
|
||||
|
||||
+110
-58
@@ -22,10 +22,10 @@
|
||||
//! 16 MB or so, and a 1 MB stack takes a depth of about 34.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex, PoisonError};
|
||||
use std::sync::{Arc, Mutex, PoisonError, Weak};
|
||||
|
||||
pub use usvg;
|
||||
|
||||
@@ -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).
|
||||
@@ -64,9 +70,9 @@ pub struct Limits {
|
||||
pub embedded_bytes: usize,
|
||||
/// SVG documents embedded inside one another, for [`image_resolver`].
|
||||
pub embedded_depth: usize,
|
||||
/// Decoded RGBA pixels retained across the raster images embedded in one drawing. Dimensions
|
||||
/// are probed before usvg hands the bytes to a decoder; repeated references to the same data
|
||||
/// URL share one charge.
|
||||
/// Cumulative raster decode pixels for a drawing. usvg sends each data-URL
|
||||
/// resolution separately, so repeated uses normally spend a separate charge.
|
||||
/// The legacy resolver can share a charge for the same live input allocation.
|
||||
pub embedded_raster_pixels: u64,
|
||||
}
|
||||
|
||||
@@ -161,6 +167,8 @@ 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> {
|
||||
#[cfg(feature = "tree-render")]
|
||||
limits: Limits,
|
||||
xml: roxmltree::Document<'s>,
|
||||
depth: usize,
|
||||
/// [`height`]: the nesting usvg converts, references followed.
|
||||
@@ -219,6 +227,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 +249,13 @@ 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 {
|
||||
#[cfg(feature = "tree-render")]
|
||||
limits: *limits,
|
||||
xml,
|
||||
depth,
|
||||
height,
|
||||
})
|
||||
}
|
||||
|
||||
/// Dimensions of an SVG viewport in SVG user units.
|
||||
@@ -421,7 +437,7 @@ struct Used {
|
||||
svg_count: AtomicUsize,
|
||||
svg_depth: AtomicUsize,
|
||||
raster_pixels: AtomicU64,
|
||||
raster_data: Mutex<HashSet<usize>>,
|
||||
raster_data: Mutex<HashMap<usize, Weak<Vec<u8>>>>,
|
||||
invalid_raster: AtomicUsize,
|
||||
raster_limit: AtomicUsize,
|
||||
invalid_svg: AtomicUsize,
|
||||
@@ -432,9 +448,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,
|
||||
@@ -474,7 +493,7 @@ impl ImageResolver {
|
||||
svg_count: AtomicUsize::new(0),
|
||||
svg_depth: AtomicUsize::new(0),
|
||||
raster_pixels: AtomicU64::new(0),
|
||||
raster_data: Mutex::new(HashSet::new()),
|
||||
raster_data: Mutex::new(HashMap::new()),
|
||||
invalid_raster: AtomicUsize::new(0),
|
||||
raster_limit: AtomicUsize::new(0),
|
||||
invalid_svg: AtomicUsize::new(0),
|
||||
@@ -542,28 +561,51 @@ fn account_invalid_data_urls(xml: &roxmltree::Document<'_>, used: &Used) {
|
||||
}
|
||||
}
|
||||
|
||||
fn raster_format(data: &[u8]) -> Option<craft_image_codecs::Format> {
|
||||
match craft_image_codecs::detect(data)? {
|
||||
format @ (craft_image_codecs::Format::Png
|
||||
| craft_image_codecs::Format::Jpeg
|
||||
| craft_image_codecs::Format::Gif
|
||||
| craft_image_codecs::Format::WebP) => Some(format),
|
||||
#[derive(Clone, Copy)]
|
||||
enum SvgRaster {
|
||||
Png,
|
||||
Jpeg,
|
||||
Gif,
|
||||
WebP,
|
||||
}
|
||||
|
||||
impl SvgRaster {
|
||||
fn codec(self) -> craft_image_codecs::Format {
|
||||
match self {
|
||||
Self::Png => craft_image_codecs::Format::Png,
|
||||
Self::Jpeg => craft_image_codecs::Format::Jpeg,
|
||||
Self::Gif => craft_image_codecs::Format::Gif,
|
||||
Self::WebP => craft_image_codecs::Format::WebP,
|
||||
}
|
||||
}
|
||||
|
||||
fn image_kind(self, data: Arc<Vec<u8>>) -> usvg::ImageKind {
|
||||
match self {
|
||||
Self::Png => usvg::ImageKind::PNG(data),
|
||||
Self::Jpeg => usvg::ImageKind::JPEG(data),
|
||||
Self::Gif => usvg::ImageKind::GIF(data),
|
||||
Self::WebP => usvg::ImageKind::WEBP(data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Match supported raster signatures to the MIME types both image resolvers accept.
|
||||
fn declared_raster(mime: &str, data: &[u8]) -> Option<SvgRaster> {
|
||||
use craft_image_codecs::Format;
|
||||
match (mime, craft_image_codecs::detect(data)?) {
|
||||
("image/png" | "text/plain", Format::Png) => Some(SvgRaster::Png),
|
||||
("image/jpg" | "image/jpeg" | "text/plain", Format::Jpeg) => Some(SvgRaster::Jpeg),
|
||||
("image/gif" | "text/plain", Format::Gif) => Some(SvgRaster::Gif),
|
||||
("image/webp" | "text/plain", Format::WebP) => Some(SvgRaster::WebP),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn raster_image(used: &Used, limits: &Limits, data: Arc<Vec<u8>>, format: craft_image_codecs::Format) -> Option<usvg::ImageKind> {
|
||||
fn raster_image(used: &Used, limits: &Limits, data: Arc<Vec<u8>>, format: SvgRaster) -> Option<usvg::ImageKind> {
|
||||
let Ok((width, height)) = craft_image_codecs::probe_allocation_size(&data) else {
|
||||
used.invalid_raster.fetch_add(1, Ordering::Relaxed);
|
||||
return None;
|
||||
};
|
||||
if !matches!(
|
||||
format,
|
||||
craft_image_codecs::Format::Png | craft_image_codecs::Format::Jpeg | craft_image_codecs::Format::Gif | craft_image_codecs::Format::WebP
|
||||
) {
|
||||
used.invalid_raster.fetch_add(1, Ordering::Relaxed);
|
||||
return None;
|
||||
}
|
||||
let pixels = u64::from(width).checked_mul(u64::from(height))?;
|
||||
if pixels == 0 || pixels > limits.embedded_raster_pixels {
|
||||
used.raster_limit.fetch_add(1, Ordering::Relaxed);
|
||||
@@ -571,12 +613,11 @@ fn raster_image(used: &Used, limits: &Limits, data: Arc<Vec<u8>>, format: craft_
|
||||
}
|
||||
let pointer = Arc::as_ptr(&data) as usize;
|
||||
let mut seen = used.raster_data.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if seen.insert(pointer) {
|
||||
if let Entry::Vacant(entry) = seen.entry(pointer) {
|
||||
let mut current = used.raster_pixels.load(Ordering::Relaxed);
|
||||
loop {
|
||||
let next = current.checked_add(pixels)?;
|
||||
if next > limits.embedded_raster_pixels {
|
||||
seen.remove(&pointer);
|
||||
used.raster_limit.fetch_add(1, Ordering::Relaxed);
|
||||
return None;
|
||||
}
|
||||
@@ -585,13 +626,16 @@ fn raster_image(used: &Used, limits: &Limits, data: Arc<Vec<u8>>, format: craft_
|
||||
Err(actual) => current = actual,
|
||||
}
|
||||
}
|
||||
// The weak reference pins the allocation identity, not its encoded bytes.
|
||||
// At most one entry is added for each nonrefundable pixel charge.
|
||||
entry.insert(Arc::downgrade(&data));
|
||||
}
|
||||
drop(seen);
|
||||
// A valid header is not proof of valid pixels. resvg can silently omit a damaged
|
||||
// image later; decode now under explicit limits so omission accounting is honest.
|
||||
let decode_limits = craft_image_codecs::Limits::new(width, height, pixels, limits.embedded_raster_pixels.saturating_mul(8));
|
||||
let options = craft_image_codecs::DecodeOptions { limits: decode_limits, keep_orientation: true };
|
||||
if let Err(error) = craft_image_codecs::decode_as_with(format, &data, &options) {
|
||||
if let Err(error) = craft_image_codecs::decode_as_with(format.codec(), &data, &options) {
|
||||
// No tree retains this allocation on failure. Forget its address before it
|
||||
// can be reused, but keep the pixel charge for the decode work already spent.
|
||||
used.raster_data.lock().unwrap_or_else(PoisonError::into_inner).remove(&pointer);
|
||||
@@ -605,15 +649,37 @@ fn raster_image(used: &Used, limits: &Limits, data: Arc<Vec<u8>>, format: craft_
|
||||
}
|
||||
return None;
|
||||
}
|
||||
Some(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,
|
||||
Some(format.image_kind(data))
|
||||
}
|
||||
|
||||
/// Decode one child within the original byte allowance, including encoding expansion.
|
||||
fn embedded_text<'a>(data: &'a [u8], room: usize, limits: &Limits) -> Result<Cow<'a, str>, svgz::Error> {
|
||||
let text = svgz::text(data, room.min(limits.bytes))?;
|
||||
if text.len() > room {
|
||||
return Err(svgz::Error::TooLarge(room));
|
||||
}
|
||||
Ok(text)
|
||||
}
|
||||
|
||||
enum ChildRefusal {
|
||||
Depth,
|
||||
Bytes,
|
||||
Invalid,
|
||||
}
|
||||
|
||||
fn preflight_child<'a>(text: &'a str, limits: &Limits) -> Result<Preflight<'a>, ChildRefusal> {
|
||||
preflight(text, limits).map_err(|error| match error {
|
||||
Error::TooDeep(_) => ChildRefusal::Depth,
|
||||
Error::Text(svgz::Error::TooLarge(_)) => ChildRefusal::Bytes,
|
||||
_ => ChildRefusal::Invalid,
|
||||
})
|
||||
}
|
||||
|
||||
/// Reserve the complete checked height within the parent's remaining XML room.
|
||||
fn child_total_depth(parent: usize, checked: &Preflight<'_>, limits: &Limits) -> Option<usize> {
|
||||
parent.checked_add(checked.height).filter(|depth| *depth <= limits.tree_depth)
|
||||
}
|
||||
|
||||
/// An embedded SVG document, if the budgets allow it.
|
||||
fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<'_>) -> Option<usvg::ImageKind> {
|
||||
if used.depth.load(Ordering::Relaxed) >= limits.embedded_depth {
|
||||
@@ -622,7 +688,7 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<'
|
||||
return None;
|
||||
}
|
||||
let left = limits.embedded_bytes.saturating_sub(used.bytes.load(Ordering::Relaxed));
|
||||
let text = match svgz::text(data, left.min(limits.bytes)) {
|
||||
let text = match embedded_text(data, left, limits) {
|
||||
Ok(text) => text,
|
||||
// What a refused document unpacked before it was refused was work too: it uses up what
|
||||
// is left, so the next image can't repeat it.
|
||||
@@ -635,25 +701,17 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<'
|
||||
return None;
|
||||
}
|
||||
};
|
||||
// The raw/inflated bytes can fit while UTF-16/Latin-1 decoding expands
|
||||
// beyond the aggregate allowance. A refusal consumes the remaining work.
|
||||
if text.len() > left {
|
||||
used.bytes.fetch_add(left, Ordering::Relaxed);
|
||||
used.svg_limit.fetch_add(1, Ordering::Relaxed);
|
||||
used.svg_resource_limit.fetch_add(1, Ordering::Relaxed);
|
||||
return None;
|
||||
}
|
||||
used.bytes.fetch_add(text.len(), Ordering::Relaxed);
|
||||
let checked = match preflight(&text, limits) {
|
||||
let checked = match preflight_child(&text, limits) {
|
||||
Ok(checked) => checked,
|
||||
// Decoding UTF-16 or Latin-1 can exceed the text cap after the
|
||||
// raw/inflated bytes fitted. This is a resource refusal, not invalid XML.
|
||||
Err(Error::Text(svgz::Error::TooLarge(_))) => {
|
||||
Err(ChildRefusal::Bytes) => {
|
||||
used.svg_limit.fetch_add(1, Ordering::Relaxed);
|
||||
used.svg_resource_limit.fetch_add(1, Ordering::Relaxed);
|
||||
return None;
|
||||
}
|
||||
Err(Error::TooDeep(_)) => {
|
||||
Err(ChildRefusal::Depth) => {
|
||||
used.svg_limit.fetch_add(1, Ordering::Relaxed);
|
||||
return None;
|
||||
}
|
||||
@@ -663,9 +721,7 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<'
|
||||
}
|
||||
};
|
||||
// The nesting usvg converts, references followed, not the source depth alone.
|
||||
let xml_depth = checked.height;
|
||||
let total_depth = used.xml_depth.load(Ordering::Relaxed).checked_add(xml_depth);
|
||||
let Some(total_depth) = total_depth.filter(|depth| *depth <= limits.tree_depth) else {
|
||||
let Some(total_depth) = child_total_depth(used.xml_depth.load(Ordering::Relaxed), &checked, limits) else {
|
||||
used.svg_limit.fetch_add(1, Ordering::Relaxed);
|
||||
return None;
|
||||
};
|
||||
@@ -700,16 +756,7 @@ pub fn image_resolver(limits: Limits) -> usvg::ImageHrefResolver<'static> {
|
||||
fn image_resolver_for(limits: Limits, used: Arc<Used>) -> usvg::ImageHrefResolver<'static> {
|
||||
usvg::ImageHrefResolver {
|
||||
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 {
|
||||
if let Some(format) = declared_raster(mime, &data) {
|
||||
return raster_image(&used, &limits, data, format);
|
||||
}
|
||||
// usvg can decode only the raster variants above. Count every other declared
|
||||
@@ -730,3 +777,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,13 @@ 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};
|
||||
#[cfg(feature = "tree-render")]
|
||||
pub(super) use raster::{decode_raster, probe_raster_pixels};
|
||||
|
||||
/// 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 +553,6 @@ fn deeper(cx: Cx) -> Result<(), Refusal> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "tree-render"))]
|
||||
mod decoder_edges;
|
||||
@@ -0,0 +1,194 @@
|
||||
//! 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>,
|
||||
}
|
||||
// Cargo dependency edges may omit the resolved Git fragment even though the
|
||||
// selected package ID always records it. Other source identities stay exact.
|
||||
fn source_matches(edge: &str, package: Option<&str>) -> bool {
|
||||
let Some(package) = package else { return false };
|
||||
if edge.starts_with("git+") && !edge.contains('#') { package.split_once('#').map_or(package, |(base, _)| base) == edge } else { package == edge }
|
||||
}
|
||||
impl LockGraph {
|
||||
pub(crate) fn parse(text: &str) -> Self {
|
||||
let lock: toml::Value = toml::from_str(text).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| source_matches(s, p.id.source.as_deref()))
|
||||
})
|
||||
.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"] {
|
||||
assert_eq!(graph.dependency(svg, codec), graph.dependency(&renderer, codec), "validator and selected renderer differ for {codec}");
|
||||
}
|
||||
let codecs = graph.dependency(svg, "craft-image-codecs");
|
||||
assert_eq!(
|
||||
graph.dependency(&codecs, "zune-jpeg"),
|
||||
graph.dependency(&renderer, "zune-jpeg"),
|
||||
"JPEG validator and renderer must use the same backend"
|
||||
);
|
||||
}
|
||||
|
||||
#[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', 'craft-image-codecs']\n\
|
||||
[[package]]\nname='craft-image-codecs'\nversion='1'\ndependencies=['",
|
||||
);
|
||||
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());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn git_edges_resolve_full_package_ids_and_reject_canonical_ambiguity() {
|
||||
let fixture = |second: bool| {
|
||||
let mut text = String::from(
|
||||
r#"
|
||||
[[package]]
|
||||
name = "consumer"
|
||||
version = "1"
|
||||
dependencies = ["codec 1 (git+https://example.invalid/codecs?rev=chosen)"]
|
||||
[[package]]
|
||||
name = "explicit"
|
||||
version = "1"
|
||||
dependencies = ["codec 1 (git+https://example.invalid/codecs?rev=chosen#aaaa)"]
|
||||
[[package]]
|
||||
name = "wrong"
|
||||
version = "1"
|
||||
dependencies = ["codec 1 (git+https://example.invalid/codecs?rev=chosen#cccc)"]
|
||||
[[package]]
|
||||
name = "codec"
|
||||
version = "1"
|
||||
source = "git+https://example.invalid/codecs?rev=chosen#aaaa"
|
||||
"#,
|
||||
);
|
||||
if second {
|
||||
text.push_str(
|
||||
r#"
|
||||
[[package]]
|
||||
name = "codec"
|
||||
version = "1"
|
||||
source = "git+https://example.invalid/codecs?rev=chosen#bbbb"
|
||||
"#,
|
||||
);
|
||||
}
|
||||
LockGraph::parse(&text)
|
||||
};
|
||||
let unique = fixture(false);
|
||||
let selected = unique.dependency(&unique.workspace_package("consumer"), "codec");
|
||||
assert_eq!(selected.source.as_deref(), Some("git+https://example.invalid/codecs?rev=chosen#aaaa"));
|
||||
assert_eq!(selected, unique.dependency(&unique.workspace_package("explicit"), "codec"));
|
||||
assert!(std::panic::catch_unwind(|| unique.dependency(&unique.workspace_package("wrong"), "codec")).is_err());
|
||||
let ambiguous = fixture(true);
|
||||
assert!(std::panic::catch_unwind(|| ambiguous.dependency(&ambiguous.workspace_package("consumer"), "codec")).is_err());
|
||||
assert_eq!(selected, ambiguous.dependency(&ambiguous.workspace_package("explicit"), "codec"));
|
||||
assert!(!source_matches("registry+one", Some("registry+one#aaaa")), "registry sources remain exact");
|
||||
assert!(
|
||||
!source_matches("git+https://example.invalid/codecs?rev=other", selected.source.as_deref()),
|
||||
"requested Git revision remains part of the identity"
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
//! 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::{SvgRaster, 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 (format, data) = match kind {
|
||||
usvg::ImageKind::PNG(data) => (SvgRaster::Png, data),
|
||||
usvg::ImageKind::JPEG(data) => (SvgRaster::Jpeg, data),
|
||||
usvg::ImageKind::GIF(data) => (SvgRaster::Gif, data),
|
||||
usvg::ImageKind::WEBP(data) => (SvgRaster::WebP, data),
|
||||
usvg::ImageKind::SVG(_) => return Ok(()),
|
||||
};
|
||||
let pixels = probe_raster_pixels(data, max_pixels)?;
|
||||
*pixels_left = pixels_left.checked_sub(pixels).ok_or(RasterValidationFailure::Limit)?;
|
||||
decode_raster(format, data, pixels)
|
||||
}
|
||||
|
||||
pub(in crate::tree) fn probe_raster_pixels(data: &[u8], max_pixels: u64) -> Result<u64, RasterValidationFailure> {
|
||||
// The allocation probe accounts for oversized GIF/WebP frames as well as the canvas.
|
||||
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);
|
||||
}
|
||||
Ok(pixels)
|
||||
}
|
||||
|
||||
pub(in crate::tree) fn decode_raster(format: SvgRaster, data: &[u8], pixels: u64) -> Result<(), RasterValidationFailure> {
|
||||
match format {
|
||||
SvgRaster::Png => {
|
||||
let decoded = tiny_skia::Pixmap::decode_png(data).map_err(|_| RasterValidationFailure::Invalid)?;
|
||||
rgba_bytes(decoded.width(), decoded.height(), pixels)?;
|
||||
Ok(())
|
||||
}
|
||||
SvgRaster::Jpeg => craft_image_codecs::validate_jpeg_rgba8(data, pixels).map_err(|error| match error {
|
||||
craft_image_codecs::CodecError::LimitExceeded(_) => RasterValidationFailure::Limit,
|
||||
_ => RasterValidationFailure::Invalid,
|
||||
}),
|
||||
SvgRaster::Gif => gif(data, pixels),
|
||||
SvgRaster::WebP => webp(data, pixels),
|
||||
}
|
||||
}
|
||||
|
||||
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 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 = rgba / 4 * channels;
|
||||
if decoder.output_buffer_size() != Some(expected) {
|
||||
return Err(RasterValidationFailure::Invalid);
|
||||
}
|
||||
// The decoder's metadata memory limit does not cap read_image scratch.
|
||||
// Admit the frame and exact output buffer before calling read_image.
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "raster_tests.rs"]
|
||||
mod raster_tests;
|
||||
@@ -0,0 +1,186 @@
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing)]
|
||||
use super::*;
|
||||
use craft_image_codecs::{ChannelLayout, Format, Image};
|
||||
use std::sync::Arc;
|
||||
|
||||
fn kind(format: Format, bytes: Vec<u8>) -> usvg::ImageKind {
|
||||
let bytes = Arc::new(bytes);
|
||||
match format {
|
||||
Format::Png => usvg::ImageKind::PNG(bytes),
|
||||
Format::Jpeg => usvg::ImageKind::JPEG(bytes),
|
||||
Format::Gif => usvg::ImageKind::GIF(bytes),
|
||||
Format::WebP => usvg::ImageKind::WEBP(bytes),
|
||||
_ => panic!("test raster format"),
|
||||
}
|
||||
}
|
||||
|
||||
fn encoded(format: Format) -> Vec<u8> {
|
||||
let image = Image::from_u8(2, 2, ChannelLayout::Rgb, [17, 83, 149].repeat(4)).unwrap();
|
||||
craft_image_codecs::encode(&image, format, &Default::default()).unwrap()
|
||||
}
|
||||
|
||||
fn segment(out: &mut Vec<u8>, marker: u8, payload: &[u8]) {
|
||||
out.extend_from_slice(&[0xff, marker]);
|
||||
out.extend_from_slice(&u16::try_from(payload.len() + 2).unwrap().to_be_bytes());
|
||||
out.extend_from_slice(payload);
|
||||
}
|
||||
|
||||
// ISO/IEC10918-1 framing, one gray 8x8 block. The single one-bit Huffman codes
|
||||
// encode DC category 0 and AC EOB. No third-party asset or camera data is used.
|
||||
fn gray_jpeg(bits: u8, entropy: &[u8]) -> Vec<u8> {
|
||||
let mut data = vec![0xff, 0xd8];
|
||||
let mut quantization = vec![0];
|
||||
quantization.extend_from_slice(&[1; 64]);
|
||||
segment(&mut data, 0xdb, &quantization);
|
||||
segment(&mut data, if bits == 8 { 0xc0 } else { 0xc1 }, &[bits, 0, 8, 0, 8, 1, 1, 0x11, 0]);
|
||||
let mut tables = Vec::new();
|
||||
for class in [0x00, 0x10] {
|
||||
tables.extend_from_slice(&[class, 1]);
|
||||
tables.extend_from_slice(&[0; 15]);
|
||||
tables.push(0);
|
||||
}
|
||||
segment(&mut data, 0xc4, &tables);
|
||||
segment(&mut data, 0xda, &[1, 1, 0, 0, 63, 0]);
|
||||
data.extend_from_slice(entropy);
|
||||
data.extend_from_slice(&[0xff, 0xd9]);
|
||||
data
|
||||
}
|
||||
|
||||
fn corrupt_jpeg() -> Vec<u8> {
|
||||
let mut data = gray_jpeg(8, &[0x3f]);
|
||||
let scan = data.windows(2).position(|marker| marker == b"\xff\xda").unwrap();
|
||||
// Keep the complete scan header and valid entropy, but reference table 1
|
||||
// while DHT defines only table 0. The renderer accepts these headers, then
|
||||
// fails pixel decoding when checking the selected Huffman tables. Random
|
||||
// all-one entropy is tolerated by its default policy and is not this guard.
|
||||
assert_eq!(data[scan + 6], 0);
|
||||
data[scan + 6] = 0x11;
|
||||
data
|
||||
}
|
||||
|
||||
fn crc32(bytes: &[u8]) -> u32 {
|
||||
let mut value = !0u32;
|
||||
for byte in bytes {
|
||||
value ^= u32::from(*byte);
|
||||
for _ in 0..8 {
|
||||
value = (value >> 1) ^ (0xedb8_8320 & 0u32.wrapping_sub(value & 1));
|
||||
}
|
||||
}
|
||||
!value
|
||||
}
|
||||
|
||||
fn corrupt_png() -> Vec<u8> {
|
||||
let mut data = encoded(Format::Png);
|
||||
let mut at = 8;
|
||||
loop {
|
||||
let length = u32::from_be_bytes(data[at..at + 4].try_into().unwrap()) as usize;
|
||||
if &data[at + 4..at + 8] == b"IDAT" {
|
||||
// Keep the complete container and an honest CRC, but destroy the
|
||||
// zlib header in the pixel stream rather than the dimension header.
|
||||
assert!(length >= 2);
|
||||
data[at + 8..at + 10].fill(0);
|
||||
let crc = crc32(&data[at + 4..at + 8 + length]);
|
||||
data[at + 8 + length..at + 12 + length].copy_from_slice(&crc.to_be_bytes());
|
||||
return data;
|
||||
}
|
||||
at += length + 12;
|
||||
}
|
||||
}
|
||||
|
||||
fn corrupt_gif() -> Vec<u8> {
|
||||
// Complete 2x2 GIF89a framing and palette, followed by a code7 when only
|
||||
// codes 0..5 are initially defined by an LZW minimum code size of 2.
|
||||
let mut data = b"GIF89a".to_vec();
|
||||
data.extend_from_slice(&[2, 0, 2, 0, 0x80, 0, 0]);
|
||||
data.extend_from_slice(&[0, 0, 0, 0, 255, 0]);
|
||||
data.extend_from_slice(&[0x2c, 0, 0, 0, 0, 2, 0, 2, 0, 0]);
|
||||
data.extend_from_slice(&[2, 1, 7, 0, 0x3b]);
|
||||
data
|
||||
}
|
||||
|
||||
fn corrupt_webp() -> Vec<u8> {
|
||||
// Complete RIFF/VP8L dimensions, with no transform/Huffman/pixel bits.
|
||||
// Chunk length and odd-byte padding remain internally consistent.
|
||||
let mut data = b"RIFF".to_vec();
|
||||
data.extend_from_slice(&18u32.to_le_bytes());
|
||||
data.extend_from_slice(b"WEBPVP8L");
|
||||
data.extend_from_slice(&5u32.to_le_bytes());
|
||||
data.extend_from_slice(&[0x2f, 1, 0x40, 0, 0, 0]);
|
||||
data
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renderer_validator_accepts_complete_png_jpeg_gif_and_webp_at_exact_pixel_allowance() {
|
||||
for format in [Format::Png, Format::Jpeg, Format::Gif, Format::WebP] {
|
||||
let data = encoded(format);
|
||||
assert_eq!(craft_image_codecs::probe_allocation_size(&data).unwrap(), (2, 2));
|
||||
let mut remaining = 4;
|
||||
assert_eq!(validate_raster(&kind(format, data), 4, &mut remaining), Ok(()), "{format:?}");
|
||||
assert_eq!(remaining, 0, "{format:?}: exact area must be charged");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renderer_validator_rejects_corrupt_pixels_after_valid_dimensions_without_refunding_work() {
|
||||
let cases = [
|
||||
(Format::Png, corrupt_png(), (2, 2)),
|
||||
// Header-only admission cannot detect this missing scan-table dependency.
|
||||
(Format::Jpeg, corrupt_jpeg(), (8, 8)),
|
||||
(Format::Gif, corrupt_gif(), (2, 2)),
|
||||
(Format::WebP, corrupt_webp(), (2, 2)),
|
||||
];
|
||||
for (format, data, dimensions) in cases {
|
||||
assert_eq!(craft_image_codecs::probe_allocation_size(&data).unwrap(), dimensions, "{format:?}");
|
||||
match format {
|
||||
Format::Gif => {
|
||||
gif::DecodeOptions::new().read_info(data.as_slice()).expect("intact GIF headers before corrupt LZW pixels");
|
||||
}
|
||||
Format::WebP => {
|
||||
image_webp::WebPDecoder::new(Cursor::new(&data)).expect("intact WebP headers before missing pixel bits");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let pixels = u64::from(dimensions.0) * u64::from(dimensions.1);
|
||||
let image = kind(format, data);
|
||||
let mut remaining = pixels * 2;
|
||||
for left in [pixels, 0] {
|
||||
assert_eq!(validate_raster(&image, pixels, &mut remaining), Err(RasterValidationFailure::Invalid), "{format:?}");
|
||||
assert_eq!(remaining, left, "{format:?}: failed decode work is nonrefundable");
|
||||
}
|
||||
assert_eq!(validate_raster(&image, pixels, &mut remaining), Err(RasterValidationFailure::Limit), "{format:?}");
|
||||
assert_eq!(remaining, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn twelve_bit_jpeg_is_valid_for_the_general_codec_but_not_the_svg_renderer() {
|
||||
for bits in [8, 12] {
|
||||
// DC=0, EOB=0, then six one-pad bits: a constant mid-gray block.
|
||||
let data = gray_jpeg(bits, &[0x3f]);
|
||||
let decoded = craft_image_codecs::decode(&data).unwrap();
|
||||
assert_eq!(decoded.dimensions(), (8, 8));
|
||||
let mut remaining = 64;
|
||||
let expected = if bits == 8 { Ok(()) } else { Err(RasterValidationFailure::Invalid) };
|
||||
assert_eq!(validate_raster(&kind(Format::Jpeg, data), 64, &mut remaining), expected, "{bits}-bit");
|
||||
assert_eq!(remaining, 0, "capability refusal still spends attempted decode work");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renderer_validator_refuses_unadmitted_headers_and_pixels_without_charging_unattempted_decode() {
|
||||
for format in [Format::Png, Format::Jpeg, Format::Gif, Format::WebP] {
|
||||
let image = kind(format, encoded(format));
|
||||
let mut remaining = 4;
|
||||
assert_eq!(validate_raster(&image, 3, &mut remaining), Err(RasterValidationFailure::Limit), "{format:?}: per-image ceiling");
|
||||
assert_eq!(remaining, 4);
|
||||
remaining = 3;
|
||||
assert_eq!(validate_raster(&image, 4, &mut remaining), Err(RasterValidationFailure::Limit), "{format:?}: cumulative allowance");
|
||||
assert_eq!(remaining, 3);
|
||||
assert_eq!(
|
||||
validate_raster(&kind(format, b"invalid raster header".to_vec()), 4, &mut remaining),
|
||||
Err(RasterValidationFailure::Invalid),
|
||||
"{format:?}"
|
||||
);
|
||||
assert_eq!(remaining, 3, "invalid dimensions cannot reserve an invented area");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,458 @@
|
||||
//! 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().saturating_add(limits.entity_bytes).saturating_mul(12);
|
||||
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))
|
||||
}
|
||||
|
||||
/// Fall back to the original, checked DOM when a repair cannot be projected.
|
||||
/// Only MIME headers already safe for usvg's parser can take this route. Count
|
||||
/// malformed declarations conservatively because usvg may drop them before
|
||||
/// invoking a callback.
|
||||
pub(super) fn unrepaired_image_omissions(checked: &super::Preflight<'_>) -> Option<usize> {
|
||||
let mut work = checked.xml.input_text().len().saturating_add(checked.limits.entity_bytes).saturating_mul(6);
|
||||
let mut omitted = 0usize;
|
||||
for node in checked.xml.descendants().filter(|node| node.is_element() && matches!(node.tag_name().name(), "image" | "feImage")) {
|
||||
for href in node.attributes().filter(|a| a.name() == "href" && matches!(a.namespace(), None | Some(XLINK_NS))) {
|
||||
let value = href.value();
|
||||
work = work.checked_sub(value.len())?;
|
||||
if !mime_header_admitted(value) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
if let Some(value) = node.attribute((XLINK_NS, "href")).or_else(|| node.attribute("href"))
|
||||
&& malformed_data_url_replacement(value, &mut work).ok()? == Some(INVALID_IMAGE)
|
||||
{
|
||||
omitted = omitted.saturating_add(1);
|
||||
}
|
||||
}
|
||||
Some(omitted)
|
||||
}
|
||||
|
||||
// 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 original_dom_fallback_does_not_bypass_mime_admission() {
|
||||
let ordinary = "<svg><image href='data:;base64'/></svg>";
|
||||
assert_eq!(unrepaired_image_omissions(&super::super::preflight(ordinary, &limits()).unwrap()), Some(1));
|
||||
|
||||
let header = "a=b;".repeat(MAX_MIME_SEPARATORS + 1);
|
||||
let excessive = format!("<svg><image href='data:image/png;{header}base64,A'/></svg>");
|
||||
assert!(unrepaired_image_omissions(&super::super::preflight(&excessive, &limits()).unwrap()).is_none());
|
||||
}
|
||||
|
||||
#[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 checked = super::super::preflight(&repaired, &Limits { bytes: 2 << 20, ..Default::default() }).unwrap();
|
||||
let value = checked.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");
|
||||
}
|
||||
@@ -547,7 +547,7 @@ mod invalid_image_accounting {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_decode_forgets_address_but_preserves_spent_budget() {
|
||||
fn failed_decode_releases_input_but_preserves_spent_budget() {
|
||||
let valid = png();
|
||||
let idat = valid.windows(4).position(|b| b == b"IDAT").unwrap();
|
||||
let invalid = Arc::new(valid[..idat + 4].to_vec());
|
||||
@@ -555,11 +555,12 @@ mod invalid_image_accounting {
|
||||
let limits = Limits { embedded_raster_pixels: 3, ..shallow() };
|
||||
let resolver = ImageResolver::new(limits);
|
||||
let used = &resolver.used;
|
||||
let format = craft_image_codecs::Format::Png;
|
||||
let format = SvgRaster::Png;
|
||||
assert!(raster_image(used, &limits, invalid, format).is_none());
|
||||
assert!(!used.raster_data.lock().unwrap().contains(&pointer));
|
||||
assert!(!used.raster_data.lock().unwrap().contains_key(&pointer));
|
||||
assert_eq!(resolver.usage().raster_pixels, 1);
|
||||
let first_data = Arc::new(valid.clone());
|
||||
let first_pointer = Arc::as_ptr(&first_data) as usize;
|
||||
let first = raster_image(used, &limits, first_data.clone(), format).unwrap();
|
||||
assert_eq!(resolver.usage().raster_pixels, 2);
|
||||
let repeated = raster_image(used, &limits, first_data, format).unwrap();
|
||||
@@ -569,6 +570,9 @@ mod invalid_image_accounting {
|
||||
assert!(raster_image(used, &limits, Arc::new(valid), format).is_none());
|
||||
assert_eq!(resolver.usage().raster_limit_images, 1);
|
||||
drop((first, repeated, second));
|
||||
let seen = used.raster_data.lock().unwrap();
|
||||
let identity = seen.get(&first_pointer).expect("the cache holds the allocation identity");
|
||||
assert!(identity.upgrade().is_none(), "the cache must not retain encoded bytes");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -0,0 +1,401 @@
|
||||
//! Opt-in complete image validation and checked-DOM normalization.
|
||||
//! Legacy ImageResolver entry points retain their existing behavior.
|
||||
use super::draw::{RasterValidationFailure, decode_raster, probe_raster_pixels};
|
||||
use super::{
|
||||
ChildRefusal, Error, ImageUsage, Limits, Preflight, child_total_depth, declared_raster, embedded_text, image_href, preflight, preflight_child,
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 its
|
||||
// expanded ancestor frames, excluding the callback leaf itself. The child
|
||||
// contributes its complete checked height, as in legacy child admission.
|
||||
let _scope = self.root_scope(checked.height.saturating_sub(1))?;
|
||||
options.image_href_resolver = self.resolver();
|
||||
self.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| {
|
||||
if let Some(format) = declared_raster(mime, &data) {
|
||||
return this.raster_with(&data, |pixels| decode_raster(format, &data, pixels)).then(|| format.image_kind(data));
|
||||
}
|
||||
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 raster_with(&self, data: &[u8], decode: impl FnOnce(u64) -> Result<(), RasterValidationFailure>) -> bool {
|
||||
let result = self.reserve_raster(data).and_then(decode);
|
||||
if let Err(error) = result {
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
match error {
|
||||
RasterValidationFailure::Invalid => state.invalid_raster = state.invalid_raster.saturating_add(1),
|
||||
RasterValidationFailure::Limit => state.raster_limits = state.raster_limits.saturating_add(1),
|
||||
}
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn reserve_raster(&self, data: &[u8]) -> Result<u64, RasterValidationFailure> {
|
||||
{
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
// usvg uses a fresh Arc for each data-URL callback. Each actual validation
|
||||
// spends encoded work, including when the header or decoder refuses it.
|
||||
state.encoded_left = state.encoded_left.checked_sub(data.len()).ok_or(RasterValidationFailure::Limit)?;
|
||||
}
|
||||
let pixels = probe_raster_pixels(data, self.limits.embedded_raster_pixels)?;
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
state.pixels_left = state.pixels_left.checked_sub(pixels).ok_or(RasterValidationFailure::Limit)?;
|
||||
// The caller decodes after releasing the mutex. The work stays charged.
|
||||
Ok(pixels)
|
||||
}
|
||||
|
||||
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 embedded_text(data, room, &self.limits) {
|
||||
Ok(text) => text,
|
||||
Err(error) => {
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if matches!(error, svgz::Error::Encoding) && !crate::sniff::is_svgz(data) {
|
||||
// Plain text validation can spend at most the input's bytes.
|
||||
// An invalid encoding does not exhaust a later sibling's room.
|
||||
state.svg_bytes_left = state.svg_bytes_left.saturating_sub(data.len());
|
||||
state.invalid_svg = state.invalid_svg.saturating_add(1);
|
||||
return None;
|
||||
}
|
||||
state.svg_bytes_left = 0;
|
||||
if matches!(error, svgz::Error::TooLarge(_)) {
|
||||
state.svg_limits = state.svg_limits.saturating_add(1);
|
||||
state.svg_resource_limits = state.svg_resource_limits.saturating_add(1);
|
||||
} else {
|
||||
// An unreadable compressed stream may have used the entire
|
||||
// allowed inflation work. Later callbacks can only classify.
|
||||
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_child(text, &self.limits) {
|
||||
Ok(checked) => checked,
|
||||
Err(error) => {
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
match error {
|
||||
ChildRefusal::Depth => state.svg_limits = state.svg_limits.saturating_add(1),
|
||||
ChildRefusal::Bytes => {
|
||||
state.svg_limits = state.svg_limits.saturating_add(1);
|
||||
state.svg_resource_limits = state.svg_resource_limits.saturating_add(1);
|
||||
}
|
||||
ChildRefusal::Invalid => 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) = child_total_depth(state.xml_depth, &checked, &self.limits) else {
|
||||
state.svg_limits = state.svg_limits.saturating_add(1);
|
||||
return None;
|
||||
};
|
||||
let previous = state.xml_depth;
|
||||
// Admission includes the child's complete height, but recursive
|
||||
// conversion reserves ancestors only, not its image callback leaf.
|
||||
state.xml_depth = total.saturating_sub(1);
|
||||
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 = self.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
|
||||
}
|
||||
}
|
||||
}
|
||||
fn convert_checked(&self, checked: Preflight<'_>, options: &usvg::Options<'_>) -> Result<usvg::Tree, Error> {
|
||||
match image_href::normalize(&checked) {
|
||||
Ok(std::borrow::Cow::Borrowed(_)) => checked.parse(options),
|
||||
Ok(std::borrow::Cow::Owned(text)) => {
|
||||
let limits = checked.limits;
|
||||
drop(checked);
|
||||
preflight(&text, &limits)?.parse(options)
|
||||
}
|
||||
Err(repair_error) => {
|
||||
// Keep the original checked DOM if its URLs are safe to pass to
|
||||
// usvg. Malformed optional URLs are dropped by its parser.
|
||||
let omitted = image_href::unrepaired_image_omissions(&checked).ok_or(repair_error)?;
|
||||
let tree = checked.parse(options)?;
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
state.invalid_raster = state.invalid_raster.saturating_add(omitted);
|
||||
Ok(tree)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod xml_tests;
|
||||
@@ -0,0 +1,352 @@
|
||||
#![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 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 (options, state, resolver) = wrapped_options(8);
|
||||
let encoded_before = state.lock().unwrap().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);
|
||||
assert!((options.image_href_resolver.resolve_data)("image/png", Arc::clone(&encoded), &options).is_none());
|
||||
let usage = resolver.usage();
|
||||
assert_eq!(usage.raster_pixels, attempt * 4);
|
||||
assert_eq!(state.lock().unwrap().encoded_left, encoded_before - attempt as usize * encoded.len());
|
||||
assert_eq!(usage.invalid_raster_images, attempt as usize);
|
||||
drop(encoded);
|
||||
assert!(weak.upgrade().is_none(), "validation must not retain failed callback buffers until parse completion");
|
||||
}
|
||||
assert!((options.image_href_resolver.resolve_data)("image/png", Arc::new(png()), &options).is_none());
|
||||
let usage = resolver.usage();
|
||||
assert_eq!((usage.invalid_raster_images, usage.raster_limit_images, usage.raster_pixels), (2, 1, 8));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_raster_callbacks_spend_exact_pixel_and_encoded_allowances_per_call() {
|
||||
let encoded = Arc::new(png());
|
||||
let (options, state, resolver) = wrapped_options(8);
|
||||
let call = || (options.image_href_resolver.resolve_data)("image/png", Arc::clone(&encoded), &options);
|
||||
state.lock().unwrap().encoded_left = encoded.len() * 2;
|
||||
assert!(call().is_some());
|
||||
assert_eq!(resolver.usage().raster_pixels, 4);
|
||||
assert_eq!(state.lock().unwrap().encoded_left, encoded.len());
|
||||
assert!(call().is_some(), "even a directly reused Arc is a new actual validation call");
|
||||
assert_eq!(state.lock().unwrap().encoded_left, 0);
|
||||
assert!(call().is_none());
|
||||
let usage = resolver.usage();
|
||||
assert_eq!((usage.raster_pixels, usage.invalid_raster_images, usage.raster_limit_images), (8, 0, 1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raster_decode_happens_after_the_work_is_charged_and_mutex_released() {
|
||||
let (_, state, resolver) = wrapped_options(4);
|
||||
let image = png();
|
||||
assert!(resolver.raster_with(&image, |admitted| {
|
||||
assert_eq!(admitted, 4);
|
||||
assert!(state.try_lock().is_ok(), "a usage snapshot must not wait for the raster decode");
|
||||
assert_eq!(resolver.usage().raster_pixels, 4);
|
||||
Ok(())
|
||||
}));
|
||||
}
|
||||
|
||||
#[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 (options, state, resolver) = wrapped_options(4);
|
||||
state.lock().unwrap().encoded_left = bytes.len();
|
||||
assert!((options.image_href_resolver.resolve_data)("image/png", Arc::new(oversized), &options).is_none());
|
||||
assert_eq!(state.lock().unwrap().encoded_left, bytes.len());
|
||||
assert_eq!(resolver.usage().raster_pixels, 0);
|
||||
assert!((options.image_href_resolver.resolve_data)("image/png", Arc::new(bytes), &options).is_some());
|
||||
assert_eq!(state.lock().unwrap().encoded_left, 0);
|
||||
let usage = resolver.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()
|
||||
}
|
||||
};
|
||||
// A failed compressed decode exhausts the primary pool. The
|
||||
// callbacks under test may use plain or compressed encodings.
|
||||
let invalid = Arc::new(crate::svgz::compress(&[0xff]));
|
||||
assert!((options.image_href_resolver.resolve_data)("image/svg+xml", invalid, &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,250 @@
|
||||
//! 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 malformed_plain_child_leaves_work_for_valid_siblings() {
|
||||
let child = drawing("<rect width='4' height='4' fill='red'/>");
|
||||
for invalid in [&[0xff][..], &[0xff, 0xfe, 0xd8][..]] {
|
||||
let contents = format!("{}{}", image(&url(invalid)), image(&url(child.as_bytes())));
|
||||
let root = drawing(&contents);
|
||||
let limits = Limits { embedded_bytes: invalid.len() + child.len(), ..Default::default() };
|
||||
let context = ValidatedImageResolver::new(limits);
|
||||
let tree = context.parse_preflight(preflight(&root, &limits).unwrap(), usvg::Options::default()).unwrap();
|
||||
let usage = context.usage();
|
||||
assert_eq!((usage.invalid_svg_images, usage.embedded_svg_count, usage.svg_limit_images), (1, 1, 0));
|
||||
assert_eq!(usage.embedded_svg_bytes, limits.embedded_bytes);
|
||||
let mut pixmap = super::super::draw::tiny_skia::Pixmap::new(4, 4).unwrap();
|
||||
super::super::draw::resvg::render(&tree, Default::default(), &mut pixmap.as_mut());
|
||||
assert_eq!(pixmap.pixel(2, 2).unwrap().red(), 255);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_malformed_image_at_the_svg_byte_cap_does_not_drop_the_drawing() {
|
||||
let root = drawing("<rect width='4' height='4' fill='red'/><image width='4' height='4' href='data:;base64'/>");
|
||||
let limits = Limits { bytes: root.len(), ..Default::default() };
|
||||
let context = ValidatedImageResolver::new(limits);
|
||||
let tree = context.parse_preflight(preflight(&root, &limits).unwrap(), usvg::Options::default()).unwrap();
|
||||
assert_eq!(context.usage().invalid_raster_images, 1);
|
||||
let mut pixmap = super::super::draw::tiny_skia::Pixmap::new(4, 4).unwrap();
|
||||
super::super::draw::resvg::render(&tree, Default::default(), &mut pixmap.as_mut());
|
||||
assert_eq!(pixmap.pixel(2, 2).unwrap().red(), 255);
|
||||
}
|
||||
|
||||
#[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());
|
||||
// Expanded root height6 includes the callback leaf: reserve5 ancestors,
|
||||
// then admit the child's complete height3. Source depth alone would wrongly
|
||||
// accept the cap7 case, while reserving the leaf twice would reject cap8.
|
||||
for (depth, admitted) in [(7, false), (8, 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);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plain_root_callback_leaf_does_not_spend_the_child_boundary_twice() {
|
||||
let child = drawing("<g><rect width='4' height='4' fill='black'/></g>"); // height3
|
||||
let root = drawing(&image(&url(child.as_bytes()))); // height2, one ancestor
|
||||
for (depth, admitted) in [(3, false), (4, true)] {
|
||||
let limits = Limits { tree_depth: depth, ..Default::default() };
|
||||
assert!(preflight(&root, &limits).is_ok());
|
||||
assert!(preflight(&child, &limits).is_ok());
|
||||
let context = ValidatedImageResolver::new(limits);
|
||||
let tree = context.parse_preflight(preflight(&root, &limits).unwrap(), usvg::Options::default()).unwrap();
|
||||
assert_eq!(context.usage().embedded_svg_count, usize::from(admitted));
|
||||
assert_eq!(context.usage().svg_limit_images, usize::from(!admitted));
|
||||
assert_eq!(context.usage().invalid_svg_images, 0);
|
||||
let pixels = super::super::draw::render(&tree, usvg::Transform::identity(), 4, 4, 0, 1 << 20).unwrap();
|
||||
let sample = pixels.pixel(1, 1).unwrap();
|
||||
assert_eq!((sample.red(), sample.green(), sample.blue()), (0, 0, 0));
|
||||
assert_eq!(sample.alpha(), if admitted { 255 } else { 0 }, "exact-boundary child must actually draw");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_callback_leaves_do_not_spend_the_descendant_boundary_twice() {
|
||||
let leaf = drawing("<rect width='4' height='4' fill='black'/>"); // height2
|
||||
let middle = drawing(&image(&url(leaf.as_bytes()))); // height2
|
||||
let root = drawing(&image(&url(middle.as_bytes()))); // height2
|
||||
// Root and middle each contribute one ancestor, then the leaf's full
|
||||
// height2: the visible rectangle fits exactly at depth4, but not depth3.
|
||||
for (depth, admitted) in [(3, false), (4, true)] {
|
||||
let limits = Limits { tree_depth: depth, ..Default::default() };
|
||||
let context = ValidatedImageResolver::new(limits);
|
||||
let tree = context.parse_preflight(preflight(&root, &limits).unwrap(), usvg::Options::default()).unwrap();
|
||||
let usage = context.usage();
|
||||
assert_eq!(usage.embedded_svg_count, if admitted { 2 } else { 1 });
|
||||
assert_eq!(usage.svg_limit_images, usize::from(!admitted));
|
||||
assert_eq!(usage.invalid_svg_images, 0);
|
||||
let pixels = super::super::draw::render(&tree, usvg::Transform::identity(), 4, 4, 0, 1 << 20).unwrap();
|
||||
let sample = pixels.pixel(1, 1).unwrap();
|
||||
assert_eq!(sample.alpha(), if admitted { 255 } else { 0 }, "exact-boundary grandchild must actually draw");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user