laravelgems/escape
Composer 安装命令:
composer require laravelgems/escape
包简介
Basic methods to escape untrusted data before inserting into different HTML contexts
README 文档
README
This library provides several methods that help you prevent XSS attacks on your pages.
These methods escape untrusted data properly. Just follow simple rules and you're safe.
Quick example
<div>
<label><?= \LaravelGems\HTML::text($label) ?></label>
<input type="text" value="<?= \LaravelGems\HTML::attr($value) ?>"/>
<script>
var Identifier = "<?= \LaravelGems\HTML::js($label) ?>";
</script>
</div>
<a href="/my/page?query=<?= \LaravelGems\HTML::param($label) ?>" onclick="callMyFunction(this, '<?= \LaravelGems\HTML::js($label) ?>');">Click Me</a>
Important:
- this library does not do any validation
- this library does not clean invalid/dangerous code
So, please do not expect that this library will protect you from something like this:
<a href="#" onclick="UNTRUSTED DATA HERE">My Link</a> <a href="UNTRUSTED DATA HERE">My Link</a>
Installation
Include HTML.php or install the composer package
composer require laravelgems/escape
HTML text
This methods uses htmlspecialchars with small addition (escaping forward slash too).
<div><?= \LaravelGems\HTML::text($untrustedData) ?></div>
HTML attribute
<input type="text" name="username" value="<?= \LaravelGems\HTML::attr($untrustedData) ?>"/>
Important - this is only safe for whitelist of attributes
Whitelist: 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
Some attributes (for example, ID) is not in a whitelist as it can be used for breaking your frontend logic by processing/watching wrong element.
Many other attributes are potentially dangerous even with properly escaped data.
CSS
<span style="property: '<?= \LaravelGems\HTML::css($untrustedData) ?>;'">text</span>
Notes:
- value should be quoted
- stay away from putting untrusted data into complex properties like url, behavior, and custom (-moz-binding)
- do not put untrusted data into IE’s expression property value which allows JavaScript.
Javascript variable
<script>var username="<?= \LaravelGems\HTML::js($untrustedData) ?>";</script> <a href="#" onclick="myClickHandler('<?= \LaravelGems\HTML::js($untrustedData) ?>')">Link</a>
URL parameter
FYI, this method is an alias to urlencode.
<a href="/profile?username=<?= \LaravelGems\HTML::param($untrustedData) ?>">Profile</a>
Warning! Never ever make something like these without validation/sanitizing
<!-- Unsafe html attributes - there no way to protect you in 100% cases without validation first --> <embed src="<?= htmlentities("javascript:alert(1)") ?>"></embed> <!-- Does not look safe, right? --> <embed src="javascript:alert(1)"></embed>
More examples (wrong vs right)
<!-- WRONG WAY: htmlentities() is not enough in JS context --> <script>var a = "<?= htmlentities($untrustedData) ?>";</script> <!-- RIGHT WAY: use \LaravelGems\HTML::js() --> <script>var a = "<?= \LaravelGems\HTML::js($untrustedData) ?>";</script>
Inspiration
Thanks to QWASP for their top 10 and cheat sheets. Thanks to Twig library for their filters.
laravelgems/escape 适用场景与选型建议
laravelgems/escape 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.08k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 12 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 laravelgems/escape 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laravelgems/escape 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 13.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-12-25