From 703d3b1cf4ae4633ee12a863df183e6ecdba2433 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 15:21:33 -0500 Subject: [PATCH] Bound SBS input bytes, wire counts and stored warnings (#237) * formats: bound SBS input, wires and stored warnings * formats: format the SBS size check --- crates/formats/src/sbs/constant.rs | 6 ++++++ crates/formats/src/sbs/read.rs | 16 +++++++++++++--- crates/formats/src/sbs/sbs_reader.rs | 8 +++++++- crates/formats/src/sbs/warnings.rs | 25 ++++++++++++++++++++++++- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/crates/formats/src/sbs/constant.rs b/crates/formats/src/sbs/constant.rs index 9a99ebf..b4f3e3b 100644 --- a/crates/formats/src/sbs/constant.rs +++ b/crates/formats/src/sbs/constant.rs @@ -9,6 +9,12 @@ pub(super) const UPDATER_VERSION: &str = env!("CARGO_PKG_VERSION"); pub const SBS_EXT: &str = "sbs"; pub(super) const MAX_WARNINGS: usize = 600; + +/// Largest SBS document read (audit: MaterialCraft #2). +pub(super) const MAX_SBS_BYTES: u64 = 256 << 20; + +/// Most wires one SBS may carry, across all graphs (audit: MaterialCraft #2). +pub(super) const MAX_WIRES: usize = 1_000_000; pub(super) const SNIFF_BYTES: usize = 4096; pub(super) const SELF_DEPENDENCY: &str = "?himself"; pub(super) const FIRST_UID: u64 = 1_000_000_000; diff --git a/crates/formats/src/sbs/read.rs b/crates/formats/src/sbs/read.rs index ecfdd84..7ea9431 100644 --- a/crates/formats/src/sbs/read.rs +++ b/crates/formats/src/sbs/read.rs @@ -1,5 +1,5 @@ use super::{ - constant::{MAX_GRAPHS, MAX_XML_NODES, SNIFF_BYTES}, + constant::{MAX_GRAPHS, MAX_SBS_BYTES, MAX_XML_NODES, SNIFF_BYTES}, placeholder::report_placeholders, sbs_read::SbsRead, sbs_reader::SbsReader, @@ -12,6 +12,9 @@ use roxmltree::Node as XmlNode; use std::collections::HashMap; pub fn read_sbs(bytes: &[u8], name: &str, linked: &dyn Fn(&str) -> Option>) -> Result { + if bytes.len() as u64 > MAX_SBS_BYTES { + return Err(FormatError::BadPackage(format!("the SBS file is {} bytes, over the {MAX_SBS_BYTES} byte limit", bytes.len()))); + } let text = std::str::from_utf8(bytes).map_err(|e| FormatError::BadPackage(format!("the SBS file is not UTF-8 text: {e}")))?; let text = text.trim_start_matches('\u{feff}'); let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_XML_NODES }; @@ -30,8 +33,15 @@ pub fn read_sbs(bytes: &[u8], name: &str, linked: &dyn Fn(&str) -> Option "Untitled package".to_string(), }; - let mut reader = - SbsReader { text, warnings: &mut warnings, dependencies: HashMap::new(), graph_names: HashMap::new(), resource_names: HashMap::new(), node_count: 0 }; + let mut reader = SbsReader { + text, + warnings: &mut warnings, + dependencies: HashMap::new(), + graph_names: HashMap::new(), + resource_names: HashMap::new(), + node_count: 0, + wire_count: 0, + }; for dependency in child_element(root, "dependencies").into_iter().flat_map(|d| children_named(d, "dependency")) { if let (Some(uid), Some(file)) = (child_value(dependency, "uid"), child_value(dependency, "filename")) { reader.dependencies.insert(uid.to_string(), file.to_string()); diff --git a/crates/formats/src/sbs/sbs_reader.rs b/crates/formats/src/sbs/sbs_reader.rs index d95d183..83487b7 100644 --- a/crates/formats/src/sbs/sbs_reader.rs +++ b/crates/formats/src/sbs/sbs_reader.rs @@ -1,7 +1,7 @@ use super::base_parameters::read_base_params; use super::component_usage; use super::connection::connect_all; -use super::constant::MAX_NODES; +use super::constant::{MAX_NODES, MAX_WIRES}; use super::constant::{NODE_HALF_SIZE, SELF_DEPENDENCY}; use super::embedded::decode_embedded; use super::filter_map::{BASE_PARAMS, FilterMap, HANDLED_PARAMS, filter_by_sbs_name}; @@ -27,6 +27,7 @@ pub(super) struct SbsReader<'text, 'warnings> { pub(super) graph_names: HashMap, pub(super) resource_names: HashMap, pub(super) node_count: usize, + pub(super) wire_count: usize, } impl<'text> SbsReader<'text, '_> { @@ -341,6 +342,11 @@ impl<'text> SbsReader<'text, '_> { } }; + if self.wire_count >= MAX_WIRES { + self.warnings.add(format!("graph `{name}`: the {MAX_WIRES} wire limit was reached; later wires were dropped")); + return wires; + } + self.wire_count += 1; wires.push(Connection { from: from.id, from_port, to: to.id, to_port }); } } diff --git a/crates/formats/src/sbs/warnings.rs b/crates/formats/src/sbs/warnings.rs index 86406ac..3b81627 100644 --- a/crates/formats/src/sbs/warnings.rs +++ b/crates/formats/src/sbs/warnings.rs @@ -5,6 +5,8 @@ use std::collections::HashMap; pub(super) struct Warnings { pub(super) lines: Vec<(String, usize)>, pub(super) line_index: HashMap, + /// Messages over the limit: they are counted but never stored (audit: MaterialCraft #2). + dropped: usize, } impl Warnings { @@ -14,13 +16,17 @@ impl Warnings { line.1 += 1; return; } + if self.lines.len() >= MAX_WARNINGS { + self.dropped += 1; + return; + } self.line_index.insert(message.clone(), self.lines.len()); self.lines.push((message, 1)); } pub(super) fn finish(self) -> Vec { - let total = self.lines.len(); + let total = self.lines.len() + self.dropped; let mut out: Vec = self.lines.into_iter().take(MAX_WARNINGS).map(|(line, count)| if count > 1 { format!("{line} (×{count})") } else { line }).collect(); if total > MAX_WARNINGS { @@ -29,3 +35,20 @@ impl Warnings { out } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn warning_messages_are_capped_as_they_arrive() { + let mut w = Warnings::default(); + for i in 0..MAX_WARNINGS + 5 { + w.add(format!("warning {i}")); + } + assert_eq!(w.lines.len(), MAX_WARNINGS); + let out = w.finish(); + assert_eq!(out.len(), MAX_WARNINGS + 1); + assert_eq!(out.last().unwrap(), "…and 5 more warnings"); + } +}