Skip to content

Conversation

@archey347
Copy link

@archey347 archey347 commented Nov 19, 2025

Changes

  • Fix resulting problems with multipart/form-data validation
    • Merge body_params with uploads, as Mojo likes to keep them separate so it can do magic stuff with Mojo::Upload
      • use JSON::Validator::FilePlaceholder for file uploads so we don't have to provide the entire file

Motivation

Currently, no validation is conducted on file uploads. The openapi spec has an option for this.

References

Requires jhthorsen/json-validator#288

if ($content_type =~ /^multipart\/form-data\s*(;|$)/i) {
# body_params only includes non-file parameters, so we need to fetch the
# uploads separately and append them with a file placeholder.
my $params = $c->req->body_params->clone;
Copy link
Author

@archey347 archey347 Nov 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does a full clone of the body params, which might cause problems if it's a large body. But then if they're doing stuff with large amounts of data, they probably should be using mojo's file upload stuff anyway.

I think this is needed, given we need to merge with the uploaded files to pass into the validator

Comment on lines +133 to +150
if ($content_type =~ /^multipart\/form-data\s*(;|$)/i) {
# body_params only includes non-file parameters, so we need to fetch the
# uploads separately and append them with a file placeholder.
my $params = $c->req->body_params->clone;

for my $upload (@{$c->req->uploads}) {
my $name = $upload->name;

my $placeholder = JSON::Validator::FilePlaceholder->new({
filename => $upload->filename,
size => $upload->size,
});

$params->append($name => $placeholder);
}

$res->{value} = $params->to_hash;
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem ideal that we have to re-merge the files/body params like this.

But I think the parsing logic in Mojo::Message can only do files/non-files at any one time.

@archey347 archey347 marked this pull request as ready for review November 19, 2025 12:59
@archey347 archey347 changed the title Combine body_params and uploads on multipart/form-data requests Include file uploads in validation Nov 19, 2025
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

Successfully merging this pull request may close these issues.

1 participant