蒸馏厂/安全

安全中间件和助手

v11.0.0 2024-05-28 09:41 UTC

README

Scrutinizer Code Quality Code Coverage Build Status Total Downloads Latest Stable Version License

安全

包用于清洗中间件或独立使用以清洗字符串。

目录

  1. 需求
  2. 安装

需求

  1. PHP 7.1.3 或更高版本

安装

在您的 composer.json 中添加

    "require": {
        "distilleries/security": "1.*",
    }

运行 composer update.

发布配置

php artisan vendor:publish --provider="Distilleries\Security\SecurityServiceProvider"

配置

    return [
       'xss_enable'=> env('SECURITY_XSS_ENABLE',true),
       'html_purifier'=> env('SECURITY_HTML_PURIFIER_ENABLE',true)
    ];

在 kernel 文件中添加中间件。

    protected $middleware = [
        \Distilleries\Security\Http\Middleware\XSS::class
    ];

独立使用

您可以直接使用 Security 类来清洗数据

清洗字符串

    $xss = new \Distilleries\Security\Helpers\Security();
    $xss->xss_clean('<a href="javascript:aler('test')">Click to alert</a>');

应返回 Click to alert

实体解码

这个函数是 html_entity_decode() 的替代品

我们不直接使用 `html_entity_decode()` 的原因是,尽管省略实体结尾的分号在技术上是不正确的,但大多数浏览器仍然会正确解释该实体。html_entity_decode() 不会转换没有分号的实体,因此我们在这里找到了自己的小解决方案。太糟糕了。

    $xss = new \Distilleries\Security\Helpers\Security();
    $xss->entity_decode(&lt;a href=&quot;javascript:alert('test')&quot;&gt;Test&lt;/a&gt;');

应返回 Test

清洗文件路径

    $xss = new \Distilleries\Security\Helpers\Security();
    $xss->sanitize_filename('./../test.jgp',true);

应显示 ./test.jpg 而不是 ./../test.jgp。最后一个参数用于允许或禁止相对路径

    $xss = new \Distilleries\Security\Helpers\Security();
    $xss->sanitize_filename('./../test.jgp',false);

应显示 test.jpg 而不是 ./../test.jgp。