PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Security.php

https://bitbucket.org/ebottabi/ci-resque
PHP | 832 lines | 671 code | 38 blank | 123 comment | 13 complexity | 29e6be67070fc9911bed854128546b4d MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Security Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Security
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/libraries/security.html
  24. */
  25. class CI_Security {
  26. protected $_xss_hash = '';
  27. protected $_csrf_hash = '';
  28. protected $_csrf_expire = 7200; // Two hours (in seconds)
  29. protected $_csrf_token_name = 'ci_csrf_token';
  30. protected $_csrf_cookie_name = 'ci_csrf_token';
  31. /* never allowed, string replacement */
  32. protected $_never_allowed_str = array(
  33. 'document.cookie' => '[removed]',
  34. 'document.write' => '[removed]',
  35. '.parentNode' => '[removed]',
  36. '.innerHTML' => '[removed]',
  37. 'window.location' => '[removed]',
  38. '-moz-binding' => '[removed]',
  39. '<!--' => '&lt;!--',
  40. '-->' => '--&gt;',
  41. '<![CDATA[' => '&lt;![CDATA['
  42. );
  43. /* never allowed, regex replacement */
  44. protected $_never_allowed_regex = array(
  45. "javascript\s*:" => '[removed]',
  46. "expression\s*(\(|&\#40;)" => '[removed]', // CSS and IE
  47. "vbscript\s*:" => '[removed]', // IE, surprise!
  48. "Redirect\s+302" => '[removed]'
  49. );
  50. /**
  51. * Constructor
  52. */
  53. public function __construct()
  54. {
  55. // CSRF config
  56. foreach(array('csrf_expire', 'csrf_token_name', 'csrf_cookie_name') as $key)
  57. {
  58. if (FALSE !== ($val = config_item($key)))
  59. {
  60. $this->{'_'.$key} = $val;
  61. }
  62. }
  63. // Append application specific cookie prefix
  64. if (config_item('cookie_prefix'))
  65. {
  66. $this->_csrf_cookie_name = config_item('cookie_prefix').$this->_csrf_cookie_name;
  67. }
  68. // Set the CSRF hash
  69. $this->_csrf_set_hash();
  70. log_message('debug', "Security Class Initialized");
  71. }
  72. // --------------------------------------------------------------------
  73. /**
  74. * Verify Cross Site Request Forgery Protection
  75. *
  76. * @return object
  77. */
  78. public function csrf_verify()
  79. {
  80. // If no POST data exists we will set the CSRF cookie
  81. if (count($_POST) == 0)
  82. {
  83. return $this->csrf_set_cookie();
  84. }
  85. // Do the tokens exist in both the _POST and _COOKIE arrays?
  86. if ( ! isset($_POST[$this->_csrf_token_name]) OR
  87. ! isset($_COOKIE[$this->_csrf_cookie_name]))
  88. {
  89. $this->csrf_show_error();
  90. }
  91. // Do the tokens match?
  92. if ($_POST[$this->_csrf_token_name] != $_COOKIE[$this->_csrf_cookie_name])
  93. {
  94. $this->csrf_show_error();
  95. }
  96. // We kill this since we're done and we don't want to
  97. // polute the _POST array
  98. unset($_POST[$this->_csrf_token_name]);
  99. // Nothing should last forever
  100. unset($_COOKIE[$this->_csrf_cookie_name]);
  101. $this->_csrf_set_hash();
  102. $this->csrf_set_cookie();
  103. log_message('debug', "CSRF token verified ");
  104. return $this;
  105. }
  106. // --------------------------------------------------------------------
  107. /**
  108. * Set Cross Site Request Forgery Protection Cookie
  109. *
  110. * @return object
  111. */
  112. public function csrf_set_cookie()
  113. {
  114. $expire = time() + $this->_csrf_expire;
  115. $secure_cookie = (config_item('cookie_secure') === TRUE) ? 1 : 0;
  116. if ($secure_cookie)
  117. {
  118. $req = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : FALSE;
  119. if ( ! $req OR $req == 'off')
  120. {
  121. return FALSE;
  122. }
  123. }
  124. setcookie($this->_csrf_cookie_name, $this->_csrf_hash, $expire, config_item('cookie_path'), config_item('cookie_domain'), $secure_cookie);
  125. log_message('debug', "CRSF cookie Set");
  126. return $this;
  127. }
  128. // --------------------------------------------------------------------
  129. /**
  130. * Show CSRF Error
  131. *
  132. * @return void
  133. */
  134. public function csrf_show_error()
  135. {
  136. show_error('The action you have requested is not allowed.');
  137. }
  138. // --------------------------------------------------------------------
  139. /**
  140. * Get CSRF Hash
  141. *
  142. * Getter Method
  143. *
  144. * @return string self::_csrf_hash
  145. */
  146. public function get_csrf_hash()
  147. {
  148. return $this->_csrf_hash;
  149. }
  150. // --------------------------------------------------------------------
  151. /**
  152. * Get CSRF Token Name
  153. *
  154. * Getter Method
  155. *
  156. * @return string self::csrf_token_name
  157. */
  158. public function get_csrf_token_name()
  159. {
  160. return $this->_csrf_token_name;
  161. }
  162. // --------------------------------------------------------------------
  163. /**
  164. * XSS Clean
  165. *
  166. * Sanitizes data so that Cross Site Scripting Hacks can be
  167. * prevented. This function does a fair amount of work but
  168. * it is extremely thorough, designed to prevent even the
  169. * most obscure XSS attempts. Nothing is ever 100% foolproof,
  170. * of course, but I haven't been able to get anything passed
  171. * the filter.
  172. *
  173. * Note: This function should only be used to deal with data
  174. * upon submission. It's not something that should
  175. * be used for general runtime processing.
  176. *
  177. * This function was based in part on some code and ideas I
  178. * got from Bitflux: http://channel.bitflux.ch/wiki/XSS_Prevention
  179. *
  180. * To help develop this script I used this great list of
  181. * vulnerabilities along with a few other hacks I've
  182. * harvested from examining vulnerabilities in other programs:
  183. * http://ha.ckers.org/xss.html
  184. *
  185. * @param mixed string or array
  186. * @return string
  187. */
  188. public function xss_clean($str, $is_image = FALSE)
  189. {
  190. /*
  191. * Is the string an array?
  192. *
  193. */
  194. if (is_array($str))
  195. {
  196. while (list($key) = each($str))
  197. {
  198. $str[$key] = $this->xss_clean($str[$key]);
  199. }
  200. return $str;
  201. }
  202. /*
  203. * Remove Invisible Characters
  204. */
  205. $str = remove_invisible_characters($str);
  206. // Validate Entities in URLs
  207. $str = $this->_validate_entities($str);
  208. /*
  209. * URL Decode
  210. *
  211. * Just in case stuff like this is submitted:
  212. *
  213. * <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a>
  214. *
  215. * Note: Use rawurldecode() so it does not remove plus signs
  216. *
  217. */
  218. $str = rawurldecode($str);
  219. /*
  220. * Convert character entities to ASCII
  221. *
  222. * This permits our tests below to work reliably.
  223. * We only convert entities that are within tags since
  224. * these are the ones that will pose security problems.
  225. *
  226. */
  227. $str = preg_replace_callback("/[a-z]+=([\'\"]).*?\\1/si", array($this, '_convert_attribute'), $str);
  228. $str = preg_replace_callback("/<\w+.*?(?=>|<|$)/si", array($this, '_decode_entity'), $str);
  229. /*
  230. * Remove Invisible Characters Again!
  231. */
  232. $str = remove_invisible_characters($str);
  233. /*
  234. * Convert all tabs to spaces
  235. *
  236. * This prevents strings like this: ja vascript
  237. * NOTE: we deal with spaces between characters later.
  238. * NOTE: preg_replace was found to be amazingly slow here on
  239. * large blocks of data, so we use str_replace.
  240. */
  241. if (strpos($str, "\t") !== FALSE)
  242. {
  243. $str = str_replace("\t", ' ', $str);
  244. }
  245. /*
  246. * Capture converted string for later comparison
  247. */
  248. $converted_string = $str;
  249. // Remove Strings that are never allowed
  250. $str = $this->_do_never_allowed($str);
  251. /*
  252. * Makes PHP tags safe
  253. *
  254. * Note: XML tags are inadvertently replaced too:
  255. *
  256. * <?xml
  257. *
  258. * But it doesn't seem to pose a problem.
  259. */
  260. if ($is_image === TRUE)
  261. {
  262. // Images have a tendency to have the PHP short opening and
  263. // closing tags every so often so we skip those and only
  264. // do the long opening tags.
  265. $str = preg_replace('/<\?(php)/i', "&lt;?\\1", $str);
  266. }
  267. else
  268. {
  269. $str = str_replace(array('<?', '?'.'>'), array('&lt;?', '?&gt;'), $str);
  270. }
  271. /*
  272. * Compact any exploded words
  273. *
  274. * This corrects words like: j a v a s c r i p t
  275. * These words are compacted back to their correct state.
  276. */
  277. $words = array(
  278. 'javascript', 'expression', 'vbscript', 'script',
  279. 'applet', 'alert', 'document', 'write', 'cookie', 'window'
  280. );
  281. foreach ($words as $word)
  282. {
  283. $temp = '';
  284. for ($i = 0, $wordlen = strlen($word); $i < $wordlen; $i++)
  285. {
  286. $temp .= substr($word, $i, 1)."\s*";
  287. }
  288. // We only want to do this when it is followed by a non-word character
  289. // That way valid stuff like "dealer to" does not become "dealerto"
  290. $str = preg_replace_callback('#('.substr($temp, 0, -3).')(\W)#is', array($this, '_compact_exploded_words'), $str);
  291. }
  292. /*
  293. * Remove disallowed Javascript in links or img tags
  294. * We used to do some version comparisons and use of stripos for PHP5,
  295. * but it is dog slow compared to these simplified non-capturing
  296. * preg_match(), especially if the pattern exists in the string
  297. */
  298. do
  299. {
  300. $original = $str;
  301. if (preg_match("/<a/i", $str))
  302. {
  303. $str = preg_replace_callback("#<a\s+([^>]*?)(>|$)#si", array($this, '_js_link_removal'), $str);
  304. }
  305. if (preg_match("/<img/i", $str))
  306. {
  307. $str = preg_replace_callback("#<img\s+([^>]*?)(\s?/?>|$)#si", array($this, '_js_img_removal'), $str);
  308. }
  309. if (preg_match("/script/i", $str) OR preg_match("/xss/i", $str))
  310. {
  311. $str = preg_replace("#<(/*)(script|xss)(.*?)\>#si", '[removed]', $str);
  312. }
  313. }
  314. while($original != $str);
  315. unset($original);
  316. // Remove evil attributes such as style, onclick and xmlns
  317. $str = $this->_remove_evil_attributes($str, $is_image);
  318. /*
  319. * Sanitize naughty HTML elements
  320. *
  321. * If a tag containing any of the words in the list
  322. * below is found, the tag gets converted to entities.
  323. *
  324. * So this: <blink>
  325. * Becomes: &lt;blink&gt;
  326. */
  327. $naughty = 'alert|applet|audio|basefont|base|behavior|bgsound|blink|body|embed|expression|form|frameset|frame|head|html|ilayer|iframe|input|isindex|layer|link|meta|object|plaintext|style|script|textarea|title|video|xml|xss';
  328. $str = preg_replace_callback('#<(/*\s*)('.$naughty.')([^><]*)([><]*)#is', array($this, '_sanitize_naughty_html'), $str);
  329. /*
  330. * Sanitize naughty scripting elements
  331. *
  332. * Similar to above, only instead of looking for
  333. * tags it looks for PHP and JavaScript commands
  334. * that are disallowed. Rather than removing the
  335. * code, it simply converts the parenthesis to entities
  336. * rendering the code un-executable.
  337. *
  338. * For example: eval('some code')
  339. * Becomes: eval&#40;'some code'&#41;
  340. */
  341. $str = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', "\\1\\2&#40;\\3&#41;", $str);
  342. // Final clean up
  343. // This adds a bit of extra precaution in case
  344. // something got through the above filters
  345. $str = $this->_do_never_allowed($str);
  346. /*
  347. * Images are Handled in a Special Way
  348. * - Essentially, we want to know that after all of the character
  349. * conversion is done whether any unwanted, likely XSS, code was found.
  350. * If not, we return TRUE, as the image is clean.
  351. * However, if the string post-conversion does not matched the
  352. * string post-removal of XSS, then it fails, as there was unwanted XSS
  353. * code found and removed/changed during processing.
  354. */
  355. if ($is_image === TRUE)
  356. {
  357. return ($str == $converted_string) ? TRUE: FALSE;
  358. }
  359. log_message('debug', "XSS Filtering completed");
  360. return $str;
  361. }
  362. // --------------------------------------------------------------------
  363. /**
  364. * Random Hash for protecting URLs
  365. *
  366. * @return string
  367. */
  368. public function xss_hash()
  369. {
  370. if ($this->_xss_hash == '')
  371. {
  372. if (phpversion() >= 4.2)
  373. {
  374. mt_srand();
  375. }
  376. else
  377. {
  378. mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);
  379. }
  380. $this->_xss_hash = md5(time() + mt_rand(0, 1999999999));
  381. }
  382. return $this->_xss_hash;
  383. }
  384. // --------------------------------------------------------------------
  385. /**
  386. * HTML Entities Decode
  387. *
  388. * This function is a replacement for html_entity_decode()
  389. *
  390. * In some versions of PHP the native function does not work
  391. * when UTF-8 is the specified character set, so this gives us
  392. * a work-around. More info here:
  393. * http://bugs.php.net/bug.php?id=25670
  394. *
  395. * NOTE: html_entity_decode() has a bug in some PHP versions when UTF-8 is the
  396. * character set, and the PHP developers said they were not back porting the
  397. * fix to versions other than PHP 5.x.
  398. *
  399. * @param string
  400. * @param string
  401. * @return string
  402. */
  403. public function entity_decode($str, $charset='UTF-8')
  404. {
  405. if (stristr($str, '&') === FALSE) return $str;
  406. // The reason we are not using html_entity_decode() by itself is because
  407. // while it is not technically correct to leave out the semicolon
  408. // at the end of an entity most browsers will still interpret the entity
  409. // correctly. html_entity_decode() does not convert entities without
  410. // semicolons, so we are left with our own little solution here. Bummer.
  411. if (function_exists('html_entity_decode') &&
  412. (strtolower($charset) != 'utf-8'))
  413. {
  414. $str = html_entity_decode($str, ENT_COMPAT, $charset);
  415. $str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str);
  416. return preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str);
  417. }
  418. // Numeric Entities
  419. $str = preg_replace('~&#x(0*[0-9a-f]{2,5});{0,1}~ei', 'chr(hexdec("\\1"))', $str);
  420. $str = preg_replace('~&#([0-9]{2,4});{0,1}~e', 'chr(\\1)', $str);
  421. // Literal Entities - Slightly slow so we do another check
  422. if (stristr($str, '&') === FALSE)
  423. {
  424. $str = strtr($str, array_flip(get_html_translation_table(HTML_ENTITIES)));
  425. }
  426. return $str;
  427. }
  428. // --------------------------------------------------------------------
  429. /**
  430. * Filename Security
  431. *
  432. * @param string
  433. * @return string
  434. */
  435. public function sanitize_filename($str, $relative_path = FALSE)
  436. {
  437. $bad = array(
  438. "../",
  439. "<!--",
  440. "-->",
  441. "<",
  442. ">",
  443. "'",
  444. '"',
  445. '&',
  446. '$',
  447. '#',
  448. '{',
  449. '}',
  450. '[',
  451. ']',
  452. '=',
  453. ';',
  454. '?',
  455. "%20",
  456. "%22",
  457. "%3c", // <
  458. "%253c", // <
  459. "%3e", // >
  460. "%0e", // >
  461. "%28", // (
  462. "%29", // )
  463. "%2528", // (
  464. "%26", // &
  465. "%24", // $
  466. "%3f", // ?
  467. "%3b", // ;
  468. "%3d" // =
  469. );
  470. if ( ! $relative_path)
  471. {
  472. $bad[] = './';
  473. $bad[] = '/';
  474. }
  475. $str = remove_invisible_characters($str, FALSE);
  476. return stripslashes(str_replace($bad, '', $str));
  477. }
  478. // ----------------------------------------------------------------
  479. /**
  480. * Compact Exploded Words
  481. *
  482. * Callback function for xss_clean() to remove whitespace from
  483. * things like j a v a s c r i p t
  484. *
  485. * @param type
  486. * @return type
  487. */
  488. protected function _compact_exploded_words($matches)
  489. {
  490. return preg_replace('/\s+/s', '', $matches[1]).$matches[2];
  491. }
  492. // --------------------------------------------------------------------
  493. /*
  494. * Remove Evil HTML Attributes (like evenhandlers and style)
  495. *
  496. * It removes the evil attribute and either:
  497. * - Everything up until a space
  498. * For example, everything between the pipes:
  499. * <a |style=document.write('hello');alert('world');| class=link>
  500. * - Everything inside the quotes
  501. * For example, everything between the pipes:
  502. * <a |style="document.write('hello'); alert('world');"| class="link">
  503. *
  504. * @param string $str The string to check
  505. * @param boolean $is_image TRUE if this is an image
  506. * @return string The string with the evil attributes removed
  507. */
  508. protected function _remove_evil_attributes($str, $is_image)
  509. {
  510. // All javascript event handlers (e.g. onload, onclick, onmouseover), style, and xmlns
  511. $evil_attributes = array('on\w*', 'style', 'xmlns');
  512. if ($is_image === TRUE)
  513. {
  514. /*
  515. * Adobe Photoshop puts XML metadata into JFIF images,
  516. * including namespacing, so we have to allow this for images.
  517. */
  518. unset($evil_attributes[array_search('xmlns', $evil_attributes)]);
  519. }
  520. do {
  521. $str = preg_replace(
  522. "#<(/?[^><]+?)([^A-Za-z\-])(".implode('|', $evil_attributes).")(\s*=\s*)([\"][^>]*?[\"]|[\'][^>]*?[\']|[^>]*?)([\s><])([><]*)#i",
  523. "<$1$6",
  524. $str, -1, $count
  525. );
  526. } while ($count);
  527. return $str;
  528. }
  529. // --------------------------------------------------------------------
  530. /**
  531. * Sanitize Naughty HTML
  532. *
  533. * Callback function for xss_clean() to remove naughty HTML elements
  534. *
  535. * @param array
  536. * @return string
  537. */
  538. protected function _sanitize_naughty_html($matches)
  539. {
  540. // encode opening brace
  541. $str = '&lt;'.$matches[1].$matches[2].$matches[3];
  542. // encode captured opening or closing brace to prevent recursive vectors
  543. $str .= str_replace(array('>', '<'), array('&gt;', '&lt;'),
  544. $matches[4]);
  545. return $str;
  546. }
  547. // --------------------------------------------------------------------
  548. /**
  549. * JS Link Removal
  550. *
  551. * Callback function for xss_clean() to sanitize links
  552. * This limits the PCRE backtracks, making it more performance friendly
  553. * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
  554. * PHP 5.2+ on link-heavy strings
  555. *
  556. * @param array
  557. * @return string
  558. */
  559. protected function _js_link_removal($match)
  560. {
  561. $attributes = $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]));
  562. return str_replace($match[1], preg_replace("#href=.*?(alert\(|alert&\#40;|javascript\:|livescript\:|mocha\:|charset\=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si", "", $attributes), $match[0]);
  563. }
  564. // --------------------------------------------------------------------
  565. /**
  566. * JS Image Removal
  567. *
  568. * Callback function for xss_clean() to sanitize image tags
  569. * This limits the PCRE backtracks, making it more performance friendly
  570. * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
  571. * PHP 5.2+ on image tag heavy strings
  572. *
  573. * @param array
  574. * @return string
  575. */
  576. protected function _js_img_removal($match)
  577. {
  578. $attributes = $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]));
  579. return str_replace($match[1], preg_replace("#src=.*?(alert\(|alert&\#40;|javascript\:|livescript\:|mocha\:|charset\=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si", "", $attributes), $match[0]);
  580. }
  581. // --------------------------------------------------------------------
  582. /**
  583. * Attribute Conversion
  584. *
  585. * Used as a callback for XSS Clean
  586. *
  587. * @param array
  588. * @return string
  589. */
  590. protected function _convert_attribute($match)
  591. {
  592. return str_replace(array('>', '<', '\\'), array('&gt;', '&lt;', '\\\\'), $match[0]);
  593. }
  594. // --------------------------------------------------------------------
  595. /**
  596. * Filter Attributes
  597. *
  598. * Filters tag attributes for consistency and safety
  599. *
  600. * @param string
  601. * @return string
  602. */
  603. protected function _filter_attributes($str)
  604. {
  605. $out = '';
  606. if (preg_match_all('#\s*[a-z\-]+\s*=\s*(\042|\047)([^\\1]*?)\\1#is', $str, $matches))
  607. {
  608. foreach ($matches[0] as $match)
  609. {
  610. $out .= preg_replace("#/\*.*?\*/#s", '', $match);
  611. }
  612. }
  613. return $out;
  614. }
  615. // --------------------------------------------------------------------
  616. /**
  617. * HTML Entity Decode Callback
  618. *
  619. * Used as a callback for XSS Clean
  620. *
  621. * @param array
  622. * @return string
  623. */
  624. protected function _decode_entity($match)
  625. {
  626. return $this->entity_decode($match[0], strtoupper(config_item('charset')));
  627. }
  628. // --------------------------------------------------------------------
  629. /**
  630. * Validate URL entities
  631. *
  632. * Called by xss_clean()
  633. *
  634. * @param string
  635. * @return string
  636. */
  637. protected function _validate_entities($str)
  638. {
  639. /*
  640. * Protect GET variables in URLs
  641. */
  642. // 901119URL5918AMP18930PROTECT8198
  643. $str = preg_replace('|\&([a-z\_0-9\-]+)\=([a-z\_0-9\-]+)|i', $this->xss_hash()."\\1=\\2", $str);
  644. /*
  645. * Validate standard character entities
  646. *
  647. * Add a semicolon if missing. We do this to enable
  648. * the conversion of entities to ASCII later.
  649. *
  650. */
  651. $str = preg_replace('#(&\#?[0-9a-z]{2,})([\x00-\x20])*;?#i', "\\1;\\2", $str);
  652. /*
  653. * Validate UTF16 two byte encoding (x00)
  654. *
  655. * Just as above, adds a semicolon if missing.
  656. *
  657. */
  658. $str = preg_replace('#(&\#x?)([0-9A-F]+);?#i',"\\1\\2;",$str);
  659. /*
  660. * Un-Protect GET variables in URLs
  661. */
  662. $str = str_replace($this->xss_hash(), '&', $str);
  663. return $str;
  664. }
  665. // ----------------------------------------------------------------------
  666. /**
  667. * Do Never Allowed
  668. *
  669. * A utility function for xss_clean()
  670. *
  671. * @param string
  672. * @return string
  673. */
  674. protected function _do_never_allowed($str)
  675. {
  676. foreach ($this->_never_allowed_str as $key => $val)
  677. {
  678. $str = str_replace($key, $val, $str);
  679. }
  680. foreach ($this->_never_allowed_regex as $key => $val)
  681. {
  682. $str = preg_replace("#".$key."#i", $val, $str);
  683. }
  684. return $str;
  685. }
  686. // --------------------------------------------------------------------
  687. /**
  688. * Set Cross Site Request Forgery Protection Cookie
  689. *
  690. * @return string
  691. */
  692. protected function _csrf_set_hash()
  693. {
  694. if ($this->_csrf_hash == '')
  695. {
  696. // If the cookie exists we will use it's value.
  697. // We don't necessarily want to regenerate it with
  698. // each page load since a page could contain embedded
  699. // sub-pages causing this feature to fail
  700. if (isset($_COOKIE[$this->_csrf_cookie_name]) &&
  701. $_COOKIE[$this->_csrf_cookie_name] != '')
  702. {
  703. return $this->_csrf_hash = $_COOKIE[$this->_csrf_cookie_name];
  704. }
  705. return $this->_csrf_hash = md5(uniqid(rand(), TRUE));
  706. }
  707. return $this->_csrf_hash;
  708. }
  709. }
  710. // END Security Class
  711. /* End of file Security.php */
  712. /* Location: ./system/libraries/Security.php */