How to check line count on uploaded files?

Discussion in 'Perl Misc' started by Bryan, Jan 8, 2004.

  1. Bryan

    Bryan Guest

    Hi,

    I need to enforce both maximum file size for uploads via CGI.pm, but
    additionally I need to limit files by the number of lines as well.

    CGI.pm apears to provide for max file size:
    $CGI::pOST_MAX=1024 * 100 ;

    But I cannot find anything on how to limit by line length. Do I have to
    check after the file is done with something like this:

    my @data = split(" ", `/usr/bin/wc -cl $file` );
    my ( $lines, $bytes ) = @data;

    Is there no way to do it on the fly?

    Thanks,
    Bryan
     
    Bryan, Jan 8, 2004
    #1
    1. Advertising

  2. Bryan

    Paul Lalli Guest

    Two suggestions:
    (both untested)

    A) Count the number of lines as you're reading the file from the user:


    $file = param('uploaded_file');
    while (<$file>){
    if ($count > $max){
    print "File too big.";
    break;
    } else {
    print OFILE;
    }
    }


    B) Read the file at once, and count the number of \n characters

    $file = param('uploaded_file');
    local $/;
    $contents = <$file>;
    $lines = ($contents =~ tr/\n//);
    if ($lines > $max ) { #file too big }

    There's probably other (better) methods as well.

    Paul Lalli

    On Thu, 8 Jan 2004, Bryan wrote:

    > Date: Thu, 08 Jan 2004 18:43:49 GMT
    > From: Bryan <>
    > Newsgroups: comp.lang.perl.misc
    > Subject: How to check line count on uploaded files?
    >
    > Hi,
    >
    > I need to enforce both maximum file size for uploads via CGI.pm, but
    > additionally I need to limit files by the number of lines as well.
    >
    > CGI.pm apears to provide for max file size:
    > $CGI::pOST_MAX=1024 * 100 ;
    >
    > But I cannot find anything on how to limit by line length. Do I have to
    > check after the file is done with something like this:
    >
    > my @data = split(" ", `/usr/bin/wc -cl $file` );
    > my ( $lines, $bytes ) = @data;
    >
    > Is there no way to do it on the fly?
    >
    > Thanks,
    > Bryan
    >
    >
     
    Paul Lalli, Jan 8, 2004
    #2
    1. Advertising

  3. Bryan

    pkent Guest

    In article <FxhLb.1520$3%>,
    Bryan <> wrote:

    > I need to enforce both maximum file size for uploads via CGI.pm, but
    > additionally I need to limit files by the number of lines as well.

    ....
    > But I cannot find anything on how to limit by line length. Do I have to
    > check after the file is done with something like this:


    Check the docs for your version of CGI.pm but my version seems to imply
    that uploaded files will be spooled to disk (perhaps this only occurs
    past a certain size, so that small uploads are done only in-core?) which
    means that CGI.pm will be able to track the size of the upload and abort
    when it hits the POST_MAX number of bytes.

    To enforce the number-of-lines rule you would read from the filehandle
    that CGI.pm gives you and count the number of end-of-line markers and
    break out when the number gets too high. I suggest that route rather
    than plain old while(<$filehandle>) so you can match all kinds of line
    breaks, and not necessarily slurp an entire, huge, malicious upload into
    memory as one line :)

    > my @data = split(" ", `/usr/bin/wc -cl $file` );
    > my ( $lines, $bytes ) = @data;


    And certainly do the line counting in perl!

    P

    --
    pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
    Remove the tea to reply
     
    pkent, Jan 8, 2004
    #3
  4. On Thu, 08 Jan 2004 18:43:49 +0000, Bryan wrote:

    > Hi,
    >
    > I need to enforce both maximum file size for uploads via CGI.pm, but
    > additionally I need to limit files by the number of lines as well.
    >
    > CGI.pm apears to provide for max file size:
    > $CGI::pOST_MAX=1024 * 100 ;


    Continue to use the $CGI::pOST_MAX setting, because ....

    > But I cannot find anything on how to limit by line length. Do I have to
    > check after the file is done with something like this:
    >
    > my @data = split(" ", `/usr/bin/wc -cl $file` );
    > my ( $lines, $bytes ) = @data;
    >
    > Is there no way to do it on the fly?


    this method of line counting, if not limited, caould be the cause of DOS
    attack againist your server.

    open(FILE, $file) or die "Can't open file\n";
    my @lines = <FILE>;
    close FILE;

    my $count = @lines;

    The line count is now in $count. Salt to taste :)

    I'm sure there are other methods. More importantly - this will work with
    text files, but will fail if it's a binary file it's dealing with.

    HTH

    --
    Jim

    Copyright notice: all code written by the author in this post is
    released under the GPL. http://www.gnu.org/licenses/gpl.txt
    for more information.

    a fortune quote ...
    Whenever I hear anyone arguing for slavery, I feel a strong
    <impulse to see it tried on him personally. -- A. Lincoln
     
    James Willmore, Jan 15, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rajesh

    Virus check files being uploaded.

    rajesh, Oct 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    465
    rajesh
    Oct 29, 2003
  2. Matt G
    Replies:
    1
    Views:
    1,201
    Deepak Kumar Vasudevan
    Aug 22, 2003
  3. rajesh

    virus check uploaded files.

    rajesh, Oct 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    362
    rajesh
    Oct 29, 2003
  4. Replies:
    2
    Views:
    146
  5. babu17
    Replies:
    1
    Views:
    149
Loading...

Share This Page