\voku\helperAntiXSS

AntiXSS - ported from "CodeIgniter"

🇷🇺 Русским гражданам

В Украине сейчас идет война. Силами РФ наносятся удары по гражданской инфраструктуре в [Харькове][1], [Киеве][2], [Чернигове][3], [Сумах][4], [Ирпене][5] и десятках других городов. Гибнут люди - и гражданское население, и военные, в том числе российские призывники, которых бросили воевать. Чтобы лишить собственный народ доступа к информации, правительство РФ запретило называть войну войной, закрыло независимые СМИ и принимает сейчас ряд диктаторских законов. Эти законы призваны заткнуть рот всем, кто против войны. За обычный призыв к миру сейчас можно получить несколько лет тюрьмы. Не молчите! Молчание - знак вашего согласия с политикой российского правительства. Вы можете сделать выбор НЕ МОЛЧАТЬ.

🇺🇸 To people of Russia

There is a war in Ukraine right now. The forces of the Russian Federation are attacking civilian infrastructure in [Kharkiv][1], [Kyiv][2], [Chernihiv][3], [Sumy][4], [Irpin][5] and dozens of other cities. People are dying – both civilians and military servicemen, including Russian conscripts who were thrown into the fighting. In order to deprive its own people of access to information, the government of the Russian Federation has forbidden calling a war a war, shut down independent media and is passing a number of dictatorial laws. These laws are meant to silence all those who are against war. You can be jailed for multiple years for simply calling for peace. Do not be silent! Silence is a sign that you accept the Russian government's policy. You can choose NOT TO BE SILENT.

  • [1] https://cloudfront-us-east-2.images.arcpublishing.com/reuters/P7K2MSZDGFMIJPDD7CI2GIROJI.jpg "Kharkiv under attack"
  • [2] https://gdb.voanews.com/01bd0000-0aff-0242-fad0-08d9fc92c5b3_cx0_cy5_cw0_w1023_r1_s.jpg "Kyiv under attack"
  • [3] https://ichef.bbci.co.uk/news/976/cpsprodpb/163DD/production/_123510119_hi074310744.jpg "Chernihiv under attack"
  • [4] https://www.youtube.com/watch?v=8K-bkqKKf2A "Sumy under attack"
  • [5] https://cloudfront-us-east-2.images.arcpublishing.com/reuters/K4MTMLEHTRKGFK3GSKAT4GR3NE.jpg "Irpin under attack"

Summary

Methods
Properties
Constants
__construct()
addEvilAttributes()
addEvilHtmlTags()
addNeverAllowedRegex()
removeNeverAllowedRegex()
addNeverAllowedOnEventsAfterwards()
addNeverAllowedStrAfterwards()
addDoNotCloseHtmlTags()
addNeverAllowedJsCallbackRegex()
addNeverAllowedCallStrings()
removeDoNotCloseHtmlTags()
isXssFound()
removeEvilAttributes()
removeEvilHtmlTags()
removeNeverAllowedOnEventsAfterwards()
removeNeverAllowedStrAfterwards()
removeNeverAllowedCallStrings()
removeNeverAllowedJsCallbackRegex()
setReplacement()
setStripe4byteChars()
xss_clean()
No public properties found
VOKU_ANTI_XSS_GT
VOKU_ANTI_XSS_LT
VOKU_ANTI_XSS_STYLE
No protected methods found
No protected properties found
N/A
_compact_exploded_javascript()
_compact_exploded_words_callback()
_decode_entity()
_decode_string()
_do()
_do_never_allowed()
_get_never_allowed_on_events_afterwards_chunks()
_do_never_allowed_afterwards()
_entity_decode()
_filter_attributes()
_get_data()
_initNeverAllowedStr()
_initNeverAllowedRegex()
_js_link_removal_callback()
_js_removal_callback()
_js_src_removal_callback()
_remove_disallowed_javascript()
_remove_evil_attributes()
_repack_utf7()
_repack_utf7_callback()
_repack_utf7_callback_back()
_sanitize_naughty_html()
_close_html_callback()
_sanitize_naughty_html_callback()
_sanitize_naughty_javascript()
$_never_allowed_regex
$_do_not_close_html_tags
$_never_allowed_js_callback_regex
$_never_allowed_call_strings
$_never_allowed_str_afterwards
$_never_allowed_on_events_afterwards
$_evil_attributes_regex
$_evil_html_tags
$_spacing_regex
$_replacement
$_never_allowed_str
$_stripe_4byte_chars
$_xss_found
$_cache_evil_attributes_regex_string
$_cache_never_allowed_regex_string
$_cache__evil_html_tags_str
N/A

