]> git.proxmox.com Git - pve-http-server.git/blobdiff - src/PVE/APIServer/AnyEvent.pm
upload: re-allow white space in filenames
[pve-http-server.git] / src / PVE / APIServer / AnyEvent.pm
index f0e2e68739f6b83740a77e54dd1031d7032939aa..39061ad82457e5ef81ba658a067bb11fc643331b 100644 (file)
@@ -9,43 +9,47 @@ package PVE::APIServer::AnyEvent;
 
 use strict;
 use warnings;
-use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
-use Socket qw(IPPROTO_TCP TCP_NODELAY SOMAXCONN);
-use POSIX qw(strftime EINTR EAGAIN);
-use Fcntl;
-use IO::File;
-use File::stat qw();
-use File::Find;
-use MIME::Base64;
-use Digest::MD5;
-use Digest::SHA;
-# use AnyEvent::Strict; # only use this for debugging
-use AnyEvent::Util qw(guard fh_nonblocking WSAEWOULDBLOCK WSAEINPROGRESS);
-use AnyEvent::Socket;
+
+use AnyEvent::HTTP;
 use AnyEvent::Handle;
-use Net::SSLeay;
-use AnyEvent::TLS;
 use AnyEvent::IO;
-use AnyEvent::HTTP;
-use Fcntl ();
+use AnyEvent::Socket;
+# use AnyEvent::Strict; # only use this for debugging
+use AnyEvent::TLS;
+use AnyEvent::Util qw(guard fh_nonblocking WSAEWOULDBLOCK WSAEINPROGRESS);
+
 use Compress::Zlib;
+use Digest::MD5;
+use Digest::SHA;
 use Encode;
-use PVE::SafeSyslog;
-use PVE::INotify;
-use PVE::Tools;
-use PVE::APIServer::Formatter;
-use PVE::APIServer::Utils;
+use Fcntl ();
+use Fcntl;
+use File::Find;
+use File::stat qw();
+use IO::File;
+use MIME::Base64;
+use Net::SSLeay;
+use POSIX qw(strftime EINTR EAGAIN);
+use Socket qw(IPPROTO_TCP TCP_NODELAY SOMAXCONN);
+use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
 
-use Net::IP;
-use URI;
-use URI::Escape;
-use HTTP::Status qw(:constants);
+#use Data::Dumper; # FIXME: remove, just use: print to_json([$var], {pretty => 1}) ."\n";
 use HTTP::Date;
 use HTTP::Headers;
 use HTTP::Request;
 use HTTP::Response;
-use Data::Dumper;
+use HTTP::Status qw(:constants);
 use JSON;
+use Net::IP;
+use URI::Escape;
+use URI;
+
+use PVE::INotify;
+use PVE::SafeSyslog;
+use PVE::Tools qw(trim);
+
+use PVE::APIServer::Formatter;
+use PVE::APIServer::Utils;
 
 my $limit_max_headers = 64;
 my $limit_max_header_size = 8*1024;
