form-net 0.7.0

Sans-IO form parsing for Rust: application/x-www-form-urlencoded and streaming multipart/form-data, with budgets checked before allocation, safe upload spilling, and typed extraction. No third-party dependencies.
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! The allocation-free urlencoded scanner.

use core::fmt;

use crate::error::{FormError, Limit, Location};
use crate::limits::{Limits, PercentPolicy};
use crate::urlencoded::decode::decode_component;

const HAS_EQUALS: u8 = 1;
const NAME_DECODE: u8 = 2;
const VALUE_DECODE: u8 = 4;

/// Iterates the pairs of a urlencoded body without allocating or copying.
///
/// Budgets are checked per sequence before the sequence is handed out: body
/// size (every byte, empty sequences included), field count, raw name and
/// value lengths, and the running total of raw value lengths. After the
/// first error the iterator returns `None`.
///
/// # Examples
///
/// ```
/// use form_net::Limits;
///
/// let pairs: Vec<_> = form_net::raw_pairs(b"a=1&&b", &Limits::DEFAULT)
///     .map(|p| p.map(|p| (p.name_raw(), p.value_raw(), p.has_equals())))
///     .collect::<Result<_, _>>()?;
/// assert_eq!(pairs, [(&b"a"[..], &b"1"[..], true), (&b"b"[..], &b""[..], false)]);
/// # Ok::<(), form_net::FormError>(())
/// ```
#[must_use]
pub fn raw_pairs<'a>(input: &'a [u8], limits: &Limits) -> RawPairs<'a> {
    RawPairs {
        input,
        pos: 0,
        budget: Budget::new(limits),
        done: false,
        checked_body: false,
    }
}

/// The per-sequence budgets, shared by [`raw_pairs`] and the streaming
/// parser so both refuse the same sequence with the same error.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Budget {
    // The effective limits this budget checks, not the whole `Limits`: the
    // iterators that carry a budget are moved by value through adapters.
    max_fields: u32,
    max_name: u32,
    max_value: u32,
    max_text: u64,
    pub(crate) body: u64,
    fields: u32,
    text: u64,
}

impl Budget {
    pub(crate) const fn new(limits: &Limits) -> Budget {
        Budget {
            max_fields: limits.max_fields,
            max_name: limits.name(),
            max_value: limits.value(),
            max_text: limits.text(),
            body: limits.body(),
            fields: 0,
            text: 0,
        }
    }

    /// Counts and checks one complete, non-empty sequence: field count,
    /// name length, value length, then the text total.
    pub(crate) fn admit(&mut self, pair: &RawPair<'_>) -> Result<(), FormError> {
        let at = Location::at(pair.offset);
        self.fields = self.fields.saturating_add(1);
        if self.fields > self.max_fields {
            return Err(FormError::limit(Limit::Fields, at));
        }
        if pair.name.len() as u64 > u64::from(self.max_name) {
            return Err(FormError::limit(Limit::NameLength, at));
        }
        let value_at = pair.value_offset();
        if pair.value.len() as u64 > u64::from(self.max_value) {
            return Err(FormError::limit(Limit::ValueLength, value_at));
        }
        self.text = self.text.saturating_add(pair.value.len() as u64);
        if self.text > self.max_text {
            return Err(FormError::limit(Limit::TextTotal, value_at));
        }
        Ok(())
    }

    /// Checks the start of a sequence that is not complete yet: when its
    /// complete form is certain to be refused, refuses it now with the error
    /// [`admit`](Self::admit) would give, so a held sequence never outgrows
    /// `urlencoded_buffer_len`.
    pub(crate) fn admit_partial(
        &self,
        offset: u64,
        name_len: usize,
        has_equals: bool,
        value_len: usize,
    ) -> Result<(), FormError> {
        let name_over = name_len as u64 > u64::from(self.max_name);
        let value_over = has_equals && value_len as u64 > u64::from(self.max_value);
        if !name_over && !value_over {
            return Ok(());
        }
        if self.fields.saturating_add(1) > self.max_fields {
            return Err(FormError::limit(Limit::Fields, Location::at(offset)));
        }
        if name_over {
            return Err(FormError::limit(Limit::NameLength, Location::at(offset)));
        }
        let value_at = offset.saturating_add(name_len as u64).saturating_add(1);
        Err(FormError::limit(Limit::ValueLength, Location::at(value_at)))
    }

