How to check line count on uploaded files?

B

Bryan

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
 
P

Paul Lalli

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
 
P

pkent

Bryan said:
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
 
J

James Willmore

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,083
Members
48,773
Latest member
Kaybee

Latest Threads

Top