@@ -114,7 +118,7 @@ sub log_aborted_request {
 }
 
 sub cleanup_reqstate {
-    my ($reqstate) = @_;
+    my ($reqstate, $deletetmpfile) = @_;
 
     delete $reqstate->{log};
     delete $reqstate->{request};
@@ -123,7 +127,7 @@ sub cleanup_reqstate {
     delete $reqstate->{starttime};
 
     if ($reqstate->{tmpfilename}) {
-       unlink $reqstate->{tmpfilename};
+       unlink $reqstate->{tmpfilename} if $deletetmpfile;
        delete $reqstate->{tmpfilename};
     }
 }
@@ -131,7 +135,7 @@ sub cleanup_reqstate {
 sub client_do_disconnect {
     my ($self, $reqstate) = @_;
 
-    cleanup_reqstate($reqstate);
+    cleanup_reqstate($reqstate, 1);
 
     my $shutdown_hdl = sub {
        my $hdl = shift;
@@ -144,7 +148,8 @@ sub client_do_disconnect {
     };
 
     if (my $proxyhdl = delete $reqstate->{proxyhdl}) {
-       &$shutdown_hdl($proxyhdl);
+       &$shutdown_hdl($proxyhdl)
+               if !$proxyhdl->{block_disconnect};
     }
 
     my $hdl = delete $reqstate->{hdl};
@@ -168,7 +173,7 @@ sub client_do_disconnect {
 sub finish_response {
     my ($self, $reqstate) = @_;
 
-    cleanup_reqstate($reqstate);
+    cleanup_reqstate($reqstate, 0);
 
     my $hdl = $reqstate->{hdl};
     return if !$hdl; # already disconnected
@@ -288,11 +293,18 @@ sub response {
 
     my $code = $resp->code;
     my $msg = $resp->message || HTTP::Status::status_message($code);
-    ($msg) = $msg =~m/^(.*)$/m;
     my $content = $resp->content;
 
+    # multiline mode only checks \n for $, so explicitly check for any \n or \r afterwards
+    ($msg) = $msg =~ m/^(.*)$/m;
+    if ($msg =~ /[\r\n]/) {
+       $code = 400; # bad request from user
+       $msg = HTTP::Status::status_message($code);
+       $content = '';
+    }
+
     if ($code =~ /^(1\d\d|[23]04)$/) {
-       # make sure content we have no content
+       # make sure informational, no content and not modified response send no content
        $content = "";
     }
 
@@ -356,6 +368,7 @@ sub response {
     } elsif ($delay && $delay > 0) {
        my $w; $w = AnyEvent->timer(after => $delay, cb => sub {
            undef $w; # delete reference
+           return if !$reqstate->{hdl}; # already disconnected
            $reqstate->{hdl}->push_write($res);
            $self->finish_response($reqstate);
        });
@@ -412,6 +425,8 @@ sub send_file_start {
 
            if (ref($download) eq 'HASH') {
                $mime = $download->{'content-type'};
+               my $encoding = $download->{'content-encoding'};
+               my $disposition = $download->{'content-disposition'};
 
                if ($download->{path} && $download->{stream} &&
                    $reqstate->{request}->header('PVEDisableProxy'))
@@ -423,6 +438,8 @@ sub send_file_start {
                        pvestreamfile => $download->{path},
                        Content_Type => $mime,
                    );
+                   $header->header('Content-Encoding' => $encoding) if defined($encoding);
+                   $header->header('Content-Disposition' => $disposition) if defined($disposition);
                    # we need some data so Content-Length gets set correctly and
                    # the proxy doesn't wait for more data - place a canary
                    my $resp = HTTP::Response->new(200, "OK", $header, "error canary");
@@ -440,6 +457,8 @@ sub send_file_start {
 
                if ($download->{stream}) {
                    my $header = HTTP::Headers->new(Content_Type => $mime);
+                   $header->header('Content-Encoding' => $encoding) if defined($encoding);
+                   $header->header('Content-Disposition' => $disposition) if defined($disposition);
                    my $resp = HTTP::Response->new(200, "OK", $header);
                    $self->response($reqstate, $resp, undef, 1, 0, $fh);
                    return;
@@ -496,15 +515,6 @@ sub websocket_proxy {
 
        my $max_payload_size = 128*1024;
 
-       my $binary;
-       if ($wsproto eq 'binary') {
-           $binary = 1;
-       } elsif ($wsproto eq 'base64') {
-           $binary = 0;
-       } else {
-           die "websocket_proxy: unsupported protocol '$wsproto'\n";
-       }
-
        if ($param->{port}) {
            $remhost = 'localhost';
            $remport = $param->{port};
@@ -520,13 +530,9 @@ sub websocket_proxy {
 
            my $string;
            my $payload;
-           if ($binary) {
-               $string = $opcode ? $opcode : "\x82"; # binary frame
-               $payload = $$data;
-           } else {
-               $string = $opcode ? $opcode : "\x81"; # text frame
-               $payload = encode_base64($$data, '');
-           }
+
+           $string = $opcode ? $opcode : "\x82"; # binary frame
+           $payload = $$data;
 
            my $payload_len = length($payload);
            if ($payload_len <= 125) {
@@ -635,16 +641,16 @@ sub websocket_proxy {
                        $payload ^= $mask;
                    }
 
-                   $payload = decode_base64($payload) if !$binary;
-
                    if ($opcode == 1 || $opcode == 2) {
                        $reqstate->{proxyhdl}->push_write($payload) if $reqstate->{proxyhdl};
                    } elsif ($opcode == 8) {
                        my $statuscode = unpack ("n", $payload);
                        $self->dprint("websocket received close. status code: '$statuscode'");
-                       if ($reqstate->{proxyhdl}) {
-                           $reqstate->{proxyhdl}->push_shutdown();
-                       }
+                       if (my $proxyhdl = $reqstate->{proxyhdl}) {
+                           $proxyhdl->{block_disconnect} = 1 if length $proxyhdl->{wbuf};
+
+                           $proxyhdl->push_shutdown();
+                       }
                        $hdl->push_shutdown();
                    } elsif ($opcode == 9) {
                        # ping received, schedule pong
@@ -665,11 +671,15 @@ sub websocket_proxy {
 
            # todo: use stop_read/start_read if write buffer grows to much
 
+           # FIXME: remove protocol in PVE/PMG 8.x
+           #
+           # for backwards, compatibility,  we have to reply with the websocket
+           # subprotocol from the request
            my $res = "$proto 101 Switching Protocols\015\012" .
                "Upgrade: websocket\015\012" .
                "Connection: upgrade\015\012" .
                "Sec-WebSocket-Accept: $wsaccept\015\012" .
-               "Sec-WebSocket-Protocol: $wsproto\015\012" .
+               ($wsproto ne "" ? "Sec-WebSocket-Protocol: $wsproto\015\012" : "") .
                "\015\012";
 
            $self->dprint($res);
@@ -695,10 +705,20 @@ sub proxy_request {
     eval {
        my $target;
        my $keep_alive = 1;
+
+       # stringify URI object and verify it starts with a slash
+       $uri = "$uri";
+       if ($uri !~ m@^/@) {
+           $self->error($reqstate, 400, "invalid proxy uri");
+           return;
+       }
+
+       my $may_stream_file;
        if ($host eq 'localhost') {
            $target = "http://$host:85$uri";
            # keep alive for localhost is not worth (connection setup is about 0.2ms)
            $keep_alive = 0;
+           $may_stream_file = 1;
        } elsif (Net::IP::ip_is_ipv6($host)) {
            $target = "https://[$host]:8006$uri";
        } else {
@@ -784,6 +804,10 @@ sub proxy_request {
                        $header->header(Location => $location);
                    }
                    if ($stream) {
+                       if (!$may_stream_file) {
+                           $self->error($reqstate, 403, 'streaming denied');
+                           return;
+                       }
                        sysopen(my $fh, "$stream", O_NONBLOCK | O_RDONLY)
                            or die "open stream path '$stream' for forwarding failed: $!\n";
                        my $resp = HTTP::Response->new($code, $msg, $header, undef);
@@ -881,7 +905,7 @@ sub handle_api2_request {
            $params = extract_params($r, $method);
        }
 
-       delete $params->{_dc}; # remove disable cache parameter
+       delete $params->{_dc} if $params; # remove disable cache parameter
 
        my $clientip = $reqstate->{peer_host};
 
@@ -917,14 +941,7 @@ sub handle_api2_request {
            die "unable to upgrade to protocol '$upgrade'\n" if !$upgrade || ($upgrade ne 'websocket');
            my $wsver = $r->header('sec-websocket-version');
            die "unsupported websocket-version '$wsver'\n" if !$wsver || ($wsver ne '13');
-           my $wsproto_str = $r->header('sec-websocket-protocol');
-           die "missing websocket-protocol header" if !$wsproto_str;
-           my $wsproto;
-           foreach my $p (PVE::Tools::split_list($wsproto_str)) {
-               $wsproto = $p if !$wsproto && $p eq 'base64';
-               $wsproto = $p if $p eq 'binary';
-           }
-           die "unsupported websocket-protocol protocol '$wsproto_str'\n" if !$wsproto;
+           my $wsproto = $r->header('sec-websocket-protocol') // "";
            my $wskey = $r->header('sec-websocket-key');
            die "missing websocket-key\n" if !$wskey;
            # Note: Digest::SHA::sha1_base64 has wrong padding
@@ -1162,111 +1179,85 @@ sub handle_request {
     }
 }
 
+my sub assert_form_disposition {
+    die "wrong Content-Disposition '$_[0]' in multipart, expected 'form-data'\n" if $_[0] ne 'form-data';
+}
+
 sub file_upload_multipart {
     my ($self, $reqstate, $auth, $method, $path, $rstate) = @_;
 
     eval {
        my $boundary = $rstate->{boundary};
        my $hdl = $reqstate->{hdl};
-
        my $startlen = length($hdl->{rbuf});
 
-       if ($rstate->{phase} == 0) { # skip everything until start
-           if ($hdl->{rbuf} =~ s/^.*?--\Q$boundary\E  \015?\012
-                       ((?:[^\015]+\015\012)* ) \015?\012//xs) {
-               my $header = $1;
-               my ($ct, $disp, $name, $filename);
-               foreach my $line (split(/\015?\012/, $header)) {
-                   # assume we have single line headers
-                   if ($line =~ m/^Content-Type\s*:\s*(.*)/i) {
-                       $ct = parse_content_type($1);
-                   } elsif ($line =~ m/^Content-Disposition\s*:\s*(.*)/i) {
-                       ($disp, $name, $filename) = parse_content_disposition($1);
-                   }
-               }
+       my $newline_re = qr/\015?\012/;
+       my $delim_re = qr/--\Q$boundary\E${newline_re}/;
+       my $close_delim_re = qr/--\Q$boundary\E--${newline_re}/;
 
-               if (!($disp && $disp eq 'form-data' && $name)) {
-                   syslog('err', "wrong content disposition in multipart - abort upload");
-                   $rstate->{phase} = -1;
-               } else {
+       # Phase 0 - preserve boundary, but remove everything before
+       if ($rstate->{phase} == 0 && $hdl->{rbuf} =~ s/^.*?($delim_re)/$1/s) {
+           $rstate->{read} += $startlen - length($hdl->{rbuf});
+           $rstate->{phase} = 1;
+       }
 
-                   $rstate->{fieldname} = $name;
+       my $extract_form_disposition = sub {
+           my ($name) = @_;
+           if ($hdl->{rbuf} =~ s/^${delim_re}Content-Disposition: (.*?); name="$name"(.*?)($delim_re)/$3/s) {
+               assert_form_disposition($1);
+               $rstate->{params}->{$name} = trim($2);
+           }
+       };
 
-                   if ($filename) {
-                       if ($name eq 'filename') {
-                           # found file upload data
-                           $rstate->{phase} = 1;
-                           $rstate->{filename} = $filename;
-                       } else {
-                           syslog('err', "wrong field name for file upload - abort upload");
-                           $rstate->{phase} = -1;
-                       }
-                   } else {
-                       # found form data for field $name
-                       $rstate->{phase} = 2;
-                   }
-               }
-           } else {
-               my $len = length($hdl->{rbuf});
-               substr($hdl->{rbuf}, 0, $len - $rstate->{maxheader}, '')
-                   if $len > $rstate->{maxheader}; # skip garbage
+       if ($rstate->{phase} == 1) { # Phase 1 - parse payload without file data
+           $extract_form_disposition->('content');
+           $extract_form_disposition->('checksum-algorithm');
+           $extract_form_disposition->('checksum');
+
+           if ($hdl->{rbuf} =~
+               s/^${delim_re}
+               Content-Disposition:\ (.*?);\ name="(.*?)";\ filename="([^"]+)"${newline_re}
+               Content-Type:\ \S*\s+
+               //sxx
+           ) {
+               assert_form_disposition($1);
+               die "wrong field name '$2' for file upload, expected 'filename'" if $2 ne "filename";
+               $rstate->{phase} = 2;
+               $rstate->{params}->{filename} = trim($3);
            }
-       } elsif ($rstate->{phase} == 1) { # inside file - dump until end marker
-           if ($hdl->{rbuf} =~ s/^(.*?)\015?\012(--\Q$boundary\E(--)? \015?\012(.*))$/$2/xs) {
-               my ($rest, $eof) = ($1, $3);
-               my $len = length($rest);
-               die "write to temporary file failed - $!"
-                   if syswrite($rstate->{outfh}, $rest) != $len;
-               $rstate->{ctx}->add($rest);
-               $rstate->{params}->{filename} = $rstate->{filename};
-               $rstate->{md5sum} = $rstate->{ctx}->hexdigest;
-               $rstate->{bytes} += $len;
-               $rstate->{phase} =  $eof ? 100 : 0;
+       }
+
+       if ($rstate->{phase} == 2) { # Phase 2 - dump content into file
+           my ($data, $write_length);
+           if ($hdl->{rbuf} =~ s/^(.*?)${newline_re}?+${close_delim_re}.*$//s) {
+               $data = $1;
+               $write_length = length($data);
+               $rstate->{phase} =  100;
            } else {
-               my $len = length($hdl->{rbuf});
-               my $wlen = $len - $rstate->{boundlen};
-               if ($wlen > 0) {
-                   my $data = substr($hdl->{rbuf}, 0, $wlen, '');
-                   die "write to temporary file failed - $!"
-                       if syswrite($rstate->{outfh}, $data) != $wlen;
-                   $rstate->{bytes} += $wlen;
-                   $rstate->{ctx}->add($data);
-               }
+               $write_length = length($hdl->{rbuf}) - $rstate->{boundlen};
+               $data = substr($hdl->{rbuf}, 0, $write_length, '') if $write_length > 0;
            }
-       } elsif ($rstate->{phase} == 2) { # inside normal field
-
-           if ($hdl->{rbuf} =~ s/^(.*?)\015?\012(--\Q$boundary\E(--)? \015?\012(.*))$/$2/xs) {
-               my ($rest, $eof) = ($1, $3);
-               my $len = length($rest);
-               $rstate->{post_size} += $len;
-               if ($rstate->{post_size} < $limit_max_post) {
-                   $rstate->{params}->{$rstate->{fieldname}} = $rest;
-                   $rstate->{phase} = $eof ? 100 : 0;
-               } else {
-                   syslog('err', "form data to large - abort upload");
-                   $rstate->{phase} = -1; # skip
-               }
+
+           if ($write_length > 0) {
+               syswrite($rstate->{outfh}, $data) == $write_length or die "write to temporary file failed - $!\n";
+               $rstate->{bytes} += $write_length;
+               $rstate->{ctx}->add($data);
            }
-       } else { # skip
-           my $len = length($hdl->{rbuf});
-           substr($hdl->{rbuf}, 0, $len, ''); # empty rbuf
        }
 
-       $rstate->{read} += ($startlen - length($hdl->{rbuf}));
+       if ($rstate->{phase} == 100) { # Phase 100 - transfer finished
+           $rstate->{md5sum} = $rstate->{ctx}->hexdigest;
+           my $elapsed = tv_interval($rstate->{starttime});
+           syslog('info', "multipart upload complete (size: %dB time: %.3fs rate: %.2fMiB/s md5sum: %s)",
+               $rstate->{bytes}, $elapsed, $rstate->{bytes} / ($elapsed * 1024 * 1024), $rstate->{md5sum}
+           );
+           $self->handle_api2_request($reqstate, $auth, $method, $path, $rstate);
+       }
 
-       if (!$rstate->{done} && ($rstate->{read} + length($hdl->{rbuf})) >= $rstate->{size}) {
-           $rstate->{done} = 1; # make sure we dont get called twice
-           if ($rstate->{phase} < 0 || !$rstate->{md5sum}) {
-               die "upload failed\n";
-           } else {
-               my $elapsed = tv_interval($rstate->{starttime});
+       $rstate->{read} += $startlen - length($hdl->{rbuf});
 
-               my $rate = int($rstate->{bytes}/($elapsed*1024*1024));
-               syslog('info', "multipart upload complete " .
-                      "(size: %d time: %ds rate: %.2fMiB/s md5sum: $rstate->{md5sum})",
-                      $rstate->{bytes}, $elapsed, $rate);
-               $self->handle_api2_request($reqstate, $auth, $method, $path, $rstate);
-           }
+       if ($rstate->{read} + length($hdl->{rbuf}) >= $rstate->{size} && $rstate->{phase} != 100) {
+           die "upload failed";
        }
     };
     if (my $err = $@) {
@@ -1467,8 +1458,7 @@ sub unshift_read_header {
                    }
 
                    my $ctype = $r->header('Content-Type');
-                   my ($ct, $boundary);
-                   ($ct, $boundary)= parse_content_type($ctype) if $ctype;
+                   my ($ct, $boundary) = $ctype ? parse_content_type($ctype) : ();
 
                    if ($auth->{isUpload} && !$self->{trusted_env}) {
                        die "upload 'Content-Type '$ctype' not implemented\n"
@@ -1502,7 +1492,9 @@ sub unshift_read_header {
                            outfh => $outfh,
                        };
                        $reqstate->{tmpfilename} = $tmpfilename;
-                       $reqstate->{hdl}->on_read(sub { $self->file_upload_multipart($reqstate, $auth, $method, $path, $state); });
+                       $reqstate->{hdl}->on_read(sub {
+                           $self->file_upload_multipart($reqstate, $auth, $method, $path, $state);
+                       });
                        return;
                    }
 
@@ -1557,6 +1549,12 @@ sub push_request_header {
                        $self->error($reqstate, 506, "http protocol version $maj.$min not supported");
                        return;
                    }
+                   if ($url =~ m|^[^/]*@|) {
+                       # if an '@' comes before the first slash proxy forwarding might consider
+                       # the frist part of the url to be part of an authority...
+                       $self->error($reqstate, 400, "invalid url");
+                       return;
+                   }
 
                    $self->{request_count}++; # only count valid request headers
                    if ($self->{request_count} >= $self->{max_requests}) {
@@ -1893,6 +1891,9 @@ sub new {
            honor_cipher_order => 1,
        };
 
+       # workaround until anyevent supports TLS 1.3 ciphersuites directly
+       my $ciphersuites = delete $self->{ssl}->{ciphersuites};
+
        foreach my $k (keys %$ssl_defaults) {
            $self->{ssl}->{$k} //= $ssl_defaults->{$k};
        }
@@ -1901,13 +1902,26 @@ sub new {
            $self->{ssl}->{dh} = 'skip2048';
        }
 
-       my $tls_ctx_flags = &Net::SSLeay::OP_NO_COMPRESSION | &Net::SSLeay::OP_SINGLE_ECDH_USE | &Net::SSLeay::OP_SINGLE_DH_USE;
-       if ( delete $self->{ssl}->{honor_cipher_order} ) {
+       my $tls_ctx_flags = 0;
+       $tls_ctx_flags |= &Net::SSLeay::OP_NO_COMPRESSION;
+       $tls_ctx_flags |= &Net::SSLeay::OP_SINGLE_ECDH_USE;
+       $tls_ctx_flags |= &Net::SSLeay::OP_SINGLE_DH_USE;
+       $tls_ctx_flags |= &Net::SSLeay::OP_NO_RENEGOTIATION;
+       if (delete $self->{ssl}->{honor_cipher_order}) {
            $tls_ctx_flags |= &Net::SSLeay::OP_CIPHER_SERVER_PREFERENCE;
        }
+       # workaround until anyevent supports disabling TLS 1.3 directly
+       if (exists($self->{ssl}->{tlsv1_3}) && !$self->{ssl}->{tlsv1_3}) {
+           $tls_ctx_flags |= &Net::SSLeay::OP_NO_TLSv1_3;
+       }
+
 
        $self->{tls_ctx} = AnyEvent::TLS->new(%{$self->{ssl}});
        Net::SSLeay::CTX_set_options($self->{tls_ctx}->{ctx}, $tls_ctx_flags);
+       if (defined($ciphersuites)) {
+           warn "Failed to set TLS 1.3 ciphersuites '$ciphersuites'\n"
+               if !Net::SSLeay::CTX_set_ciphersuites($self->{tls_ctx}->{ctx}, $ciphersuites);
+       }
     }
 
     if ($self->{spiceproxy}) {