    pub(crate) fn reset(&mut self) {
        self.fields = 0;
        self.text = 0;
    }
}

/// Scans one sequence from `start`: returns where it ends (the next `&`, or
/// the end), where its first `=` is, and its flags. The byte set narrows as
/// facts are learned: once a side is known to need decoding, its `%` and
/// `+` stop mattering, so a value full of escapes is crossed in eight-byte
/// steps instead of stopping at every `%`.
#[inline]
fn scan(input: &[u8], start: usize) -> (usize, Option<usize>, u8) {
    let mut flags = 0u8;
    let mut i = start;
    // The name: up to the first `=` or `&`.
    let eq = loop {
        i = if flags & NAME_DECODE == 0 {
            crate::scan::find_any(input, i, *b"&=%+")
        } else {
            crate::scan::find_any(input, i, *b"&=")
        };
        match input.get(i) {
            None | Some(b'&') => return (i, None, flags),
            Some(b'=') => break i,
            Some(_) => flags |= NAME_DECODE,
        }
        i = i.wrapping_add(1);
    };
    flags |= HAS_EQUALS;
    i = eq.wrapping_add(1);
    // The value: `=` is data now; after the first `%` or `+` only `&` ends it.
    loop {
        i = if flags & VALUE_DECODE == 0 {
            crate::scan::find_any(input, i, *b"&%+")
        } else {
            crate::scan::find_byte(input, i, b'&')
        };
        match input.get(i) {
            None | Some(b'&') => return (i, Some(eq), flags),
            Some(_) => flags |= VALUE_DECODE,
        }
        i = i.wrapping_add(1);
    }
}

/// The iterator returned by [`raw_pairs`].
#[derive(Clone)]
pub struct RawPairs<'a> {
    input: &'a [u8],
    pos: usize,
    budget: Budget,
    done: bool,
    checked_body: bool,
}

impl fmt::Debug for RawPairs<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RawPairs")
            .field("position", &self.pos)
            .field("fields", &self.budget.fields)
            .finish_non_exhaustive()
    }
}

impl<'a> RawPairs<'a> {
    fn fail(&mut self, err: FormError) -> Option<Result<RawPair<'a>, FormError>> {
        self.done = true;
        Some(Err(err))
    }
}

impl<'a> Iterator for RawPairs<'a> {
    type Item = Result<RawPair<'a>, FormError>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.done {
            return None;
        }
        if !self.checked_body {
            self.checked_body = true;
            if self.input.len() as u64 > self.budget.body {
                return self.fail(FormError::limit(
                    Limit::Body,
                    Location::at(self.budget.body),
                ));
            }
        }
        loop {
            if self.pos >= self.input.len() {
                self.done = true;
                return None;
            }
            let start = self.pos;
            let (end, eq, flags) = scan(self.input, start);
            self.pos = end.saturating_add(1).min(self.input.len());
            if end == start {
                continue;
            }
            let seq = self.input.get(start..end).unwrap_or(&[]);
            let (name, value) = match eq {
                Some(e) => {
                    let at = e.wrapping_sub(start);
                    (
                        seq.get(..at).unwrap_or(&[]),
                        seq.get(at.wrapping_add(1)..).unwrap_or(&[]),
                    )
                }
                None => (seq, &[][..]),
            };
            let pair = RawPair {
                name,
                value,
                flags,
                offset: start as u64,
            };
            if let Err(e) = self.budget.admit(&pair) {
                return self.fail(e);
            }
            return Some(Ok(pair));
        }
    }
}

impl core::iter::FusedIterator for RawPairs<'_> {}