Constants

VOKU_ANTI_XSS_GT

VOKU_ANTI_XSS_GT = 'voku::anti-xss::gt'

VOKU_ANTI_XSS_LT

VOKU_ANTI_XSS_LT = 'voku::anti-xss::lt'

VOKU_ANTI_XSS_STYLE

VOKU_ANTI_XSS_STYLE = 'voku::anti-xss::STYLE'

Properties

$_never_allowed_regex

$_never_allowed_regex : string[]

List of never allowed regex replacements.

Type

string[]

$_do_not_close_html_tags

$_do_not_close_html_tags : string[]

List of html tags that will not close automatically.

Type

string[]

$_never_allowed_js_callback_regex

$_never_allowed_js_callback_regex : string[]

List of never allowed call statements.

Type

string[]

$_never_allowed_call_strings

$_never_allowed_call_strings : string[]

List of simple never allowed call statements.

Type

string[]

$_never_allowed_str_afterwards

$_never_allowed_str_afterwards : string[]

Type

string[]

$_never_allowed_on_events_afterwards

$_never_allowed_on_events_afterwards : string[]

List of never allowed strings, afterwards.

Type

string[]

$_evil_attributes_regex

$_evil_attributes_regex : string[]

https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet#Event_Handlers

Type

string[]

$_evil_html_tags

$_evil_html_tags : string[]

Type

string[]

$_spacing_regex

$_spacing_regex : string

Type

string

$_replacement

$_replacement : string

The replacement-string for not allowed strings.

Type

string

$_never_allowed_str

$_never_allowed_str : string[]

List of never allowed strings.

Type

string[]

$_stripe_4byte_chars

$_stripe_4byte_chars : bool

If your DB (MySQL) encoding is "utf8" and not "utf8mb4", then you can't save 4-Bytes chars from UTF-8 and someone can create stored XSS-attacks.

Type

bool

$_xss_found

$_xss_found : bool|null

Type

bool|null

$_cache_evil_attributes_regex_string

$_cache_evil_attributes_regex_string : string

Type

string

$_cache_never_allowed_regex_string

$_cache_never_allowed_regex_string : string

Type

string

$_cache__evil_html_tags_str

$_cache__evil_html_tags_str : string

Type

string

Methods

__construct()

__construct() : mixed

__construct()

Returns

mixed —

addEvilAttributes()

addEvilAttributes(string[]  $strings) : $this

Add some strings to the "_evil_attributes"-array.

Parameters

string[] $strings

Returns

$this —

addEvilHtmlTags()

addEvilHtmlTags(string[]  $strings) : $this

Add some strings to the "_evil_html_tags"-array.

Parameters

string[] $strings

Returns

$this —

addNeverAllowedRegex()

addNeverAllowedRegex(string[]  $strings) : $this

Add some strings to the "_never_allowed_regex"-array.

Parameters

string[] $strings

Returns

$this —

removeNeverAllowedRegex()

removeNeverAllowedRegex(string[]  $strings) : $this

Remove some strings from the "_never_allowed_regex"-array.


WARNING: Use this method only if you have a really good reason.

Parameters

string[] $strings

Returns

$this —

addNeverAllowedOnEventsAfterwards()

addNeverAllowedOnEventsAfterwards(string[]  $strings) : $this

Add some strings to the "_never_allowed_on_events_afterwards"-array.

Parameters

string[] $strings

Returns

$this —

addNeverAllowedStrAfterwards()

addNeverAllowedStrAfterwards(string[]  $strings) : $this

Add some strings to the "_never_allowed_str_afterwards"-array.

Parameters

string[] $strings

Returns

$this —

addDoNotCloseHtmlTags()

addDoNotCloseHtmlTags(string[]  $strings) : $this

