Recommend this page to a friend! |
Classes of Lars Moelleken | PHP Anti XSS Filter | README.md | Download |
|
Download [//]: # (AUTO-GENERATED BY "PHP README Helper": base file -> docs/base.md) :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 DEMO:http://anti-xss-demo.suckup.de/ NOTES:1) Use filter_input() - don't use GLOBAL-Array (e.g. $_SESSION, $_GET, $_POST, $_SERVER) directly 2) Use html-sanitizer or HTML Purifier if you need a more configurable solution 3) Add "Content Security Policy's" -> Introduction to Content Security Policy 4) DO NOT WRITE YOUR OWN REGEX TO PARSE HTML! 5) READ THIS TEXT -> XSS (Cross Site Scripting) Prevention Cheat Sheet 6) TEST THIS TOOL -> Zed Attack Proxy (ZAP) Install via "composer require"
Usage:
Example 1: (HTML Character)
Example 2: (Hexadecimal HTML Character)
Example 3: (Unicode Hex Character)
Example 4: (Unicode Character)
Example 5.1: (non Inline CSS)
Example 5.2: (with Inline CSS)
Example 6: (check if an string contains a XSS attack)
Example 7: (allow e.g. iframes)
Unit Test:1) Composer is a prerequisite for running the tests.
2) The tests can be executed by running this command from the root directory:
AntiXss methods<p id="voku-php-readme-class-methods"></p><table><tr><td><a href="#adddonotclosehtmltagsstring-strings-this">addDoNotCloseHtmlTags</a> </td><td><a href="#addevilattributesstring-strings-this">addEvilAttributes</a> </td><td><a href="#addevilhtmltagsstring-strings-this">addEvilHtmlTags</a> </td><td><a href="#addneverallowedcallstringsstring-strings-this">addNeverAllowedCallStrings</a> </td></tr><tr><td><a href="#addneverallowedjscallbackregexstring-strings-this">addNeverAllowedJsCallbackRegex</a> </td><td><a href="#addneverallowedoneventsafterwardsstring-strings-this">addNeverAllowedOnEventsAfterwards</a> </td><td><a href="#addneverallowedregexstring-strings-this">addNeverAllowedRegex</a> </td><td><a href="#addneverallowedstrafterwardsstring-strings-this">addNeverAllowedStrAfterwards</a> </td></tr><tr><td><a href="#isxssfound-boolnull">isXssFound</a> </td><td><a href="#removedonotclosehtmltagsstring-strings-this">removeDoNotCloseHtmlTags</a> </td><td><a href="#removeevilattributesstring-strings-this">removeEvilAttributes</a> </td><td><a href="#removeevilhtmltagsstring-strings-this">removeEvilHtmlTags</a> </td></tr><tr><td><a href="#removeneverallowedcallstringsstring-strings-this">removeNeverAllowedCallStrings</a> </td><td><a href="#removeneverallowedjscallbackregexstring-strings-this">removeNeverAllowedJsCallbackRegex</a> </td><td><a href="#removeneverallowedoneventsafterwardsstring-strings-this">removeNeverAllowedOnEventsAfterwards</a> </td><td><a href="#removeneverallowedregexstring-strings-this">removeNeverAllowedRegex</a> </td></tr><tr><td><a href="#removeneverallowedstrafterwardsstring-strings-this">removeNeverAllowedStrAfterwards</a> </td><td><a href="#setreplacementstring-string-this">setReplacement</a> </td><td><a href="#setstripe4bytecharsbool-bool-this">setStripe4byteChars</a> </td><td><a href="#xss_cleanstringstring-str-stringstring">xss_clean</a> </td></tr></table> addDoNotCloseHtmlTags(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Add some strings to the "_do_not_close_html_tags"-array. Parameters:
- Return:
- addEvilAttributes(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Add some strings to the "_evil_attributes"-array. Parameters:
- Return:
- addEvilHtmlTags(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Add some strings to the "_evil_html_tags"-array. Parameters:
- Return:
- addNeverAllowedCallStrings(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Add some strings to the "_never_allowed_call_strings"-array. Parameters:
- Return:
- addNeverAllowedJsCallbackRegex(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Add some strings to the "_never_allowed_js_callback_regex"-array. Parameters:
- Return:
- addNeverAllowedOnEventsAfterwards(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Add some strings to the "_never_allowed_on_events_afterwards"-array. Parameters:
- Return:
- addNeverAllowedRegex(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Add some strings to the "_never_allowed_regex"-array. Parameters:
- Return:
- addNeverAllowedStrAfterwards(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Add some strings to the "_never_allowed_str_afterwards"-array. Parameters:
- Return:
- isXssFound(): bool|null<a href="#voku-php-readme-class-methods">?</a> Check if the "AntiXSS->xss_clean()"-method found an XSS attack in the last run. Parameters: __nothing__ Return:
- removeDoNotCloseHtmlTags(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Remove some strings from the "_do_not_close_html_tags"-array. <p> <br /> WARNING: Use this method only if you have a really good reason. </p> Parameters:
- Return:
- removeEvilAttributes(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Remove some strings from the "_evil_attributes"-array. <p> <br /> WARNING: Use this method only if you have a really good reason. </p> Parameters:
- Return:
- removeEvilHtmlTags(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Remove some strings from the "_evil_html_tags"-array. <p> <br /> WARNING: Use this method only if you have a really good reason. </p> Parameters:
- Return:
- removeNeverAllowedCallStrings(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Remove some strings from the "_never_allowed_call_strings"-array. <p> <br /> WARNING: Use this method only if you have a really good reason. </p> Parameters:
- Return:
- removeNeverAllowedJsCallbackRegex(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Remove some strings from the "_never_allowed_js_callback_regex"-array. <p> <br /> WARNING: Use this method only if you have a really good reason. </p> Parameters:
- Return:
- removeNeverAllowedOnEventsAfterwards(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Remove some strings from the "_never_allowed_on_events_afterwards"-array. <p> <br /> WARNING: Use this method only if you have a really good reason. </p> Parameters:
- Return:
- removeNeverAllowedRegex(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Remove some strings from the "_never_allowed_regex"-array. <p> <br /> WARNING: Use this method only if you have a really good reason. </p> Parameters:
- Return:
- removeNeverAllowedStrAfterwards(string[] $strings): $this<a href="#voku-php-readme-class-methods">?</a> Remove some strings from the "_never_allowed_str_afterwards"-array. <p> <br /> WARNING: Use this method only if you have a really good reason. </p> Parameters:
- Return:
- setReplacement(string $string): $this<a href="#voku-php-readme-class-methods">?</a> Set the replacement-string for not allowed strings. Parameters:
- Return:
- setStripe4byteChars(bool $bool): $this<a href="#voku-php-readme-class-methods">?</a> Set the option to stripe 4-Byte chars. <p> <br /> INFO: use it if your DB (MySQL) can't use "utf8mb4" -> preventing stored XSS-attacks </p> Parameters:
- Return:
- xss_clean(string|string[] $str): string|string[]<a href="#voku-php-readme-class-methods">?</a> XSS Clean <p> <br /> 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... </p> <p> <br /> <strong>Note:</strong> Should only be used to deal with data upon submission. It's not something that should be used for general runtime processing. </p> Parameters:
- Return:
- SupportFor support and donations please visit Github | Issues | PayPal | Patreon. For status updates and release announcements please visit Releases | Twitter | Patreon. For professional support please contact me. Thanks
License |