/// One `name=value` sequence, undecoded.
///
/// `Debug` prints the offset and the lengths only: values can be secrets.
#[derive(Clone, Copy)]
pub struct RawPair<'a> {
    name: &'a [u8],
    value: &'a [u8],
    flags: u8,
    offset: u64,
}

impl fmt::Debug for RawPair<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RawPair")
            .field("offset", &self.offset)
            .field("name_len", &self.name.len())
            .field("value_len", &self.value.len())
            .finish()
    }
}

impl<'a> RawPair<'a> {
    /// Splits one sequence (no `&` in it) found at `offset` in the body.
    pub(crate) fn split(seq: &'a [u8], offset: u64) -> RawPair<'a> {
        let (_, eq, flags) = scan(seq, 0);
        let (name, value) = match eq {
            Some(e) => (
                seq.get(..e).unwrap_or(&[]),
                seq.get(e.wrapping_add(1)..).unwrap_or(&[]),
            ),
            None => (seq, &[][..]),
        };
        RawPair {
            name,
            value,
            flags,
            offset,
        }
    }

    /// The name as text when it needs no decoding. With `whole` (the body,
    /// already validated as UTF-8 in one pass) it is cut from it: the cut
    /// falls at ASCII `&` or `=`, so it is an O(1) boundary check instead of
    /// a UTF-8 pass per name (D-104).
    #[cfg(feature = "alloc")]
    #[inline]
    pub(crate) fn name_text(&self, whole: Option<&'a str>) -> Option<&'a str> {
        if self.name_needs_decoding() {
            return None;
        }
        let start = usize::try_from(self.offset).ok()?;
        match whole {
            Some(w) => w.get(start..start.checked_add(self.name.len())?),
            None => core::str::from_utf8(self.name).ok(),
        }
    }

    /// As [`name_text`](Self::name_text), for the value.
    #[cfg(feature = "alloc")]
    #[inline]
    pub(crate) fn value_text(&self, whole: Option<&'a str>) -> Option<&'a str> {
        if self.value_needs_decoding() {
            return None;
        }
        if self.value.is_empty() {
            return Some("");
        }
        let start = usize::try_from(self.offset)
            .ok()?
            .checked_add(self.name.len())?
            .checked_add(1)?;
        match whole {
            Some(w) => w.get(start..start.checked_add(self.value.len())?),
            None => core::str::from_utf8(self.value).ok(),
        }
    }

    fn value_offset(&self) -> Location {
        Location::at(
            self.offset
                .saturating_add(self.name.len() as u64)
                .saturating_add(1),
        )
    }

    /// The name as sent, before decoding.
    ///
    /// # Examples
    ///
    /// ```
    /// let p = form_net::raw_pairs(b"a+b=1", &form_net::Limits::DEFAULT).next().unwrap()?;
    /// assert_eq!(p.name_raw(), b"a+b");
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    #[must_use]
    pub const fn name_raw(&self) -> &'a [u8] {
        self.name
    }

    /// The value as sent, before decoding.
    ///
    /// # Examples
    ///
    /// ```
    /// let p = form_net::raw_pairs(b"a=%41", &form_net::Limits::DEFAULT).next().unwrap()?;
    /// assert_eq!(p.value_raw(), b"%41");
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    #[must_use]
    pub const fn value_raw(&self) -> &'a [u8] {
        self.value
    }

    /// Whether the sequence contained `=`: `a` and `a=` both have an empty
    /// value, but only `a=` has an equals sign.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::Limits;
    ///
    /// let mut it = form_net::raw_pairs(b"a&b=", &Limits::DEFAULT);
    /// assert!(!it.next().unwrap()?.has_equals());
    /// assert!(it.next().unwrap()?.has_equals());
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    #[must_use]
    pub const fn has_equals(&self) -> bool {
        self.flags & HAS_EQUALS != 0
    }

    /// Whether the name contains `%` or `+`.
    ///
    /// # Examples
    ///
    /// ```
    /// let p = form_net::raw_pairs(b"a+b=1", &form_net::Limits::DEFAULT).next().unwrap()?;
    /// assert!(p.name_needs_decoding() && !p.value_needs_decoding());
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    #[must_use]
    pub const fn name_needs_decoding(&self) -> bool {
        self.flags & NAME_DECODE != 0
    }

    /// Whether the value contains `%` or `+`.
    ///
    /// # Examples
    ///
    /// ```
    /// let p = form_net::raw_pairs(b"a=1%20", &form_net::Limits::DEFAULT).next().unwrap()?;
    /// assert!(p.value_needs_decoding());
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    #[must_use]
    pub const fn value_needs_decoding(&self) -> bool {
        self.flags & VALUE_DECODE != 0
    }

    /// The name with no copy, when it needs no decoding and is valid UTF-8.
    ///
    /// # Examples
    ///
    /// ```
    /// let p = form_net::raw_pairs(b"email=x", &form_net::Limits::DEFAULT).next().unwrap()?;
    /// assert_eq!(p.name_str(), Some("email"));
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    #[must_use]
    pub fn name_str(&self) -> Option<&'a str> {
        if self.name_needs_decoding() {
            None
        } else {
            core::str::from_utf8(self.name).ok()
        }
    }

