2011-10-31 108 views
1

我有基於來自URL查詢字符串返回結果一個簡單的搜索腳本:request_param在Smarty的模板逃逸

$filter_query = request_param('query'); 
if ($filter_query) { 
    $topic_filters['query'] = $filter_query; 
    $smarty->assign('query', $filter_query); 
} 

但是目前這種暴露在XSS和濫用其未消毒的輸入'查詢'。

即時通訊使用Smarty模板,是否有任何內置功能自動執行此操作?

回答

1

在您的Smarty模板中,使用escape modifier來避免輸出對XSS攻擊。默認情況下,它會跳出& " ' < >。如果您需要編碼其他實體,請使用參數:htmlall修改escape。 (see the documentation

{* Inside your template... *} 
This is the value of {$query|escape} 

否則,您可以用htmlspecialchars()

// Or beforehand in PHP, which protects you from forgetting to do it in your template 
// if you use the same variable in many locations. 
$smarty->assign('query', htmlspecialchars($filter_query)); 
0

分配給Smarty之前逃吧我有一個類似的問題:代碼

onmouseover="action({$keyid},'{$label}');" 

引起JavaScript錯誤時,$標籤包含引號。我解決了使用Smarty命令轉義:'引號'。請注意,在這種情況下,單獨逃脫並不會解決問題,因爲它不會替換'with',而是替換爲'&#039;

所以我修改了代碼:

onmouseover="action({$keyid},'{$label|escape:'quotes'}');" 

和它的作品! 希望對某人有用......