Add some strings to the "_do_not_close_html_tags"-array.

Parameters

string[] $strings

Returns

$this —

addNeverAllowedJsCallbackRegex()

addNeverAllowedJsCallbackRegex(string[]  $strings) : $this

Add some strings to the "_never_allowed_js_callback_regex"-array.

Parameters

string[] $strings

Returns

$this —

addNeverAllowedCallStrings()

addNeverAllowedCallStrings(string[]  $strings) : $this

Add some strings to the "_never_allowed_call_strings"-array.

Parameters

string[] $strings

Returns

$this —

removeDoNotCloseHtmlTags()

removeDoNotCloseHtmlTags(string[]  $strings) : $this

Remove some strings from the "_do_not_close_html_tags"-array.


WARNING: Use this method only if you have a really good reason.

Parameters

string[] $strings

Returns

$this —

isXssFound()

isXssFound() : bool|null

Check if the "AntiXSS->xss_clean()"-method found an XSS attack in the last run.

Returns

bool|null —

Will return null if the "xss_clean()" wasn't running at all.

removeEvilAttributes()

removeEvilAttributes(string[]  $strings) : $this

Remove some strings from the "_evil_attributes"-array.


WARNING: Use this method only if you have a really good reason.

Parameters

string[] $strings

Returns

$this —

removeEvilHtmlTags()

removeEvilHtmlTags(string[]  $strings) : $this

Remove some strings from the "_evil_html_tags"-array.


WARNING: Use this method only if you have a really good reason.

Parameters

string[] $strings

Returns

$this —

removeNeverAllowedOnEventsAfterwards()

removeNeverAllowedOnEventsAfterwards(string[]  $strings) : $this

Remove some strings from the "_never_allowed_on_events_afterwards"-array.


WARNING: Use this method only if you have a really good reason.

Parameters

string[] $strings

Returns

$this —

removeNeverAllowedStrAfterwards()

removeNeverAllowedStrAfterwards(string[]  $strings) : $this

Remove some strings from the "_never_allowed_str_afterwards"-array.


WARNING: Use this method only if you have a really good reason.

Parameters

string[] $strings

Returns

$this —

removeNeverAllowedCallStrings()

removeNeverAllowedCallStrings(string[]  $strings) : $this

Remove some strings from the "_never_allowed_call_strings"-array.


WARNING: Use this method only if you have a really good reason.

Parameters

string[] $strings

Returns

$this —

removeNeverAllowedJsCallbackRegex()

removeNeverAllowedJsCallbackRegex(string[]  $strings) : $this

Remove some strings from the "_never_allowed_js_callback_regex"-array.


WARNING: Use this method only if you have a really good reason.

Parameters

string[] $strings

Returns

$this —

setReplacement()

setReplacement(string  $string) : $this

Set the replacement-string for not allowed strings.

Parameters

string $string

Returns

$this —

setStripe4byteChars()

setStripe4byteChars(bool  $bool) : $this

Set the option to stripe 4-Byte chars.


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

Parameters

bool $bool

Returns

$this —

xss_clean()

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

XSS Clean


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


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

Parameters

string|string[] $str

input data e.g. string or array of strings

Returns

string|string[] —

_compact_exploded_javascript()

_compact_exploded_javascript(string  $str) : string

Compact any exploded words.


INFO: This corrects words like: j a v a s c r i p t
These words are compacted back to their correct state.

Parameters

string $str

Returns

string —

_compact_exploded_words_callback()

_compact_exploded_words_callback(string[]  $matches) : string

Compact exploded words.


INFO: Callback method for xss_clean() to remove whitespace from things like 'j a v a s c r i p t'.

Parameters

string[] $matches

Returns

string —

_decode_entity()

_decode_entity(string[]  $match) : string

HTML-Entity decode callback.

Parameters

string[] $match

Returns

string —

_decode_string()

_decode_string(string  $str) : string

Decode the html-tags but keep links without XSS.

Parameters

string $str

Returns

string —

_do()

_do(string  $str) : string

Parameters

string $str

Returns

string —

_do_never_allowed()

_do_never_allowed(string  $str) : string

Remove never allowed strings.

Parameters

string $str

Returns

string —

_get_never_allowed_on_events_afterwards_chunks()

