Laravel Blade Form::select -- 现在强制逃跑?

Laravel Blade Form::select -- now forcing escape?

我从 L5.0 开始就有这个代码 运行。随着对 L5.3.30 + 依赖项的最新更新,它似乎被破坏了。也许我从一开始就做错了什么?

这里是简化代码:

    {!! Form::select('currency', ['USD'=>'USD: *escape code here*'], 
        null, ['class'=>'form-control', "required", 'id'=>'currency']) !!}

在过去的几年中,此代码返回了一个 select 框,其中包含如下文本:"USD: $"

composer 更新到 L5.3.30 后,在所有服务器(测试、开发、生产)上,它现在 returns html 符号改为:"USD: escape code here"

我已经暂时(并成功地)修补了这个问题:

 <select name = 'currency' id="currency" required class="form-control">
       @foreach (\Helper::currency() as $k=>$v)
                  <option  value="{{$k}}">{!! $v !!}</option>
       @endforeach
 </select>

以上代码在 $v var 中包含货币的转义码,并正确显示在 select 框中。

请帮忙 - 这会破坏我应用程序中的很多表单。

谢谢。

编辑:我仍然可以使用 {!! !!} 其他地方。它似乎只影响 Form::select() 项。因此我开始认为这不是 Laravel 的 blade escape 的问题,而是 Laravel Collective Form 函数

的最新版本的问题

解决方案:我向 Laravel Collective Dev 团队注意到了这一点,但这显然没有被回滚。请参阅 https://github.com/LaravelCollective/html/issues/296 了解最新信息。

改为使用

{!! your code !!}

尝试使用

{{ your code }}

{!! !!}用于显示未转义的数据。来自 doc

Displaying Unescaped Data

By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

Hello, {!! $name !!}.

Be very careful when echoing content that is supplied by users of your application. Always use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.

是的,这是 Laravel 5.0 的新语法。只是切换到 {!! !!} 足以使用表单元素进行修复。

https://laravel.com/docs/5.0/upgrade

Blade 标签更改

For better security by default, Laravel 5.0 escapes all output from both the {{ }} and {{{ }}} Blade directives. A new {!! !!} directive has been introduced to display raw, unescaped output. The most secure option when upgrading your application is to only use the new {!! !!} directive when you are certain that it is safe to display raw output.

However, if you must use the old Blade syntax, add the following lines at the bottom of AppServiceProvider@register:

\Blade::setRawTags('{{', '}}');
\Blade::setContentTags('{{{', '}}}');
\Blade::setEscapedContentTags('{{{', '}}}');

This should not be done lightly, and may make your application more vulnerable > to XSS exploits. Also, comments with {{-- will no longer work.

你可以 "fix" 通过将 Laravelcollective html 包降级到版本 5.3.0(从当前版本 5.3.1 降级)。只需编辑 composer.json "require"

"laravelcollective/html": "5.3.*",

有了这个:

"laravelcollective/html": "5.3.0",

缺点是您将使用旧版本,它可能有一些其他问题已在 5.3.1 中修复,但我没有任何具体信息。

关注这个问题 https://github.com/LaravelCollective/html/issues/296 and this commit https://github.com/LaravelCollective/html/pull/297/files?diff=split ,

我更改了第 683 行,("$this->html->escapeAll()" 到 "e()" )

之前

return $this->toHtmlString('<optgroup label="' . $this->html->escapeAll($label) . '">' . implode('', $html) . '</optgroup>');

之后
return $this->toHtmlString('<optgroup label="' . e($label) . '">' . implode('', $html) . '</optgroup>');

在最新版本发布之前,它对我有用。