Skip to content

Multipart via Request builder imposes length limit #3131

Open
@ldanilek

Description

  • [✅ ] I have looked for existing issues (including closed) about this

#1623 is close but different
#1409 mentions the issue, i think, but it's closed

Bug Report

Version

$ cargo tree | grep axum
│   │   ├── axum v0.7.5
│   │   │   ├── axum-core v0.4.3
│   │   │   ├── axum-macros v0.4.1 (proc-macro)
│   │   │   │   ├── axum v0.7.5 (*)
│   │   │   ├── axum-core v0.4.3 (*)
│   │   │   ├── axum v0.7.5 (*)
│   │   ├── axum v0.7.5 (*)
│   ├── axum v0.7.5 (*)
├── axum v0.7.5 (*)
├── axum-extra v0.9.3
│   ├── axum v0.7.5 (*)
│   ├── axum-core v0.4.3 (*)
│   ├── axum v0.7.5 (*)
│   ├── axum-extra v0.9.3 (*)
│   ├── axum v0.7.5 (*)
│   │   │   │       ├── axum v0.6.20
│   │   │   │       │   ├── axum-core v0.3.4
│   ├── axum v0.7.5 (*)
│   ├── axum-extra v0.9.3 (*)
│   ├── axum v0.7.5 (*)
│   ├── axum v0.7.5 (*)
│   ├── axum-extra v0.9.3 (*)
├── axum v0.7.5 (*)
├── axum-extra v0.9.3 (*)
└── utoipa-axum v0.1.2
    ├── axum v0.7.5 (*)
├── axum v0.7.5 (*)
├── axum v0.7.5 (*)

Platform

Darwin MacBookPro 24.0.0 Darwin Kernel Version 24.0.0: Tue Sep 24 23:39:07 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T6000 arm64

Crates

axum, axum-core

Description

I'm trying to parse a Multipart form out of a stream of Bytes. Here's the code:

pub async fn run_parse_multi_part(
        &self,
        content_type: String,
        request_stream: BoxStream<'static, anyhow::Result<bytes::Bytes>>,
    ) -> anyhow::Result<Vec<FormPart>> {
        let request = Request::builder()
            .header("Content-Type", content_type)
            .body(axum::body::Body::from_stream(request_stream))?;
        let mut multipart = axum::extract::Multipart::from_request(request, &()).await?;
        let mut results = vec![];
        while let Some(mut field) = multipart.next_field().await? {
            let name = field.name()?.to_string();
            let (file, text) = match field.file_name() {
                None => (None, Some(field.text().await?)),
                Some(file_name) => {
                    let bytes = field.bytes().await?;
                    (Some(bytes), None)
                },
            };
            results.push(FormPart { name, text, file });
        }
        Ok(results)
    }

This imposes a length limit of 2MiB on the stream, after which it throws a "length limit exceeded" error.

The length limit is imposed by Multipart::from_request calling req.with_limited_body().
I tried to remove the length limit by adding .extension(axum::extract::DefaultBodyLimit::disable()) to the Request::builder(), but that doesn't work because the extension type needs to be DefaultBodyLimitKind, which is not public.

It would be great if DefaultBodyLimitKind could be public, so I can add an extension to remove the limit. Or maybe DefaultBodyLimit could implement fn add_extension(req: request::Builder) -> request::Builder?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions