eth-valkyoth-codec 0.16.0

Bounded no_std Ethereum wire codec policy.
Documentation
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use crate::DecodeError;

/// Resource limits required by every untrusted decoder.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DecodeLimits {
    /// Maximum accepted input length in bytes.
    pub max_input_bytes: usize,
    /// Maximum items accepted in any single decoded list.
    pub max_list_items: usize,
    /// Maximum nested list depth.
    ///
    /// RLP list decoding is also capped by
    /// [`crate::MAX_RLP_LIST_TRAVERSAL_DEPTH`], even when this configured limit
    /// is higher.
    pub max_nesting_depth: usize,
    /// Maximum total allocation a decoder may request.
    pub max_total_allocation: usize,
    /// Maximum proof nodes accepted by a proof decoder.
    pub max_proof_nodes: usize,
    /// Maximum total decoded items visited by one decoder invocation.
    pub max_total_items: usize,
}

impl DecodeLimits {
    /// Limits for unit tests, conformance fixtures, and fuzz targets.
    ///
    /// This is not a production policy. Production decoders should choose
    /// deployment-specific limits or start from
    /// [`Self::DEPLOYMENT_STARTING_POINT`].
    #[cfg(any(test, feature = "testing"))]
    pub const TEST_FIXTURE: Self = Self {
        max_input_bytes: 1 << 20,
        max_list_items: 4096,
        max_nesting_depth: 64,
        max_total_allocation: 1 << 20,
        max_proof_nodes: 1024,
        max_total_items: 8192,
    };

    /// Starting point for externally reachable wire decoders.
    ///
    /// Using this constant unchanged in production is a security
    /// misconfiguration. Changing only one field is likely also a
    /// misconfiguration. Copy it, review every limit against the deployment's
    /// concurrency, memory, and protocol policy, and tighten values before
    /// release.
    #[doc(alias = "DEPLOYMENT_TEMPLATE")]
    pub const DEPLOYMENT_STARTING_POINT: Self = Self {
        max_input_bytes: 2 << 20,
        max_list_items: 16_384,
        max_nesting_depth: 64,
        max_total_allocation: 4 << 20,
        max_proof_nodes: 4096,
        max_total_items: 65_536,
    };

    /// Creates a reviewed deployment policy from explicit values.
    ///
    /// This helper intentionally has no defaults. Production deployments should
    /// pass every value from their own concurrency, memory, and protocol review
    /// instead of copying [`Self::DEPLOYMENT_STARTING_POINT`] and changing one
    /// field.
    #[must_use]
    pub const fn reviewed_policy(
        max_input_bytes: usize,
        max_list_items: usize,
        max_nesting_depth: usize,
        max_total_allocation: usize,
        max_proof_nodes: usize,
        max_total_items: usize,
    ) -> Self {
        Self {
            max_input_bytes,
            max_list_items,
            max_nesting_depth,
            max_total_allocation,
            max_proof_nodes,
            max_total_items,
        }
    }

    /// Validates the input length before parsing starts.
    pub fn check_input_len(self, len: usize) -> Result<(), DecodeError> {
        if len > self.max_input_bytes {
            return Err(DecodeError::InputTooLarge);
        }
        Ok(())
    }

    /// Validates a decoded list item count.
    pub fn check_list_count(self, count: usize) -> Result<(), DecodeError> {
        if count > self.max_list_items {
            return Err(DecodeError::ListTooLong);
        }
        Ok(())
    }

    /// Validates the current nesting depth.
    ///
    /// RLP list decoding also enforces [`crate::MAX_RLP_LIST_TRAVERSAL_DEPTH`]
    /// as a hard traversal-stack cap.
    pub fn check_nesting_depth(self, depth: usize) -> Result<(), DecodeError> {
        if depth > self.max_nesting_depth {
            return Err(DecodeError::NestingTooDeep);
        }
        Ok(())
    }

    /// Validates one requested allocation against the allocation budget.
    ///
    /// This helper is for single-allocation checks only. Decoders that can make
    /// more than one allocation must use [`DecodeAccumulator`] to enforce the
    /// cumulative budget.
    pub fn check_single_allocation_limit(self, size: usize) -> Result<(), DecodeError> {
        if size > self.max_total_allocation {
            return Err(DecodeError::AllocationExceeded);
        }
        Ok(())
    }

    /// Validates one proof-node count against the proof-node budget.
    ///
    /// Decoders that traverse more than one proof segment must use
    /// [`DecodeAccumulator::account_proof_nodes`] for cumulative accounting.
    pub fn check_proof_node_count(self, count: usize) -> Result<(), DecodeError> {
        if count > self.max_proof_nodes {
            return Err(DecodeError::ProofTooLarge);
        }
        Ok(())
    }

