PageRenderTime 74ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Kohana/Security.php

http://github.com/kohana/core
PHP | 138 lines | 43 code | 14 blank | 81 comment | 3 complexity | d95daadbf3348fb8eba6d566b7cb651a MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Security helper class.
  4. *
  5. * @package Kohana
  6. * @category Security
  7. * @author Kohana Team
  8. * @copyright (c) 2007-2012 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. */
  11. class Kohana_Security {
  12. /**
  13. * @var string key name used for token storage
  14. */
  15. public static $token_name = 'security_token';
  16. /**
  17. * Generate and store a unique token which can be used to help prevent
  18. * [CSRF](http://wikipedia.org/wiki/Cross_Site_Request_Forgery) attacks.
  19. *
  20. * $token = Security::token();
  21. *
  22. * You can insert this token into your forms as a hidden field:
  23. *
  24. * echo Form::hidden('csrf', Security::token());
  25. *
  26. * And then check it when using [Validation]:
  27. *
  28. * $array->rules('csrf', array(
  29. * array('not_empty'),
  30. * array('Security::check'),
  31. * ));
  32. *
  33. * This provides a basic, but effective, method of preventing CSRF attacks.
  34. *
  35. * @param boolean $new force a new token to be generated?
  36. * @return string
  37. * @uses Session::instance
  38. */
  39. public static function token($new = FALSE)
  40. {
  41. $session = Session::instance();
  42. // Get the current token
  43. $token = $session->get(Security::$token_name);
  44. if ($new === TRUE OR ! $token)
  45. {
  46. // Generate a new unique token
  47. if (function_exists('openssl_random_pseudo_bytes'))
  48. {
  49. // Generate a random pseudo bytes token if openssl_random_pseudo_bytes is available
  50. // This is more secure than uniqid, because uniqid relies on microtime, which is predictable
  51. $token = base64_encode(openssl_random_pseudo_bytes(32));
  52. }
  53. else
  54. {
  55. // Otherwise, fall back to a hashed uniqid
  56. $token = sha1(uniqid(NULL, TRUE));
  57. }
  58. // Store the new token
  59. $session->set(Security::$token_name, $token);
  60. }
  61. return $token;
  62. }
  63. /**
  64. * Check that the given token matches the currently stored security token.
  65. *
  66. * if (Security::check($token))
  67. * {
  68. * // Pass
  69. * }
  70. *
  71. * @param string $token token to check
  72. * @return boolean
  73. * @uses Security::token
  74. */
  75. public static function check($token)
  76. {
  77. return Security::slow_equals(Security::token(), $token);
  78. }
  79. /**
  80. * Compare two hashes in a time-invariant manner.
  81. * Prevents cryptographic side-channel attacks (timing attacks, specifically)
  82. *
  83. * @param string $a cryptographic hash
  84. * @param string $b cryptographic hash
  85. * @return boolean
  86. */
  87. public static function slow_equals($a, $b)
  88. {
  89. $diff = strlen($a) ^ strlen($b);
  90. for($i = 0; $i < strlen($a) AND $i < strlen($b); $i++)
  91. {
  92. $diff |= ord($a[$i]) ^ ord($b[$i]);
  93. }
  94. return $diff === 0;
  95. }
  96. /**
  97. * Deprecated for security reasons.
  98. * See https://github.com/kohana/kohana/issues/107
  99. *
  100. * Remove image tags from a string.
  101. *
  102. * $str = Security::strip_image_tags($str);
  103. *
  104. * @deprecated since version 3.3.6
  105. * @param string $str string to sanitize
  106. * @return string
  107. */
  108. public static function strip_image_tags($str)
  109. {
  110. return preg_replace('#<img\s.*?(?:src\s*=\s*["\']?([^"\'<>\s]*)["\']?[^>]*)?>#is', '$1', $str);
  111. }
  112. /**
  113. * Encodes PHP tags in a string.
  114. *
  115. * $str = Security::encode_php_tags($str);
  116. *
  117. * @param string $str string to sanitize
  118. * @return string
  119. */
  120. public static function encode_php_tags($str)
  121. {
  122. return str_replace(array('<?', '?>'), array('&lt;?', '?&gt;'), $str);
  123. }
  124. }