2017 © Pedro Pelรกez
 

library anti-xss

anti xss-library

image

voku/anti-xss

anti xss-library

  • Tuesday, June 12, 2018
  • by voku
  • Repository
  • 20 Watchers
  • 150 Stars
  • 180,220 Installations
  • PHP
  • 12 Dependents
  • 0 Suggesters
  • 55 Forks
  • 0 Open issues
  • 67 Versions
  • 13 % Grown

The README.md

SWUbanner, (*1)

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

:secret: AntiXSS

"Cross-site scripting (XSS) is a type of computer security vulnerability typically found in Web applications. XSS enables attackers to inject client-side script into Web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same origin policy. Cross-site scripting carried out on websites accounted for roughly 84% of all security vulnerabilities documented by Symantec as of 2007." - http://en.wikipedia.org/wiki/Cross-site_scripting, (*3)

DEMO:

http://anti-xss-demo.suckup.de/, (*4)

NOTES:

1) Use filter_input() - don't use GLOBAL-Array (e.g. $_SESSION, $_GET, $_POST, $_SERVER) directly, (*5)

2) Use html-sanitizer or HTML Purifier if you need a more configurable solution, (*6)

3) Add "Content Security Policy's" -> Introduction to Content Security Policy, (*7)

4) DO NOT WRITE YOUR OWN REGEX TO PARSE HTML!, (*8)

5) READ THIS TEXT -> XSS (Cross Site Scripting) Prevention Cheat Sheet, (*9)

6) TEST THIS TOOL -> Zed Attack Proxy (ZAP), (*10)

Install via "composer require"

composer require voku/anti-xss

Usage:


use voku\helper\AntiXSS; require_once __DIR__ . '/vendor/autoload.php'; // example path $antiXss = new AntiXSS();

Example 1: (HTML Character), (*11)

$harm_string = "Hello, i try to  your site";
$harmless_string = $antiXss->xss_clean($harm_string);

// Hello, i try to alert('Hack'); your site

Example 2: (Hexadecimal HTML Character), (*12)

$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 >

Example 3: (Unicode Hex Character), (*13)

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

// <a >CLICK</a>

Example 4: (Unicode Character), (*14)

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

// <a >CLICK</a>

Example 5.1: (non Inline CSS), (*15)

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

// <li >

Example 5.2: (with Inline CSS), (*16)

$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;)">

Example 6: (check if an string contains a XSS attack), (*17)

$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

Example 7: (allow e.g. iframes), (*18)

$harm_string = "



";

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

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

// 




Unit Test:

1) Composer is a prerequisite for running the tests., (*19)

composer install

2) The tests can be executed by running this command from the root directory:, (*20)

./vendor/bin/phpunit

AntiXss methods

, (*21)

addDoNotCloseHtmlTags addEvilAttributes addEvilHtmlTags addNeverAllowedCallStrings
addNeverAllowedJsCallbackRegex addNeverAllowedOnEventsAfterwards addNeverAllowedRegex addNeverAllowedStrAfterwards
isXssFound removeDoNotCloseHtmlTags removeEvilAttributes removeEvilHtmlTags
removeNeverAllowedCallStrings removeNeverAllowedJsCallbackRegex removeNeverAllowedOnEventsAfterwards removeNeverAllowedRegex
removeNeverAllowedStrAfterwards setReplacement setStripe4byteChars xss_clean

addDoNotCloseHtmlTags(string[] $strings): $this

โ†‘ Add some strings to the "_do_not_close_html_tags"-array., (*22)

Parameters: - string[] $strings, (*23)

Return: - $this, (*24)


addEvilAttributes(string[] $strings): $this

โ†‘ Add some strings to the "_evil_attributes"-array., (*25)

Parameters: - string[] $strings, (*26)

Return: - $this, (*27)


addEvilHtmlTags(string[] $strings): $this

โ†‘ Add some strings to the "_evil_html_tags"-array., (*28)

Parameters: - string[] $strings, (*29)

Return: - $this, (*30)


addNeverAllowedCallStrings(string[] $strings): $this

โ†‘ Add some strings to the "_never_allowed_call_strings"-array., (*31)

Parameters: - string[] $strings, (*32)

Return: - $this, (*33)


addNeverAllowedJsCallbackRegex(string[] $strings): $this

โ†‘ Add some strings to the "_never_allowed_js_callback_regex"-array., (*34)

