Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
here a way you could go, to counter the problems listed and explained here

  1. code injection by string interpolation, like /@{[ do_evil() ]}/
  2. code injection by regex, like /(?{ do_evil() })/
  3. exponential time regexes with excessive backtracking, something like /((x*)*)*/ IIRC

This will compile a regex into an anonymous sub without executing it

use re qw(debug); my $sub = eval "sub { m/$evil_re/ }";

the re debug will emit regex-opcodes for the regexes involved to STDERR

Final program: 1: EVAL (4) 4: EXACT <\n> (6) 6: END (0)

the 1: EVAL here tells you that an EVAL was involved which you need to reject, you don't want embedded Perl code

$evil_re = "(?{ BEGIN { do_evil() } })";

with Keyword::Simple disabling BEGIN,END,... etc you won't risk that the compilation of the sub inside the eval will run any code (see here)

with Safe you'll be able to additionally disable a bunch of external commands. (see here)

For this to work you need to spawn an external command for each regex and capture STDERR, you can use this to also limit the maximal runtime.

Since your code looks a lot like a test suite, you might wanna use the TAP protocol anyway.

NB: No guaranties whatsoever!

HTH! :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


In reply to Re: Is it safe to use external strings for regexes? by LanX
in thread Is it safe to use external strings for regexes? by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-19 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found