use axum::body::Bytes;
use axum::extract::rejection::BytesRejection;
use cpa_common::gostr::quote;
use cpa_core::gostr::GoStr;
use crate::body_limit::Read;
use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
pub(super) struct FilePart {
pub filename: String,
pub data: Vec<u8>,
}
pub(super) type Files = BTreeMap<String, Vec<FilePart>>;
const NOT_MULTIPART: &str = "request Content-Type isn't multipart/form-data";
const MISSING_BOUNDARY: &str = "no multipart boundary param in Content-Type";
const TOO_LARGE: &str = "multipart: message too large";
const MAX_PARTS: usize = 1000;
const PEEK_BUFFER: usize = 4096;
pub(super) fn is_form(content_type: &str) -> bool {
content_type.split([' ', ';']).next() == Some("multipart/form-data")
}
pub(super) fn form_files(content_type: &str, body: &[u8]) -> Result<Vec<FilePart>, String> {
Ok(form(content_type, body)?.0.into_values().flatten().collect())
}
pub(super) fn form(content_type: &str, body: &[u8]) -> Result<(Files, HashMap<String, Vec<String>>), String> {
let mut hit_limit = false;
read_form(content_type, body, &mut hit_limit, false)
}
pub(crate) enum Failed {
Read(BytesRejection),
Form(u16, String),
}
pub(crate) fn body(read: Read, content_type: &str) -> Result<Bytes, Failed> {
match read.body {
Err(rejection) if crate::body_limit::is_too_large(&rejection) => {
let mut hit_limit = false;
match read_form(content_type, &read.prefix, &mut hit_limit, true) {
Ok(_) => Ok(read.prefix),
Err(e) => Err(Failed::Form(if hit_limit { 413 } else { 400 }, e)),
}
}
body => body.map_err(Failed::Read),
}
}
fn read_form(
content_type: &str,
body: &[u8],
hit_limit: &mut bool,
limited: bool,
) -> Result<(Files, HashMap<String, Vec<String>>), String> {
let (media, params) = parse_media_type(content_type).map_err(|_| NOT_MULTIPART.to_owned())?;
if media != "multipart/form-data" {
return Err(NOT_MULTIPART.into());
}
let boundary = params.get("boundary").ok_or(MISSING_BOUNDARY)?;
let mut reader = Reader {
buf: body,
pos: 0,
nl: b"\r\n",
dash_boundary: format!("--{boundary}").into_bytes(),
parts_read: 0,
limited,
hit_limit,
window: 0,
over: false,
};
let mut files: Files = BTreeMap::new();
let mut values: HashMap<String, Vec<String>> = HashMap::new();
let mut parts = 0;
while let Some(headers) = reader.next_part()? {
parts += 1;
if parts > MAX_PARTS {
return Err(TOO_LARGE.into());
}
let (disposition, params) = headers
.get("content-disposition")
.and_then(|v| parse_media_type(v).ok())
.unwrap_or_default();
let name = if disposition == "form-data" {
params.get("name").cloned().unwrap_or_default()
} else {
String::new()
};
if name.is_empty() {
if reader.body().is_err() {
reader.pos = reader.buf.len();
}
continue;
}
let data = reader.body()?;
let filename = params.get("filename").map(String::as_str).unwrap_or_default();
if !filename.is_empty() {
files.entry(name).or_default().push(FilePart {
filename: go_base(filename).to_owned(),
data,
});
} else {
values
.entry(name)
.or_default()
.push(String::from_utf8_lossy(&data).into_owned());
}
}
Ok((files, values))
}
pub(super) fn go_base(path: &str) -> &str {
if path.is_empty() {
return ".";
}
let trimmed = path.trim_end_matches('/');
if trimmed.is_empty() {
return "/";
}
trimmed.rsplit('/').next().unwrap_or(trimmed)
}
struct Reader<'a, 'h> {
buf: &'a [u8],
pos: usize,
nl: &'static [u8],
dash_boundary: Vec<u8>,
parts_read: usize,
limited: bool,
hit_limit: &'h mut bool,
window: usize,
over: bool,
}
enum Slice<'a> {
Line(&'a [u8]),
Full(&'a [u8]),
Over(&'a [u8]),
}
enum Scan {
Data,
End,
UnexpectedEof,
}
impl<'a> Reader<'a, '_> {
fn read_slice(&mut self) -> (&'a [u8], bool) {
let rest = &self.buf[self.pos..];
match rest.iter().position(|&b| b == b'\n') {
Some(i) => {
self.pos += i + 1;
(&rest[..=i], false)
}
None => {
self.pos = self.buf.len();
(rest, true)
}
}
}
fn is_final_boundary(&self, line: &[u8]) -> bool {
let mut dash_dash = self.dash_boundary.clone();
dash_dash.extend_from_slice(b"--");
line.strip_prefix(dash_dash.as_slice())
.map(skip_lwsp)
.is_some_and(|rest| rest.is_empty() || rest == self.nl)
}
fn is_boundary_delimiter_line(&mut self, line: &[u8]) -> bool {
let Some(rest) = line.strip_prefix(self.dash_boundary.as_slice()).map(skip_lwsp) else {
return false;
};
if self.parts_read == 0 && rest == b"\n" {
self.nl = b"\n";
}
rest == self.nl
}
fn next_part(&mut self) -> Result<Option<HashMap<String, String>>, String> {
if self.dash_boundary == b"--" {
return Err("multipart: boundary is empty".into());
}
let mut expect_new_part = false;
loop {
let line = if self.limited {
match self.slice_go() {
Slice::Line(line) => line,
Slice::Full(_) => return Err("multipart: NextPart: bufio: buffer full".into()),
Slice::Over(_) => return Err(format!("multipart: NextPart: {}", self.end("EOF"))),
}
} else {
let (line, eof) = self.read_slice();
if eof && self.is_final_boundary(line) {
return Ok(None);
}
if eof {
return Err("multipart: NextPart: EOF".into());
}
line
};
if self.is_boundary_delimiter_line(line) {
self.parts_read += 1;
return self.headers();
}
if self.is_final_boundary(line) {
return Ok(None);
}
if expect_new_part {
return Err(format!("multipart: expecting a new Part; got line {}", quote(line)));
}
if self.parts_read == 0 {
continue;
}
if line == self.nl {
expect_new_part = true;
continue;
}
return Err(format!("multipart: unexpected line in Next(): {}", quote(line)));
}
}
fn fill(&mut self) {
let want = PEEK_BUFFER - (self.window - self.pos);
let left = self.buf.len() - self.window;
if want <= left {
self.window += want;
} else {
self.window = self.buf.len();
self.over = true;
}
}
fn slice_go(&mut self) -> Slice<'a> {
loop {
let buffered = &self.buf[self.pos..self.window];
if let Some(i) = buffered.iter().position(|&b| b == b'\n') {
self.pos += i + 1;
return Slice::Line(&buffered[..=i]);
}
if self.over {
self.pos = self.window;
return Slice::Over(buffered);
}
if buffered.len() >= PEEK_BUFFER {
self.pos += PEEK_BUFFER;
return Slice::Full(buffered);
}
self.fill();
}
}
fn read_line_go(&mut self) -> Option<(&'a [u8], bool)> {
match self.slice_go() {
Slice::Full(line) => match line.strip_suffix(b"\r") {
Some(line) => {
self.pos -= 1;
Some((line, true))
}
None => Some((line, true)),
},
Slice::Over([]) => None,
Slice::Over(line) => Some((line, false)),
Slice::Line(line) => {
let line = &line[..line.len() - 1];
Some((line.strip_suffix(b"\r").unwrap_or(line), false))
}
}
}
fn read_line_slice_go(&mut self, lim: usize) -> Result<Option<Cow<'a, [u8]>>, String> {
let mut line: Option<Vec<u8>> = None;
loop {
let Some((part, more)) = self.read_line_go() else {
return Ok(None);
};
if line.as_ref().map_or(0, Vec::len) + part.len() > lim {
return Err(TOO_LARGE.into());
}
match (&mut line, more) {
(None, false) => return Ok(Some(Cow::Borrowed(part))),
(None, true) => line = Some(part.to_vec()),
(Some(line), _) => line.extend_from_slice(part),
}
if !more {
return Ok(line.map(Cow::Owned));
}
}
}
fn skip_space_go(&mut self) -> usize {
let mut n = 0;
loop {
if self.pos == self.window {
if self.over {
return n;
}
self.fill();
continue;
}
if !matches!(self.buf[self.pos], b' ' | b'\t') {
return n;
}
self.pos += 1;
n += 1;
}
}
fn header_line(&mut self, lim: usize) -> Result<Option<Cow<'a, [u8]>>, String> {
if self.limited {
return self.read_line_slice_go(lim);
}
Ok(self.read_line().map(Cow::Borrowed))
}
fn end(&mut self, eof: &str) -> String {
if !self.limited {
return eof.to_owned();
}
*self.hit_limit = true;
crate::body_limit::MESSAGE.to_owned()
}
fn read_line(&mut self) -> Option<&'a [u8]> {
if self.pos >= self.buf.len() {
return None;
}
let (line, _) = self.read_slice();
Some(match line.strip_suffix(b"\n") {
Some(line) => line.strip_suffix(b"\r").unwrap_or(line),
None => line,
})
}
fn headers(&mut self) -> Result<Option<HashMap<String, String>>, String> {
let mut headers = HashMap::new();
if self.limited && self.pos == self.window && !self.over {
self.fill();
}
let limited = self.limited;
let visible = if limited { self.window } else { self.buf.len() };
if matches!(self.buf[..visible].get(self.pos), Some(b' ' | b'\t')) {
let Some(line) = self.header_line(80)? else {
return self.ended();
};
if line.len() > 80 {
return Err(TOO_LARGE.into());
}
return Err(format!("malformed MIME header initial line: {}", quote(&line)));
}
loop {
let Some(first) = self.header_line(usize::MAX)? else {
return self.ended();
};
let first = &*first;
if first.is_empty() {
return Ok(Some(headers));
}
if !first.contains(&b':') {
return Err(format!("malformed MIME header: missing colon: {}", quote(first)));
}
let trim = if self.limited { go_trim } else { trim_ascii_space };
let mut kv = trim(first).to_vec();
loop {
let spaces = if limited {
self.skip_space_go()
} else {
let spaces = self.buf[self.pos..]
.iter()
.take_while(|&&b| b == b' ' || b == b'\t')
.count();
self.pos += spaces;
spaces
};
if spaces == 0 {
break;
}
kv.push(b' ');
match self.header_line(usize::MAX)? {
Some(line) => kv.extend_from_slice(trim(&line)),
None => break,
}
}
let malformed = || format!("malformed MIME header line: {}", quote(&kv));
let colon = kv.iter().position(|&b| b == b':').unwrap_or(kv.len());
let (key, value) = (&kv[..colon], &kv[(colon + 1).min(kv.len())..]);
if !key.iter().all(|&b| is_header_field_byte(b) || b == b' ') {
return Err(malformed());
}
if value.iter().any(|&b| (b < 0x20 && b != b'\t') || b == 0x7f) {
return Err(malformed());
}
if !key.contains(&b' ') {
let value = value.iter().skip_while(|&&b| b == b' ' || b == b'\t');
headers
.entry(String::from_utf8_lossy(key).to_ascii_lowercase())
.or_insert_with(|| String::from_utf8_lossy(&value.copied().collect::<Vec<_>>()).into_owned());
}
}
}
fn ended(&mut self) -> Result<Option<HashMap<String, String>>, String> {
match self.limited {
false => Ok(None),
true => Err(self.end("EOF")),
}
}
fn body(&mut self) -> Result<Vec<u8>, String> {
if self.limited {
return self.body_go();
}
let mut nl_dash = self.nl.to_vec();
nl_dash.extend_from_slice(&self.dash_boundary);
let mut data = Vec::new();
let mut total = 0;
loop {
let rest = &self.buf[self.pos..];
let (n, scan) = scan_until_boundary(rest, &self.dash_boundary, &nl_dash, total);
data.extend_from_slice(&rest[..n]);
self.pos += n;
total += n;
match scan {
Scan::Data => {}
Scan::End => return Ok(data),
Scan::UnexpectedEof => return Err(self.end("unexpected EOF")),
}
}
}
}
impl Reader<'_, '_> {
fn body_go(&mut self) -> Result<Vec<u8>, String> {
let mut nl_dash = self.nl.to_vec();
nl_dash.extend_from_slice(&self.dash_boundary);
let mut data = Vec::new();
let (mut total, mut read_err) = (0, false);
loop {
let buffered = &self.buf[self.pos..self.window];
let (n, end) = scan_go(buffered, &self.dash_boundary, &nl_dash, total, read_err);
data.extend_from_slice(&buffered[..n]);
self.pos += n;
total += n;
match end {
Some(true) => return Ok(data),
Some(false) => return Err(self.end("unexpected EOF")),
None if n > 0 => {}
None => {
let want = buffered.len() + 1;
while self.window - self.pos < want.min(PEEK_BUFFER) && !self.over {
self.fill();
}
read_err = self.window - self.pos < want;
}
}
}
}
}
fn scan_go(buf: &[u8], dash: &[u8], nl_dash: &[u8], total: usize, read_err: bool) -> (usize, Option<bool>) {
let err = read_err.then_some(false);
if total == 0 {
if buf.starts_with(dash) {
return match match_after_go(buf, dash.len(), read_err) {
-1 => (dash.len(), None),
0 => (0, None),
_ => (0, Some(true)),
};
}
if dash.starts_with(buf) {
return (0, err);
}
}
if let Some(i) = buf.windows(nl_dash.len()).position(|w| w == nl_dash) {
return match match_after_go(&buf[i..], nl_dash.len(), read_err) {
-1 => (i + nl_dash.len(), None),
0 => (i, None),
_ => (i, Some(true)),
};
}
if nl_dash.starts_with(buf) {
return (0, err);
}
if let Some(i) = buf.iter().rposition(|&b| b == nl_dash[0])
&& nl_dash.starts_with(&buf[i..])
{
return (i, None);
}
(buf.len(), err)
}
fn match_after_go(buf: &[u8], prefix: usize, read_err: bool) -> i8 {
match (buf.get(prefix), buf.get(prefix + 1)) {
(None, _) => i8::from(read_err),
(Some(b' ' | b'\t' | b'\r' | b'\n'), _) => 1,
(Some(b'-'), None) => -i8::from(read_err),
(Some(b'-'), Some(b'-')) => 1,
_ => -1,
}
}
fn scan_until_boundary(buf: &[u8], dash: &[u8], nl_dash: &[u8], total: usize) -> (usize, Scan) {
if total == 0 {
if buf.starts_with(dash) {
return match match_after_prefix(buf, dash) {
true => (0, Scan::End),
false => (dash.len(), Scan::Data),
};
}
if dash.starts_with(buf) {
return (0, Scan::UnexpectedEof);
}
}
if let Some(i) = buf.windows(nl_dash.len()).position(|w| w == nl_dash) {
return match match_after_prefix(&buf[i..], nl_dash) {
true => (i, Scan::End),
false => (i + nl_dash.len(), Scan::Data),
};
}
if nl_dash.starts_with(buf) {
return (0, Scan::UnexpectedEof);
}
if let Some(i) = buf.iter().rposition(|&b| b == nl_dash[0])
&& nl_dash.starts_with(&buf[i..])
{
return (i, Scan::Data);
}
(buf.len(), Scan::UnexpectedEof)
}
fn match_after_prefix(buf: &[u8], prefix: &[u8]) -> bool {
match buf.get(prefix.len()) {
None => true,
Some(b' ' | b'\t' | b'\r' | b'\n') => true,
Some(b'-') => buf.get(prefix.len() + 1) == Some(&b'-'),
Some(_) => false,
}
}
fn skip_lwsp(b: &[u8]) -> &[u8] {
let n = b.iter().take_while(|&&c| c == b' ' || c == b'\t').count();
&b[n..]
}
fn go_trim(b: &[u8]) -> &[u8] {
let is_space = |c: &u8| matches!(c, b' ' | b'\t');
let start = b.iter().position(|c| !is_space(c)).unwrap_or(b.len());
let end = b.iter().rposition(|c| !is_space(c)).map_or(start, |i| i + 1);
&b[start..end]
}
fn trim_ascii_space(b: &[u8]) -> &[u8] {
let is_space = |c: &u8| matches!(c, b' ' | b'\t' | b'\n' | b'\r');
let start = b.iter().position(|c| !is_space(c)).unwrap_or(b.len());
let end = b.iter().rposition(|c| !is_space(c)).map_or(start, |i| i + 1);
&b[start..end]
}
fn is_header_field_byte(b: u8) -> bool {
b.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&b)
}
fn is_tspecial(c: u8) -> bool {
b"()<>@,;:\\\"/[]?=".contains(&c)
}
fn is_token_char(c: u8) -> bool {
c > 0x20 && c < 0x7f && !is_tspecial(c)
}
fn consume_token(v: &str) -> (&str, &str) {
let i = v.bytes().position(|c| !is_token_char(c)).unwrap_or(v.len());
v.split_at(i)
}
fn consume_value(v: &str) -> Option<(String, &str)> {
if !v.starts_with('"') {
let (token, rest) = consume_token(v);
return Some((token.to_owned(), rest));
}
let bytes = v.as_bytes();
let mut out = Vec::new();
let mut i = 1;
while i < bytes.len() {
match bytes[i] {
b'"' => return Some((String::from_utf8_lossy(&out).into_owned(), &v[i + 1..])),
b'\\' if i + 1 < bytes.len() && is_tspecial(bytes[i + 1]) => {
out.push(bytes[i + 1]);
i += 1;
}
b'\r' | b'\n' => return None,
b => out.push(b),
}
i += 1;
}
None
}
fn consume_media_param(v: &str) -> Option<(String, String, &str)> {
let rest = v.trim_start().strip_prefix(';')?.trim_start();
let (param, rest) = consume_token(rest);
if param.is_empty() {
return None;
}
let rest = rest.trim_start().strip_prefix('=')?.trim_start();
let (value, after) = consume_value(rest)?;
if value.is_empty() && after.len() == rest.len() {
return None;
}
Some((param.go_lower(), value, after))
}
pub(super) fn parse_media_type(v: &str) -> Result<(String, HashMap<String, String>), ()> {
let base = v.split(';').next().unwrap_or_default();
let media = base.go_lower().trim().to_owned();
let (kind, rest) = consume_token(&media);
if kind.is_empty() {
return Err(());
}
if !rest.is_empty() {
let (sub, rest) = consume_token(rest.strip_prefix('/').ok_or(())?);
if sub.is_empty() || !rest.is_empty() {
return Err(());
}
}
let mut params = HashMap::new();
let mut continuation: HashMap<String, HashMap<String, String>> = HashMap::new();
let mut v = &v[base.len()..];
loop {
v = v.trim_start();
if v.is_empty() {
break;
}
let Some((key, value, rest)) = consume_media_param(v) else {
if v.trim() == ";" {
break;
}
return Err(());
};
let map = match key.split_once('*') {
Some((base_name, _)) => continuation.entry(base_name.to_owned()).or_default(),
None => &mut params,
};
if map.get(&key).is_some_and(|existing| *existing != value) {
return Err(());
}
map.insert(key, value);
v = rest;
}
for (key, pieces) in continuation {
if let Some(v) = pieces.get(&format!("{key}*")) {
if let Some(decoded) = decode_2231(v) {
params.insert(key, decoded);
}
continue;
}
let mut buf = String::new();
let mut valid = false;
for n in 0.. {
if let Some(v) = pieces.get(&format!("{key}*{n}")) {
valid = true;
buf.push_str(v);
continue;
}
let Some(v) = pieces.get(&format!("{key}*{n}*")) else {
break;
};
valid = true;
if n == 0 {
buf.push_str(&decode_2231(v).unwrap_or_default());
} else {
buf.push_str(&percent_unescape(v).unwrap_or_default());
}
}
if valid {
params.insert(key, buf);
}
}
Ok((media, params))
}
fn decode_2231(v: &str) -> Option<String> {
let (charset, rest) = v.split_once('\'')?;
let (_, value) = rest.split_once('\'')?;
match charset.go_lower().as_str() {
"us-ascii" | "utf-8" => percent_unescape(value),
_ => None,
}
}
fn percent_unescape(s: &str) -> Option<String> {
let b = s.as_bytes();
let mut out = Vec::with_capacity(b.len());
let mut i = 0;
while i < b.len() {
if b[i] == b'%' {
let hex = std::str::from_utf8(b.get(i + 1..i + 3)?).ok()?;
out.push(
u8::from_str_radix(hex, 16)
.ok()
.filter(|_| hex.bytes().all(|c| c.is_ascii_hexdigit()))?,
);
i += 3;
} else {
out.push(b[i]);
i += 1;
}
}
Some(String::from_utf8_lossy(&out).into_owned())
}
#[cfg(test)]
mod tests {
use super::*;
const CT: &str = "multipart/form-data; boundary=b";
#[test]
fn reads_file_fields_sorted_by_field_and_skips_values() {
let body = b"--b\r\nContent-Disposition: form-data; name=\"z\"; filename=\"dir/a.json\"\r\n\r\n{}\r\n--b\r\nContent-Disposition: form-data; name=\"f\"\r\n\r\nv\r\n--b\r\nContent-Disposition: form-data; name=\"c\"; filename=\"b.json\"\r\n\r\n[1]\r\n--b--\r\n";
let files = form_files(CT, body).unwrap();
let got: Vec<_> = files.iter().map(|f| (f.filename.as_str(), f.data.as_slice())).collect();
assert_eq!(got, [("b.json", &b"[1]"[..]), ("a.json", &b"{}"[..])]);
}
#[test]
fn media_type_params_follow_go() {
let (t, p) = parse_media_type("Form-Data; Name=\"a\\\"b\"; filename*=UTF-8''x%2Ey; k*0=p; k*1*=%41").unwrap();
assert_eq!(t, "form-data");
assert_eq!(p["name"], "a\"b");
assert_eq!(p["filename"], "x.y");
assert_eq!(p["k"], "pA");
assert!(parse_media_type("a/b; x=1; x=2").is_err());
assert!(parse_media_type("a/b; x=1; x=1").is_ok());
assert!(parse_media_type("a/b/c").is_err());
assert!(parse_media_type("a/b; x=\"open").is_err());
}
#[test]
fn hostile_bodies_never_panic() {
const PIECES: &[&[u8]] = &[
b"--b",
b"--",
b"\r\n",
b"\n",
b"\r",
b" ",
b"\t",
b"-",
b"b",
b"{}",
b"Content-Disposition: form-data; name=\"f\"; filename=\"x.json\"",
b"Content-Disposition: form-data; name=f",
b"X:",
b":",
b"\x7f",
b"\xff",
];
let mut seed: u64 = 0x9e37_79b9_7f4a_7c15;
let mut next = || {
seed ^= seed << 13;
seed ^= seed >> 7;
seed ^= seed << 17;
seed
};
for _ in 0..20_000 {
let mut body = Vec::new();
for _ in 0..(next() % 24) {
body.extend_from_slice(PIECES[(next() % PIECES.len() as u64) as usize]);
}
if let Ok(files) = form_files(CT, &body) {
assert!(
files
.iter()
.all(|f| !f.filename.is_empty() && !f.filename.contains('/'))
);
}
}
}
}