voku/anti-xss

GitHub: voku/anti-xss

一个基于 PHP 的跨站脚本(XSS)防护库,提供细粒度的输入清洗与检测方法。

Stars: 713 | Forks: 121

[![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md) [![构建状态](https://static.pigsec.cn/wp-content/uploads/repos/cas/8c/8ca6394fb8f0a967aabc7abfe37904ce254a39ebf8718f6bc5a20ee7dc798d17.svg)](https://github.com/voku/anti-xss/actions) [![codecov.io](http://codecov.io/github/voku/anti-xss/coverage.svg?branch=master)](http://codecov.io/github/voku/anti-xss?branch=master) [![Codacy 徽章](https://api.codacy.com/project/badge/Grade/8e3c9da417124971b8d8e0c1046c24c7)](https://www.codacy.com/app/voku/anti-xss) [![最新稳定版本](https://poser.pugx.org/voku/anti-xss/v/stable)](https://packagist.org/packages/voku/anti-xss) [![总下载量](https://poser.pugx.org/voku/anti-xss/downloads)](https://packagist.org/packages/voku/anti-xss) [![许可证](https://poser.pugx.org/voku/anti-xss/license)](https://packagist.org/packages/voku/anti-xss) [![使用 Paypal 为此项目捐款](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.me/moelleken) [![使用 Patreon 为此项目捐款](https://img.shields.io/badge/patreon-donate-yellow.svg)](https://www.patreon.com/voku) # :secret: AntiXSS “跨站脚本攻击 (XSS) 是一种通常存在于 Web 应用程序中的计算机安全漏洞。XSS 使得攻击者能够将客户端脚本注入到其他用户浏览的 Web 页面中。攻击者可能会利用跨站脚本漏洞来绕过诸如同源策略之类的访问控制。截至 2007 年,根据 Symantec 的记录,在网站上实施的跨站脚本攻击约占所有安全漏洞的 84%。” - http://en.wikipedia.org/wiki/Cross-site_scripting ### 演示: [http://anti-xss-demo.suckup.de/](http://anti-xss-demo.suckup.de/) ### 注意事项: 1. 使用 [filter_input()](http://php.net/manual/de/function.filter-input.php) - 不要直接使用 GLOBAL 数组 (例如 $_SESSION, $_GET, $_POST, $_SERVER) 2. 如果你需要更可配置的解决方案,请使用 [html-sanitizer](https://github.com/tgalopin/html-sanitizer) 或 [HTML Purifier](http://htmlpurifier.org/) 3. 添加“Content Security Policy” -> [Content Security Policy 简介](http://www.html5rocks.com/en/tutorials/security/content-security-policy/) 4. 不要编写自己的正则表达式来解析 HTML! 5. 阅读此文本 -> [XSS (跨站脚本攻击) 防范备忘单](https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.md) 6. 测试此工具 -> [Zed Attack Proxy (ZAP)](https://github.com/zaproxy/zaproxy) ### 禁用自动编码更改 此包依赖于 `voku/portable-utf8`,它会在自动加载期间通过 `ini_set()` 将 `default_charset` 设置为 `UTF-8`。 如果你需要阻止此行为,请在加载 Composer 自动加载器**之前**定义以下常量: ``` define('PORTABLE_UTF8__DISABLE_AUTO_ENCODING', true); require_once __DIR__ . '/vendor/autoload.php'; ``` ### 通过 "composer require" 安装 ``` composer require voku/anti-xss ``` ### 用法: ``` use voku\helper\AntiXSS; require_once __DIR__ . '/vendor/autoload.php'; // example path $antiXss = new AntiXSS(); ``` 示例 1: (HTML 字符) ``` $harm_string = "Hello, i try to your site"; $harmless_string = $antiXss->xss_clean($harm_string); // Hello, i try to alert('Hack'); your site ``` 示例 2: (十六进制 HTML 字符) ``` $harm_string = ""; $harmless_string = $antiXss->xss_clean($harm_string); // ``` 示例 3: (Unicode 十六进制字符) ``` $harm_string = "CLICK"; $harmless_string = $antiXss->xss_clean($harm_string); // CLICK ``` 示例 4: (Unicode 字符) ``` $harm_string = "CLICK"; $harmless_string = $antiXss->xss_clean($harm_string); // CLICK ``` 示例 5.1: (非内联 CSS) ``` $harm_string = '
  • '; $harmless_string = $antiXss->xss_clean($harm_string); //
  • ``` 示例 5.2: (带内联 CSS) ``` $harm_string = '
  • '; $antiXss->removeEvilAttributes(array('style')); // allow style-attributes $harmless_string = $antiXss->xss_clean($harm_string); //
  • ``` 示例 6: (检查字符串是否包含 XSS 攻击) ``` $harm_string = "\x3cscript src=http://www.example.com/malicious-code.js\x3e\x3c/script\x3e"; $harmless_string = $antiXss->xss_clean($harm_string); // $antiXss->isXssFound(); // true ``` 示例 7: (允许例如 iframe) ``` $harm_string = ""; $antiXss->removeEvilHtmlTags(array('iframe')); $harmless_string = $antiXss->xss_clean($harm_string); // ``` ### 单元测试: 1. [Composer](https://getcomposer.org) 是运行测试的前提条件。 ``` composer install ``` 2. 可以通过从根目录运行以下命令来执行测试: ``` XDEBUG_MODE=coverage ./vendor/bin/phpunit -c phpunit.xml ``` ### 带有静态分析的变异测试: CI 在 PHP 8.3 的 pull-request 任务中运行带有 PHPStan 集成的 [Infection](https://infection.github.io/)。它使用 `infection.json5.dist`,要求变异差异的 MSI 达到 100%,并且如果有任何变异体超时就会失败,从而确保清理循环不会悄无声息地发生回退。 要在本地使用 PHP 8.3+ 运行相同的工具链: ``` composer config --no-plugins allow-plugins.infection/extension-installer true composer require --dev phpstan/phpstan:^2.1 infection/infection:^0.32.7 --no-update composer update XDEBUG_MODE=coverage ./vendor/bin/infection --configuration=infection.json5.dist ``` ### 用于未来 LLM 字典检查的 prompt 当你希望 LLM 在不将 AntiXSS 字典手动复制到测试中的情况下,扩展围绕这些字典的回归测试覆盖率时,请使用此 prompt: ``` You are working in the voku/anti-xss repository. 1. Run the current PHPUnit suite first with: XDEBUG_MODE=coverage ./vendor/bin/phpunit -c phpunit.xml 2. Inspect /src/voku/helper/AntiXSS.php for dictionary-style private arrays such as: - _never_allowed_on_events_afterwards - _evil_attributes_regex - _naughty_javascript_patterns - _naughty_javascript_patterns_strict - _never_allowed_str_afterwards 3. For each dictionary that has a safe generic assertion shape, add or extend provider-based tests that iterate every current entry automatically. 4. Prefer reflection-backed test providers over copying the source dictionaries into test files, so newly added entries are covered automatically. 5. For each dictionary, test both the intended blocking behavior and at least one important boundary rule when relevant (for example strict vs. whitespace-separated JavaScript callbacks, or executable vs. non-executable event attribute forms). 6. Make the smallest possible production change only if the expanded dictionary coverage exposes a real regression. 7. Re-run PHPUnit after each small step and continue iterating across the targeted dictionaries until you find and fix at least one real regression for the task, or confirm that the remaining dictionaries are already covered. ``` ## AntiXss 方法

    addDoNotCloseHtmlTags addEvilAttributes addEvilHtmlTags addNeverAllowedCallStrings
    addNeverAllowedJsCallbackRegex addNeverAllowedOnEventsAfterwards addNeverAllowedRegex addNeverAllowedStrAfterwards
    addNaughtyJavascriptPatterns isXssFound removeDoNotCloseHtmlTags removeEvilAttributes
    removeEvilHtmlTags removeNeverAllowedCallStrings removeNeverAllowedJsCallbackRegex removeNeverAllowedOnEventsAfterwards
    removeNeverAllowedRegex removeNeverAllowedStrAfterwards setKeepPreAndCodeTagContent setReplacement setStripe4byteChars
    xss_clean
    ## addDoNotCloseHtmlTags(string[] $strings): $this 向 "_do_not_close_html_tags" 数组中添加一些字符串。 **参数:** - `string[] $strings` **返回:** - `$this` ## addEvilAttributes(string[] $strings): $this 向 "_evil_attributes" 数组中添加一些字符串。 **参数:** - `string[] $strings` **返回:** - `$this` ## addEvilHtmlTags(string[] $strings): $this 向 "_evil_html_tags" 数组中添加一些字符串。 **参数:** - `string[] $strings` **返回:** - `$this` ## addNeverAllowedCallStrings(string[] $strings): $this 向 "_never_allowed_call_strings" 数组中添加一些字符串。 **参数:** - `string[] $strings` **返回:** - `$this` ## addNeverAllowedJsCallbackRegex(string[] $strings): $this 向 "_never_allowed_js_callback_regex" 数组中添加一些字符串。 **参数:** - `string[] $strings` **返回:** - `$this` ## addNeverAllowedOnEventsAfterwards(string[] $strings): $this 向 "_never_allowed_on_events_afterwards" 数组中添加一些字符串。 **参数:** - `string[] $strings` **返回:** - `$this` ## addNeverAllowedRegex(string[] $strings): $this 向 "_never_allowed_regex" 数组中添加一些字符串。 **参数:** - `string[] $strings` **返回:** - `$this` ## addNeverAllowedStrAfterwards(string[] $strings): $this 向 "_never_allowed_str_afterwards" 数组中添加一些字符串。 **参数:** - `string[] $strings` **返回:** - `$this` ## addNaughtyJavascriptPatterns(string[] $strings): $this 向 "_naughty_javascript_patterns" 数组中添加一些字符串。 **参数:** - `string[] $strings` **返回:** - `$this` ## isXssFound(): bool|null 检查 "AntiXSS->xss_clean()" 方法是否在上次运行中发现了 XSS 攻击。 **参数:** __无__ **返回:** - `bool|null

    如果 "xss_clean()" 根本没有运行过,则返回 null。

    ` ## removeDoNotCloseHtmlTags(string[] $strings): $this 从 "_do_not_close_html_tags" 数组中移除一些字符串。


    警告:仅在你有充分理由的情况下才使用此方法。

    **参数:** - `string[] $strings` **返回:** - `$this` ## removeEvilAttributes(string[] $strings): $this 从 "_evil_attributes" 数组中移除一些字符串。


    警告:仅在你有充分理由的情况下才使用此方法。

    **参数:** - `string[] $strings` **返回:** - `$this` ## removeEvilHtmlTags(string[] $strings): $this 从 "_evil_html_tags" 数组中移除一些字符串。


    警告:仅在你有充分理由的情况下才使用此方法。

    **参数:** - `string[] $strings` **返回:** - `$this` ## removeNeverAllowedCallStrings(string[] $strings): $this 从 "_never_allowed_call_strings" 数组中移除一些字符串。


    警告:仅在你有充分理由的情况下才使用此方法。

    **参数:** - `string[] $strings` **返回:** - `$this` ## removeNeverAllowedJsCallbackRegex(string[] $strings): $this 从 "_never_allowed_js_callback_regex" 数组中移除一些字符串。


    警告:仅在你有充分理由的情况下才使用此方法。

    **参数:** - `string[] $strings` **返回:** - `$this` ## removeNeverAllowedOnEventsAfterwards(string[] $strings): $this 从 "_never_allowed_on_events_afterwards" 数组中移除一些字符串。


    警告:仅在你有充分理由的情况下才使用此方法。

    **参数:** - `string[] $strings` **返回:** - `$this` ## removeNeverAllowedRegex(string[] $strings): $this 从 "_never_allowed_regex" 数组中移除一些字符串。


    警告:仅在你有充分理由的情况下才使用此方法。

    **参数:** - `string[] $strings` **返回:** - `$this` ## removeNeverAllowedStrAfterwards(string[] $strings): $this 从 "_never_allowed_str_afterwards" 数组中移除一些字符串。


    警告:仅在你有充分理由的情况下才使用此方法。

    **参数:** - `string[] $strings` **返回:** -this` ## setReplacement(string $string): $this 设置用于替换不允许的字符串的替换字符串。 **参数:** - `string $string` **返回:** - `$this` ## setKeepPreAndCodeTagContent(bool $bool): $this 设置保留 "pre" 和 "code" 标签内内容的选项。


    警告:仅当你明确希望 "pre" / "code" 块中类似代码的文本保持不变时才启用此功能。

    **参数:** - `bool $bool` **返回:** - `$this` ## setStripe4byteChars(bool $bool): $this 设置过滤 4 字节字符的选项。


    提示:如果你的数据库 (MySQL) 不能使用 "utf8mb4" -> 则使用此选项来防范存储型 XSS 攻击

    **参数:** - `bool $bool` **返回:** - `$this` ## xss_clean(string|string[] $str): string|string[] XSS 清理


    对数据进行清理,从而防范“跨站脚本”攻击。 此方法执行了大量的工作,并且非常 彻底,旨在防范即使是最隐蔽的 XSS 攻击企图。但请记住,没有 任何事情是 100% 万无一失的……


    注意:应该仅用于在提交时处理数据。 它不应用于常规的 runtime 处理。

    **参数:** - `TXssCleanInput $str

    输入数据,例如字符串或字符串数组

    ` **返回:** - `string|string[]` ### 支持 如需支持与捐款,请访问 [Github](https://github.com/voku/anti-xss/) | [Issues](https://github.com/voku/anti-xss/issues) | [PayPal](https://paypal.me/moelleken) | [Patreon](https://www.patreon.com/voku)。 如需状态更新和版本发布公告,请访问 [Releases](https://github.com/voku/anti-xss/releases) | [Twitter](https://twitter.com/suckup_de) | [Patreon](https://www.patreon.com/voku/posts)。 如需专业支持,请联系[我](https://about.me/voku)。 ### 致谢 - 感谢 [GitHub](https://github.com) (Microsoft) 托管代码以及提供包括问题管理等在内的优秀基础设施。 - 感谢 [IntelliJ](https://www.jetbrains.com),因为他们制作了最好的 PHP IDE,并且给了我 PhpStorm 的开源许可证! - 感谢 [Travis CI](https://travis-ci.com/),因为它是现有最棒、最易用的持续集成工具! - 感谢 [StyleCI](https://styleci.io/) 提供简单但功能强大的代码风格检查。 - 感谢 [PHPStan](https://github.com/phpstan/phpstan) && [Psalm](https://github.com/vimeo/psalm) 提供真正出色的静态分析工具,并发现了代码中的 bug! ### 许可证 [![FOSSA 状态](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fvoku%2Fanti-xss.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fvoku%2Fanti-xss?ref=badge_large)
  • 标签:API密钥检测, ffuf, OpenVAS, PHP, Web安全, XSS防护, 蓝队分析, 输入验证, 输出过滤