在Laravel5.8中,我可以使用双花括号来转义字符串{{ "<script>alert('does not work');</script>" }}或变量{{ $comment }}
但是,如果我使用LaravelCollective/html包来创建html元素,双花括号不会转义任何东西。{{ Form::input('edit', 'test') }}创建一个输入。
LaravelCollective/html是如何做到这一点的,或者这是Blade语法本身的一个例外?如果任何东西有时被转义,有时不被转义,那么{{ }}是一种安全的转义方式吗?我是否可以创建一个自定义类来欺骗Laravel,使其认为我正在使用LaravelCollective/html,并使用{{ }}使用自定义类/对象打印未转义的内容
发布于 2020-04-14 20:47:00
看起来LaravelCollective/html是通过向BladeCompiler::directive()注册一个自定义处理程序来实现的,所以它是Blade提供的一个扩展特性。因此,使用LaravelCollective对html元素进行转义和创建都可以安全地使用{{ }}。
作为参考,这里有一段来自LaravelCollective/Html的HtmlServiceProvider.php代码片段,介绍了它是如何实现的。
/**
* Register Blade directives.
*
* @return void
*/
protected function registerBladeDirectives()
{
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
$namespaces = [
'Html' => get_class_methods(HtmlBuilder::class),
'Form' => get_class_methods(FormBuilder::class),
];
foreach ($namespaces as $namespace => $methods) {
foreach ($methods as $method) {
if (in_array($method, $this->directives)) {
$snakeMethod = Str::snake($method);
$directive = strtolower($namespace).'_'.$snakeMethod;
$bladeCompiler->directive($directive, function ($expression) use ($namespace, $method) {
return "<?php echo $namespace::$method($expression); ?>";
});
}
}
}
});
}https://stackoverflow.com/questions/61193806
复制相似问题