multra 1.3.1

An async parser for `multipart/form-data` content-type in Rust.
Documentation
use std::collections::HashSet;

use crate::size_limit::SizeLimit;

/// Represents some rules to be applied on the stream and field's content size
/// to prevent `DoS` attacks.
///
/// It's recommended to add some rules on field (specially text field) size to
/// avoid potential `DoS` attacks from attackers running the server out of memory.
/// This type provides some API to apply constraints on very granular level to
/// make `multipart/form-data` safe. By default, total stream and field content
/// sizes are unbounded; preamble and header sizes have finite limits. Configure
/// explicit total and per-field limits for untrusted input.
///
/// # Examples
///
/// ```
/// use multra::{Multipart, Constraints, SizeLimit};
/// # use bytes::Bytes;
/// # use std::convert::Infallible;
/// # use futures_util::stream::once;
///
/// # async fn run() {
/// # let data = "--X-BOUNDARY\r\nContent-Disposition: form-data; name=\"my_text_field\"\r\n\r\nabcd\r\n--X-BOUNDARY--\r\n";
/// # let some_stream = once(async move { Result::<Bytes, Infallible>::Ok(Bytes::from(data)) });
/// // Create some constraints to be applied to the fields to prevent DoS attack.
/// let constraints = Constraints::new()
///      // We only accept `my_text_field` and `my_file_field` fields,
///      // For any unknown field, we will throw an error.
///      .allowed_fields(vec!["my_text_field", "my_file_field"])
///      .size_limit(
///          SizeLimit::new()
///              // Set 15mb as size limit for the whole stream body.
///              .whole_stream(15 * 1024 * 1024)
///              // Set 10mb as size limit for all fields.
///              .per_field(10 * 1024 * 1024)
///              // Set 30kb as size limit for our text field only.
///              .for_field("my_text_field", 30 * 1024),
///      );
///
/// // Create a `Multipart` instance from a stream and the constraints.
/// let mut multipart = Multipart::with_constraints(some_stream, "X-BOUNDARY", constraints);
///
/// while let Some(field) = multipart.next_field().await.unwrap() {
///     let content = field.text().await.unwrap();
///     assert_eq!(content, "abcd");
/// }
/// # }
/// # tokio::runtime::Runtime::new().unwrap().block_on(run());
/// ```
#[derive(Debug, Default)]
#[must_use]
pub struct Constraints {
    pub(crate) size_limit: SizeLimit,
    pub(crate) allowed_fields: Option<HashSet<String>>,
    pub(crate) max_fields: Option<usize>,
}

impl Constraints {
    /// Creates a set of rules with default behaviour.
    pub fn new() -> Self {
        Self::default()
    }

    /// Applies rules on field's content length.
    pub fn size_limit(self, size_limit: SizeLimit) -> Self {
        Self { size_limit, ..self }
    }

    /// Sets the maximum number of fields in a multipart body.
    ///
    /// No field count limit is configured by default. Every part whose headers
    /// are parsed counts once, including empty fields, repeated names, and
    /// fields dropped without reading their data. This is independent of
    /// [`Self::allowed_fields`] and [`Self::size_limit`].
    ///
    /// An additional part is rejected with [`crate::Error::FieldCountExceeded`]
    /// before its headers are parsed. A limit of zero accepts an empty
    /// multipart body only. Even without an explicit limit, the parser rejects
    /// more than [`usize::MAX`] fields to keep field indices from overflowing.
    ///
    /// ```
    /// use multra::Constraints;
    ///
    /// let constraints = Constraints::new().max_fields(100);
    /// ```
    pub const fn max_fields(mut self, limit: usize) -> Self {
        self.max_fields = Some(limit);
        self
    }

    /// Specify which fields should be allowed, for any unknown field, the
    /// [`next_field`](crate::Multipart::next_field) will throw an error.
    pub fn allowed_fields<N: Into<String>>(self, allowed_fields: Vec<N>) -> Self {
        let allowed_fields = allowed_fields.into_iter().map(Into::into).collect();

        Self {
            allowed_fields: Some(allowed_fields),
            ..self
        }
    }

    pub(crate) fn is_it_allowed(&self, field: Option<&str>) -> bool {
        self.allowed_fields
            .as_ref()
            .is_none_or(|allowed_fields| field.is_some_and(|field| allowed_fields.contains(field)))
    }
}