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
//! Declared-total validation for message assembly.
//!
//! A protocol may declare the total body length of a logical message on its
//! first frame. That declaration is a commitment, not merely a hint: it is used
//! to reject messages that cannot fit the configured size limit, and it is
//! enforced against the bytes that are actually accumulated. Treating it as
//! advisory would let a peer understate the size of the message that is
//! dispatched (`CWE-20`, `CWE-130`).
//!
//! The helpers here are pure so that the state machine in
//! [`super::state`] can apply them at each frame boundary without growing that
//! module past its size budget.
//!
//! Contract, in full:
//!
//! - a declaration smaller than the first frame's own body can never be satisfied, so the first
//! frame is rejected;
//! - when the first frame is also the last, its body must equal the declaration exactly;
//! - each continuation must keep the accumulated body within the declaration; overshooting is
//! rejected as soon as it is detected; and
//! - the final frame must bring the accumulated body to exactly the declared total.
//!
//! Protocols that supply `None` are unaffected.
use ;
/// Validate the declaration carried by a first frame.
///
/// Checks that the declaration is satisfiable by the first frame alone, and
/// that a single-frame message matches its declaration exactly.
///
/// # Errors
///
/// Returns [`MessageAssemblyError::DeclaredTotalTooSmall`] when the declaration
/// is smaller than the first frame's body, or
/// [`MessageAssemblyError::DeclaredTotalMismatch`] when the first frame is also
/// the final frame and its body does not equal the declaration.
pub const
/// Validate that appending `body_len` bytes keeps the assembly within its
/// declared total.
///
/// Called before the bytes are buffered, so a peer cannot use a single
/// oversized continuation to overrun the declaration and only be detected at
/// completion.
///
/// # Errors
///
/// Returns [`MessageAssemblyError::DeclaredTotalExceeded`] when the accumulated
/// body would exceed the declared total, including when the addition would
/// overflow `usize`.
pub const
/// Validate that a completed assembly matches its declared total exactly.
///
/// # Errors
///
/// Returns [`MessageAssemblyError::DeclaredTotalMismatch`] when the assembled
/// body length differs from the declaration. This catches an under-declared
/// multi-frame message, whose shorter-than-declared body would otherwise be
/// dispatched unchanged.
pub const