    /// Validates one decoded item count against the cumulative item budget.
    ///
    /// This helper is for single-count checks only. Decoders that visit items
    /// incrementally must use [`DecodeAccumulator::account_items`].
    pub fn check_item_count(self, count: usize) -> Result<(), DecodeError> {
        if count > self.max_total_items {
            return Err(DecodeError::ItemCountExceeded);
        }
        Ok(())
    }

    /// Starts stateful budget accounting for a decoder invocation.
    #[must_use]
    pub const fn accumulator(self) -> DecodeAccumulator {
        DecodeAccumulator {
            limits: self,
            total_allocated: 0,
            total_items: 0,
            proof_nodes: 0,
        }
    }

    /// Rejects use of the unchanged deployment starting point as a production
    /// policy.
    ///
    /// This is an identity check against the starter template. Changing only
    /// one field is likely also a misconfiguration. Review every limit against
    /// the deployment's concurrency, memory, and protocol constraints before
    /// treating a policy as production-ready.
    pub fn validate_deployment_policy(self) -> Result<(), DecodeError> {
        if self == Self::DEPLOYMENT_STARTING_POINT {
            return Err(DecodeError::UnreviewedDeploymentPolicy);
        }
        Ok(())
    }
}

/// Stateful budget accounting for one decoder invocation.
#[derive(Debug, Eq, PartialEq)]
pub struct DecodeAccumulator {
    limits: DecodeLimits,
    total_allocated: usize,
    total_items: usize,
    proof_nodes: usize,
}

impl DecodeAccumulator {
    /// Returns the active decode limits.
    #[must_use]
    pub const fn limits(&self) -> DecodeLimits {
        self.limits
    }

    /// Returns the cumulative allocation accounted so far.
    #[must_use]
    pub const fn total_allocated(&self) -> usize {
        self.total_allocated
    }

    /// Returns the cumulative decoded items accounted so far.
    #[must_use]
    pub const fn total_items(&self) -> usize {
        self.total_items
    }

    /// Returns the cumulative proof nodes accounted so far.
    #[must_use]
    pub const fn proof_nodes(&self) -> usize {
        self.proof_nodes
    }

    /// Validates the input length before parsing starts.
    pub fn check_input_len(&self, len: usize) -> Result<(), DecodeError> {
        self.limits.check_input_len(len)
    }

    /// Validates a decoded list item count.
    pub fn check_list_count(&self, count: usize) -> Result<(), DecodeError> {
        self.limits.check_list_count(count)
    }

    /// Validates the current nesting depth.
    pub fn check_nesting_depth(&self, depth: usize) -> Result<(), DecodeError> {
        self.limits.check_nesting_depth(depth)
    }

    /// Accounts for one allocation against the cumulative allocation budget.
    pub fn check_allocation(&mut self, size: usize) -> Result<(), DecodeError> {
        let new_total = self
            .total_allocated
            .checked_add(size)
            .ok_or(DecodeError::AllocationExceeded)?;
        if new_total > self.limits.max_total_allocation {
            return Err(DecodeError::AllocationExceeded);
        }
        self.total_allocated = new_total;
        Ok(())
    }

    /// Accounts for decoded items against the cumulative item budget.
    pub fn account_items(&mut self, count: usize) -> Result<(), DecodeError> {
        let new_total = self
            .total_items
            .checked_add(count)
            .ok_or(DecodeError::ItemCountExceeded)?;
        if new_total > self.limits.max_total_items {
            return Err(DecodeError::ItemCountExceeded);
        }
        self.total_items = new_total;
        Ok(())
    }

