如何使用Perl以一些安全性和最少的资源提供映像?
发布时间:2020-12-15 21:44:08 所属栏目:大数据 来源:网络整理
导读:我在这里发现了一些相关的帖子,但没有任何关于.. 我需要“正确地”提供图像(humm)并尽可能少地使用资源. 我正在研究一个子(下面)但是,由于我使用CGI这个事实并不太资源友好.这只是我的假设.我是一个Perl新手但是,我比它更喜欢它. 该查询将由“somescript.pl?
我在这里发现了一些相关的帖子,但没有任何关于..
我需要“正确地”提供图像(humm)并尽可能少地使用资源. 我正在研究一个子(下面)但是,由于我使用CGI这个事实并不太资源友好.这只是我的假设.我是一个Perl新手但是,我比它更喜欢它. 该查询将由“somescript.pl?img=image.png”生成 #!/usr/bin/perl -Tw use strict; use warnings; use CGI; #I should drop warnings after all is said and done. Also name my vars generically. Right? #I dont know if this query method will work or is even the best method. $query = new CGI; my @img = $query->param; if ( $_ eq "img" ) { my $file = $query->param($_); } if ( $_ ne "img" ) { ## I will send to an error sub that serves up a error image } # Prob a one liner to take care of the above. Not within my ability though. # Still figuring all this out here.. Very verbose sorry... # I will strip everything but lowercase alpha and the "." # with s =~ /[something like A-Z] I will look it up //g; # Still.. prob all above will fit in a one liner by a PERL guru! # Below is related to -Taint,I was told this is important to use the -T. $ENV{PATH} = "bin:/usr/bin"; delete( $ENV{qw(IFS CDPATH BASH_ENV ENV)} ); # now I will grab the images extension. my $ext = ( $file =~ m/[^.]+$/ )[0]; #I was informed to use the "three" but,I am unsure what that means. # My attempt based on my reading many posts here. my $length = ( stat($file) )[10]; my $image = do { local $/ = undef; print "Content-type: image/$extn"; print "Content-length: $length nn"; binmode STDOUT; open( FH,"<",$file ) || die "Could not find $file: $!"; my $buffer = ""; while ( read( FH,$buffer,10240 ) ) { print $buffer; } close(FH); }; 正如你所看到的,我在这里的尝试显然是初学者. 我在堆栈溢出中找到了很好的建议.我感谢过去和现在的所有人. 解决方法
>如果您要使用扩展名作为MIME类型的替代品,那么您最好将所有JPEG图像命名为.jpeg而不是.jpg!
File::MMagic 或
File::MimeInfo 将提供更好的通用解决方案.
>(stat $file)[10]不是内容长度,它是ctime,对你来说毫无价值. (stat $file)[7]有效,但-s $文件也可以正常工作,对任何Perl程序员来说都很明显,无需参考stat手册. (2a:一旦打开文件句柄而不是文件名,就在文件句柄上使用-s,以避免因文件替换而竞争.) >我可以访问文件系统上的任何文件,这些文件系统可由CGI运行的用户读取,例如,image.pl?image=../../../../../../../etc/passwd.我建议特别提到images目录,这样你就不依赖于getcwd,并使用 File::Spec -> no_upwards和File :: Spec-> catfile来构建一个只能在images目录中的路径名. >如果CGI可以避免死亡,那么它就不是真正好的形式.如果找不到该文件,则返回404状态.如果请求是非法的,则返回400或403状态等. >如果您使用path_info来允许image.pl/foo.png而不是image.pl?img=foo.png,那么您的网址会更好. >除非您添加更多逻辑,否则您提供的图像将不会被客户端缓存. >伙计,这些正在堆积起来.您是否考虑过找到一些已经为此目的编写的代码而不是编写自己的代码?
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |