Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Insecure Dependency in Taint Mode

by pryrt (Abbot)
on Nov 05, 2022 at 22:29 UTC ( #11147998=note: print w/replies, xml ) Need Help??


in reply to Re^2: Insecure Dependency in Taint Mode
in thread Insecure Dependency in Taint Mode

I have replaced $ENV{'DOCUMENT_ROOT'} with hardcoded paths

Sorry I hadn't noticed that statement. Though if you knew that already, it would have been nice if your SSCCE had removed that distraction. Especially because when I ran my original code, I was able to show a taint problem with $ENV{'DOCUMENT_ROOT'} and no problem when I had an untainted variable instead. I assumed that must be the culprit, since it matched your shown code.

Taking out that, I can run an equivalent of every single line from your SSCCE script, with some extra debug prints, without flagging a taint problem.

Command: HTTP_HOST=127.0.0.1 perl -T sscce-t.pl

Output:

content-type: text/plain; RealBin = /home1/pryrtcom PDF::API2::VERSION = 2.043 ROOT = $root = /home1/pryrtcom/public_html [Sat Nov 5 16:07:36 2022] sscce-t.pl: Use of uninitialized value in v +ec at /home1/pryrtcom/perl5/lib/perl5/PDF/API2/Resource/CIDFont/TrueT +ype/FontFile.pm line 554. [Sat Nov 5 16:07:36 2022] sscce-t.pl: Use of uninitialized value in s +calar assignment at /home1/pryrtcom/perl5/lib/perl5/PDF/API2/Resource +/CIDFont/TrueType/FontFile.pm line 554. Font => PDF::API2::Resource::CIDFont::TrueType=HASH(0x469f530) Page => PDF::API2::Page=HASH(0x2c752e8) Text => PDF::API2::Content::Text=HASH(0x3b97148) set font => PDF::API2::Content::Text=HASH(0x3b97148) set pos => PDF::API2::Content::Text=HASH(0x3b97148) set text => 156.096 save => Content-type: text/plain 127.0.0.1/../output.pdf

Source:

#!/usr/bin/perl -T use warnings; use strict; use autodie; use CGI::Carp qw/fatalsToBrowser/; BEGIN { print "\n" x 10; print "content-type: text/plain;\n\n"; $|=1; $\ = "\n"; } use FindBin qw($RealBin); my $safepath; BEGIN { print "RealBin = $RealBin"; if( $RealBin =~ m!^(/home1/pryrtcom)!) { $safepath = "$1/../lib"; } else { die "insecure access!"; } } use lib $safepath; # no reason to put quotes around it use cPanelUserConfig; use PDF::API2; print "PDF::API2::VERSION = ", $PDF::API2::VERSION; my $root = '/home1/pryrtcom/public_html'; print "ROOT = \$root = $root"; my $pdf = PDF::API2->open("$root/../local.pdf"); print "Font => ", my $font = $pdf->font("/usr/share/fonts/dejavu/DejaV +uSans.ttf"); print "Page => ", my $page = $pdf->open_page(1); print "Text => ", my $text = $page->text; print "set font => ", $text->font($font, 36); print "set pos => ", $text->position(656, 403); print "set text => ", $text->text('ABC-123'); print "save => ", $pdf->save("$root/../output.pdf"); undef $\; print "\n"x4; print "Content-type: text/plain\n\n"; print "$ENV{'HTTP_HOST'}/../output.pdf\n";

(I tried with a font I uploaded, or one that was present on my host. Either one gave me the error when I ran the $pdf->font(...) line. I don't know if you're just not seeing that error because it's in a server logfile that you haven't checked, or whether you're not getting that warning, maybe because of using a different font or a different version of PDF::API2 -- that's one of the reasons I included the print of the module version in my code.)

But with that code, I could not replicate your taint error.

To emphasize to the advice from kcott, you need to narrow it down to which line of code is actually causing the taint problem. His example of loading a PDF and then immediately saving it (instead of loading, manipulating, and saving) will narrow it down to whether it's one of your manipulation commands that's causing the problem, or just writing the PDF to disk. Also, wrapping individual commands in eval (like I did in my first code example), with extra debug prints around, so you know exactly where it happens, would also be helpful to you. Also, if you have shell access to your host, it would be good to try running it from the command-line rather than just through the browser -- this will make it easier to see side warnings that are buried in a log file you haven't checked, and will also show if there's maybe something different going on between running through web interface and running through your host's command line.

But as my two SSCCE's have shown, there is nothing inherently taint-unsafe with any of the PDF::API2 v2.043 commands that I ran, which I believe match in spirit the method calls you showed, so the problem seems to me to be something unique about the way you are using them, or arguments that you are passing to them, rather than inherent to the library.

Replies are listed 'Best First'.
Re^4: Insecure Dependency in Taint Mode
by Bod (Vicar) on Nov 05, 2022 at 22:47 UTC

    Well - I'm feeling a little confused and even more stupid...
    I identified that $ENV{'DOCUMENT_ROOT'} could be the problem and hardcoded them and still came up with the problem. But now I hardcode them again I cannot replicate the problem - in other words, it works!

    Here is a working example, in case anyone stumbles along this path as much as anything else

    #!/usr/bin/perl -T use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use FindBin qw($RealBin); my $safepath; BEGIN { if ($RealBin =~ m!^(/home/...path.../(test|uk)/www)!) { $safepath = "$1/../lib"; } else { die "Insecure access!"; } } use lib "$safepath"; use cPanelUserConfig; use PDF::API2; my $pdf = PDF::API2->open("/home/...path.../test/data/xmas/GiftSubscr +iption.pdf"); my $font = $pdf->font("/home/...path.../test/data/xmas/Merriweather.tt +f"); my $page = $pdf->open_page(1); my $text = $page->text; $text->font($font, 36); $text->position(656, 403); $text->text('XXX'); print "Content-type: application/pdf\n\n"; print $pdf->to_string;

    I can only assume that I hardcoded one environment variable but not the other but that seems pretty strange. I don't know what I did or didn't do to keep the problem whilst I thought I had got rid of $ENV{'DOCUMENT_ROOT'}.

    Now I need to go back to the environment variables and untaint them with some sense checking.

    Sorry for a bit of a wild goose chase
    I'll go back to watching fireworks...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2023-06-09 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (35 votes). Check out past polls.

    Notices?