typo3/html-sanitizer
Composer 安装命令:
composer require typo3/html-sanitizer
包简介
HTML sanitizer aiming to provide XSS-safe markup based on explicitly allowed tags, attributes and values.
README 文档
README
TYPO3 HTML Sanitizer
ℹ️ Common safe HTML tags & attributes as given in
\TYPO3\HtmlSanitizer\Builder\CommonBuilderstill might be adjusted, extended or rearranged to more specific builders.
In a Nutshell
This typo3/html-sanitizer package aims to be a standalone component that can be used by any PHP-based
project or library. Albeit it is released within the TYPO3 namespace, it is agnostic to specifics of
TYPO3 CMS.
\TYPO3\HtmlSanitizer\Behaviorcontains declarative settings for a particular process for sanitizing HTML.\TYPO3\HtmlSanitizer\Visitor\VisitorInterface(multiple different visitors can exist at the same time) are actually doing the work based on the declaredBehavior. Visitors can modify nodes or mark them for deletion.\TYPO3\HtmlSanitizer\Sanitizercan be considered as the working instance, invoking visitors, parsing and serializing HTML. In general this instance does not contain much logic on how to handle particular nodes, attributes or values\TYPO3\HtmlSanitizer\Builder\BuilderInterfacecan be used to create multiple different builder instances - in terms of "presets" - which combine declaring a particularBehavior, initialization ofVisitorInterfaceinstances, and finally returning a ready-to-useSanitizerinstance
Installation
composer req typo3/html-sanitizer
Example & API
<?php use TYPO3\HtmlSanitizer\Behavior; use TYPO3\HtmlSanitizer\Behavior\NodeInterface; use TYPO3\HtmlSanitizer\Sanitizer; use TYPO3\HtmlSanitizer\Visitor\CommonVisitor; require_once 'vendor/autoload.php'; $commonAttrs = [ new Behavior\Attr('id'), new Behavior\Attr('class'), new Behavior\Attr('data-', Behavior\Attr::NAME_PREFIX), ]; $hrefAttr = (new Behavior\Attr('href')) ->addValues(new Behavior\RegExpAttrValue('#^https?://#')); // attention: only `Behavior` implementation uses immutability // (invoking `withFlags()` or `withTags()` returns new instance) $behavior = (new Behavior()) ->withFlags(Behavior::ENCODE_INVALID_TAG | Behavior::ENCODE_INVALID_COMMENT) ->withoutNodes(new Behavior\Comment()) ->withNodes(new Behavior\CdataSection()) ->withTags( (new Behavior\Tag('div', Behavior\Tag::ALLOW_CHILDREN)) ->addAttrs(...$commonAttrs), (new Behavior\Tag('a', Behavior\Tag::ALLOW_CHILDREN)) ->addAttrs(...$commonAttrs) ->addAttrs($hrefAttr->withFlags(Behavior\Attr::MANDATORY)), (new Behavior\Tag('br')) ) ->withNodes( (new Behavior\NodeHandler( new Behavior\Tag('typo3'), new Behavior\Handler\ClosureHandler( static function (NodeInterface $node, ?DOMNode $domNode): ?DOMNode { return $domNode === null ? null : new DOMText(sprintf('%s says: "%s"', strtoupper($domNode->nodeName), $domNode->textContent )); } ) )) ); $visitors = [new CommonVisitor($behavior)]; $sanitizer = new Sanitizer($behavior, ...$visitors); $html = <<< EOH <div id="main"> <typo3>Inspiring People To Share</typo3> <!-- will be encoded, due to Behavior::ENCODE_INVALID_COMMENT --> <a class="no-href">invalidated, due to missing mandatory `href` attr</a> <a href="https://typo3.org/" data-type="url" wrong-attr="is-removed">TYPO3</a><br> (the <span>SPAN, SPAN, SPAN</span> tag shall be encoded to HTML entities) </div> EOH; echo $sanitizer->sanitize($html);
will result in the following sanitized output
<div id="main"> TYPO3 says: "Inspiring People To Share" <!-- will be encoded, due to Behavior::ENCODE_INVALID_COMMENT --> <a class="no-href">invalidated, due to missing mandatory `href` attr</a> <a href="https://typo3.org/" data-type="url">TYPO3</a><br> (the <span>SPAN, SPAN, SPAN</span> tag shall be encoded to HTML entities) </div>
ℹ️ Changes
- since
v2.1.0newly introduced nodesBehavior\CommentandBehavior\CdataSectionare enabled per default for backward compatibility reasons, use e.g.$behavior->withoutNodes(new Behavior\Comment())to remove them (later versions of this package won't have this fallback anymore) - since
v2.1.0it is suggested to provide a\TYPO3\HtmlSanitizer\Behaviorwhen creating a new instance of\TYPO3\HtmlSanitizer\Sanitizer, e.g.new Sanitizer($behavior, ...$visitors)
Find more details on all changes in UPGRADING.md.
Behavior flags
Behavior::ENCODE_INVALID_TAGkeeps invalid tags, but "disarms" them (see<span>in example)Behavior::ENCODE_INVALID_ATTRkeeps invalid attributes, but "disarms" the whole(!) tagBehavior::ENCODE_INVALID_COMMENT"disarms" unexpected HTML comments by completely encoding themBehavior::ENCODE_INVALID_CDATA_SECTION"disarms" unexpected HTML CDATA sections by completely encoding themBehavior::REMOVE_UNEXPECTED_CHILDRENremoves children forTagentities that were created without explicitly usingTag::ALLOW_CHILDREN, but actually contained child nodesBehavior::ALLOW_CUSTOM_ELEMENTSallow using custom elements (having a hyphen-) - however, it is suggested to explicitly name all known and allowed tags and avoid using this flag
License
In general the TYPO3 core is released under the GNU General Public License version
2 or any later version (GPL-2.0-or-later). In order to avoid licensing issues and
incompatibilities this package is licenced under the MIT License. In case you
duplicate or modify source code, credits are not required but really appreciated.
Local Testing
Composer project oliverhader/html-sanitizer-demo offers a local development server to ease manual testing for potentially vulnerable XSS payloads.
Security Contact
In case of finding additional security issues in the TYPO3 project or in this package in particular, please get in touch with the TYPO3 Security Team, or directly report a vulnerability via GitHub.
typo3/html-sanitizer 适用场景与选型建议
typo3/html-sanitizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.41M 次下载、GitHub Stars 达 28, 最近一次更新时间为 2021 年 07 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 typo3/html-sanitizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 typo3/html-sanitizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10.41M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 27
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-06