    /// Accounts for proof nodes against the cumulative proof-node budget.
    pub fn account_proof_nodes(&mut self, count: usize) -> Result<(), DecodeError> {
        let new_total = self
            .proof_nodes
            .checked_add(count)
            .ok_or(DecodeError::ProofTooLarge)?;
        if new_total > self.limits.max_proof_nodes {
            return Err(DecodeError::ProofTooLarge);
        }
        self.proof_nodes = new_total;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn rejects_oversized_input() {
        let limits = DecodeLimits {
            max_input_bytes: 2,
            ..DecodeLimits::TEST_FIXTURE
        };
        assert_eq!(limits.check_input_len(3), Err(DecodeError::InputTooLarge));
    }

    #[test]
    fn rejects_oversized_list() {
        let limits = DecodeLimits {
            max_list_items: 2,
            ..DecodeLimits::TEST_FIXTURE
        };
        assert_eq!(limits.check_list_count(3), Err(DecodeError::ListTooLong));
    }

    #[test]
    fn rejects_excessive_nesting_depth() {
        let limits = DecodeLimits {
            max_nesting_depth: 2,
            ..DecodeLimits::TEST_FIXTURE
        };
        assert_eq!(
            limits.check_nesting_depth(3),
            Err(DecodeError::NestingTooDeep)
        );
    }

    #[test]
    fn rejects_excessive_allocation() {
        let limits = DecodeLimits {
            max_total_allocation: 2,
            ..DecodeLimits::TEST_FIXTURE
        };
        assert_eq!(
            limits.check_single_allocation_limit(3),
            Err(DecodeError::AllocationExceeded)
        );
    }

    #[test]
    fn rejects_excessive_proof_nodes() {
        let limits = DecodeLimits {
            max_proof_nodes: 2,
            ..DecodeLimits::TEST_FIXTURE
        };
        assert_eq!(
            limits.check_proof_node_count(3),
            Err(DecodeError::ProofTooLarge)
        );
    }

    #[test]
    fn rejects_excessive_total_items() {
        let limits = DecodeLimits {
            max_total_items: 2,
            ..DecodeLimits::TEST_FIXTURE
        };
        assert_eq!(
            limits.check_item_count(3),
            Err(DecodeError::ItemCountExceeded)
        );
    }

    #[test]
    fn fixture_and_deployment_starting_point_limits_are_distinct() {
        let production = DecodeLimits::DEPLOYMENT_STARTING_POINT;
        let fixture = DecodeLimits::TEST_FIXTURE;

        assert!(production.max_input_bytes > fixture.max_input_bytes);
        assert!(production.max_total_allocation > fixture.max_total_allocation);
        assert!(production.max_proof_nodes > fixture.max_proof_nodes);
        assert!(production.max_total_items > fixture.max_total_items);
    }

    #[test]
    fn accumulator_rejects_cumulative_allocation_over_budget() {
        let limits = DecodeLimits {
            max_total_allocation: 4,
            ..DecodeLimits::TEST_FIXTURE
        };
        let mut accumulator = limits.accumulator();

        assert_eq!(accumulator.check_allocation(3), Ok(()));
        assert_eq!(accumulator.total_allocated(), 3);
        assert_eq!(
            accumulator.check_allocation(2),
            Err(DecodeError::AllocationExceeded)
        );
        assert_eq!(accumulator.total_allocated(), 3);
    }

    #[test]
    fn accumulator_rejects_allocation_counter_overflow() {
        let limits = DecodeLimits {
            max_total_allocation: usize::MAX,
            ..DecodeLimits::TEST_FIXTURE
        };
        let mut accumulator = limits.accumulator();

        assert_eq!(accumulator.check_allocation(usize::MAX), Ok(()));
        assert_eq!(
            accumulator.check_allocation(1),
            Err(DecodeError::AllocationExceeded)
        );
    }

    #[test]
    fn accumulator_rejects_cumulative_items_over_budget() {
        let limits = DecodeLimits {
            max_total_items: 4,
            ..DecodeLimits::TEST_FIXTURE
        };
        let mut accumulator = limits.accumulator();

        assert_eq!(accumulator.account_items(3), Ok(()));
        assert_eq!(accumulator.total_items(), 3);
        assert_eq!(
            accumulator.account_items(2),
            Err(DecodeError::ItemCountExceeded)
        );
        assert_eq!(accumulator.total_items(), 3);
    }

    #[test]
    fn accumulator_rejects_cumulative_proof_nodes_over_budget() {
        let limits = DecodeLimits {
            max_proof_nodes: 4,
            ..DecodeLimits::TEST_FIXTURE
        };
        let mut accumulator = limits.accumulator();

        assert_eq!(accumulator.account_proof_nodes(3), Ok(()));
        assert_eq!(accumulator.proof_nodes(), 3);
        assert_eq!(
            accumulator.account_proof_nodes(2),
            Err(DecodeError::ProofTooLarge)
        );
        assert_eq!(accumulator.proof_nodes(), 3);
    }

    #[test]
    fn deployment_starting_point_must_be_reviewed_before_use() {
        assert_eq!(
            DecodeLimits::DEPLOYMENT_STARTING_POINT.validate_deployment_policy(),
            Err(DecodeError::UnreviewedDeploymentPolicy)
        );

        let reviewed = DecodeLimits {
            max_input_bytes: DecodeLimits::DEPLOYMENT_STARTING_POINT.max_input_bytes / 2,
            ..DecodeLimits::DEPLOYMENT_STARTING_POINT
        };
        assert_eq!(reviewed.validate_deployment_policy(), Ok(()));
    }

    #[test]
    fn reviewed_policy_requires_every_limit_explicitly() {
        let reviewed = DecodeLimits::reviewed_policy(1024, 32, 8, 4096, 16, 128);

        assert_eq!(reviewed.max_input_bytes, 1024);
        assert_eq!(reviewed.max_list_items, 32);
        assert_eq!(reviewed.max_nesting_depth, 8);
        assert_eq!(reviewed.max_total_allocation, 4096);
        assert_eq!(reviewed.max_proof_nodes, 16);
        assert_eq!(reviewed.max_total_items, 128);
        assert_eq!(reviewed.validate_deployment_policy(), Ok(()));
    }
}