]> git.proxmox.com Git - pve-http-server.git/commitdiff
fix multipart upload: ignore additional headers
authorMatthias Heiserer <m.heiserer@proxmox.com>
Mon, 12 Dec 2022 15:07:55 +0000 (16:07 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 13 Dec 2022 12:16:31 +0000 (13:16 +0100)
Reported in the forum:
https://forum.proxmox.com/threads/image-upload-fails-after-upgrading-from-7-1-to-7-3.119051/#post-516517

When additional headers existed in the request body, the upload failed.
With this patch, all additional headers get ignored.

Example: The following upload would fail because no headers were
expected after Content-Disposition.

```
--EPIHyQJFC5ftgoXHMe8-Jc6E7FqA4oMb0QBfOTz
Content-Disposition: form-data; name="content"
Content-Type: text/plain; charset=ISO-8859-1

iso
```
would fail. These headers now also get ignored, as we don't use them.

Also, upload now works when the Content-Disposition header isn't the
first, i.e.:
```
--XVH95dt1-A3J8mWiLCmHCW4roSC7-gBntjATBy--
Content-Type: text/plain; charset=ISO-8859-1
Content-Disposition: form-data; name="content"
```

Fixed upload was tested using
* Curl
* GUI
* Apache HttpClient 5

Signed-off-by: Matthias Heiserer <m.heiserer@proxmox.com>
Reviewed-by: Daniel Tschlatscher <d.tschlatscher@proxmox.com>
Tested-by:  Daniel Tschlatscher <d.tschlatscher@proxmox.com>
src/PVE/APIServer/AnyEvent.pm

index 545c122b04d00755270be4857105a9689a5f84bd..2f8718fedaac8b3076673f1f8d628d20812d029e 100644 (file)
@@ -1201,11 +1201,19 @@ sub file_upload_multipart {
            $rstate->{phase} = 1;
        }
 
+       my $remove_until_data = sub {
+           my ($hdl) = @_;
+           # remove any remaining multipart "headers" like Content-Type
+           $hdl->{rbuf} =~ s/^.*?${newline_re}{2}//s;
+       };
+
        my $extract_form_disposition = sub {
            my ($name) = @_;
-           if ($hdl->{rbuf} =~ s/^${delim_re}Content-Disposition: (.*?); name="$name"(.*?)($delim_re)/$3/s) {
+           if ($hdl->{rbuf} =~ s/^${delim_re}.*?Content-Disposition: (.*?); name="$name"(.*?${delim_re})/$2/s) {
                assert_form_disposition($1);
-               $rstate->{params}->{$name} = trim($2);
+               $remove_until_data->($hdl);
+               $hdl->{rbuf} =~ s/^(.*?)(${delim_re})/$2/s;
+               $rstate->{params}->{$name} = trim($1);
            }
        };