Dear perlmonks---this is an indirect followup question to my need to write a safe mini interpreter. I want to figure out how module Safe works. the problem is that I want web user A to enter questions that web user B should answer. I need to avoid web user A doing mischief. This is easier to explain with an example:
#!/usr/bin/perl -w use strict; use Safe; my $secret="my-password-should-never-be-shown\n"; ## the following are lines that a web user could try to pass to my pro +gram my $_ = <<HEREDOC; sub round { return sprintf("%.\$_[1]f", \$_[0]); } ; my \$in=rand(); m +y \$answer=(\$in+log(10)); print "what is \$in+log(10)?\\n"; round(\$ +answer,3) ## this should evaluate just fine print \$secret; ## yikes, sho +uld be an error system("ls") ## yikes, sho +uld be an error `ls` ## yikes, sho +uld be an error open(FIN, ">", "really-bad-to-write-to-fs") ## yikes, sho +uld be an error HEREDOC foreach (split(/\n/, $_)) { print "\n----------------\nExecuting '$_'\n\n"; # this executes everything blindly and is a really bad idea my $result=eval($_); print "UNSAFE '$_'\n\t\t-> ".($result||"undef")."\n\n\n"; # this does not do what I had hoped it to do # I want the first line be executed, and all other lines to be trapp +ed. my $compartment= new Safe; $result= $compartment->reval("$_"); print "SAFE '$_'\n\t\t-> ".($result||"undef")."\n\n\n"; }

Unfortunately, my reval does not execute anything. is there a set of ops that are what I should open up to allow exactly and only what I used in the first line?

Advice appreciated.


In reply to use of Safe module by iaw4

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.