Re: [SMARTY] bug? security hole? undocumented magic in php?
| From: | Greg Stark | Date: | Fri, 25 Apr 2003 20:55:52 +0000 |
| Subject: | Re: [SMARTY] bug? security hole? undocumented magic in php? | ||
| References: | 1 2 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-9382@lists.php.net to get a copy of this message | ||
"CPT John W. Holmes" <holmes072000@charter.net> writes:
> > In the code:
> >
> > $template_source = preg_replace("!({$ldq})\*(.*?)\*({$rdq})!se",
> >
> > "'\\1*'.str_repeat(\"\n\", substr_count('\\2', \"\n\"))
> > .'*\\3'",
> > $template_source);
...
>
> There must be some magic happening. Otherwise something like the following
> would produce an error:
>
> $str = "This %%is ' a%% test";
>
> $newstr = preg_replace('/%%(.*)%%/e',"strtoupper('\\1')",$str);
>
> PHP must apply addslashes() to either the matches or the entire string at
> some point.
Well not addslashes(). That would introduce spurious blackslashes before other
characters like double quotes and nul bytes.
Hm, one minute.
Oh look, it IS calling addslashes. If you repeat the above test with
$str = 'This %%is " a%% test';
$newstr = preg_replace('/%%(.*)%%/e',"strtoupper('\\1')",$str);
you get the output:
This IS \" A test
Well that's just wonderful. So, uh, how does one use preg_replace without
risking introducing extra slashes then?
Hm, perhaps I should be using preg_replace(...,"\\1")
no, that doesn't work, then there are spurious backslashes introduced if there
are single-quotes in the original string.
And, no, I can't call stripslashes because that would remove slashes that
addslashes didn't put there, notably bare \s in the input string.
What a tarpit.
So it turns out the particular example i quoted isn't a problem for Smarty
because it doesn't really matter if there are extra spurious backslashes in
the input string. All it's doing is counting newlines.
It's still a bug that it uses "\\1 \\2 \\3" I think it's supposed to be
"\$1 \$2 \$3".
--
greg