-
Notifications
You must be signed in to change notification settings - Fork 0
CI_Security
Security Class
- Class name: CI_Security
- Namespace:
public array $filename_bad_chars = array('../', '<!--', '-->', '<', '>', "'", '"', '&', '$', '#', '{', '}', '[', ']', '=', ';', '?', '%20', '%22', '%3c', '%253c', '%3e', '%0e', '%28', '%29', '%2528', '%26', '%24', '%3f', '%3b', '%3d')
List of sanitize filename strings
- Visibility: public
public string $charset = 'UTF-8'
Character set
Will be overridden by the constructor.
- Visibility: public
protected string $_xss_hash
XSS Hash
Random Hash for protecting URLs.
- Visibility: protected
protected string $_csrf_hash
CSRF Hash
Random hash for Cross Site Request Forgery protection cookie
- Visibility: protected
protected integer $_csrf_expire = 7200
CSRF Expire time
Expiration time for Cross Site Request Forgery protection cookie. Defaults to two hours (in seconds).
- Visibility: protected
protected string $_csrf_token_name = 'ci_csrf_token'
CSRF Token name
Token name for Cross Site Request Forgery protection cookie.
- Visibility: protected
protected string $_csrf_cookie_name = 'ci_csrf_token'
CSRF Cookie name
Cookie name for Cross Site Request Forgery protection cookie.
- Visibility: protected
protected array $_never_allowed_str = array('document.cookie' => '[removed]', 'document.write' => '[removed]', '.parentNode' => '[removed]', '.innerHTML' => '[removed]', '-moz-binding' => '[removed]', '<!--' => '<!--', '-->' => '-->', '<![CDATA[' => '<![CDATA[', '<comment>' => '<comment>')
List of never allowed strings
- Visibility: protected
protected array $_never_allowed_regex = array('javascript\s*:', '(document|(document\.)?window)\.(location|on\w*)', 'expression\s*(\(|&\#40;)', 'vbscript\s*:', 'wscript\s*:', 'jscript\s*:', 'vbs\s*:', 'Redirect\s+30\d', "([\"'])?data\s*:[^\\1]*?base64[^\\1]*?,[^\\1]*?\\1?")
List of never allowed regex replacements
- Visibility: protected
void CI_Security::__construct()
Class constructor
- Visibility: public
\CI_Security CI_Security::csrf_verify()
CSRF Verify
- Visibility: public
\CI_Security CI_Security::csrf_set_cookie()
CSRF Set Cookie
- Visibility: public
void CI_Security::csrf_show_error()
Show CSRF Error
- Visibility: public
string CI_Security::get_csrf_hash()
Get CSRF Hash
- Visibility: public
string CI_Security::get_csrf_token_name()
Get CSRF Token Name
- Visibility: public
string CI_Security::xss_clean(string|array<mixed,string> $str, boolean $is_image)
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. Nothing is ever 100% foolproof, of course, but I haven't been able to get anything passed the filter.
Note: Should only be used to deal with data upon submission. It's not something that should be used for general runtime processing.
- Visibility: public
- $str string|array<mixed,string> - <p>Input data</p>
- $is_image boolean - <p>Whether the input is an image</p>
string CI_Security::xss_hash()
XSS Hash
Generates the XSS hash if needed and returns it.
- Visibility: public
string CI_Security::get_random_bytes(integer $length)
Get random bytes
- Visibility: public
- $length integer - <p>Output length</p>
string CI_Security::entity_decode(string $str, string $charset)
HTML Entities Decode
A replacement for html_entity_decode()
The reason we are not using html_entity_decode() by itself is because while it is not technically correct to leave out the semicolon at the end of an entity most browsers will still interpret the entity correctly. html_entity_decode() does not convert entities without semicolons, so we are left with our own little solution here. Bummer.
- Visibility: public
- $str string - <p>Input</p>
- $charset string - <p>Character set</p>
string CI_Security::sanitize_filename(string $str, boolean $relative_path)
Sanitize Filename
- Visibility: public
- $str string - <p>Input file name</p>
- $relative_path boolean - <p>Whether to preserve paths</p>
string CI_Security::strip_image_tags(string $str)
Strip Image Tags
- Visibility: public
- $str string
string CI_Security::_compact_exploded_words(array $matches)
Compact Exploded Words
Callback method for xss_clean() to remove whitespace from things like 'j a v a s c r i p t'.
- Visibility: protected
- $matches array
string CI_Security::_remove_evil_attributes(string $str, boolean $is_image)
Remove Evil HTML Attributes (like event handlers and style)
It removes the evil attribute and either:
- Everything up until a space. For example, everything between the pipes:
- Everything inside the quotes. For example, everything between the pipes:
- Visibility: protected
- $str string - <p>The string to check</p>
- $is_image boolean - <p>Whether the input is an image</p>
string CI_Security::_sanitize_naughty_html(array $matches)
Sanitize Naughty HTML
Callback method for xss_clean() to remove naughty HTML elements.
- Visibility: protected
- $matches array
string CI_Security::_js_link_removal(array $match)
JS Link Removal
Callback method for xss_clean() to sanitize links.
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.
- Visibility: protected
- $match array
string CI_Security::_js_img_removal(array $match)
JS Image Removal
Callback method for xss_clean() to sanitize image tags.
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.
- Visibility: protected
- $match array
string CI_Security::_convert_attribute(array $match)
Attribute Conversion
- Visibility: protected
- $match array
string CI_Security::_filter_attributes(string $str)
Filter Attributes
Filters tag attributes for consistency and safety.
- Visibility: protected
- $str string
string CI_Security::_decode_entity(array $match)
HTML Entity Decode Callback
- Visibility: protected
- $match array
string CI_Security::_do_never_allowed($str)
Do Never Allowed
- Visibility: protected
- $str mixed
string CI_Security::_csrf_set_hash()
Set CSRF Hash and Cookie
- Visibility: protected