Re: [SMARTY] prefiltering variables

From: Date: Sat, 15 Jun 2002 02:20:21 +0000
Subject: Re: [SMARTY] prefiltering variables
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-4874@lists.php.net to get a copy of this message
Since I was using a wrapper around the template engine anyway (in case someone wanted to switch), I wrote this function for the wrapper:
    #.................................................. htmlEscape
    #
    # Convert characters such as "<" to their corresponding
    # HTML escape sequences.
    #
    function htmlEscape( &$item )
    {
        if( is_array( $item ) )
        {
            foreach( $item as $key => $value )
            {
                $item[$key] = $this->htmlEscape( $value );
            }
            return $item;
        }
        else
        {
            return htmlspecialchars( $item );
        }
    }
Doesn't handle objects, but that's not a problem in my case. cheers, Travis travis@dawnstar.org wrote:
Thank you, that is very helpful and solves my immediate problem. HOWEVER, this clutters up templates and is still bug-prone. Practically every single user-entered variable will need this modifier. If I forget even one "escape", the user could fall prey to a cross-site scripting exploit. On the other hand, if I could do something like: $smarty->auto_html_escape = true; $smarty->display( "htmltemplate.tpl" ); $smarty->auto_html_escape = false; $smarty->display( "csvtemplate.tpl" ); It would be far easier to produce a safe application. Make sense? cheers, Travis On Fri, 14 Jun 2002, Monte Ohrt wrote:
{$description|escape} That should do it. travis@dawnstar.org wrote:
Is there some kind of "variable prefilter" option to cause Smarty to filter/transform the values assigned in the template? For example, I'd like to render some variable called $description, so in my PHP I do this: $smarty->assign( "description", $description ); Then, in the template I do this: {$description} But suppose $description contains the string: <script>alert('This is an evil alert');</script> I'd like Smarty to know that it should output: &lt;script&gt;alert('This is an evil alert');&lt;/script&gt; I know I can filter such user-entered data at the input stage, but I don't like this at all for two reasons: (1) It's a lot of extra code, and (2) there's no reason to assume anything about how the information is to be extracted from the database. Maybe I'll be pulling it into an Excel spreadsheet, in which case I don't want to see all those &gt;'s. I also know I can filter this when I assign the variable from PHP, but that's even worse, because I may have to loop through arrays, and arrays of arrays, and so on. Very messy and bug prone. I can't use a prefilter, because the data isn't in the template yet. I can't use a postfilter because at that point there is no way to differentiate between the template and the data. So I'd really like Smarty to do it. It seems the cleanest approach. Is there a way to do this already, or can this be added? cheers, Travis
-- Travis Low <mailto:travis@dawnstar.org> <http://dawnstar.org/travis>

« previous php.smarty.general (#4874) next »