Parameters: - string[] $strings, (*35)

Return: - $this, (*36)


addNeverAllowedOnEventsAfterwards(string[] $strings): $this

โ†‘ Add some strings to the "_never_allowed_on_events_afterwards"-array., (*37)

Parameters: - string[] $strings, (*38)

Return: - $this, (*39)


addNeverAllowedRegex(string[] $strings): $this

โ†‘ Add some strings to the "_never_allowed_regex"-array., (*40)

Parameters: - string[] $strings, (*41)

Return: - $this, (*42)


addNeverAllowedStrAfterwards(string[] $strings): $this

โ†‘ Add some strings to the "_never_allowed_str_afterwards"-array., (*43)

Parameters: - string[] $strings, (*44)

Return: - $this, (*45)


isXssFound(): bool|null

โ†‘ Check if the "AntiXSS->xss_clean()"-method found an XSS attack in the last run., (*46)

Parameters: nothing, (*47)

Return: - bool|null <p>Will return null if the "xss_clean()" wasn't running at all.</p>, (*48)


removeDoNotCloseHtmlTags(string[] $strings): $this

โ†‘ Remove some strings from the "_do_not_close_html_tags"-array., (*49)


WARNING: Use this method only if you have a really good reason. , (*50)

Parameters: - string[] $strings, (*51)

Return: - $this, (*52)


removeEvilAttributes(string[] $strings): $this

โ†‘ Remove some strings from the "_evil_attributes"-array., (*53)


WARNING: Use this method only if you have a really good reason. , (*54)

Parameters: - string[] $strings, (*55)

Return: - $this, (*56)


removeEvilHtmlTags(string[] $strings): $this

โ†‘ Remove some strings from the "_evil_html_tags"-array., (*57)


WARNING: Use this method only if you have a really good reason. , (*58)

Parameters: - string[] $strings, (*59)

Return: - $this, (*60)


removeNeverAllowedCallStrings(string[] $strings): $this

โ†‘ Remove some strings from the "_never_allowed_call_strings"-array., (*61)


WARNING: Use this method only if you have a really good reason. , (*62)

Parameters: - string[] $strings, (*63)

Return: - $this, (*64)


removeNeverAllowedJsCallbackRegex(string[] $strings): $this

โ†‘ Remove some strings from the "_never_allowed_js_callback_regex"-array., (*65)


WARNING: Use this method only if you have a really good reason. , (*66)

Parameters: - string[] $strings, (*67)

Return: - $this, (*68)


removeNeverAllowedOnEventsAfterwards(string[] $strings): $this

โ†‘ Remove some strings from the "_never_allowed_on_events_afterwards"-array., (*69)


WARNING: Use this method only if you have a really good reason. , (*70)

Parameters: - string[] $strings, (*71)

Return: - $this, (*72)


removeNeverAllowedRegex(string[] $strings): $this

โ†‘ Remove some strings from the "_never_allowed_regex"-array., (*73)


WARNING: Use this method only if you have a really good reason. , (*74)

Parameters: - string[] $strings, (*75)

Return: - $this, (*76)


removeNeverAllowedStrAfterwards(string[] $strings): $this

โ†‘ Remove some strings from the "_never_allowed_str_afterwards"-array., (*77)


WARNING: Use this method only if you have a really good reason. , (*78)

Parameters: - string[] $strings, (*79)

Return: - $this, (*80)


setReplacement(string $string): $this

โ†‘ Set the replacement-string for not allowed strings., (*81)

Parameters: - string $string, (*82)

Return: - $this, (*83)


setStripe4byteChars(bool $bool): $this

โ†‘ Set the option to stripe 4-Byte chars., (*84)


INFO: use it if your DB (MySQL) can't use "utf8mb4" -> preventing stored XSS-attacks , (*85)

Parameters: - bool $bool, (*86)

Return: - $this, (*87)


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

โ†‘ XSS Clean, (*88)


Sanitizes data so that "Cross Site Scripting" hacks can be prevented. This method does a fair amount of work but it is extremely thorough, designed to prevent even the most obscure XSS attempts. But keep in mind that nothing is ever 100% foolproof... , (*89)


Note: Should only be used to deal with data upon submission. It's not something that should be used for general runtime processing. , (*90)

Parameters: - TXssCleanInput $str <p>input data e.g. string or array of strings</p>, (*91)

Return: - string|string[], (*92)


Support

