1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! Per-document ceiling on tracked dependencies (#796).
//!
//! Nothing upstream of [`crate::ecosystem::Ecosystem::parse_manifest`] bounds dependency
//! *count* the way `deps_lsp::document::loader::MAX_FILE_SIZE` bounds file *size*: every
//! dependency a parser finds becomes an entry in several `DocumentState` maps (cached
//! versions, resolved versions, outcomes, licenses, vulnerabilities) and one outbound
//! registry request, so both memory and outbound request volume scale linearly with a
//! number an untrusted manifest's author controls. Two layers enforce the ceiling:
//!
//! 1. **[`DependencyBudget`]** (the primary fix, #796 critic finding C1) is threaded
//! through each ecosystem crate's own dependency-collecting loop(s), so the backing
//! `Vec` a concrete `ParseResult` retains for the life of the document — e.g.
//! `NpmParseResult.dependencies` — never grows past the cap in the first place. An
//! earlier version of this fix truncated only the `Vec<&dyn Dependency>`
//! [`ParseResult::dependencies`] returns, *after* the full oversized backing
//! collection had already been built and retained — measured to still cost ~0.9KB of
//! sustained RSS per *declared* (not tracked) dependency, fully attacker-controlled.
//! Each ecosystem's own manifest-format parser (`toml-span`/`serde_json`/`quick-xml`/
//! `yaml-rust2`) still builds one in-memory AST of the whole input before dependency
//! extraction runs — that transient cost is bounded by
//! `deps_lsp::document::loader::MAX_FILE_SIZE`, the same way it always was, and is not
//! what this ceiling targets.
//! 2. **[`crate::ecosystem::ParsedManifest`]**, which
//! [`crate::ecosystem::parse_manifest_blocking`] returns for every ecosystem's parse result
//! (the single chokepoint), is a belt-and-braces backstop: if a well-behaved ecosystem's own
//! budget already holds it at or under the cap, its view equals the parse result's own; it
//! only does real work — bounding the *view* `dependencies()` returns and thus the registry
//! fetch fan-out — if some ecosystem parser is ever added or changed without wiring in
//! [`DependencyBudget`].
use crateParseResult;
/// Maximum number of dependencies [`ParseResult::dependencies`] returns for one open
/// document, enforced by [`crate::ecosystem::ParsedManifest`].
///
/// Hardcoded rather than exposed as a `deps-lsp` config setting, mirroring
/// `deps_lsp::document::loader::MAX_FILE_SIZE`'s own "security limit, not a user
/// preference" reasoning: the largest real-world manifests (large monorepo
/// `package.json`/`Cargo.toml` files) sit in the low hundreds of dependencies, so this
/// leaves generous headroom while still bounding the pathological/adversarial case — a
/// manifest with hundreds of thousands of declarations (#796's measured repro: a 6.92MB
/// `package.json` with 330,000 unique deps drove peak RSS to 850MB and would have fanned
/// out roughly 480,000 registry requests at the 10MB file-size cap).
pub const MAX_DEPENDENCIES_PER_DOCUMENT: usize = 5000;
/// Bounds how many dependencies an ecosystem's own manifest parser retains.
///
/// Checked once per manifest entry, before the (potentially expensive:
/// name/range/version-requirement/...) `Dependency` struct for it is built into its
/// concrete `Vec<XDependency>` — never truncated after the fact.
///
/// Each ecosystem crate constructs one `DependencyBudget::new(MAX_DEPENDENCIES_PER_DOCUMENT)`
/// at the top of its parse entry point and threads `&mut` it through every dependency-
/// collecting loop that manifest format has (a manifest can declare dependencies across
/// several sections/targets — e.g. Cargo's `[dependencies]`/`[dev-dependencies]`/
/// `[build-dependencies]`, each possibly repeated per `[target.<spec>]` — all sharing the
/// same budget, since the ceiling is per-document, not per-section). Call [`Self::allow`]
/// as the first thing in the loop body, before doing any other per-entry work, and only
/// build/push the entry when it returns `true`.
///
/// # Examples
///
/// ```
/// use deps_core::dependency_cap::{DependencyBudget, DependencyTruncation};
///
/// let mut budget = DependencyBudget::new(2);
/// assert!(budget.allow());
/// assert!(budget.allow());
/// assert!(!budget.allow(), "third entry exceeds the cap of 2");
///
/// assert_eq!(
/// budget.truncation(),
/// Some(DependencyTruncation { kept: 2, total: 3 })
/// );
/// ```
/// Kept versus declared dependency counts once the per-document ceiling truncated it (#796).
///
/// Named fields replace a positional `(kept, total)` tuple so the two counts cannot be
/// swapped at a producer or consumer.
///
/// # Examples
///
/// ```
/// use deps_core::dependency_cap::DependencyTruncation;
///
/// let truncation = DependencyTruncation { kept: 2, total: 3 };
/// assert_eq!(truncation.omitted(), 1);
/// ```