erfanhemmati/anti-xss

dev-main 2023-04-23 13:07 UTC

This package is auto-updated.

Last update: 2024-09-23 16:21:42 UTC


README

Build Status codecov.io Codacy Badge Latest Stable Version Total Downloads License Donate to this project using Paypal Donate to this project using Patreon

㊙️ AntiXSS

"跨站脚本(XSS)是一种计算机安全漏洞,通常出现在Web应用程序中。XSS允许攻击者将客户端脚本注入到其他用户查看的Web页面中。跨站脚本漏洞可能被攻击者用来绕过如同源策略等访问控制。截至2007年,Symantec记录的所有安全漏洞中,由网站执行的网络跨站脚本攻击占大约84%。" - http://en.wikipedia.org/wiki/Cross-site_scripting

DEMO

http://anti-xss-demo.suckup.de/

注意

  1. 使用 filter_input() - 不要直接使用全局数组(例如 $_SESSION, $_GET, $_POST, $_SERVER)

  2. 如果需要更灵活的解决方案,请使用 html-sanitizerHTML Purifier

  3. 添加 "内容安全策略" -> 内容安全策略简介

  4. 不要编写自己的正则表达式来解析HTML!

  5. 阅读此文本 -> XSS(跨站脚本)预防技巧清单

  6. 测试此工具 -> Zed Attack Proxy (ZAP)

通过 "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 <script>alert('Hack');</script> your site";
$harmless_string = $antiXss->xss_clean($harm_string);

// Hello, i try to alert&#40;'Hack'&#41;; your site

示例 2: (十六进制 HTML 字符)

$harm_string = "<IMG SRC=&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69&#x70&#x74&#x3A&#x61&#x6C&#x65&#x72&#x74&#x28&#x27&#x58&#x53&#x53&#x27&#x29>";
$harmless_string = $antiXss->xss_clean($harm_string);
    
// <IMG >

示例 3: (十六进制 Unicode 字符)

$harm_string = "<a href='&#x2000;javascript:alert(1)'>CLICK</a>";
$harmless_string = $antiXss->xss_clean($harm_string);
    
// <a >CLICK</a>

示例 4: (Unicode 字符)

$harm_string = "<a href=\"\u0001java\u0003script:alert(1)\">CLICK<a>";
$harmless_string = $antiXss->xss_clean($harm_string);
    
// <a >CLICK</a>

示例 5.1: (非内联 CSS)

$harm_string = '<li style="list-style-image: url(javascript:alert(0))">';
$harmless_string = $antiXss->xss_clean($harm_string);

// <li >

示例 5.2: (带有内联 CSS)

$harm_string = '<li style="list-style-image: url(javascript:alert(0))">';
$antiXss->removeEvilAttributes(array('style')); // allow style-attributes
$harmless_string = $antiXss->xss_clean($harm_string);

// <li style="list-style-image: url(alert&#40;0&#41;)">

示例 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 = "<iframe width="560" onclick="alert('xss')" height="315" src="https://www.youtube.com/embed/foobar?rel=0&controls=0&showinfo=0" frameborder="0" allowfullscreen></iframe>";

$antiXss->removeEvilHtmlTags(array('iframe'));

$harmless_string = $antiXss->xss_clean($harm_string);

// <iframe width="560"  height="315" src="https://www.youtube.com/embed/foobar?rel=0&controls=0&showinfo=0" frameborder="0" allowfullscreen></iframe>

单元测试

  1. 运行测试需要 Composer 作为先决条件。
composer install
  1. 可以从根目录运行以下命令来执行测试
./vendor/bin/phpunit

AntiXss 方法

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

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

isXssFound(): bool|null

↑ 检查 "AntiXSS->xss_clean()" 方法在上次运行中是否发现了 XSS 攻击。

参数:

返回

  • 布尔值或null <p>如果“xss_clean()”根本未运行,则返回null。</p>

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

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

setStripe4byteChars(bool $bool): $this

设置是否删除4字节字符的选项。


信息:如果你的数据库(MySQL)无法使用“utf8mb4” -> 防止存储XSS攻击

参数

  • bool $bool

返回

  • $this

xss_clean(string|string[] $str): string|string[]

XSS清理


清洗数据,以防止“跨站脚本”攻击。此方法做了大量工作,非常彻底,旨在防止最微妙的XSS尝试。但请注意,没有任何事情是100%万无一失的...


注意:仅应用于处理提交后的数据。它不是应该用于通用运行时处理的东西。

参数

  • TXssCleanInput $str <p>输入数据,例如字符串或字符串数组</p>

返回

  • string|string[]

支持

有关支持和捐款,请访问 Github | 问题 | PayPal | Patreon

有关状态更新和发布公告,请访问 发布 | Twitter | Patreon

有关专业支持,请联系

谢谢

  • 感谢 GitHub(微软)托管代码和良好的基础设施,包括问题管理等。
  • 感谢 IntelliJ,因为它们是PHP最佳IDE,并且它们给了我PhpStorm的开源许可证!
  • 感谢 Travis CI,因为它是最好的、最简单的持续集成工具!
  • 感谢 StyleCI,因为它的代码风格检查简单但强大。
  • 感谢 PHPStan && Psalm 提供了真正出色的静态分析工具,并发现了代码中的错误!

许可

FOSSA Status