【问题标题】:LaravelCollective/html escape inconsistency with double curly bracesLaravelCollective/html 转义不一致与双花括号
【发布时间】:2020-07-26 08:40:18
【问题描述】:

在 Laravel 5.8 上,我可以使用双花括号来转义字符串 {{ "<script>alert('does not work');</script>" }} 或变量 {{ $comment }}

但是,如果我使用 LaravelCollective/html 包创建 html 元素,双花括号不会转义任何内容。 {{ Form::input('edit', 'test') }} 创建一个输入。

LaravelCollective/html 如何实现这一点,或者这是 Blade 语法本身的一个例外? {{ }} 是否是一种安全的方式来逃避任何东西,如果它有时会逃脱,有时不会?我可以创建一个自定义类来欺骗 Laravel,让 Laravel 认为我正在使用 LaravelCollective/html,并使用带有 {{ }} 的自定义类/对象打印未转义的内容吗?

【问题讨论】:

    标签: laravel laravel-blade laravelcollective


    【解决方案1】:

    LaravelCollective/html 似乎通过使用BladeCompiler::directive() 注册一个自定义处理程序来实现这一点,因此它是 Blade 提供的扩展功能。因此,使用 {{ }} 通过 LaravelCollective 转义和创建 html 元素是安全的。

    作为参考,这里有一个来自 LaravelCollective/Html 中 HtmlServiceProvider.php 的 sn-p 说明它是如何完成的。

    /**
     * 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); ?>";
                        });
                    }
                }
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多