    /// The value with no copy, when it needs no decoding and is valid UTF-8.
    ///
    /// # Examples
    ///
    /// ```
    /// let p = form_net::raw_pairs(b"q=a+b", &form_net::Limits::DEFAULT).next().unwrap()?;
    /// assert_eq!(p.value_str(), None);
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    #[must_use]
    pub fn value_str(&self) -> Option<&'a str> {
        if self.value_needs_decoding() {
            None
        } else {
            core::str::from_utf8(self.value).ok()
        }
    }

    /// Decodes the name into `out` (see [`decode_into`](crate::decode_into)).
    ///
    /// # Errors
    ///
    /// `Encoding { InvalidPercent | InvalidUtf8 }` located in the body;
    /// `BufferTooSmall` when `out` is shorter than the raw name.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::PercentPolicy;
    ///
    /// let p = form_net::raw_pairs(b"a%20b=1", &form_net::Limits::DEFAULT).next().unwrap()?;
    /// let mut out = [0u8; 16];
    /// assert_eq!(p.decode_name(&mut out, PercentPolicy::Refuse)?, "a b");
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    pub fn decode_name<'b>(
        &self,
        out: &'b mut [u8],
        policy: PercentPolicy,
    ) -> Result<&'b str, FormError> {
        decode_component(self.name, out, policy).map_err(|e| e.shifted(self.offset))
    }

    /// Decodes the value into `out` (see [`decode_into`](crate::decode_into)).
    ///
    /// # Errors
    ///
    /// As [`decode_name`](Self::decode_name).
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::PercentPolicy;
    ///
    /// let p = form_net::raw_pairs(b"q=rust+forms", &form_net::Limits::DEFAULT).next().unwrap()?;
    /// let mut out = [0u8; 16];
    /// assert_eq!(p.decode_value(&mut out, PercentPolicy::Refuse)?, "rust forms");
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    pub fn decode_value<'b>(
        &self,
        out: &'b mut [u8],
        policy: PercentPolicy,
    ) -> Result<&'b str, FormError> {
        let at = self
            .offset
            .saturating_add(self.name.len() as u64)
            .saturating_add(1);
        decode_component(self.value, out, policy).map_err(|e| e.shifted(at))
    }

    /// The byte offset of the sequence in the body.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut it = form_net::raw_pairs(b"a=1&bb=2", &form_net::Limits::DEFAULT);
    /// let _ = it.next();
    /// assert_eq!(it.next().unwrap()?.offset(), 4);
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    #[must_use]
    pub const fn offset(&self) -> u64 {
        self.offset
    }
}

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

    type Pairs<'a> = Vec<(&'a [u8], &'a [u8])>;

    fn pairs(input: &[u8]) -> Result<Pairs<'_>, FormError> {
        raw_pairs(input, &Limits::DEFAULT)
            .map(|p| p.map(|p| (p.name_raw(), p.value_raw())))
            .collect()
    }

    #[test]
    fn test_raw_pairs_whatwg_splitting() {
        assert_eq!(pairs(b"").unwrap(), []);
        assert_eq!(pairs(b"&&&").unwrap(), []);
        assert_eq!(pairs(b"=").unwrap(), [(&b""[..], &b""[..])]);
        assert_eq!(pairs(b"a==b").unwrap(), [(&b"a"[..], &b"=b"[..])]);
        assert_eq!(pairs(b"=a").unwrap(), [(&b""[..], &b"a"[..])]);
        assert_eq!(pairs(b"a;b=c").unwrap(), [(&b"a;b"[..], &b"c"[..])]);
        assert_eq!(
            pairs(b"a=1&b=2&").unwrap(),
            [(&b"a"[..], &b"1"[..]), (&b"b"[..], &b"2"[..])]
        );
    }

    #[test]
    fn test_raw_pairs_flags_per_side() {
        let p = raw_pairs(b"a%=b", &Limits::DEFAULT)
            .next()
            .unwrap()
            .unwrap();
        assert!(p.name_needs_decoding() && !p.value_needs_decoding());
        let p = raw_pairs(b"a=b+%", &Limits::DEFAULT)
            .next()
            .unwrap()
            .unwrap();
        assert!(!p.name_needs_decoding() && p.value_needs_decoding());
        let p = raw_pairs(b"a=b=+", &Limits::DEFAULT)
            .next()
            .unwrap()
            .unwrap();
        assert!(p.value_needs_decoding() && p.has_equals());
    }

    #[test]
    fn test_raw_pairs_field_limit_n_and_n_plus_one() {
        let l = Limits::DEFAULT.with_max_fields(2);
        assert_eq!(raw_pairs(b"a&b", &l).count(), 2);
        let r: Result<Vec<_>, _> = raw_pairs(b"a&b&c", &l).collect();
        assert!(matches!(
            r,
            Err(FormError::Limit {
                limit: Limit::Fields,
                at
            }) if at.offset == 4
        ));
    }

    #[test]
    fn test_raw_pairs_length_limits_at_the_edge() {
        let l = Limits::DEFAULT
            .with_max_name_bytes(3)
            .with_max_value_bytes(3);
        assert!(raw_pairs(b"abc=def", &l).all(|p| p.is_ok()));
        let e = raw_pairs(b"abcd=1", &l).next().unwrap().unwrap_err();
        assert!(matches!(
            e,
            FormError::Limit {
                limit: Limit::NameLength,
                ..
            }
        ));
        let e = raw_pairs(b"a=defg", &l).next().unwrap().unwrap_err();
        assert!(matches!(e, FormError::Limit { limit: Limit::ValueLength, at } if at.offset == 2));
        let l = Limits::DEFAULT.with_max_text_bytes(4);
        let r: Result<Vec<_>, _> = raw_pairs(b"a=12&b=345", &l).collect();
        assert!(matches!(
            r,
            Err(FormError::Limit {
                limit: Limit::TextTotal,
                ..
            })
        ));
    }

    #[test]
    fn test_raw_pairs_body_limit_counts_empty_sequences() {
        let l = Limits::DEFAULT.with_max_body_bytes(4);
        assert_eq!(raw_pairs(b"&&&&", &l).count(), 0);
        let e = raw_pairs(b"&&&&&", &l).next().unwrap().unwrap_err();
        assert!(matches!(
            e,
            FormError::Limit {
                limit: Limit::Body,
                ..
            }
        ));
    }

    #[test]
    fn test_raw_pairs_stops_after_error() {
        let l = Limits::DEFAULT.with_max_fields(0);
        let mut it = raw_pairs(b"a&b", &l);
        assert!(it.next().unwrap().is_err());
        assert!(it.next().is_none());
    }

    #[test]
    fn test_raw_pair_debug_hides_values() {
        let p = raw_pairs(b"password=hunter2", &Limits::DEFAULT)
            .next()
            .unwrap()
            .unwrap();
        let s = alloc::format!("{p:?}");
        assert!(!s.contains("hunter2") && !s.contains("password"));
    }
}