Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multipart via Request builder imposes length limit #3131

Open
ldanilek opened this issue Dec 31, 2024 · 1 comment
Open

Multipart via Request builder imposes length limit #3131

ldanilek opened this issue Dec 31, 2024 · 1 comment

Comments

@ldanilek
Copy link

  • [✅ ] 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?

@jplatte
Copy link
Member

jplatte commented Dec 31, 2024

If your input is not already an HTTP request, I don't think you should be creating a request to then use axum's Multipart extractor on it.

Could you not use multer directly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants