蒸馏厂/安全

安全中间件和助手

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。