diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 58d8077e7..546f3e637 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -134,6 +134,9 @@ Sanitization, animation fitting and numeric output policy remain caller-owned. `crates/video/src/screen_video.rs` extracts AniCraft `crates/xfl/src/video.rs` codec-3 decoding and its synthetic partial-block fixture, introduced by Tanish Manku in `1f3e603b7ff53a3da364e41ec4da8d43dc83aab0` with pixel-loop cleanup `2b7571363b4b647266df758f95b6e397021cfd4d` (MIT OR Apache-2.0). Import commits preserve Tanish as primary author. Caller-owned bounds and neutral RGBA output separate codec work from the consuming app's FLV first-frame/report policy. Boundary tests and integration are subsequent Dylan M. Taylor changes. +## Source Map v3 parser and lookup + +`crates/sourcemap` extracts the bounded parser, source inventory and generated-position lookup from Craft Anything `crates/formats/src/sourcemap.rs` at `e5c9c94106747ed44b9a3fb0decce867c2285f93` (MIT OR Apache-2.0). Core-history ports preserve Brandon Thomas, BFlat, Afloat, RRFZ, Nicolas Bohn and Alex Joaquim Pereira as primary authors, with exact source revisions in each commit. Six regression files preserve their source authors; nine core unit tests retain the original tests. App annotation scanning, data URLs, base64 and file I/O are intentionally not imported. Workspace wiring is a subsequent change; existing limits and errors are unchanged. ## JSONC span-preserving editing `crates/jsonc` is extracted from Craft Anything `crates/formats/src/jsonc.rs` at `e5c9c94106747ed44b9a3fb0decce867c2285f93` (MIT OR Apache-2.0). The original implementation and hardening by BFlat, newline preservation by NetterSilver, and subsequent RRFZ/Afloat correctness fixes retain separate primary-author port commits with immutable source revisions. Shared API wiring and optional input-byte admission are subsequent changes. Compatibility parsing and all existing formatting/editing tests are retained; application file I/O and policy are not imported. diff --git a/Cargo.lock b/Cargo.lock index 6412bd28a..05847f390 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3103,6 +3103,15 @@ dependencies = [ name = "craft-sniff" version = "0.1.0" +[[package]] +name = "craft-sourcemap" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-ssip" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 6dad17db6..5f38bef09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/craft-libs" [workspace.dependencies] +craft-sourcemap = { path = "crates/sourcemap" } craft-pcap = { path = "crates/pcap" } craft-har = { path = "crates/har" } craft-jsonc = { path = "crates/jsonc" } diff --git a/crates/sourcemap/Cargo.toml b/crates/sourcemap/Cargo.toml new file mode 100644 index 000000000..b36df0107 --- /dev/null +++ b/crates/sourcemap/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "craft-sourcemap" +description = "Bounded Source Map v3 parser, source inventory and generated-position lookup" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true +[lints] +workspace = true +[dependencies] +serde = { workspace = true } +serde_json = { workspace = true } + +thiserror = { workspace = true } diff --git a/crates/sourcemap/src/lib.rs b/crates/sourcemap/src/lib.rs new file mode 100644 index 000000000..a3c23ffa2 --- /dev/null +++ b/crates/sourcemap/src/lib.rs @@ -0,0 +1,816 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +//! Source maps (revision 3), including indexed maps with sections. +//! +//! Lookups follow the "original positions for a generated position" rule: on the generated line, +//! take the greatest segment column not after the requested column and return every segment that +//! starts there. Lines are 0-based here (callers add one for display); columns are 0-based UTF-16 +//! code units as the format defines. + +use serde::de::{self, DeserializeSeed, MapAccess, SeqAccess, Visitor}; +use std::cell::Cell; +use std::fmt; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Limits { + /// Biggest map JSON accepted. + pub max_bytes: usize, + /// Most decoded segments across the whole map. + pub max_segments: usize, + /// Most generated lines across the whole map, including empty lines and inline sections. + pub max_lines: usize, + /// Deepest section nesting (the format allows one level; we tolerate a few). + pub max_depth: usize, + /// Most `sources`, `sourcesContent`, `names` and ignore-list entries and index-map sections + /// across the whole map, counted while the JSON is read, so a map of tiny entries is refused + /// before it is kept. An entry a repeated key replaces gives its slot back. + pub max_entries: usize, +} + +impl Default for Limits { + fn default() -> Self { + Limits { max_bytes: 64 * 1024 * 1024, max_segments: 4_000_000, max_lines: 1_000_000, max_depth: 8, max_entries: 4_000_000 } + } +} + +/// Limits on retained source names after applying each map's root. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct InventoryLimits { + pub max_sources: usize, + /// Total UTF-8 bytes in the expanded source paths, across all sections. + pub max_bytes: usize, +} + +impl Default for InventoryLimits { + fn default() -> Self { + Self { max_sources: 1_000_000, max_bytes: 64 * 1024 * 1024 } + } +} + +/// An ordered prefix of the map's source names, with their complete identities. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct SourceInventory { + pub sources: Vec, + /// More names existed than the limits or available allocations could retain. + pub truncated: bool, + /// Index-map sections that pointed at an external map nobody read, so the sources they + /// would add are unknown (not absent). + pub external_sections: usize, +} + +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum SourceMapError { + #[error("source map is {0} bytes; the limit is {1}")] + TooLarge(usize, usize), + #[error("source map is not valid JSON: {0}")] + BadJson(String), + #[error("not a version 3 source map")] + BadVersion, + #[error("source map mappings are malformed at segment {0}")] + BadMappings(usize), + #[error("source map index has invalid sections")] + BadSections, + #[error("source map has more than {0} segments")] + TooManySegments(usize), + #[error("source map has more than {0} generated lines")] + TooManyLines(usize), + #[error("could not reserve memory for generated source-map lines")] + LineAllocationFailed, + #[error("source map sections nest deeper than {0}")] + TooDeep(usize), + #[error("source map has more than {0} source, content, name, ignore-list and section entries")] + TooManyEntries(usize), +} + +/// One decoded mapping segment. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Segment { + pub generated_column: u32, + /// (source index, original line, original column, name index) + pub original: Option<(u32, u32, u32, Option)>, +} + +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct SourceMap { + pub file: Option, + pub source_root: Option, + pub sources: Vec>, + pub sources_content: Vec>, + pub names: Vec, + pub ignore_list: Vec, + /// Segments per generated line, sorted by column. + pub lines: Vec>, + pub segments: usize, + /// Indexed maps: (line offset, column offset, map). + pub sections: Vec<(u32, u32, SourceMap)>, + /// Index-map sections that point at an external map (`url`), which isn't fetched here: the + /// sources that map would add are unknown, not absent. + pub external_sections: usize, +} + +/// A position in an original source. +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)] +pub struct OriginalPosition { + pub source_index: u32, + pub source: Option, + /// 0-based. + pub line: u32, + /// 0-based. + pub column: u32, + pub name: Option, + /// 0-based generated column of the matched segment. + pub generated_column: u32, +} + +impl SourceMap { + /// The source path with `sourceRoot` prefixed before URL interpretation. + pub fn source_path(&self, index: u32) -> Option { + let s = self.sources.get(index as usize)?.as_ref()?; + // Generators commonly emit an empty root to mean no root. Keep those source identities + // unchanged even though the literal ECMA-426 prefix algorithm would prepend a slash. + Some(match self.source_root.as_deref().filter(|root| !root.is_empty()) { + Some(root) => { + let join = if root.ends_with('/') { "" } else { "/" }; + format!("{root}{join}{s}") + } + None => s.clone(), + }) + } + + pub fn source_content(&self, index: u32) -> Option<&str> { + self.sources_content.get(index as usize)?.as_deref() + } + + /// Every original position for a generated (line, column), both 0-based. + pub fn original_positions(&self, line: u32, column: u32) -> Vec { + if !self.sections.is_empty() { + // Last section that starts at or before the position. + let end = self.sections.partition_point(|(l, c, _)| (*l, *c) <= (line, column)); + let Some((l, c, map)) = end.checked_sub(1).and_then(|i| self.sections.get(i)) else { return Vec::new() }; + let rel_line = line.saturating_sub(*l); + let rel_col = if rel_line == 0 { column.saturating_sub(*c) } else { column }; + return map + .original_positions(rel_line, rel_col) + .into_iter() + .filter_map(|mut position| { + if rel_line == 0 { + position.generated_column = position.generated_column.checked_add(*c)?; + } + Some(position) + }) + .collect(); + } + let Some(segs) = self.lines.get(line as usize) else { return Vec::new() }; + // Greatest generated column <= column. + let idx = segs.partition_point(|s| s.generated_column <= column); + let Some(best) = idx.checked_sub(1).and_then(|i| segs.get(i)) else { return Vec::new() }; + let col = best.generated_column; + let first = segs.partition_point(|s| s.generated_column < col); + segs.get(first..idx) + .unwrap_or_default() + .iter() + .filter_map(|s| { + let (src, l, c, n) = s.original?; + Some(OriginalPosition { + source_index: src, + source: self.source_path(src), + line: l, + column: c, + name: n.and_then(|n| self.names.get(n as usize).cloned()), + generated_column: col, + }) + }) + .collect() + } + + /// Sources flattened across sections, within the default inventory limits. + /// Use `source_inventory` to choose limits and observe truncation. + pub fn all_sources(&self) -> Vec { + self.source_inventory(InventoryLimits::default()).sources + } + + /// Retain a bounded prefix, checking expanded lengths before constructing paths. + pub fn source_inventory(&self, limits: InventoryLimits) -> SourceInventory { + let mut sources = Vec::new(); + let mut bytes_left = limits.max_bytes; + let truncated = !self.collect_sources(limits.max_sources, &mut bytes_left, &mut sources); + SourceInventory { sources, truncated, external_sections: self.external_sections_total() } + } + + /// This map's external sections plus those of every map it holds. + fn external_sections_total(&self) -> usize { + self.sections.iter().fold(self.external_sections, |n, (_, _, map)| n.saturating_add(map.external_sections_total())) + } + + /// Append this map and its sections using one shared entry and byte budget. + fn collect_sources(&self, max_sources: usize, bytes_left: &mut usize, out: &mut Vec) -> bool { + let root = self.source_root.as_deref().unwrap_or_default(); + let join = if root.is_empty() || root.ends_with('/') { "" } else { "/" }; + for source in self.sources.iter().flatten() { + let Some(bytes) = root.len().checked_add(join.len()).and_then(|n| n.checked_add(source.len())) else { return false }; + if out.len() >= max_sources || bytes > *bytes_left { + return false; + } + let mut path = String::new(); + if out.try_reserve(1).is_err() || path.try_reserve_exact(bytes).is_err() { + return false; + } + path.push_str(root); + path.push_str(join); + path.push_str(source); + out.push(path); + *bytes_left = bytes_left.saturating_sub(bytes); + } + for (_, _, map) in &self.sections { + if !map.collect_sources(max_sources, bytes_left, out) { + return false; + } + } + true + } +} + +/// Parse a source map. +pub fn parse(json: &[u8], limits: Limits) -> Result { + read(json, limits, true) +} + +/// Read a map's sources, their content, names and ignore list without decoding its mappings or +/// checking its section offsets, so a map [`parse`] refuses for those +/// ([`SourceMapError::BadMappings`] or [`SourceMapError::BadSections`]) can still list its sources. +/// The map has no lines or segments. A map whose mappings, as far as they decode, name a source +/// past the end of its `sources` list is still refused, since the list is known to be short, and so are a section +/// that has neither a map nor a URL, or both, and a `sections` value that isn't an array. +pub fn parse_sources(json: &[u8], limits: Limits) -> Result { + read(json, limits, false) +} + +fn read(json: &[u8], limits: Limits, mappings: bool) -> Result { + if json.len() > limits.max_bytes { + return Err(SourceMapError::TooLarge(json.len(), limits.max_bytes)); + } + // Some servers prefix maps with `)]}'` to defeat XSSI; the format allows skipping that line. + let body = if json.starts_with(b")]}") { json.iter().position(|b| *b == b'\n').and_then(|p| json.get(p + 1..)).unwrap_or(&[]) } else { json }; + let entries = Entries { left: Cell::new(limits.max_entries), exhausted: Cell::new(false) }; + let mut de = serde_json::Deserializer::from_slice(body); + let raw = RawMapSeed(&entries).deserialize(&mut de).and_then(|raw| de.end().map(|()| raw)); + let raw = match raw { + Ok(raw) => raw, + Err(_) if entries.exhausted.get() => return Err(SourceMapError::TooManyEntries(limits.max_entries)), + Err(e) => return Err(SourceMapError::BadJson(e.to_string())), + }; + let mut budget = limits.max_segments; + let mut line_budget = limits.max_lines; + from_raw(raw, 0, limits, &mut budget, &mut line_budget, mappings) +} + +/// `mappings`: decode the mappings and check section offsets, as [`parse`] does. +fn from_raw( + raw: Option, + depth: usize, + limits: Limits, + budget: &mut usize, + line_budget: &mut usize, + mappings: bool, +) -> Result { + if depth > limits.max_depth { + return Err(SourceMapError::TooDeep(limits.max_depth)); + } + let Some(raw) = raw.filter(|raw| raw.version == Some(3)) else { + return Err(SourceMapError::BadVersion); + }; + if let Some(sections) = raw.sections { + // The index has its own file name. Only its section maps describe original sources. + let mut map = SourceMap { file: raw.file, ..Default::default() }; + let sections = sections.ok_or(SourceMapError::BadSections)?; + for section in sections { + let offset = |n: Option| n.and_then(|n| u32::try_from(n).ok()).ok_or(SourceMapError::BadSections); + let (line, col) = section.offset.unwrap_or_default(); + let (line, col) = if mappings { (offset(line)?, offset(col)?) } else { (offset(line).unwrap_or(0), offset(col).unwrap_or(0)) }; + if mappings && map.sections.last().is_some_and(|(l, c, _)| (*l, *c) > (line, col)) { + return Err(SourceMapError::BadSections); + } + let (sub, external) = match (section.map, section.url) { + (Some(m), None) => (from_raw(m.map(|m| *m), depth + 1, limits, budget, line_budget, mappings)?, false), + // External maps are never fetched here. Retain their start so a lookup + // cannot leak through to the previous inline map, and count it so the + // sources it would add are reported unknown rather than absent. + (None, Some(true)) => (SourceMap::default(), true), + _ => return Err(SourceMapError::BadSections), + }; + if external { + map.external_sections = map.external_sections.saturating_add(1); + } + map.segments = map.segments.saturating_add(sub.segments); + map.sections.push((line, col, sub)); + } + return Ok(map); + } + let mut map = SourceMap { + file: raw.file, + source_root: raw.source_root, + sources: raw.sources, + sources_content: raw.sources_content, + names: raw.names.into_iter().map(Option::unwrap_or_default).collect(), + ignore_list: raw + .ignore_list + .or(raw.google_ignore_list) + .unwrap_or_default() + .into_iter() + .filter_map(|n| n.and_then(|n| u32::try_from(n).ok())) + .collect(), + ..Default::default() + }; + let m = raw.mappings.as_deref().unwrap_or_default(); + if mappings { + decode_mappings(m, &mut map, budget, line_budget, limits.max_lines)?; + } else if let Some(count) = past_sources(m, map.sources.len()) { + return Err(SourceMapError::BadMappings(count)); + } + Ok(map) +} + +/// The entry budget shared by every list in a map and its sections while the JSON is read. +struct Entries { + left: Cell, + exhausted: Cell, +} + +impl Entries { + fn take(&self) -> Result<(), E> { + let Some(left) = self.left.get().checked_sub(1) else { + self.exhausted.set(true); + return Err(E::custom("too many source-map entries")); + }; + self.left.set(left); + Ok(()) + } + + /// Return the slots of entries that won't be kept after all. Callers drop those entries + /// first, so a replaced list and its replacement are never held together. + fn give_back(&self, n: usize) { + self.left.set(self.left.get().saturating_add(n)); + } +} + +/// The fields a map is built from, read without keeping anything else. Any JSON value is +/// accepted where the format expects another kind; it reads as absent, as before. +#[derive(Default)] +struct RawMap { + version: Option, + file: Option, + source_root: Option, + sources: Vec>, + sources_content: Vec>, + names: Vec>, + /// Present even when it isn't an array, so `x_google_ignoreList` stays unused then. + ignore_list: Option>>, + google_ignore_list: Option>>, + mappings: Option, + /// `Some(None)`: present but not an array. + sections: Option>>, +} + +impl RawMap { + /// The entry slots this map and its sections hold. + fn held(&self) -> usize { + let lists = |l: &Option>>| l.as_ref().map_or(0, Vec::len); + self.sources.len() + + self.sources_content.len() + + self.names.len() + + lists(&self.ignore_list) + + lists(&self.google_ignore_list) + + held_by(&self.sections) + } +} + +fn held_by(sections: &Option>>) -> usize { + sections.iter().flatten().flatten().map(|s| 1 + s.map.iter().flatten().map(|m| m.held()).sum::()).sum() +} + +#[derive(Default)] +struct RawSection { + offset: Option<(Option, Option)>, + /// `Some(None)`: present but not an object. Boxed, so a section that isn't a map stays small. + map: Option>>, + /// Whether the URL is a string. + url: Option, +} + +/// Visitor methods that read any other kind of value, without keeping it, as the default. +macro_rules! skip { + ($de:lifetime; bool) => { fn visit_bool(self, _: bool) -> Result { Ok(Default::default()) } }; + ($de:lifetime; i64) => { fn visit_i64(self, _: i64) -> Result { Ok(Default::default()) } }; + ($de:lifetime; u64) => { fn visit_u64(self, _: u64) -> Result { Ok(Default::default()) } }; + ($de:lifetime; f64) => { fn visit_f64(self, _: f64) -> Result { Ok(Default::default()) } }; + ($de:lifetime; str) => { fn visit_str(self, _: &str) -> Result { Ok(Default::default()) } }; + ($de:lifetime; unit) => { fn visit_unit(self) -> Result { Ok(Default::default()) } }; + ($de:lifetime; seq) => { + fn visit_seq>(self, mut seq: A) -> Result { + while seq.next_element_seed(Skip)?.is_some() {} + Ok(Default::default()) + } + }; + ($de:lifetime; map) => { + fn visit_map>(self, mut map: A) -> Result { + while map.next_key_seed(Skip)?.is_some() { + map.next_value_seed(Skip)?; + } + Ok(Default::default()) + } + }; + ($de:lifetime; $($kind:ident),+) => { $(skip!($de; $kind);)+ }; +} + +/// Read a value the map doesn't keep, with the checks a full read makes (string escapes and +/// UTF-8, number range, nesting depth), so skipping it can't let a broken map through. +#[derive(Clone, Copy)] +struct Skip; + +impl<'de> DeserializeSeed<'de> for Skip { + type Value = (); + fn deserialize>(self, d: D) -> Result<(), D::Error> { + d.deserialize_any(self) + } +} + +impl<'de> Visitor<'de> for Skip { + type Value = (); + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("any JSON value") + } + skip!('de; bool, i64, u64, f64, str, unit, seq, map); +} + +/// A string, or `None` for any other value. +#[derive(Clone, Copy)] +struct MaybeStr; + +impl<'de> Visitor<'de> for MaybeStr { + type Value = Option; + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("any JSON value") + } + fn visit_str(self, s: &str) -> Result { + Ok(Some(s.to_owned())) + } + fn visit_string(self, s: String) -> Result { + Ok(Some(s)) + } + skip!('de; bool, i64, u64, f64, unit, seq, map); +} + +/// A non-negative integer, or `None` for any other value. +#[derive(Clone, Copy)] +struct MaybeU64; + +impl<'de> Visitor<'de> for MaybeU64 { + type Value = Option; + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("any JSON value") + } + fn visit_u64(self, n: u64) -> Result { + Ok(Some(n)) + } + skip!('de; bool, i64, f64, str, unit, seq, map); +} + +/// One value read through `V`, after taking its slot from the entry budget. +struct Entry<'a, V>(&'a Entries, V); + +impl<'de, V: Visitor<'de>> DeserializeSeed<'de> for Entry<'_, V> { + type Value = V::Value; + fn deserialize>(self, d: D) -> Result { + self.0.take()?; + d.deserialize_any(self.1) + } +} + +/// A list whose entries count against the budget; any other value reads as an empty list. +struct EntryList<'a, V>(&'a Entries, V); + +impl<'de, V: Visitor<'de> + Copy> Visitor<'de> for EntryList<'_, V> { + type Value = Vec; + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("any JSON value") + } + fn visit_seq>(self, mut seq: A) -> Result { + let mut out = Vec::new(); + while let Some(value) = seq.next_element_seed(Entry(self.0, self.1))? { + out.push(value); + } + Ok(out) + } + skip!('de; bool, i64, u64, f64, str, unit, map); +} + +impl<'de, V: Visitor<'de> + Copy> DeserializeSeed<'de> for EntryList<'_, V> { + type Value = Vec; + fn deserialize>(self, d: D) -> Result { + d.deserialize_any(self) + } +} + +/// A map object, or `None` for any other value. +#[derive(Clone, Copy)] +struct RawMapSeed<'a>(&'a Entries); + +impl<'de> Visitor<'de> for RawMapSeed<'_> { + type Value = Option; + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("any JSON value") + } + fn visit_map>(self, mut fields: A) -> Result { + let mut raw = RawMap::default(); + // A repeated key replaces the earlier value, as it did when the whole JSON was read. + while let Some(key) = fields.next_key::()? { + match key { + Field::Version => raw.version = fields.next_value_seed(Any(MaybeU64))?, + Field::File => raw.file = fields.next_value_seed(Any(MaybeStr))?, + Field::SourceRoot => raw.source_root = fields.next_value_seed(Any(MaybeStr))?, + Field::Sources => { + self.0.give_back(std::mem::take(&mut raw.sources).len()); + raw.sources = fields.next_value_seed(EntryList(self.0, MaybeStr))?; + } + Field::SourcesContent => { + self.0.give_back(std::mem::take(&mut raw.sources_content).len()); + raw.sources_content = fields.next_value_seed(EntryList(self.0, MaybeStr))?; + } + Field::Names => { + self.0.give_back(std::mem::take(&mut raw.names).len()); + raw.names = fields.next_value_seed(EntryList(self.0, MaybeStr))?; + } + // `ignoreList` wins over `x_google_ignoreList` wherever either appears. + Field::IgnoreList => { + let unused = raw.ignore_list.take().into_iter().chain(raw.google_ignore_list.take()); + self.0.give_back(unused.map(|l| l.len()).sum()); + raw.ignore_list = Some(fields.next_value_seed(EntryList(self.0, MaybeU64))?); + } + Field::GoogleIgnoreList if raw.ignore_list.is_some() => fields.next_value_seed(Skip)?, + Field::GoogleIgnoreList => { + self.0.give_back(raw.google_ignore_list.take().map_or(0, |l| l.len())); + raw.google_ignore_list = Some(fields.next_value_seed(EntryList(self.0, MaybeU64))?); + } + Field::Mappings => raw.mappings = fields.next_value_seed(Any(MaybeStr))?, + Field::Sections => { + self.0.give_back(held_by(&raw.sections.take())); + raw.sections = Some(fields.next_value_seed(Any(Sections(self.0)))?); + } + Field::Other => fields.next_value_seed(Skip)?, + } + } + Ok(Some(raw)) + } + skip!('de; bool, i64, u64, f64, str, unit, seq); +} + +impl<'de> DeserializeSeed<'de> for RawMapSeed<'_> { + type Value = Option; + fn deserialize>(self, d: D) -> Result { + d.deserialize_any(self) + } +} + +/// Read any JSON value through `V`. +struct Any(V); + +impl<'de, V: Visitor<'de>> DeserializeSeed<'de> for Any { + type Value = V::Value; + fn deserialize>(self, d: D) -> Result { + d.deserialize_any(self.0) + } +} + +#[derive(serde::Deserialize)] +#[serde(field_identifier, rename_all = "camelCase")] +enum Field { + Version, + File, + SourceRoot, + Sources, + SourcesContent, + Names, + IgnoreList, + #[serde(rename = "x_google_ignoreList")] + GoogleIgnoreList, + Mappings, + Sections, + #[serde(other)] + Other, +} + +/// The sections array, or `None` for any other value. +struct Sections<'a>(&'a Entries); + +impl<'de> Visitor<'de> for Sections<'_> { + type Value = Option>; + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("any JSON value") + } + fn visit_seq>(self, mut seq: A) -> Result { + let mut out = Vec::new(); + while let Some(section) = seq.next_element_seed(Entry(self.0, Section(self.0)))? { + out.push(section); + } + Ok(Some(out)) + } + skip!('de; bool, i64, u64, f64, str, unit, map); +} + +/// One section; any value that isn't an object reads as a section without an offset. +struct Section<'a>(&'a Entries); + +impl<'de> Visitor<'de> for Section<'_> { + type Value = RawSection; + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("any JSON value") + } + fn visit_map>(self, mut fields: A) -> Result { + let mut section = RawSection::default(); + while let Some(key) = fields.next_key::()? { + match key { + SectionField::Offset => section.offset = Some(fields.next_value_seed(Any(Offset))?), + SectionField::Map => { + self.0.give_back(section.map.take().flatten().map_or(0, |m| m.held())); + section.map = Some(fields.next_value_seed(RawMapSeed(self.0))?.map(Box::new)); + } + SectionField::Url => section.url = Some(fields.next_value_seed(Any(MaybeStr))?.is_some()), + SectionField::Other => fields.next_value_seed(Skip)?, + } + } + Ok(section) + } + skip!('de; bool, i64, u64, f64, str, unit, seq); +} + +#[derive(serde::Deserialize)] +#[serde(field_identifier, rename_all = "lowercase")] +enum SectionField { + Offset, + Map, + Url, + #[serde(other)] + Other, +} + +/// A section offset's line and column; any value that isn't an object has neither. +struct Offset; + +impl<'de> Visitor<'de> for Offset { + type Value = (Option, Option); + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("any JSON value") + } + fn visit_map>(self, mut fields: A) -> Result { + let (mut line, mut column) = (None, None); + while let Some(key) = fields.next_key::()? { + match key { + OffsetField::Line => line = fields.next_value_seed(Any(MaybeU64))?, + OffsetField::Column => column = fields.next_value_seed(Any(MaybeU64))?, + OffsetField::Other => fields.next_value_seed(Skip)?, + } + } + Ok((line, column)) + } + skip!('de; bool, i64, u64, f64, str, unit, seq); +} + +#[derive(serde::Deserialize)] +#[serde(field_identifier, rename_all = "lowercase")] +enum OffsetField { + Line, + Column, + #[serde(other)] + Other, +} + +fn b64(c: u8) -> Option { + Some(match c { + b'A'..=b'Z' => i64::from(c - b'A'), + b'a'..=b'z' => i64::from(c - b'a') + 26, + b'0'..=b'9' => i64::from(c - b'0') + 52, + b'+' => 62, + b'/' => 63, + _ => return None, + }) +} + +/// Decode one VLQ field from `bytes` at `*pos`. +fn vlq(bytes: &[u8], pos: &mut usize) -> Option { + let mut result = 0u32; + let mut shift = 0u32; + loop { + let d = b64(*bytes.get(*pos)?)? as u32; + *pos += 1; + let bits = d & 31; + if bits != 0 { + if bits > u32::MAX.checked_shr(shift)? { + return None; + } + result |= bits << shift; + } + if d & 32 == 0 { + break; + } + // Redundant zero groups do not change the value. + shift = shift.saturating_add(5); + } + let magnitude = i64::from(result >> 1); + Some(match (result & 1, magnitude) { + (1, 0) => i64::from(i32::MIN), + (1, n) => -n, + _ => magnitude, + }) +} + +/// Decode mapping deltas, rejecting fields that cannot name a declared source or symbol. +fn decode_mappings(m: &str, map: &mut SourceMap, budget: &mut usize, line_budget: &mut usize, line_limit: usize) -> Result<(), SourceMapError> { + let bytes = m.as_bytes(); + let (mut src, mut ol, mut oc, mut name) = (0i64, 0i64, 0i64, 0i64); + let mut pos = 0usize; + let mut line: Vec = Vec::new(); + let mut col = 0i64; + let mut count = 0usize; + let to_u32 = |v: i64, count: usize| u32::try_from(v).map_err(|_| SourceMapError::BadMappings(count)); + while pos <= bytes.len() { + match bytes.get(pos) { + None | Some(b';') => { + if *line_budget == 0 { + return Err(SourceMapError::TooManyLines(line_limit)); + } + *line_budget -= 1; + line.sort_by_key(|s| s.generated_column); + map.lines.try_reserve(1).map_err(|_| SourceMapError::LineAllocationFailed)?; + map.lines.push(std::mem::take(&mut line)); + col = 0; + pos += 1; + continue; + } + Some(b',') => { + pos += 1; + continue; + } + _ => {} + } + if *budget == 0 { + return Err(SourceMapError::TooManySegments(count)); + } + *budget -= 1; + count += 1; + col += vlq(bytes, &mut pos).ok_or(SourceMapError::BadMappings(count))?; + let mut seg = Segment { generated_column: to_u32(col, count)?, original: None }; + if !matches!(bytes.get(pos), None | Some(b',') | Some(b';')) { + src += vlq(bytes, &mut pos).ok_or(SourceMapError::BadMappings(count))?; + ol += vlq(bytes, &mut pos).ok_or(SourceMapError::BadMappings(count))?; + oc += vlq(bytes, &mut pos).ok_or(SourceMapError::BadMappings(count))?; + let mut n = None; + if !matches!(bytes.get(pos), None | Some(b',') | Some(b';')) { + name += vlq(bytes, &mut pos).ok_or(SourceMapError::BadMappings(count))?; + n = Some(to_u32(name, count)?); + } + let source = to_u32(src, count)?; + if source as usize >= map.sources.len() || n.is_some_and(|index| index as usize >= map.names.len()) { + return Err(SourceMapError::BadMappings(count)); + } + seg.original = Some((source, to_u32(ol, count)?, to_u32(oc, count)?, n)); + } + if !matches!(bytes.get(pos), None | Some(b',') | Some(b';')) { + return Err(SourceMapError::BadMappings(count)); + } + line.push(seg); + } + map.segments = count; + Ok(()) +} + +/// The segment, counted as [`decode_mappings`] does, that names a source past the end of a list +/// of `sources`. Mappings that can't be decoded end the search: past them nothing is known. +fn past_sources(m: &str, sources: usize) -> Option { + let bytes = m.as_bytes(); + let (mut pos, mut src, mut count) = (0usize, 0i64, 0usize); + let at_end = |pos: usize| matches!(bytes.get(pos), None | Some(b',') | Some(b';')); + while pos < bytes.len() { + if matches!(bytes[pos], b',' | b';') { + pos += 1; + continue; + } + count += 1; + vlq(bytes, &mut pos)?; + if at_end(pos) { + continue; + } + src += vlq(bytes, &mut pos)?; + // A negative index is wrong too, but the deltas after it still decode. + if usize::try_from(src).is_ok_and(|src| src >= sources) { + return Some(count); + } + while !at_end(pos) { + vlq(bytes, &mut pos)?; + } + } + None +} + +#[cfg(test)] +mod tests; diff --git a/crates/sourcemap/src/tests.rs b/crates/sourcemap/src/tests.rs new file mode 100644 index 000000000..414f3b64c --- /dev/null +++ b/crates/sourcemap/src/tests.rs @@ -0,0 +1,122 @@ +use super::*; +#[test] +fn mapping_indices_must_refer_to_declared_entries() { + for mappings in ["ACAA", "AAAAC"] { + let json = serde_json::json!({"version":3,"sources":["source.js"],"names":["name"],"mappings":mappings}); + assert!(matches!(parse(json.to_string().as_bytes(), Limits::default()), Err(SourceMapError::BadMappings(1)))); + } + let valid = br#"{"version":3,"sources":[null],"names":["name"],"mappings":"AAAAA"}"#; + assert!(parse(valid, Limits::default()).is_ok()); +} + +#[test] +fn lookup_keeps_coincident_mappings_and_stops_at_an_unmapped_segment() { + let map = parse(br#"{"version":3,"sources":["a","b"],"mappings":"EAAA,ACAA,E"}"#, Limits::default()).unwrap(); + assert!(map.original_positions(0, 1).is_empty()); + let matches = map.original_positions(0, 3); + assert_eq!(matches.iter().map(|p| p.source_index).collect::>(), [0, 1]); + assert!(map.original_positions(0, 4).is_empty()); +} + +#[test] +fn relative_sources_keep_the_url_root_delimiter() { + for (root, expected) in [ + ("webpack:///", "webpack:///src/main.js"), + ("file:///", "file:///src/main.js"), + ("https://host/scripts/", "https://host/scripts/src/main.js"), + ("src", "src/src/main.js"), + ] { + let json = serde_json::json!({"version":3,"sourceRoot":root,"sources":["src/main.js"],"mappings":"AAAA"}); + let map = parse(json.to_string().as_bytes(), Limits::default()).unwrap(); + assert_eq!(map.all_sources(), [expected]); + assert_eq!(map.original_positions(0, 0)[0].source.as_deref(), Some(expected)); + } +} + +#[test] +fn decodes_and_looks_up() { + // Generated "a;b" from two lines of src.js. + let m = br#"{"version":3,"sources":["src.js"],"names":["foo"],"mappings":"AAAAA,EAAE;AACA","sourcesContent":["x\ny"]}"#; + let map = parse(m, Limits::default()).unwrap(); + assert_eq!(map.lines.len(), 2); + let p = map.original_positions(0, 1); + assert_eq!(p[0].source.as_deref(), Some("src.js")); + assert_eq!(p[0].name.as_deref(), Some("foo")); + assert_eq!((p[0].line, p[0].column), (0, 0)); + let q = map.original_positions(0, 2); + assert_eq!(q[0].column, 2); + let r = map.original_positions(1, 0); + assert_eq!(r[0].line, 1); + assert!(map.original_positions(5, 0).is_empty()); + assert_eq!(map.source_content(0), Some("x\ny")); +} + +#[test] +fn indexed_maps() { + let m = br#"{"version":3,"sections":[{"offset":{"line":0,"column":0},"map":{"version":3,"sources":["a.js"],"names":[],"mappings":"AAAA"}},{"offset":{"line":1,"column":0},"map":{"version":3,"sources":["b.js"],"names":[],"mappings":"AAAA"}}]}"#; + let map = parse(m, Limits::default()).unwrap(); + assert_eq!(map.original_positions(1, 3)[0].source.as_deref(), Some("b.js")); + assert_eq!(map.all_sources(), vec!["a.js", "b.js"]); +} + +#[test] +fn validates_index_map_shape_and_offsets() { + let inline = serde_json::json!({"version":3,"mappings":"A"}); + for sections in [ + serde_json::json!({}), + serde_json::json!([{"map":inline}]), + serde_json::json!([{"offset":{"line":-1,"column":0},"map":inline}]), + serde_json::json!([{"offset":{"line":0,"column":4294967296_u64},"map":inline}]), + serde_json::json!([{"offset":{"line":0,"column":0}}]), + serde_json::json!([{"offset":{"line":0,"column":0},"map":inline,"url":"later.map"}]), + serde_json::json!([{"offset":{"line":0,"column":2},"map":inline},{"offset":{"line":0,"column":1},"map":inline}]), + ] { + let json = serde_json::json!({"version":3,"sections":sections}).to_string(); + assert_eq!(parse(json.as_bytes(), Limits::default()), Err(SourceMapError::BadSections)); + } +} + +#[test] +fn an_external_section_ends_the_previous_inline_mapping() { + let inline = serde_json::json!({"version":3,"sources":["first.js"],"mappings":"AAAA"}); + let sections = serde_json::json!([ + {"offset":{"line":0,"column":0},"map":inline}, + {"offset":{"line":0,"column":4},"url":"external.map"}, + {"offset":{"line":1,"column":0},"map":inline} + ]); + let json = serde_json::json!({"version":3,"sections":sections}).to_string(); + let map = parse(json.as_bytes(), Limits::default()).unwrap(); + assert_eq!(map.original_positions(0, 3)[0].source.as_deref(), Some("first.js")); + assert!(map.original_positions(0, 4).is_empty()); + assert_eq!(map.original_positions(1, 0)[0].generated_column, 0); +} + +#[test] +fn vlq_signed_extremes_and_overflow() { + for (field, value) in [ + ("B", Some(i64::from(i32::MIN))), + ("+/////D", Some(i64::from(i32::MAX))), + ("//////D", Some(-i64::from(i32::MAX))), + ("ggggggE", None), + ("ggggggggggA", Some(0)), + ] { + assert_eq!(vlq(field.as_bytes(), &mut 0), value, "{field}"); + } + let map = parse(br#"{"version":3,"mappings":"+/////D,C,B"}"#, Limits::default()).unwrap(); + assert_eq!(map.lines[0].iter().map(|s| s.generated_column).collect::>(), [0, 2147483647, 2147483648]); + assert!(parse(br#"{"version":3,"mappings":"B"}"#, Limits::default()).is_err()); +} + +#[test] +fn hostile_mappings_do_not_panic() { + for m in ["", ";;;", "A", "gggggggggggggggggggggggggg", "AAAA,", "////", "AAAAAAAAAAAAAAA", "D", "AD,AA", "+", "!!"] { + let j = format!(r#"{{"version":3,"sources":[],"names":[],"mappings":"{m}"}}"#); + if let Ok(map) = parse(j.as_bytes(), Limits::default()) { + let _ = map.original_positions(0, 0); + let _ = map.original_positions(u32::MAX, u32::MAX); + } + } + let tight = Limits { max_segments: 1, ..Limits::default() }; + assert!(parse(br#"{"version":3,"sources":[],"names":[],"mappings":"A,A"}"#, tight).is_err()); + assert!(parse(b"[]", Limits::default()).is_err()); +} diff --git a/crates/sourcemap/tests/entry_budgets.rs b/crates/sourcemap/tests/entry_budgets.rs new file mode 100644 index 000000000..65ae533ba --- /dev/null +++ b/crates/sourcemap/tests/entry_budgets.rs @@ -0,0 +1,145 @@ +use craft_sourcemap::{Limits, SourceMap, SourceMapError, parse}; +use serde_json::json; + +fn with(max_entries: usize) -> Limits { + Limits { max_entries, ..Limits::default() } +} + +fn decode(value: serde_json::Value, max_entries: usize) -> Result { + parse(&serde_json::to_vec(&value).unwrap(), with(max_entries)) +} + +#[test] +fn entries_up_to_the_limit_are_kept() { + let value = json!({"version":3,"sources":["a.js","b.js"],"sourcesContent":["a();",null],"names":["x"],"mappings":"AAAAA,CCAA"}); + let map = decode(value.clone(), 5).unwrap(); + assert_eq!(map.sources, vec![Some("a.js".to_string()), Some("b.js".to_string())]); + assert_eq!(map.sources_content, vec![Some("a();".to_string()), None]); + assert_eq!(map.names, vec!["x".to_string()]); + assert_eq!(decode(value, 4), Err(SourceMapError::TooManyEntries(4))); +} + +#[test] +fn every_kind_of_entry_counts_once() { + for value in [ + json!({"version":3,"sources":["a","b","c"],"mappings":""}), + json!({"version":3,"sourcesContent":["a","b","c"],"mappings":""}), + json!({"version":3,"names":["a","b","c"],"mappings":""}), + json!({"version":3,"ignoreList":[0,1,2],"mappings":""}), + json!({"version":3,"x_google_ignoreList":[0,1,2],"mappings":""}), + // Entries that are not strings still take a slot each. + json!({"version":3,"sources":[null, 1, {"a":[1,2,3]}],"mappings":""}), + ] { + assert_eq!(decode(value.clone(), 2), Err(SourceMapError::TooManyEntries(2)), "{value}"); + assert!(decode(value.clone(), 3).is_ok(), "{value}"); + } +} + +#[test] +fn entries_a_repeated_key_replaces_give_their_slots_back() { + let json = br#"{"version":3,"mappings":"","sources":["a","b"],"sources":["c"],"names":["x"],"names":[]}"#; + let map = parse(json, with(2)).unwrap(); + assert_eq!(map.sources, vec![Some("c".to_string())]); + assert!(map.names.is_empty()); + // `ignoreList` is kept over `x_google_ignoreList`, before or after it. + let first = br#"{"version":3,"mappings":"","x_google_ignoreList":[0,0],"ignoreList":[0]}"#; + assert_eq!(parse(first, with(2)).unwrap().ignore_list, vec![0]); + let after = br#"{"version":3,"mappings":"","ignoreList":[0],"x_google_ignoreList":[0,0,0]}"#; + assert_eq!(parse(after, with(1)).unwrap().ignore_list, vec![0]); + // A replaced sections array gives back its sections and what their maps held: without that, + // the second array would need slots four and five. + let sections = br#"{"version":3,"sections":[{"offset":{"line":0,"column":0},"map":{"version":3,"sources":["a"],"mappings":""}},{"offset":{"line":1,"column":0},"url":"b.map"}],"sections":[{"offset":{"line":0,"column":0},"url":"c.map"}]}"#; + assert_eq!(parse(sections, with(3)).unwrap().sections.len(), 1); + // So does a section's replaced map: one section, then one source instead of two. + let map = br#"{"version":3,"sections":[{"offset":{"line":0,"column":0},"map":{"version":3,"sources":["a","b"],"mappings":""},"map":{"version":3,"sources":["c"],"mappings":""}}]}"#; + assert_eq!(parse(map, with(3)).unwrap().sections[0].2.sources, vec![Some("c".to_string())]); +} + +#[test] +fn sections_and_their_maps_share_the_budget() { + let section = |line: u32, name: &str| json!({"offset":{"line":line,"column":0},"map":{"version":3,"sources":[name],"mappings":"AAAA"}}); + let value = json!({"version":3,"sections":[section(0, "a.js"), section(1, "b.js")]}); + // Two sections and one source in each. + assert_eq!(decode(value.clone(), 4).unwrap().sections.len(), 2); + assert_eq!(decode(value, 3), Err(SourceMapError::TooManyEntries(3))); + // Sections without a map count too, so a long list of them is refused early. + let external = json!({"offset":{"line":0,"column":0},"url":"a.map"}); + let value = json!({"version":3,"sections":[external.clone(), external.clone(), external]}); + assert_eq!(decode(value.clone(), 3).unwrap().sections.len(), 3); + assert_eq!(decode(value, 2), Err(SourceMapError::TooManyEntries(2))); + let not_sections = br#"{"version":3,"sections":[0,0,0]}"#; + assert_eq!(parse(not_sections, with(2)), Err(SourceMapError::TooManyEntries(2))); + assert_eq!(parse(not_sections, with(3)), Err(SourceMapError::BadSections)); +} + +#[test] +fn reading_stops_at_the_limit() { + // Everything after the entry that goes over the limit is never read: the broken tail would + // otherwise be reported as bad JSON. + let json = br#"{"version":3,"sources":["a","b","c","d" this is not JSON"#; + assert_eq!(parse(json, with(2)), Err(SourceMapError::TooManyEntries(2))); + assert!(matches!(parse(json, Limits::default()), Err(SourceMapError::BadJson(_)))); +} + +#[test] +fn unknown_fields_are_skipped_without_counting() { + let value = json!({"version":3,"x_vendor":[1,2,3,4,5,6,7,8],"sources":["a.js"],"mappings":"AAAA"}); + assert_eq!(decode(value, 1).unwrap().sources.len(), 1); +} + +#[test] +fn values_the_map_does_not_keep_are_still_checked() { + let deep = format!("{{\"version\":3,\"mappings\":\"\",\"x\":{}{}}}", "[".repeat(200), "]".repeat(200)); + for json in [ + br#"{"version":3,"mappings":"","x":"\ud800"}"#.as_slice(), + br#"{"version":3,"mappings":"","sources":{"a":"\udc00"}}"#, + br#"{"version":3,"mappings":"","x":1e400}"#, + br#"{"version":3,"mappings":"","sources":[{"a":1e400}]}"#, + b"{\"version\":3,\"mappings\":\"\",\"x\":\"\xff\"}", + br#"{"version":3,"mappings":"","x":[1,]}"#, + deep.as_bytes(), + ] { + let got = parse(json, Limits::default()); + let full = serde_json::from_slice::(json).unwrap_err().to_string(); + assert_eq!(got, Err(SourceMapError::BadJson(full)), "{}", String::from_utf8_lossy(json)); + } +} + +#[test] +fn a_hostile_map_of_empty_names_is_refused_by_default() { + // About 21 million empty strings fit in the 64 MiB a map may have. + let limit = Limits::default().max_entries; + let names = vec!["\"\""; limit + 1].join(","); + let json = format!("{{\"version\":3,\"mappings\":\"\",\"names\":[{names}]}}").into_bytes(); + assert_eq!(parse(&json, Limits::default()), Err(SourceMapError::TooManyEntries(limit))); + // The same map is well formed: with room for every name it reads. + assert_eq!(parse(&json, with(limit + 1)).unwrap().names.len(), limit + 1); +} + +#[test] +fn sources_are_read_from_a_map_with_broken_mappings_or_offsets() { + use craft_sourcemap::parse_sources; + let bad = br#"{"version":3,"sourceRoot":"src","sources":["a.js"],"sourcesContent":["a();"],"names":["x"],"mappings":"!!"}"#; + assert_eq!(parse(bad, Limits::default()), Err(SourceMapError::BadMappings(1))); + let map = parse_sources(bad, Limits::default()).unwrap(); + assert_eq!((map.source_path(0).as_deref(), map.sources_content.len(), map.names.len(), map.lines.len()), (Some("src/a.js"), 1, 1, 0)); + let unordered = br#"{"version":3,"sections":[{"offset":{"line":2,"column":0},"map":{"version":3,"sources":["a.js"],"mappings":"AAAA"}},{"offset":{"line":-1,"column":0},"map":{"version":3,"sources":["b.js"],"mappings":""}},{"offset":{"line":0,"column":0},"map":{"version":3,"sources":["c.js"],"mappings":"!"}}]}"#; + assert_eq!(parse(unordered, Limits::default()), Err(SourceMapError::BadSections)); + let map = parse_sources(unordered, Limits::default()).unwrap(); + assert_eq!(map.source_inventory(Default::default()).sources, vec!["a.js".to_string(), "b.js".to_string(), "c.js".to_string()]); + // Past mappings that can't be decoded, the indices aren't known. + assert_eq!(parse_sources(br#"{"version":3,"sources":["a"],"mappings":"AAAA,!,ACAA"}"#, Limits::default()).unwrap().sources.len(), 1); + // What a section holds must still be known, and the version and limits still apply. + for (json, limit, err) in [ + (br#"{"version":3,"sections":[{"offset":{"line":0,"column":0}}]}"#.as_slice(), 9, SourceMapError::BadSections), + (br#"{"version":3,"sections":{}}"#, 9, SourceMapError::BadSections), + (br#"{"version":3,"sections":[{"offset":{"line":0,"column":0},"map":{"version":2,"sources":["a"]}}]}"#, 9, SourceMapError::BadVersion), + (br#"{"version":3,"sources":["a","b"],"mappings":"!"}"#, 1, SourceMapError::TooManyEntries(1)), + // Mappings that name a source past the end of the list show it is short. + (br#"{"version":3,"mappings":"AAAA"}"#, 9, SourceMapError::BadMappings(1)), + (br#"{"version":3,"sources":["a"],"mappings":"AAAA;ACAA,!"}"#, 9, SourceMapError::BadMappings(2)), + (br#"{"version":3,"sources":["a"],"mappings":"AAAA,ADAA,AEAA"}"#, 9, SourceMapError::BadMappings(3)), + ] { + assert_eq!(parse_sources(json, with(limit)), Err(err), "{}", String::from_utf8_lossy(json)); + } +} diff --git a/crates/sourcemap/tests/external_sections.rs b/crates/sourcemap/tests/external_sections.rs new file mode 100644 index 000000000..643cd52b1 --- /dev/null +++ b/crates/sourcemap/tests/external_sections.rs @@ -0,0 +1,84 @@ +//! An index-map section that points at an external map (`url`) adds no sources here, but its +//! sources are unknown rather than absent: the inventory says how many such sections it saw. + +use craft_sourcemap::{InventoryLimits, Limits, SourceMap, parse, parse_sources}; +use serde_json::{Value, json}; + +fn decode(value: Value) -> SourceMap { + parse(&serde_json::to_vec(&value).unwrap(), Limits::default()).unwrap() +} + +/// An external section adds no sources but is counted, so the inventory can say part of the map +/// was never read. +#[test] +fn an_external_section_is_counted_and_adds_no_sources() { + let map = decode(json!({ + "version": 3, + "sections": [ + {"offset": {"line": 0, "column": 0}, "map": {"version": 3, "sources": ["a.js"], "mappings": ""}}, + {"offset": {"line": 10, "column": 0}, "url": "vendor.js.map"} + ] + })); + assert_eq!(map.all_sources(), ["a.js"]); + assert_eq!(map.external_sections, 1); + let inventory = map.source_inventory(InventoryLimits::default()); + assert_eq!(inventory.sources, ["a.js"]); + assert!(!inventory.truncated); + assert_eq!(inventory.external_sections, 1); +} + +/// The count reaches an external section inside a nested index map. +#[test] +fn external_sections_are_counted_through_nested_index_maps() { + let map = decode(json!({ + "version": 3, + "sections": [ + {"offset": {"line": 0, "column": 0}, "map": {"version": 3, "sections": [ + {"offset": {"line": 0, "column": 0}, "map": {"version": 3, "sources": ["a.js"], "mappings": ""}}, + {"offset": {"line": 4, "column": 0}, "url": "inner.js.map"} + ]}}, + {"offset": {"line": 20, "column": 0}, "url": "outer.js.map"} + ] + })); + assert_eq!(map.all_sources(), ["a.js"]); + assert_eq!(map.source_inventory(InventoryLimits::default()).external_sections, 2); +} + +/// A map with only inline sections reports nothing unread. +#[test] +fn an_inline_only_map_counts_no_external_sections() { + let map = decode(json!({ + "version": 3, + "sections": [{"offset": {"line": 0, "column": 0}, "map": {"version": 3, "sources": ["a.js"], "mappings": ""}}] + })); + assert_eq!(map.source_inventory(InventoryLimits::default()).external_sections, 0); +} + +/// A map read for its sources alone (its mappings are refused) still says an external section +/// left part of the list unread. +#[test] +fn a_sources_only_read_still_counts_external_sections() { + let bytes = serde_json::to_vec(&json!({ + "version": 3, + "sections": [ + {"offset": {"line": 0, "column": 0}, "map": {"version": 3, "sources": ["a.js"]}}, + {"offset": {"line": 8, "column": 0}, "url": "vendor.js.map"} + ] + })) + .unwrap(); + let map = parse_sources(&bytes, Limits::default()).unwrap(); + assert_eq!(map.source_inventory(InventoryLimits::default()).external_sections, 1); +} + +/// The count spends no inventory budget: a map with many external sections still retains its +/// inline sources in full. +#[test] +fn many_external_sections_do_not_spend_the_source_budget() { + let mut sections: Vec = (0..1_000).map(|i| json!({"offset": {"line": i, "column": 0}, "url": format!("{i}.map")})).collect(); + sections.push(json!({"offset": {"line": 1_000, "column": 0}, "map": {"version": 3, "sources": ["a.js", "b.js"], "mappings": ""}})); + let map = decode(json!({"version": 3, "sections": sections})); + let inventory = map.source_inventory(InventoryLimits { max_sources: 2, max_bytes: 16 }); + assert_eq!(inventory.sources, ["a.js", "b.js"]); + assert!(!inventory.truncated); + assert_eq!(inventory.external_sections, 1_000); +} diff --git a/crates/sourcemap/tests/inventory_budgets.rs b/crates/sourcemap/tests/inventory_budgets.rs new file mode 100644 index 000000000..7491108e4 --- /dev/null +++ b/crates/sourcemap/tests/inventory_budgets.rs @@ -0,0 +1,104 @@ +use craft_sourcemap::{InventoryLimits, Limits, SourceMap, parse}; +use serde_json::{Value, json}; + +fn decode(value: Value) -> SourceMap { + parse(&serde_json::to_vec(&value).unwrap(), Limits::default()).unwrap() +} + +#[test] +fn all_sources_bounds_expanded_path_bytes_before_collecting() { + let root = "r".repeat(2 * 1024 * 1024); + let bytes = serde_json::to_vec(&json!({ + "version": 3, "sourceRoot": root, "sources": vec!["/x.js"; 40], "mappings": "" + })) + .unwrap(); + let map = parse(&bytes, Limits::default()).unwrap(); + let sources = map.all_sources(); + assert!(sources.iter().map(String::len).sum::() <= 64 * 1024 * 1024); + assert_eq!(sources.len(), 31); + assert!(sources.iter().all(|source| source.ends_with("//x.js"))); +} + +#[test] +fn repeated_large_roots_stop_before_relative_and_absolute_looking_paths_expand() { + for source in ["x.js", "/x.js", "https://example.test/x.js"] { + let bytes = serde_json::to_vec(&json!({ + "version": 3, "sourceRoot": "r".repeat(1024 * 1024), + "sources": vec![source; 10_000], "mappings": "" + })) + .unwrap(); + assert!(bytes.len() < 2 * 1024 * 1024); + let map = parse(&bytes, Limits::default()).unwrap(); + let inventory = map.source_inventory(InventoryLimits { max_sources: 5000, max_bytes: 2 * 1024 * 1024 }); + assert_eq!(inventory.sources.len(), 1); + assert_eq!(inventory.sources[0].len(), 1024 * 1024 + 1 + source.len()); + assert!(inventory.sources[0].ends_with(source)); + assert!(inventory.truncated); + } +} + +#[test] +fn byte_limits_preserve_complete_utf8_identities_at_the_exact_boundary() { + let map = decode(json!({"version": 3, "sourceRoot": "é/", "sources": ["東京.js", "東京.js", null], "mappings": ""})); + let expected = "é/東京.js"; + let limits = InventoryLimits { max_sources: 2, max_bytes: 2 * expected.len() }; + let exact = map.source_inventory(limits); + assert_eq!(exact.sources, [expected, expected]); + assert!(!exact.truncated); + let short = map.source_inventory(InventoryLimits { max_bytes: limits.max_bytes - 1, ..limits }); + assert_eq!(short.sources, [expected]); + assert!(short.truncated); + let too_small = map.source_inventory(InventoryLimits { max_bytes: expected.len() - 1, ..limits }); + assert!(too_small.sources.is_empty()); + assert!(too_small.truncated); +} + +#[test] +fn entry_limits_count_empty_paths_but_not_null_sources() { + let map = decode(json!({"version": 3, "sourceRoot": "", "sources": [null, "", "", null], "mappings": ""})); + let exact = map.source_inventory(InventoryLimits { max_sources: 2, max_bytes: 0 }); + assert_eq!(exact.sources, ["", ""]); + assert!(!exact.truncated); + let one = map.source_inventory(InventoryLimits { max_sources: 1, max_bytes: 0 }); + assert_eq!(one.sources, [""]); + assert!(one.truncated); + let zero = map.source_inventory(InventoryLimits { max_sources: 0, max_bytes: 0 }); + assert!(zero.sources.is_empty()); + assert!(zero.truncated); + let absent = decode(json!({"version": 3, "sources": [null], "mappings": ""})); + assert!(!absent.source_inventory(InventoryLimits { max_sources: 0, max_bytes: 0 }).truncated); +} + +#[test] +fn indexed_sections_share_entry_and_byte_limits_in_source_order() { + let map = decode(json!({"version": 3, "sourceRoot": "outer", "sections": [ + {"offset": {"line": 0, "column": 0}, "map": { + "version": 3, "sourceRoot": "src", "sources": [null, "/first.js"], "mappings": "" + }}, + {"offset": {"line": 1, "column": 0}, "url": "external.map"}, + {"offset": {"line": 2, "column": 0}, "map": {"version": 3, "sections": [ + {"offset": {"line": 0, "column": 0}, "map": { + "version": 3, "sourceRoot": "file:///", "sources": ["second.js", "third.js"], "mappings": "" + }} + ]}} + ]})); + let expected = ["src//first.js", "file:///second.js", "file:///third.js"]; + let complete = map.source_inventory(InventoryLimits { max_sources: 3, max_bytes: expected.iter().map(|s| s.len()).sum() }); + assert_eq!(complete.sources, expected); + assert!(!complete.truncated); + let bytes = map.source_inventory(InventoryLimits { max_sources: 3, max_bytes: expected[0].len() + expected[1].len() }); + assert_eq!(bytes.sources, expected[..2]); + assert!(bytes.truncated); + let entries = map.source_inventory(InventoryLimits { max_sources: 1, max_bytes: 100 }); + assert_eq!(entries.sources, expected[..1]); + assert!(entries.truncated); +} + +#[test] +fn an_oversized_path_stops_the_prefix_without_shortening_or_skipping_identities() { + let map = decode(json!({"version": 3, "sources": ["a.js", "b".repeat(40), "c.js"], "mappings": ""})); + let inventory = map.source_inventory(InventoryLimits { max_sources: 3, max_bytes: 8 }); + assert_eq!(inventory.sources, ["a.js"]); + assert!(inventory.truncated); + assert_eq!(map.source_path(1).unwrap(), "b".repeat(40)); +} diff --git a/crates/sourcemap/tests/line_budgets.rs b/crates/sourcemap/tests/line_budgets.rs new file mode 100644 index 000000000..f987cd765 --- /dev/null +++ b/crates/sourcemap/tests/line_budgets.rs @@ -0,0 +1,72 @@ +use craft_sourcemap as sourcemap; +use craft_sourcemap::{Limits, SourceMapError}; +use serde_json::{Value, json}; + +fn decode(value: Value, limits: Limits) -> Result { + sourcemap::parse(&serde_json::to_vec(&value).unwrap(), limits) +} + +fn map(mappings: &str) -> Value { + json!({"version":3,"sources":["input.js"],"names":[],"mappings":mappings}) +} + +#[test] +fn empty_lines_spend_the_default_budget_even_with_no_segments() { + let limits = Limits { max_segments: 0, ..Limits::default() }; + assert_eq!(decode(map(&";".repeat(1_000_000)), limits), Err(SourceMapError::TooManyLines(1_000_000))); +} + +#[test] +fn exact_and_zero_line_limits_include_the_final_empty_line() { + let limits = Limits { max_lines: 3, max_segments: 0, ..Limits::default() }; + let parsed = decode(map(";;"), limits).unwrap(); + assert_eq!(parsed.lines.len(), 3); + assert_eq!(parsed.segments, 0); + assert_eq!(decode(map(";;;"), limits), Err(SourceMapError::TooManyLines(3))); + assert_eq!(decode(map(""), Limits { max_lines: 0, ..limits }), Err(SourceMapError::TooManyLines(0))); + assert_eq!(decode(map(""), Limits { max_lines: 1, ..limits }).unwrap().lines.len(), 1); +} + +#[test] +fn nested_sections_share_one_generated_line_budget() { + let value = json!({"version":3,"sections":[ + {"offset":{"line":0,"column":0},"map":map(";")}, + {"offset":{"line":2,"column":0},"map":{"version":3,"sections":[ + {"offset":{"line":0,"column":0},"map":map(";")} + ]}} + ]}); + let limits = Limits { max_lines: 3, max_segments: 0, ..Limits::default() }; + assert_eq!(decode(value.clone(), limits), Err(SourceMapError::TooManyLines(3))); + let parsed = decode(value, Limits { max_lines: 4, ..limits }).unwrap(); + assert_eq!(parsed.sections[0].2.lines.len(), 2); + assert_eq!(parsed.sections[1].2.sections[0].2.lines.len(), 2); +} + +#[test] +fn external_sections_do_not_consume_undecoded_lines() { + let value = json!({"version":3,"sections":[ + {"offset":{"line":0,"column":0},"url":"other.map"}, + {"offset":{"line":2,"column":0},"map":map("")} + ]}); + let parsed = decode(value, Limits { max_lines: 1, max_segments: 0, ..Limits::default() }).unwrap(); + assert!(parsed.sections[0].2.lines.is_empty()); + assert_eq!(parsed.sections[1].2.lines.len(), 1); +} + +#[test] +fn positions_after_empty_lines_keep_their_coordinates() { + let parsed = decode(map("AAAA;;AACA"), Limits { max_lines: 3, max_segments: 2, ..Limits::default() }).unwrap(); + assert_eq!(parsed.lines.len(), 3); + assert!(parsed.original_positions(1, 0).is_empty()); + let position = &parsed.original_positions(2, 0)[0]; + assert_eq!(position.source.as_deref(), Some("input.js")); + assert_eq!((position.line, position.column), (1, 0)); +} + +#[test] +fn line_and_segment_limits_are_independent() { + let limits = Limits { max_lines: 10, max_segments: 1, ..Limits::default() }; + assert!(matches!(decode(map("A,A"), limits), Err(SourceMapError::TooManySegments(_)))); + assert_eq!(decode(map(";"), Limits { max_lines: 1, max_segments: 0, ..limits }), Err(SourceMapError::TooManyLines(1))); + assert_eq!(decode(map(";"), Limits { max_lines: 2, max_segments: 0, ..limits }).unwrap().segments, 0); +} diff --git a/crates/sourcemap/tests/root_prefix.rs b/crates/sourcemap/tests/root_prefix.rs new file mode 100644 index 000000000..fd8a8a814 --- /dev/null +++ b/crates/sourcemap/tests/root_prefix.rs @@ -0,0 +1,80 @@ +use craft_sourcemap::{Limits, SourceMap, parse}; +use serde_json::{Value, json}; + +fn decode(value: Value) -> SourceMap { + parse(&serde_json::to_vec(&value).unwrap(), Limits::default()).unwrap() +} + +#[test] +fn root_prefix_precedes_absolute_looking_sources() { + for (root, source, expected) in [ + ("webpack:///", "/src/main.js", "webpack:////src/main.js"), + ("https://example.test/build", "/src/main.js", "https://example.test/build//src/main.js"), + ("src", "https://example.test/main.js", "src/https://example.test/main.js"), + ("src/", "//example.test/main.js", "src///example.test/main.js"), + ("sources", "node:internal/main", "sources/node:internal/main"), + ] { + let map = decode(json!({"version": 3, "sourceRoot": root, "sources": [source], "mappings": "AAAA"})); + assert_eq!(map.original_positions(0, 0)[0].source.as_deref(), Some(expected), "root {root:?}, source {source:?}"); + assert_eq!(map.all_sources(), [expected]); + } +} + +#[test] +fn empty_root_keeps_the_same_identity_as_an_absent_or_null_root() { + for source in ["src/main.js", "/src/main.js", "https://example.test/main.js"] { + let mut value = json!({"version": 3, "sources": [source], "mappings": "AAAA"}); + assert_eq!(decode(value.clone()).all_sources(), [source]); + value["sourceRoot"] = Value::Null; + assert_eq!(decode(value.clone()).all_sources(), [source]); + value["sourceRoot"] = json!(""); + let map = decode(value); + assert_eq!(map.all_sources(), [source]); + assert_eq!(map.original_positions(0, 0)[0].source.as_deref(), Some(source)); + } +} + +#[test] +fn each_source_keeps_its_index_and_embedded_content() { + let map = decode(json!({ + "version": 3, "sourceRoot": "https://example.test/originals/", + "sources": ["/first.js", null, "https://other.test/third.js"], + "sourcesContent": ["first();", null, "third();"], "mappings": "AAAA,CEAA" + })); + let first = map.original_positions(0, 0); + let third = map.original_positions(0, 1); + assert_eq!(first[0].source_index, 0); + assert_eq!(third[0].source_index, 2); + assert_eq!(first[0].source.as_deref(), Some("https://example.test/originals//first.js")); + assert_eq!(third[0].source.as_deref(), Some("https://example.test/originals/https://other.test/third.js")); + assert_eq!(map.source_content(0), Some("first();")); + assert_eq!(map.source_content(2), Some("third();")); + assert_eq!(map.source_path(1), None); + assert_eq!(map.source_path(3), None); +} + +#[test] +fn indexed_maps_use_each_embedded_maps_own_root() { + let map = decode(json!({ + "version": 3, "sourceRoot": "outer", "sections": [ + {"offset": {"line": 0, "column": 3}, "map": { + "version": 3, "sourceRoot": "file:///", "sources": ["/first.js"], "mappings": "AAAA" + }}, + {"offset": {"line": 1, "column": 0}, "map": { + "version": 3, "sources": ["second.js"], "mappings": "AAAA" + }} + ] + })); + assert_eq!(map.original_positions(0, 3)[0].source.as_deref(), Some("file:////first.js")); + assert_eq!(map.original_positions(1, 0)[0].source.as_deref(), Some("second.js")); + assert_eq!(map.all_sources(), ["file:////first.js", "second.js"]); +} + +#[test] +fn relative_sources_still_preserve_url_root_delimiters() { + for (root, expected) in [("webpack:///", "webpack:///main.js"), ("file:///", "file:///main.js"), ("src", "src/main.js"), ("src/", "src/main.js")] + { + let map = decode(json!({"version": 3, "sourceRoot": root, "sources": ["main.js"], "mappings": "AAAA"})); + assert_eq!(map.all_sources(), [expected]); + } +} diff --git a/crates/sourcemap/tests/section_columns.rs b/crates/sourcemap/tests/section_columns.rs new file mode 100644 index 000000000..d3dff81cd --- /dev/null +++ b/crates/sourcemap/tests/section_columns.rs @@ -0,0 +1,24 @@ +use craft_sourcemap::{Limits, parse}; +use serde_json::json; + +#[test] +fn indexed_lookup_reports_columns_in_the_generated_file() { + let json = json!({"version":3,"sections":[{"offset":{"line":3,"column":11},"map":{"version":3,"sources":["one.js"],"mappings":"IAAA;IACA"}}]}); + let map = parse(json.to_string().as_bytes(), Limits::default()).unwrap(); + assert!(map.original_positions(3, 14).is_empty()); + let first = map.original_positions(3, 17); + assert_eq!(first[0].generated_column, 15); + assert_eq!((first[0].line, first[0].column), (0, 0)); + assert_eq!(map.original_positions(4, 4)[0].generated_column, 4); +} + +#[test] +fn nested_section_columns_are_added_only_on_each_first_line() { + for (inner_line, query_line, query_column) in [(0, 3, 18), (1, 4, 7)] { + let json = json!({"version":3,"sections":[{"offset":{"line":3,"column":11},"map":{"version":3,"sections":[{"offset":{"line":inner_line,"column":3},"map":{"version":3,"sources":["nested.js"],"mappings":"IAAA"}}]}}]}); + let map = parse(json.to_string().as_bytes(), Limits::default()).unwrap(); + let positions = map.original_positions(query_line, query_column); + assert_eq!(positions[0].generated_column, query_column); + assert_eq!(positions[0].source.as_deref(), Some("nested.js")); + } +}