Skip to main content

powerio/collection/
nested.rs

1use super::{
2    Arc, ArchiveLimits, ArtifactPath, Cancellation, Collection, CollectionEntry, CollectionLimits,
3    CollectionSelection, Error, Result, SourceTreeLimits, codes, selection_error,
4};
5use std::sync::Mutex;
6
7/// Counters shared by a collection, its clones, and every nested child: the
8/// archive content admitted into the session's namespaces. They grow only
9/// while a namespace is retained; a child that fails verification releases
10/// what it reserved.
11#[derive(Clone, Debug)]
12pub(super) struct Session(Arc<Mutex<Usage>>);
13#[derive(Debug)]
14struct Usage {
15    entries: usize,
16    expanded: u64,
17}
18impl Session {
19    pub(super) fn new(entries: &[CollectionEntry]) -> Self {
20        Self(Arc::new(Mutex::new(Usage {
21            entries: entries.len(),
22            expanded: entries.iter().map(|e| e.bytes).sum(),
23        })))
24    }
25    /// Count the admitted ZIP's own entries, directories included.
26    pub(super) fn initialize_archive(&self, entries: usize) -> Result<()> {
27        let mut usage = self
28            .0
29            .lock()
30            .map_err(|_| selection_error("collection budget lock poisoned"))?;
31        usage.entries = entries;
32        Ok(())
33    }
34}
35
36/// The input and decompression bytes one operation touches, bounded by
37/// `CollectionLimits::work_bytes`. Every operation starts from zero: a
38/// selection parsed twice costs the same each time and never fails because
39/// of earlier operations on the collection.
40pub(super) struct Work {
41    used: u64,
42    limit: u64,
43}
44impl Work {
45    pub(super) fn new(limit: u64) -> Self {
46        Self { used: 0, limit }
47    }
48    pub(super) fn charge(&mut self, bytes: u64) -> Result<()> {
49        self.used = self.used.saturating_add(bytes);
50        if self.used > self.limit {
51            Err(Error::new(&codes::LIMIT, "collection work limit exceeded"))
52        } else {
53            Ok(())
54        }
55    }
56    fn remaining(&self) -> u64 {
57        self.limit.saturating_sub(self.used)
58    }
59}
60impl Collection {
61    /// Open exactly one named child ZIP. Its namespace is independent: sources
62    /// cannot resolve against a parent or sibling. All descendants and clones
63    /// share the monotonic entry and expansion counters of admitted content,
64    /// so reopening a child counts again, while each open's own input and
65    /// decompression work is bounded on its own.
66    pub fn open_nested(&self, member: &str) -> Result<Self> {
67        self.cancel.check()?;
68        if self.zip_layers >= self.limits.max_zip_layers {
69            return Err(Error::new(
70                &codes::LIMIT,
71                "ZIP decoding layer limit exceeded",
72            ));
73        }
74        let path = ArtifactPath::new(member)?;
75        let bytes = self
76            .tree
77            .source(None, None, self.limits.tree)?
78            .buffer(&path)?
79            .shared_bytes();
80        let mut work = Work::new(self.limits.work_bytes);
81        work.charge(bytes.len() as u64)?;
82        let mut usage = self
83            .session
84            .0
85            .lock()
86            .map_err(|_| selection_error("collection budget lock poisoned"))?;
87        let limits = self.remaining_limits(&usage)?;
88        // Hold the budget lock during admission: sibling threads cannot each
89        // allocate the same remaining allowance concurrently. The declared
90        // expansion is reserved before any payload read; a child that fails
91        // verification retains nothing, so it releases that reservation.
92        let archive = powerio_archive::Archive::open(Arc::clone(&bytes), limits.archive)?;
93        let entries = archive.entries().len();
94        let expanded = archive
95            .entries()
96            .map(powerio_archive::ArchiveEntry::size)
97            .sum();
98        work.charge(expanded)?;
99        work.charge(bytes.len() as u64)?;
100        usage.entries += entries;
101        usage.expanded += expanded;
102        drop(archive);
103        let mut child = match Self::from_zip_cancellable(member, bytes, limits, self.cancel.clone())
104        {
105            Ok(child) => child,
106            Err(error) => {
107                usage.entries -= entries;
108                usage.expanded -= expanded;
109                return Err(error);
110            }
111        };
112        child.session = self.session.clone();
113        child.limits = self.limits;
114        child.zip_layers = self.zip_layers + 1;
115        child.origin_name.clone_from(&self.origin_name);
116        child.containers.clone_from(&self.containers);
117        child.containers.push(member.to_owned());
118        Ok(child)
119    }
120    /// Cancel subsequent discovery, child acquisition, and selected parses.
121    /// ZIP reads check the signal between chunks. Electrical parsers retain
122    /// their own bounded work rules; synchronous WASM may be terminated by its worker.
123    pub fn cancellation(&self) -> Cancellation {
124        self.cancel.clone()
125    }
126
127    fn remaining_limits(&self, usage: &Usage) -> Result<CollectionLimits> {
128        let entries = self
129            .limits
130            .archive
131            .max_entries()
132            .min(self.limits.tree.max_files())
133            .saturating_sub(usage.entries);
134        let bytes = self
135            .limits
136            .archive
137            .max_expanded_bytes()
138            .min(self.limits.tree.max_bytes())
139            .saturating_sub(usage.expanded);
140        if entries == 0 || bytes == 0 {
141            return Err(Error::new(
142                &codes::LIMIT,
143                "cumulative collection budget exhausted",
144            ));
145        }
146        Ok(CollectionLimits {
147            tree: SourceTreeLimits::new(entries, bytes, self.limits.tree.max_depth())?,
148            archive: ArchiveLimits::new(
149                self.limits.archive.max_input_bytes(),
150                entries,
151                bytes,
152                self.limits.archive.max_ratio(),
153            )?,
154            ..self.limits
155        })
156    }
157    pub(super) fn charge_selection(&self, selected: &CollectionSelection) -> Result<()> {
158        // Charge the scoped source, conservatively including retained companions.
159        // This does not estimate model/Arrow allocation or replace parser guards.
160        // A separate view keeps accounting reads out of parser observations.
161        let mut work = Work::new(self.limits.work_bytes);
162        let source = self.source(selected)?;
163        let primary = source.primary_buffer().ok();
164        let acquired_tables = primary
165            .as_ref()
166            .is_some_and(|p| selected.has_sincal_tables(p.content_bytes()));
167        let mut buffers = Vec::new();
168        for name in source.entry_names()? {
169            if primary.as_ref().is_none_or(|p| p.name() != name.as_str()) {
170                buffers.push(source.root_buffer(name.as_str())?);
171            }
172        }
173        if let Some(primary) = primary {
174            buffers.push(primary);
175        }
176        for buffer in buffers {
177            let bytes = buffer.content_bytes();
178            self.cancel.check()?;
179            work.charge(bytes.len() as u64)?;
180            // Native ZIP layers are charged even for nonstandard file suffixes.
181            // A signature indicates transport, never an electrical format choice.
182            // Match adapter admission, including prefixed archives selected by
183            // suffix and BOM-stripped ZIP signatures. Companion assets of a
184            // DSS/GLM source are not themselves decoded by a native adapter.
185            let extension = buffer.name().rsplit_once('.').map(|(_, e)| e);
186            let sincal = matches!(
187                selected.format_token(),
188                Some("sincal" | "sincal-balanced" | "sincal-multiconductor")
189            ) || selected.format.is_none()
190                && extension.is_some_and(|e| e.eq_ignore_ascii_case("sinx"));
191            let native = !acquired_tables
192                && !selected.has_companions()
193                && (sincal && !bytes.starts_with(b"SQLite format 3\0")
194                    || extension.is_some_and(|e| e.eq_ignore_ascii_case("zip"))
195                    || bytes.starts_with(b"PK\x03\x04")
196                    || bytes.starts_with(b"PK\x05\x06"));
197            if native {
198                if self.zip_layers >= self.limits.max_zip_layers {
199                    return Err(Error::new(
200                        &codes::LIMIT,
201                        "native ZIP exceeds decoding layer limit",
202                    ));
203                }
204                // Verification streams the member and retains nothing, so its
205                // expansion is this operation's work, not admitted content.
206                let allowance = work.remaining() / 2;
207                if allowance == 0 {
208                    return Err(Error::new(&codes::LIMIT, "collection work limit exceeded"));
209                }
210                let archive = ArchiveLimits::new(
211                    self.limits.archive.max_input_bytes(),
212                    self.limits.archive.max_entries(),
213                    self.limits.archive.max_expanded_bytes().min(allowance),
214                    self.limits.archive.max_ratio(),
215                )?;
216                powerio_archive::session::verify_native(
217                    bytes,
218                    archive,
219                    &self.cancel,
220                    |_, expanded| {
221                        work.charge(expanded.saturating_mul(2))?;
222                        Ok(())
223                    },
224                )?;
225            }
226        }
227        Ok(())
228    }
229}