您如何看待laravel 4中的安全性?我的意思是laravel如何管理xss攻击?如何在laravel 4框架中应用xss过滤器?
在codeigniter中,你有一些像xss_clean($ _ GET ['yourValue'])来清理用户输入的xss代码。
laravel如何处理这些问题?您使用Input :: get('yourValue')获取用户值,但是如何对其应用xss过滤器?它具有开箱即用的功能或者什么?
您如何看待laravel 4中的安全性?我的意思是laravel如何管理xss攻击?如何在laravel 4框架中应用xss过滤器?
在codeigniter中,你有一些像xss_clean($ _ GET ['yourValue'])来清理用户输入的xss代码。
laravel如何处理这些问题?您使用Input :: get('yourValue')获取用户值,但是如何对其应用xss过滤器?它具有开箱即用的功能或者什么?
在laravel模板自带形式的用户输入应包含在三个花括号来净化它的任何数据:
<h1>{{{ $input }}}</h1>
有在Laravel没有本地XSS清洁功能,但如果你对一个很绝望有可用的笨安全库的端口位置:
您可以使用App::before
事件来过滤所有的输入这样
App::before(function($request)
{
Input::merge(array_strip_tags(Input::all()));
}
的array_strip_tags
功能如下,我已经把它放在一个辅助文件来直接调用它,你可以使用它作为一个辅助函数或作为一个库,但它很容易使用它作为一个辅助功能,里面global.php
文件app/start/
文件夹内只创建一个帮助文件,并给它一个名称,例如custom_helper.php
,包括像这样
require '/custom_helpers.php';
功能array_strip_tags
function array_strip_tags($array)
{
$result = array();
foreach ($array as $key => $value) {
$key = strip_tags($key);
if (is_array($value)) {
$result[$key] = array_strip_tags($value);
}
else {
$result[$key] = strip_tags($value);
}
}
return $result;
}
这是从我的一个工作项目中复制而来的。
这只是剥离标签,而不是阻止xss –
@SvenB,那么你还期望什么'xss'过滤,'跨站点脚本'基本上注入脚本''标签,我在这种情况下剥离标签。这里有什么问题?如果我错过了某些东西,你能解释一下'预防xss吗? –
我相信不幸的是,Laravel没有内置的XSS过滤器。然而,有一个包可以尝试laravel-xss,它很容易使用,你只需要做一些事情:$user->about = Xss::clean(Input::get('about');
,你就开始了!
也有另一种包装用于laravel XSS滤波器可下载here
使用例:
简单形式的代码段
{{Form::open(['route' => 'posts.store'])}}
{{Form::text('title')}}
{{Form::textarea('body')}}
{{Form::submit('Post')}}
{{Form::close()}}
过滤包的使用
$rules = ['title' => 'required|min:13', 'body' => 'required|min:150'];
$validator = Validator(Input::all(), $rules);
if($validator->passes()){
$xss = new XSS;
$xss->clean(Input::all());
$input = $xss->get();
$post = new Post;
$post->title = $input->title;
$post->body = $input->body;
// to test the results you can dd($input); & happy coding everyone!
}
当然,你会想注入这些依赖关系或建立一个门面,而不要使用“新”! – hackel
这是我如何解决这个问题。启发了@ the-alpha的解决方案。我正在使用Laravel 4.2。
app/start/global.php
:
function array_strip_tags($array)
{
$result = array();
foreach ($array as $key => $value) {
$key = strip_tags($key);
if (is_array($value)) {
$result[$key] = array_strip_tags($value);
}
else {
$result[$key] = strip_tags($value);
}
}
return $result;
}
app/filters.php
:
Route::filter('strip_tags', function()
{
Input::merge(array_strip_tags(Input::all()));
});
app/routes.php
:
Route::group(array('before' => 'strip_tags'), function(){
// all routes under this route group will get all their inputs passed through the strip_tags php's function
Route::any('/', ['as' => 'home', 'uses' => '[email protected]']);
Route::any('/some-page', ['as' => 'some-page', 'uses' => '[email protected]']);
}
创建一个新的辅助文件,并把这两种方法在你的帮手。
public static function globalXssClean()
{
// Recursive cleaning for array [] inputs, not just strings.
$sanitized = static::arrayStripTags(Input::get());
Input::merge($sanitized);
}
public static function arrayStripTags($array)
{
$result = array();
foreach ($array as $key => $value) {
// Don't allow tags on key either, maybe useful for dynamic forms.
$key = strip_tags($key);
// If the value is an array, we will just recurse back into the
// function to keep stripping the tags out of the array,
// otherwise we will set the stripped value.
if (is_array($value)) {
$result[$key] = static::arrayStripTags($value);
} else {
// I am using strip_tags(), you may use htmlentities(),
// also I am doing trim() here, you may remove it, if you wish.
$result[$key] = trim(strip_tags($value));
}
}
return $result;
}
然后把这个代码在你之前,过滤器的开始(在Laravel 4应该是在app/filters.php)。
App::before(function($request)
{
Helper::globalXssClean();
});
我检查了Laravel的保护{{{...}}}
针对xss攻击。它只使用htmlentities()
函数,如下所示:htmlentities('javascript:alert("xss")', ENT_QUOTES, 'UTF-8', false);
只有正确使用它时,才能保护您免受xss的影响,这意味着不要在某些HTML标记中使用它,因为这会导致XSS攻击可能性。例如:
$a = htmlentities('javascript:alert("xss")', ENT_QUOTES, 'UTF-8', false);
echo '<a href="'.$a.'">link</a>';
在这种情况下,您的代码易受xss影响。
你用jquery得到的数据怎么样? (ajax calls) –