定制 baddiservices/safehtml 二次开发

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

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

baddiservices/safehtml

Composer 安装命令:

composer require baddiservices/safehtml

包简介

Safe HTML package help to prevent XSS vulnerability via HTML content

README 文档

README

Licence PHP Version Open issues Stars Downloads Twitter Follow

Safe HTML package help to prevent XSS vulnerability via HTML content.

Installation

Use Composer to install the package:

$ composer require baddiservices/safehtml

Examples

Validate the input is HTML or not

...

use BADDIServices\SafeHTML\SafeHTML;

class DemoController extends Controller
{
    /** @var SafeHTML **/
    private $safeHTML;

    public function __construct(SafeHTML $safeHTML)
    {
        $this->safeHTML = $safeHTML;
    }

    public function IndexAction(Request $request)
    {
        $htmlContent = $request->input("content");
        if ($this->validate($htmlContent)) {
            // TODO: is valid HTML continue the process
        }
    }
}

Prevent XSS from HTML

...

$sanitizedHTML = $safeHTML->sanitizeHTML($content);

Prevent XSS from text

...

$sanitizedText = $safeHTML->sanitize($text);

Prevent XSS from link

...

$sanitizedURL = $safeHTML->sanitizeURL($url);

Available methods

Method Description
validate($value) Verify text is HTML
sanitize($value) Sanitize text to prevent HTML tags
sanitizeAll($values) Sanitize array of texts to prevent HTML tags
sanitizeHTML($value) Sanitize HTML to prevent XSS vulnerability
encodeEntities($value) Encode special characters to HTML entities
decodeEntities($value) Decode HTML entities to their corresponding characters
setBlackListPath($blackListPath) Set a custom path of the blacklist json file
getEncoding() Get characters encoding
setEncoding($encodage) Set characters encoding

Blacklist file example

You can check the blacklist used by default

{
    "tags": {
        "not-allowed": [],
        "not-allowed-empty": []
    },
    "attributes": {
        "not-allowed": []
    }
}

Exceptions

Name Code Description
BlackListNotLoadedException 11 Failed to load blacklist file

Contribute

Contributions to the package are always welcome!

  • Report any bugs or issues you find.
  • Clone the code source and submit your pull request.

统计信息

  • 总下载量: 132
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固