For support and donations please visit Github | Issues | PayPal | Patreon., (*93)

For status updates and release announcements please visit Releases | Twitter | Patreon., (*94)

For professional support please contact me., (*95)

Thanks

  • Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
  • Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
  • Thanks to Travis CI for being the most awesome, easiest continous integration tool out there!
  • Thanks to StyleCI for the simple but powerfull code style check.
  • Thanks to PHPStan && Psalm for relly great Static analysis tools and for discover bugs in the code!

License

FOSSA Status, (*96)

The Versions

12/06 2018

dev-master

9999999-dev https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

26/04 2018

4.1.1

4.1.1.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

26/04 2018

dev-php_old

dev-php_old https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

26/04 2018

2.3.1

2.3.1.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

17/04 2018

dev-analysis-zELdv9

dev-analysis-zELdv9 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

17/04 2018

2.3.0

2.3.0.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

17/04 2018

4.1.0

4.1.0.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

12/04 2018

2.2.2

2.2.2.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

12/04 2018

4.0.3

4.0.3.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

14/02 2018

2.2.1

2.2.1.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

14/02 2018

4.0.2

4.0.2.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

07/01 2018

4.0.1

4.0.1.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

23/12 2017

4.0.0

4.0.0.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

22/11 2017

2.2.0

2.2.0.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

21/11 2017

3.1.0

3.1.0.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

19/11 2017

3.0.1

3.0.1.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

19/11 2017

3.0.0

3.0.0.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

08/05 2017

2.1.7

2.1.7.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

08/05 2017

2.1.6

2.1.6.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

07/05 2017

2.1.5

2.1.5.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

07/05 2017

2.1.4

2.1.4.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

04/04 2017

2.1.3

2.1.3.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

10/01 2017

2.1.2

2.1.2.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

05/01 2017

2.1.1

2.1.1.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

04/01 2017

2.1.0

2.1.0.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

28/11 2016

2.0.10

2.0.10.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

08/10 2016

2.0.9

2.0.9.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

07/10 2016

2.0.8

2.0.8.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

07/10 2016

2.0.7

2.0.7.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

04/10 2016

2.0.6

2.0.6.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

29/08 2016

2.0.5

2.0.5.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

12/08 2016

2.0.4

2.0.4.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

12/08 2016

2.0.3

2.0.3.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

10/08 2016

2.0.2

2.0.2.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

07/07 2016

2.0.1

2.0.1.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

06/07 2016

2.0.0

2.0.0.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

02/06 2016

1.2.14

1.2.14.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

14/04 2016

1.2.13

1.2.13.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

21/03 2016

1.2.12

1.2.12.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

01/02 2016

1.2.11

1.2.11.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

23/12 2015

1.2.10

1.2.10.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

16/10 2015

1.2.9

1.2.9.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

11/10 2015

1.2.8

1.2.8.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

21/09 2015

1.2.7

1.2.7.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

05/09 2015

1.2.6

1.2.6.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

20/08 2015

1.2.5

1.2.5.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

20/08 2015

1.2.4

1.2.4.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

20/08 2015

1.2.3

1.2.3.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

13/08 2015

1.2.2

1.2.2.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

13/08 2015

1.2.1

1.2.1.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

12/08 2015

1.2.0

1.2.0.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

08/08 2015

1.1.1

1.1.1.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

27/07 2015

1.1.0

1.1.0.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

24/07 2015

1.0.13

1.0.13.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

MIT

The Requires

 

The Development Requires

security xss clean anti-xss

21/07 2015

1.0.12

1.0.12.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss

20/07 2015

1.0.11

1.0.11.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss

13/07 2015

1.0.10

1.0.10.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss

28/06 2015

1.0.9

1.0.9.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss

19/06 2015

1.0.8

1.0.8.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss

17/06 2015

1.0.7

1.0.7.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss

17/06 2015

1.0.6

1.0.6.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss

13/06 2015

1.0.5

1.0.5.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss

26/03 2015

1.0.4

1.0.4.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss

18/03 2015

1.0.3

1.0.3.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss

16/03 2015

1.0.2

1.0.2.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss

16/03 2015

1.0.1

1.0.1.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss

13/03 2015

1.0

1.0.0.0 https://github.com/voku/anti-xss

anti xss-library

  Sources   Download

CC-BY-3.0

The Requires

 

The Development Requires

security xss clean anti-xss