Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

CGI.pm POST_MAX not working

by adjuvant (Novice)
on Jan 19, 2018 at 20:37 UTC ( [id://1207560]=perlquestion: print w/replies, xml ) Need Help??

adjuvant has asked for the wisdom of the Perl Monks concerning the following question:

I've been searching the web and playing with my code for a while now and can't find a solution to this problem.

I would like to limit uploads to my site with CGI. I'm setting $CGI::POST_MAX = 1024 * 1024 * 20; for a 20 Mb limit, but when I test it with a 900 Mb file, the upload goes through just fine. I've tried every modification I can think of including moving the $CGI::POST_MAX statement around, changing use CGI; to use CGI qw( :standard ); and updating CGI.pm using CPAN. I now have version 4.38 of CGI.pm installed. I would welcome any ideas to be able to protect my server from DDoS attacks.

Here is my code snippet:

#!/usr/bin/perl -T my $version = "0.1"; use strict; use warnings; use CGI; use CGI::Carp qw/fatalsToBrowser/; $CGI::POST_MAX = 1024 * 1024 * 20; # maximum upload filesize is 20M $| = 1; my $q = CGI->new; print $q->header; print $q->start_html(-title=>"mysite", -style=>{-src=>'/plasma/style.css'}); print $q->start_multipart_form(-method=>'POST', -action=>'/cgi-bin/start_analysis.cgi') +; print $q->filefield(-name=>"uf_core", -size=>20); print "\tFile type: "; print $q->popup_menu(-name=>"urt", -values=>['fasta','genbank'], -default=>'fasta'); print "<br><br>"; print qq{<input type="file" name="uf_qry" size="20" multiple="true" /> +}; print "<br>"; print $q->submit(-value=>"ANALYZE!"); print $q->end_multipart_form; print $q->end_html;
Thanks!

Replies are listed 'Best First'.
Re: CGI.pm POST_MAX not working
by poj (Abbot) on Jan 19, 2018 at 21:46 UTC

    Do you have  $CGI::POST_MAX = 1024 * 1024 * 20 in /cgi-bin/start_analysis.cgi ?

    poj

      Are you kidding me? That was absolutely the solution. For the internet's future reference (and in case anyone besides me is still using CGI), POST_MAX has to be set in the script receiving the POST, not the script sending the POST. I don't know why that wasn't clear, but it absolutely wasn't. The CGI documentation left me with the impression that POST_MAX would prevent a script from submitting a too-large post, not that it would prevent a script from receiving a too-large post.

      Thank you!

        Don't feel bad. Easy and common mistake for early attempts in this area. Note however that the first form/CGI does not send the POST at all; it sends nothing to the second. It presents a form to the browser which gives the browser the parameters that it should POST and where to do it.

Re: CGI.pm POST_MAX not working
by Discipulus (Canon) on Jan 19, 2018 at 21:22 UTC
    hi adjuvant,

    ..mmh what I'd try too will be setting $CGI::POST_MAX even before CGI::Carp or even in a BEGIN block just after use CGI

    you can also try to do the job on your own, like in A serious security problem with CGI.pm 3.01?:

    BEGIN # run before anything { my $POST_MAX = -1; my $CL = defined($ENV{'CONTENT_LENGTH'}) ? $ENV{'CONTENT_LENGTH'} : +0; if(($POST_MAX > 0) and ($CL > $POST_MAX)) { print "Content-Type: text/plain\n", "Status: 413\n\n", "413 Request entity too large"; exit; } }

    Infact this seems very simliar to what happens in the CGI.pm code:

    METHOD: { # avoid unreasonably large postings if (($POST_MAX > 0) && ($content_length > $POST_MAX)) { #discard the post, unread $self->cgi_error("413 Request entity too large"); last METHOD; }
    You can insert temporarly some debug statements here in the module to dump what $content_length is at the moment.

    See also Detecting when a $CGI::POST_MAX limit is exceeded and CGI.pm file upload freaking me out

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Cool. Thank you. Now that poj has pointed out the apparently obvious to me below, I like your begin-block approach as it provides for better error-handling and can give the user with more useful feedback. Thanks for taking the time to reply.

      ..mmh what I'd try too will be setting $CGI::POST_MAX even before CGI::Carp or even in a BEGIN block just after use CGI

      No need for any such extra typing , the documentation doesn't lie, and it is as simple as use CGI; $CGI::POST_MAX=...; ... and it should works 100% of the time.

      you can also try to do the job on your own, like in A serious security problem with CGI.pm 3.01?:

      No no no, that node is ~two decades old. If you're using CGI.pm that old upgrade.

      Infact this seems very simliar to what happens in the CGI.pm code: You can insert temporarly some debug statements here in the module to dump what $content_length is at the moment.

      A presumed beginner is supposed to debug CGI.pm? No way. Not funny. No.

      See also Detecting when a $CGI::POST_MAX limit is exceeded and CGI.pm file upload freaking me out

      Meh, long and exactly relevant .... shortcut https://metacpan.org/pod/CGI#Retrieving-cgi-errors

Re: CGI.pm POST_MAX not working
by karlgoethebier (Abbot) on Jan 20, 2018 at 09:25 UTC

      Thanks, Karl. This project is already way too far along in CGI for me to consider re-doing it in Mojolicious or something similar, but if I ever try to convert another bioinformatic pipeline of mine to a web application (honestly, not likely), I'll consider another approach like you've suggested.

      Thanks!

      Hmm, none of those links show how to limit the size of uploads

        Ibidem, a bit below...?

        "To protect you from excessively large files there is also a limit of 16MiB by default, which you can tweak with the attribute "max_request_size" in Mojolicious."
        # Increase limit to 1GiB app->max_request_size(1073741824);

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: CGI.pm POST_MAX not working
by Anonymous Monk on Jan 20, 2018 at 22:57 UTC

    Hi

    Which version of CGI.pm do you have?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1207560]
Approved by Discipulus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found