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
// SPDX-License-Identifier: MIT OR Apache-2.0
// This file is part of Nitr.
// See https://nitrweb.com/ for more information
// Copyright (C) 2024-present Jose Quintana <joseluisq.net>
//! `multipart/form-data` parsing, with the parser and every limit on the
//! Rust side.
//!
//! The invariant that shapes this API: **an uploaded file never passes
//! through the Lua state's heap.** A state has an 8 MiB memory limit by
//! default, so a buffered-then-handed-over design would make "upload a
//! file" mean "crash the state". `part:save(path)` streams socket → disk in
//! Rust and Lua only ever holds a handle.
//!
//! That is also why parts are delivered to a callback rather than collected
//! into a table first. Collecting would mean either buffering everything
//! (the thing we are avoiding) or spooling to temp files, which needs a
//! reaper and a disk-space policy. Streaming each part as it arrives needs
//! neither, at the cost of the handler seeing parts in the order the client
//! sent them.
//!
//! # Where a saved file may land
//!
//! `part:save(path)` is the only filesystem *write* Lua can reach, and
//! both of its inputs are attacker-influenced: the path is a Lua string
//! built by the handler, and `part.filename` is a header the client chose.
//! So the path is resolved against the configured `[multipart]
//! upload_dir` and never escapes it — the lexical rule is shared with
//! static serving ([`crate::safe_path`]), and the parent is canonicalized
//! so a symlinked directory cannot lead out either. With no `upload_dir`
//! configured, `save` is unavailable rather than unconstrained.
//!
//! Two residuals are documented rather than closed. Both need an
//! attacker who *already* has write access inside the upload root — a
//! separate local process, not an HTTP client — so they are a hardening
//! gap, not a way in:
//!
//! - **A symlink swapped between check and open.** `symlink_metadata`
//! runs before `File::create`, and nothing holds the directory still in
//! between. `O_NOFOLLOW` (or `FILE_FLAG_OPEN_REPARSE_POINT`) would close
//! it, which means a `libc`/`rustix` dependency the workspace does not
//! carry.
//! - **A pre-existing hardlink.** A hardlink inside the root pointing at
//! an inode outside it reports as an ordinary regular file — there is
//! nothing in its metadata to distinguish it — so it passes the
//! `is_file` check and `File::create` truncates through it. Note this
//! one is *not* covered by `O_NOFOLLOW`: closing it needs writing to a
//! fresh temporary name and renaming into place, or an `st_nlink`/
//! `st_dev` check on the opened descriptor.
use ExternalResult as _;
pub use LuaPart;
pub use ;
pub use ;
/// What a fuzzed multipart walk observed: how many parts were admitted,
/// and the largest number of bytes any single field yielded.
/// Drives the whole multipart parse over an in-memory body the way
/// `req:multipart()` does — boundary extraction, part counting against
/// `max_parts`, and the per-field byte cap applied while reading — but
/// without a Lua state or disk writes. Exposed for the fuzz target only;
/// the Lua-facing `LuaPart` methods are covered by the integration tests.
///
/// `chunk_size` splits the body into that many bytes per stream frame
/// (`0` means one frame for the whole body). A real body arrives from
/// hyper as many frames, and multer carries boundary-matching state
/// across them — the delimiter, and the CRLF before it, can straddle a
/// frame edge. Feeding one frame would leave that state machine, the
/// most intricate part of the parser, entirely unexercised.
pub async
/// The `boundary` parameter of a `multipart/form-data` content type.
pub