richard5mith has asked for the wisdom of the Perl Monks concerning the following question:
I'm writing a little language in Perl (http://www.bearscript.com/docs/) and am adding support for doing regular expressions within the code.
The one I'm concerned about is the replace function, which is the equivelant of s///. This is because I want the user to be able to do backreferences, which as far as I can tell, based on another Perlmonks seeker of wisdom, is only possible using the /ee modifier.
This is essentially what I've got at the moment. This very simply converts a UK date to an ISO date.
$date = "10-09-2004"; print &replace($date, q"(\d+)-(\d+)-(\d+)", q"$3-$2-$1"), "\n"; sub replace { my $value = shift; my $this = shift; my $with = shift; my $modifiers = shift; $this = &pattern($this, $modifiers); if ($modifiers =~ /g/) { $value =~ s/$this/qq{qq{$with}}/gee; } else { $value =~ s/$this/qq{qq{$with}}/ee; } return $value; } sub pattern { my $pattern = shift; my $modifiers = shift; if ($modifiers =~ /[^gism]/) { die "Only m, i, s and g are valid pattern modifiers."; } $modifiers =~ s/g//; return qr"(?$modifiers)$pattern"; }
Now bare in mind that the first two lines there are essentially generated by my interpreter (well, it generates code that is equivelant to that), so it's always a q"" construct when passing the parameters to the functions (which are hand-written and not generated by the interpreter). So nothing is interpolated before it's passed through to the function.
My question is one of security. Since I'm essentially eval'ing the value of $with, what could they possibly include within the third parameter of the call to &replace that could do something nasty? I'm aware the user could peek at the Perl variables they normally don't see, but that's not an issue. And when I've tried to include calls to functions and other things within that parameter, I've not managed to get any unwanted side-effects. But maybe I'm just not creative enough. Am I missing something here, is this safe?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Security with /ee modifier
by PodMaster (Abbot) on Sep 26, 2004 at 15:17 UTC | |
by richard5mith (Beadle) on Sep 26, 2004 at 15:32 UTC | |
by merlyn (Sage) on Sep 26, 2004 at 15:48 UTC | |
by PodMaster (Abbot) on Sep 26, 2004 at 15:55 UTC | |
by richard5mith (Beadle) on Sep 26, 2004 at 16:00 UTC | |
|
Re: Security with /ee modifier
by Zaxo (Archbishop) on Sep 26, 2004 at 15:02 UTC | |
by richard5mith (Beadle) on Sep 26, 2004 at 15:15 UTC | |
|
Re: Security with /ee modifier
by nobull (Friar) on Sep 26, 2004 at 20:28 UTC | |
by gaal (Parson) on Oct 04, 2004 at 16:28 UTC | |
by bart (Canon) on Oct 05, 2004 at 05:48 UTC | |
by gaal (Parson) on Oct 05, 2004 at 07:43 UTC | |
by ihb (Deacon) on Oct 04, 2004 at 17:13 UTC |