_get_never_allowed_on_events_afterwards_chunks() : array

Returns

array —

_do_never_allowed_afterwards()

_do_never_allowed_afterwards(string  $str) : string

Remove never allowed string, afterwards.


INFO: clean-up also some string, if there is no html-tag

Parameters

string $str

Returns

string —

_entity_decode()

_entity_decode(string  $str) : string

Entity-decoding.

Parameters

string $str

Returns

string —

_filter_attributes()

_filter_attributes(string  $str) : string

Filters tag attributes for consistency and safety.

Parameters

string $str

Returns

string —

_get_data()

_get_data(string  $file) : string[]

get data from "/data/*.php"

Parameters

string $file

Returns

string[] —

_initNeverAllowedStr()

_initNeverAllowedStr() : void

initialize "$this->_never_allowed_str"

_initNeverAllowedRegex()

_initNeverAllowedRegex() : void

initialize "$this->_never_allowed_regex"

_js_link_removal_callback()

_js_link_removal_callback(string[]  $match) : string

Callback method for xss_clean() to sanitize links.


INFO: This limits the PCRE backtracks, making it more performance friendly and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in PHP 5.2+ on link-heavy strings.

Parameters

string[] $match

Returns

string —

_js_removal_callback()

_js_removal_callback(string[]  $match, string  $search) : string

Callback method for xss_clean() to sanitize tags.


INFO: This limits the PCRE backtracks, making it more performance friendly and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in PHP 5.2+ on image tag heavy strings.

Parameters

string[] $match
string $search

Returns

string —

_js_src_removal_callback()

_js_src_removal_callback(string[]  $match) : string

Callback method for xss_clean() to sanitize image tags.


INFO: This limits the PCRE backtracks, making it more performance friendly and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in PHP 5.2+ on image tag heavy strings.

Parameters

string[] $match

Returns

string —

_remove_disallowed_javascript()

_remove_disallowed_javascript(string  $str) : string

Remove disallowed Javascript in links or img tags


We used to do some version comparisons and use of stripos(), but it is dog slow compared to these simplified non-capturing preg_match(), especially if the pattern exists in the string


Note: It was reported that not only space characters, but all in the following pattern can be parsed as separators between a tag name and its attributes: [\d\s"\'`;,\/\=\(\x00\x0B\x09\x0C] ... however, UTF8::clean() above already strips the hex-encoded ones, so we'll skip them below.

Parameters

string $str

Returns

string —

_repack_utf7()

_repack_utf7(string  $str) : string

UTF-7 decoding function.

Parameters

string $str

HTML document for recode ASCII part of UTF-7 back to ASCII.

Returns

string —

_repack_utf7_callback()

_repack_utf7_callback(string[]  $strings) : string

Additional UTF-7 decoding function.

Parameters

string[] $strings

Array of strings for recode ASCII part of UTF-7 back to ASCII.

Returns

string —

_repack_utf7_callback_back()

_repack_utf7_callback_back(string  $str) : string

Additional UTF-7 encoding function.

Parameters

string $str

String for recode ASCII part of UTF-7 back to ASCII.

Returns

string —

_sanitize_naughty_html()

_sanitize_naughty_html(string  $str) : string

Sanitize naughty HTML elements.


If a tag containing any of the words in the list below is found, the tag gets converted to entities.



So this:
Becomes: <blink>

Parameters

string $str

Returns

string —

_close_html_callback()

_close_html_callback(string[]  $matches) : string

Parameters

string[] $matches

Returns

string —

_sanitize_naughty_html_callback()

_sanitize_naughty_html_callback(string[]  $matches) : string

Sanitize naughty HTML.


Callback method for AntiXSS->sanitize_naughty_html() to remove naughty HTML elements.

Parameters

string[] $matches

Returns

string —

_sanitize_naughty_javascript()

_sanitize_naughty_javascript(string  $str) : string

Sanitize naughty scripting elements


Similar to above, only instead of looking for tags it looks for PHP and JavaScript commands that are disallowed. Rather than removing the code, it simply converts the parenthesis to entities rendering the code un-executable.



For example:

eval('some code')

Becomes:
eval('some code')

Parameters

string $str

Returns

string —