baddiservices/safehtml
Composer 安装命令:
composer require baddiservices/safehtml
包简介
Safe HTML package help to prevent XSS vulnerability via HTML content
README 文档
README
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
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-20