Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Is it safe to use external strings for regexes? (use Safe)

by LanX (Saint)
on Oct 06, 2021 at 16:21 UTC ( [id://11137268]=note: print w/replies, xml ) Need Help??


in reply to Re: Is it safe to use external strings for regexes?
in thread Is it safe to use external strings for regexes?

FWIW: there is the Safe module to disallow certain Op-codes inside a (r)eval.

use Safe; $compartment = new Safe; $compartment->permit(qw(time sort :browse)); $result = $compartment->reval($unsafe_code);

Unfortunately I couldn't find a way to disable compiletime blocks like BEGIN and there doesn't seem to be another way to disable or override BEGIN...

I'd love to be corrected.

UPDATE

oh Keyword::Simple could do the trick :)

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

Replies are listed 'Best First'.
Re^3: Is it safe to use external strings for regexes? (Keyword::Simple)
by LanX (Saint) on Oct 06, 2021 at 17:06 UTC
    It's indeed possible to bend the parser in a way that it thinks BEGIN and family are subs

    use strict; use warnings; use Keyword::Simple; sub no_begin ($&){ warn "no_begin(@_) called"; } my @code; BEGIN{ my @compile_blocks = qw(BEGIN UNITCHECK CHECK INIT END); for my $block (@compile_blocks) { # bend parser Keyword::Simple::define $block, sub { my ($ref) = @_; substr($$ref, 0, 0) = "no_begin '$block', sub"; }; # test code push @code , <<__CODE__; $block { die "owened by $block" } __CODE__ } } BEGIN { die "owened by BEGIN" }; UNITCHECK { die "owened by UNITCHECK" }; CHECK { die "owened by CHECK" }; INIT { die "owened by INIT" }; END { die "owened by END" }; eval join "\n", @code;

    -*- mode: compilation; default-directory: "d:/tmp/pm/" -*- Compilation started at Wed Oct 6 19:04:01 C:/Strawberry/perl/bin\perl.exe -w d:/tmp/pm/KW_simple_regex_BEGIN.pl no_begin(BEGIN CODE(0x694268)) called at d:/tmp/pm/KW_simple_regex_BEG +IN.pl line 6. no_begin(UNITCHECK CODE(0x6556d0)) called at d:/tmp/pm/KW_simple_regex +_BEGIN.pl line 6. no_begin(CHECK CODE(0x6942b0)) called at d:/tmp/pm/KW_simple_regex_BEG +IN.pl line 6. no_begin(INIT CODE(0x6c8aa0)) called at d:/tmp/pm/KW_simple_regex_BEGI +N.pl line 6. no_begin(END CODE(0x6c8c38)) called at d:/tmp/pm/KW_simple_regex_BEGIN +.pl line 6. Bareword found where operator expected at (eval 5) line 5, near "} CHECK" (Missing operator before CHECK?)

    But unfortunately does evaling the code not catch parsing errors anymore... (reason here BEGIN{} blocks don't need a trailing semicolon)

    so the answer is:

    • Yes BEGIN* blocks can be disabled.
    • But this is best done in an extra process

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found