laravelgems/blade-escape

自定义 blade 指令以对抗 XSS

1.0.0 2016-12-25 13:34 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:14:42 UTC


README

Blade Escape 是一个服务提供者,它扩展了 Blade 指令,并允许使用 Laragems\Escape 库。

<div style="background-color: @css($color);">
    <label>@text($label)</label>
    <input type="text" name="custom" value="@attr($value)"/>
</div>
<a href="/profile?u=@param($username)">Profile</a>
<button onclick="callMyFunction('@js($username)');">Validate</button>
<script>
    var username = "@js($username)";
</script>

安装

composer require laravelgems/blade-escape

之后,将服务提供者添加到 config\app.php

        /*
         * Package Service Providers...
         */
         ...
         LaravelGems\BladeEscape\Providers\BladeEscapeServiceProvider::class,
         ...

HTML - @text($variable), 安全

<p>@text($resume)</p>
<div>@text($bio)</div>

HTML 属性 - @attr(@variable), 当符合以下规则时安全

属性值应该有引号。用于白名单属性:align, alink, alt, bgcolor, border, cellpadding, cellspacing, class, color, cols, colspan, coords, dir, face, height, hspace, ismap, lang, marginheight, marginwidth, multiple, nohref, noresize, noshade, nowrap, ref, rel, rev, rows, rowspan, scrolling, shape, span, summary, tabindex, title, usemap, valign, value, vlink, vspace, width

<input type="text" value="@attr($variable)"/>
<img src="image.png" alt="@attr($variable)"/>

URL 参数 - @param($variable), 安全

<a href="search?keyword=@param($variable)">Click Me</a>

JavaScript 参数 - @js($variable), 当符合以下规则时安全

值应该有引号。避免使用危险函数(如 eval 等),例如 - setTimeout("@js($variable)")(可能被破解!)

<script>
    var username = "@js($variable)";
</script>
<a href="#" onclick="displayDialog('@js($title)');">Click</a>

CSS - @css($variable), 当符合以下规则时安全

由引号包围。避免使用复杂的属性如 urlbehavior 和自定义(如 -moz-binding)。不要将不受信任的数据放入 IE 的 expression 属性值中

<style>
    .article { background-color: '@css($color)';}
</style>
<span style="width: '@css($width)';"></span>

必读: QWASP - XSS 防范备忘单

你不喜欢指令的名称。好吧,只需在发布的配置中更改它们即可。