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
use std::collections::HashMap;
use crate::constants;
/// Represents size limit of the stream to prevent `DoS` attacks.
///
/// Please refer [`Constraints`](crate::Constraints) for more info.
#[derive(Debug)]
#[must_use]
pub struct SizeLimit {
pub(crate) whole_stream: u64,
pub(crate) preamble: u64,
pub(crate) per_field: u64,
pub(crate) headers: u64,
pub(crate) boundary_padding: u64,
pub(crate) field_map: HashMap<String, u64>,
}
impl SizeLimit {
/// Creates the default size limits.
///
/// The whole stream and each field are unbounded, while the preamble is
/// limited to 32 KiB, each field's header block to 64 KiB, and each
/// boundary's transport padding to 8 KiB.
pub fn new() -> Self {
Self::default()
}
/// Sets size limit for the whole stream.
pub const fn whole_stream(mut self, limit: u64) -> Self {
self.whole_stream = limit;
self
}
/// Sets the size limit for data before the first multipart boundary.
///
/// The default is 32 KiB. Multipart form submissions normally have no
/// preamble, but MIME permits one before the first boundary.
pub const fn preamble(mut self, limit: u64) -> Self {
self.preamble = limit;
self
}
/// Sets size limit for each field.
pub const fn per_field(mut self, limit: u64) -> Self {
self.per_field = limit;
self
}
/// Sets size limit for a field's full header block.
pub const fn headers(mut self, limit: u64) -> Self {
self.headers = limit;
self
}
/// Sets the maximum number of spaces and tabs after each boundary.
///
/// The default is 8 KiB. This applies independently of field and stream
/// limits, to opening, intermediate, and closing boundaries. The boundary
/// marker and terminating CRLF are not included in this limit.
pub const fn boundary_padding(mut self, limit: u64) -> Self {
self.boundary_padding = limit;
self
}
/// Sets size limit for a specific field, it overrides the
/// [`per_field`](Self::per_field) value for this field.
///
/// It is useful when you want to set a size limit on a textual field which
/// will be stored in memory to avoid potential `DoS` attacks from
/// attackers running the server out of memory.
pub fn for_field<N: Into<String>>(mut self, field_name: N, limit: u64) -> Self {
self.field_map.insert(field_name.into(), limit);
self
}
pub(crate) fn extract_size_limit_for(&self, field: Option<&str>) -> u64 {
field
.and_then(|field| self.field_map.get(field))
.copied()
.unwrap_or(self.per_field)
}
}
impl Default for SizeLimit {
fn default() -> Self {
Self {
whole_stream: constants::DEFAULT_WHOLE_STREAM_SIZE_LIMIT,
preamble: constants::DEFAULT_PREAMBLE_SIZE_LIMIT,
per_field: constants::DEFAULT_PER_FIELD_SIZE_LIMIT,
headers: constants::DEFAULT_HEADERS_SIZE_LIMIT,
boundary_padding: constants::DEFAULT_BOUNDARY_PADDING_SIZE_LIMIT,
field_map: HashMap::default(),
}
}
}