Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Insecure Dependency in Taint Mode

by kcott (Archbishop)
on Nov 05, 2022 at 01:15 UTC ( [id://11147976]=note: print w/replies, xml ) Need Help??


in reply to Insecure Dependency in Taint Mode

G'day Bod,

"Is it possible to use PDF::API2 in taint mode ..."

I'd say the short answer is "yes".

Obviously, I can't reproduce your environment — apart from anything else, there's insufficient information, such as your Apache? configuration — however, the following test works fine.

ken@titan ~/tmp/www $ perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -T -E ' use FindBin qw{$RealBin}; my $safepath; BEGIN { if ($RealBin =~ m!^(/home/.+?/(tmp|xyz)/www)!) { $safepath = "$1/../lib"; } else { die "Insecure access!"; } } use lib "$safepath"; use PDF::API2; my $pdf = PDF::API2->new("some.pdf"); $pdf->save("some.pdf"); '

Changing the last two (new() & save()) lines to:

my $pdf = PDF::API2->open("some.pdf");

also works without any problems. I checked:

ken@titan ~/tmp/www $ file some.pdf some.pdf: PDF document, version 1.4, 0 pages

I'm running Perl v5.36.0 and PDF::API2 v2.043.

You've included completely unknown code with "use cPanelUserConfig;". Clearly, we're unable to test that. Try removing all of the code related to PDF::API2 and see if cPanelUserConfig is the cause of your taint issue. Your report suggests that you think PDF::API2 is the source of the problem, but you don't say why; you could be working from a false premise.

I'd move strict and warnings to the start of your code. Was there a reason for putting these in the middle of the script?

It's been 10-20 years since I last wrote any CGI code; my knowledge is certainly not current. I seem to recall that its documentation included troubleshooting tips involving running a CGI script from the command line. Perhaps look into that and see if it's any help.

Have a look at "FindBin: KNOWN ISSUES" and what it says about issues with mod_perl. I'm very much guessing with this — I don't even know if you're using mod_perl — but it may be worth a look.

Take a look through "perlsec - Perl security" for anything that might help. Perhaps the suggested:

delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; # Make %ENV safer

If none of those suggestions help, you'll have to step through your code to find out where problems are occurring.

— Ken

Replies are listed 'Best First'.
Re^2: Insecure Dependency in Taint Mode
by Bod (Parson) on Nov 05, 2022 at 11:45 UTC
    I'd say the short answer is "yes"

    That's encouraging - thanks kcott

    You've included completely unknown code with "use cPanelUserConfig;"

    cPanelUserConfig is a cPanel module. It performs some magic with @INC so that user installed modules can be found by Perl. PDF::API2 is a module I have installed so without cPanelUserConfig we won't get very far. Pretty much every script I write server side has this so I can be pretty sure this is not the cause of the issue.

    I'd move strict and warnings to the start of your code. Was there a reason for putting these in the middle of the script?

    As both strict and warnings operate for all the block regardless of where they are declared, I tend to use them to separate the head code from the actual mechanics of code. I'm sure there are proper terms that I don't know but by "head code" I mean the part of the code that sets thing up correctly such as the shebang, setting paths and bringing in modules to use. Everything below is what actually does the work of the script.

    Your report suggests that you think PDF::API2 is the source of the problem, but you don't say why

    Sorry - I should have explained that identical head code exists in all the other scripts on the site and they all run with taint mode on. It's only since I tried using PDF::API2 that I have had this problem and the error reports it as being in File which is used by PDF::API2 and not called by my script anywhere else.

    I'm out of the office currently but will look at the other things you suggest a little later and try removing/substituting code until I find the exact cause of the issue.

      "cPanelUserConfig ..."

      I don't really want to harp on cPanelUserConfig. Here's a few quick notes:

      • I wasn't suggesting cPanelUserConfig was a problem; it was an unknown, so therefore a possible culprit.
      • I followed your "cPanel" link. It seems that I'd need to create an account to learn more about cPanelUserConfig (or search the web for an unofficial copy). I wasn't prepared to do either, so it remains an "unknown" for me.
      • cPanelUserConfig may be a handy shortcut, which is fine; however, your use of the lib pragma shows that you already know how to manipulate @INC, so it's not quite as absolutely essential as you may think.
      • I'd aim to move from "... pretty sure this is not the cause ..." to "... 100% certain this is not the cause ...".
      "As both strict and warnings operate for all the block regardless of where they are declared, ..."

      No, that's incorrect. Here's a handful of examples that I threw together to demonstrate:

      $ perl -e 'BEGIN { $x = 1 } use strict;' $ perl -e 'use strict; BEGIN { $x = 1 }' Global symbol "$x" requires explicit package name (did you forget to d +eclare "my $x"?) at -e line 1. BEGIN not safe after errors--compilation aborted at -e line 1. $ perl -e 'local $x; use strict;' $ perl -e 'use strict; local $x;' Global symbol "$x" requires explicit package name (did you forget to d +eclare "my $x"?) at -e line 1. Execution of -e aborted due to compilation errors. $ perl -e 'use strict; local $x; no strict "vars";' Global symbol "$x" requires explicit package name (did you forget to d +eclare "my $x"?) at -e line 1. BEGIN not safe after errors--compilation aborted at -e line 1. $ perl -e 'use strict; no strict "vars"; local $x;' $
      "... the error reports it as being in File ..."

      Actually, the error reports ".../IO/File.pm" which is the core module IO::File. I seem to recall that you were stuck with a fairly early version of Perl by your ISP; although, I do think IO::File would have been available in whatever early version that was:

      $ corelist IO::File Data for 2022-05-27 IO::File was first released with perl 5.00307
      "... which is used by PDF::API2 and not called by my script anywhere else."

      You don't necessarily need to have a "use IO::File;" statement. Consider these two:

      $ perl -E 'print("Hello, world!\n"); say $INC{"IO/File.pm"};' Hello, world! $ perl -E 'STDOUT->print("Hello, world!\n"); say $INC{"IO/File.pm"};' Hello, world! /home/ken/perl5/perlbrew/perls/perl-5.36.0/lib/5.36.0/cygwin-thread-mu +lti/IO/File.pm
      "... will look at the other things you suggest a little later ..."

      You might also like to try Carp::Always to get verbose backtraces. It can produce a lot of output, but could be a quick way to track down the source of the "Insecure dependency ..." message.

      Good luck with your troubleshooting. :-)

      — Ken

        The cPanelUserConfig module's sole purpose is to add the user-specific folders for a cPanel-based shared hosting site. On my cPanel-based shared hosting provider, this @INC populating can be accomplisehd via use cPanelUserConfig; or through a shebang of #!/usr/bin/perlml -- but it may be that other hosts are set up differently.

        I was going to paste the contents of my provider's cPanelUserConfig.pm , since it's only a couple lines of code, but the copyright notice says I'd have to check the cPanel license terms to copy, which I didn't feel like doing. It's essentially a for-loop that adds predefined user-specific folders to @INC through unshift @INC, ... commands. This allows a perl script run on the shared hosting server to see the Perl modules that a given user has added through the cPanel interface that their host provides. Without this, users could not use the extra modules they have installed without manually inserting the various paths to user-installed scripts via @INC manipulation or multiple use lib ... statements. (Doing it manually could be fragile, because occasionally a shared hosting provider will change the path that leads up to where a user's home-directory, which could cause unexpected crashes in your scripts. Using the cPanelUserConfig or equivalent should make it more robust.)

        So to the cPanel-based shared-hosting customer: You must include a line similar to the above to use the modules you install through cPanel -- but check your host's documentation, because they will be the canonical source for their specific implementation of cPanel.

        And to the person replying to a question that includes use cPanelUserConfig; : This is just manipulating the @INC so that the script can see customer-installed scripts on a shared-hosting provider; it is not where you should be looking for bugs, and the questioner cannot just "remove" it, because otherwise the SSCCE they tried to provide won't work on their shared host.

        (I decided to make this tangential post so that it can be used in the future, since it seems to come up once a year or so since I became a monk, and either the person asking the question or the person answering doesn't know what it does. This can be accessed in the future using [cPanelUserConfig Reference] )

        My previous posts describing the purpose of cPanelUserConfig :

      • Re^5: Error Message - PL_perl_destruct_level at /usr/lib64/perl5/DynaLoader.pm
      • Re: GD and LWP giving 500 errors
      • Re: When modules install in perl5
      • Re: Apache/CGI fcould not use Spreadsheet::ParseExcel
      • Re^2: Using eval: $@ isn't returning the error I expect

      "As both strict and warnings operate for all the block regardless of where they are declared, I tend to use them to separate the head code from the actual mechanics of code."

      This is not true, and you can verify it using:

      $x = 1; use strict; $y = 2;

      Perl will complain about the use of symbol $y, but not $x because $x was used before strictures.

      Both strict and warnings use lexical scope, which means their effect starts where they're imported, and continues until the end of the block they were imported into (that is, the closing curly brace "}") or if they weren't imported into a block, until the end of the file.

      ... both strict and warnings operate for all the block regardless of where they are declared ...

      My understanding is that both strict and warnings are lexical.

      c:\@Work\Perl\monks>perl $x = 42; print "$x \n"; print ">$y< \n"; use strict; use warnings; ^Z 42 ><


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found