定制 protonemedia/laravel-xss-protection 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

protonemedia/laravel-xss-protection

Composer 安装命令:

composer require protonemedia/laravel-xss-protection

包简介

Laravel XSS protection, middleware and sanitization

README 文档

README

Latest Version on Packagist run-tests Total Downloads

Laravel Middleware to protect your app against Cross-site scripting (XSS). It sanitizes request input by utilising the voku/anti-xss package, and it can sanatize Blade echo statements as well. This package was inspired by the Security Core package.

  • PHP 8.2 and higher
  • Laravel 10 and higher

PHP 8.4+ Compatibility

This package includes a vendored version of voku/portable-utf8 in the vendor-lib directory. The upstream package hasn't released a PHP 8.4 compatible version yet (the fixes are merged but not tagged), so we bundle the fixed version to avoid deprecation warnings about implicitly nullable parameters.

Sponsor Us

❤️ We proudly support the community by developing Laravel packages and giving them away for free. If this package saves you time or if you're relying on it professionally, please consider sponsoring the maintenance and development and check out our latest premium package: Inertia Table. Keeping track of issues and pull requests takes time, but we're happy to help!

Installation

You can install the package via composer:

composer require protonemedia/laravel-xss-protection

You may publish the config file with:

php artisan vendor:publish --tag="xss-protection-config"

Middleware Usage

You may use the ProtoneMedia\LaravelXssProtection\Middleware\XssCleanInput middleware in the route that handles the form submission.

use App\Http\Controllers\CreateAccountController;
use ProtoneMedia\LaravelXssProtection\Middleware\XssCleanInput;

Route::post('account', CreateAccountController::class)->middleware(XssCleanInput::class);

If your app has a lot of forms handled by many different controllers, you could opt to register it as global middleware.

// inside app\Http\Kernel.php

protected $middleware = [
   // ...
   \ProtoneMedia\LaravelXssProtection\Middleware\XssCleanInput::class,
];

If you register the middleware globally, you may exclude requests by using the static skipWhen method. You can add a callback to interact with the request:

XssCleanInput::skipWhen(function (Request $request) {
    return $request->is('admin.*');
});

You can also exclude keys by using the static skipKeyWhen method. This also allows you to interact with the value and request.

XssCleanInput::skipKeyWhen(function (string $key, $value, Request $request) {
    return in_array($key, [
        'current_password',
        'password',
        'password_confirmation',
    ]);
});

Configuration

File uploads

By default, the middleware allows file uploads. However, you may disallow file uploads by changing the middleware.allow_file_uploads configuration key to false.

Blade echo statements

By default, the middleware sanitizes Blade echo statements like {{ $name }}, {{{ $name }}}, and {!! $name !!}. You may allow echo statements by changing the middleware.allow_blade_echoes configuration key to true.

Completely replace malicious input

By default, the middleware transforms malicious input to null. You may configure the middleware to only transform the malicious part by setting the middleware.completely_replace_malicious_input configuration key to false. That way, an input string like hey <script>alert('laravel')</script> will be transformed to hey instead of null.

Terminate request

Instead of transforming malicious input, you may configure the middleware to terminate the request whenever anything malicious has been found. You may do this by setting the middleware.terminate_request_on_malicious_input to true, which will throw an HttpException with status code 403.

Dispatch event

You may configure the middleware to dispatch an event whenever malicious input has been found. Setting the middleware.dispatch_event_on_malicious_input to true will dispatch an ProtoneMedia\LaravelXssProtection\Events\MaliciousInputFound event with the sanitized keys, the original request and the sanitized request.

use Illuminate\Support\Facades\Event;
use ProtoneMedia\LaravelXssProtection\Events\MaliciousInputFound;

Event::listen(function (MaliciousInputFound $event) {
    $event->sanitizedKeys;
    $event->originalRequest;
    $event->sanitizedRequest;
});

Additional configuration for voku/anti-xss

As of version 1.6.0, you may provide additional configuration for the voku/anti-xss package. You may do this by filling the middleware.anti_xss key.

'anti_xss' => [
    'evil' => [
        'attributes' => ['href'],
        'tags' => ['video'],
    ],

    'replacement' => '*redacted*',
]

Changelog

Please see CHANGELOG for more information about what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Other Laravel packages

  • Inertia Modal: With Inertia Modal, you can easily open any route in a Modal or Slideover without having to change anything about your existing routes or controllers.
  • Inertia Table: The Ultimate Table for Inertia.js with built-in Query Builder.
  • Laravel Blade On Demand: Laravel package to compile Blade templates in memory.
  • Laravel Cross Eloquent Search: Laravel package to search through multiple Eloquent models.
  • Laravel Eloquent Scope as Select: Stop duplicating your Eloquent query scopes and constraints in PHP. This package lets you re-use your query scopes and constraints by adding them as a subquery.
  • Laravel FFMpeg: This package provides an integration with FFmpeg for Laravel. The storage of the files is handled by Laravel's Filesystem.
  • Laravel MinIO Testing Tools: Run your tests against a MinIO S3 server.
  • Laravel Mixins: A collection of Laravel goodies.
  • Laravel Paddle: Paddle.com API integration for Laravel with support for webhooks/events.
  • Laravel Task Runner: Write Shell scripts like Blade Components and run them locally or on a remote server.
  • Laravel Verify New Email: This package adds support for verifying new email addresses: when a user updates its email address, it won't replace the old one until the new one is verified.

Security

If you discover any security-related issues, please email code@protone.media instead of using the issue tracker. Please do not email any questions, open an issue if you have a question.

Credits

License

The MIT License (MIT). Please see License File for more information.

Treeware

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

protonemedia/laravel-xss-protection 适用场景与选型建议

protonemedia/laravel-xss-protection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 666.69k 次下载、GitHub Stars 达 149, 最近一次更新时间为 2022 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「protonemedia」 「laravel-xss-protection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 protonemedia/laravel-xss-protection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 protonemedia/laravel-xss-protection 我们能提供哪些服务?
定制开发 / 二次开发

基于 protonemedia/laravel-xss-protection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 666.69k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 149
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 149
  • Watchers: 6
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-02