Hash.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @subpackage Element
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Form_Element_Xhtml */
  22. require_once 'Zend/Form/Element/Xhtml.php';
  23. /** @see Zend_Crypt_Math */
  24. require_once 'Zend/Crypt/Math.php';
  25. /**
  26. * CSRF form protection
  27. *
  28. * @category Zend
  29. * @package Zend_Form
  30. * @subpackage Element
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @version $Id$
  34. */
  35. class Zend_Form_Element_Hash extends Zend_Form_Element_Xhtml
  36. {
  37. /**
  38. * Use formHidden view helper by default
  39. * @var string
  40. */
  41. public $helper = 'formHidden';
  42. /**
  43. * Actual hash used.
  44. *
  45. * @var mixed
  46. */
  47. protected $_hash;
  48. /**
  49. * Salt for CSRF token
  50. * @var string
  51. */
  52. protected $_salt = 'salt';
  53. /**
  54. * @var Zend_Session_Namespace
  55. */
  56. protected $_session;
  57. /**
  58. * TTL for CSRF token
  59. * @var int
  60. */
  61. protected $_timeout = 300;
  62. /**
  63. * Constructor
  64. *
  65. * Creates session namespace for CSRF token, and adds validator for CSRF
  66. * token.
  67. *
  68. * @param string|array|Zend_Config $spec
  69. * @param array|Zend_Config $options
  70. * @return void
  71. */
  72. public function __construct($spec, $options = null)
  73. {
  74. parent::__construct($spec, $options);
  75. $this->setAllowEmpty(false)
  76. ->setRequired(true)
  77. ->initCsrfValidator();
  78. }
  79. /**
  80. * Set session object
  81. *
  82. * @param Zend_Session_Namespace $session
  83. * @return Zend_Form_Element_Hash
  84. */
  85. public function setSession($session)
  86. {
  87. $this->_session = $session;
  88. return $this;
  89. }
  90. /**
  91. * Get session object
  92. *
  93. * Instantiate session object if none currently exists
  94. *
  95. * @return Zend_Session_Namespace
  96. */
  97. public function getSession()
  98. {
  99. if (null === $this->_session) {
  100. require_once 'Zend/Session/Namespace.php';
  101. $this->_session = new Zend_Session_Namespace($this->getSessionName());
  102. }
  103. return $this->_session;
  104. }
  105. /**
  106. * Initialize CSRF validator
  107. *
  108. * Creates Session namespace, and initializes CSRF token in session.
  109. * Additionally, adds validator for validating CSRF token.
  110. *
  111. * @return Zend_Form_Element_Hash
  112. */
  113. public function initCsrfValidator()
  114. {
  115. $session = $this->getSession();
  116. if (isset($session->hash)) {
  117. $rightHash = $session->hash;
  118. } else {
  119. $rightHash = null;
  120. }
  121. $this->addValidator('Identical', true, array($rightHash));
  122. return $this;
  123. }
  124. /**
  125. * Salt for CSRF token
  126. *
  127. * @param string $salt
  128. * @return Zend_Form_Element_Hash
  129. */
  130. public function setSalt($salt)
  131. {
  132. $this->_salt = (string) $salt;
  133. return $this;
  134. }
  135. /**
  136. * Retrieve salt for CSRF token
  137. *
  138. * @return string
  139. */
  140. public function getSalt()
  141. {
  142. return $this->_salt;
  143. }
  144. /**
  145. * Retrieve CSRF token
  146. *
  147. * If no CSRF token currently exists, generates one.
  148. *
  149. * @return string
  150. */
  151. public function getHash()
  152. {
  153. if (null === $this->_hash) {
  154. $this->_generateHash();
  155. }
  156. return $this->_hash;
  157. }
  158. /**
  159. * Get session namespace for CSRF token
  160. *
  161. * Generates a session namespace based on salt, element name, and class.
  162. *
  163. * @return string
  164. */
  165. public function getSessionName()
  166. {
  167. return __CLASS__ . '_' . $this->getSalt() . '_' . $this->getName();
  168. }
  169. /**
  170. * Set timeout for CSRF session token
  171. *
  172. * @param int $ttl
  173. * @return Zend_Form_Element_Hash
  174. */
  175. public function setTimeout($ttl)
  176. {
  177. $this->_timeout = (int) $ttl;
  178. return $this;
  179. }
  180. /**
  181. * Get CSRF session token timeout
  182. *
  183. * @return int
  184. */
  185. public function getTimeout()
  186. {
  187. return $this->_timeout;
  188. }
  189. /**
  190. * Override getLabel() to always be empty
  191. *
  192. * @return null
  193. */
  194. public function getLabel()
  195. {
  196. return null;
  197. }
  198. /**
  199. * Initialize CSRF token in session
  200. *
  201. * @return void
  202. */
  203. public function initCsrfToken()
  204. {
  205. $session = $this->getSession();
  206. $session->setExpirationHops(1, null, true);
  207. $session->setExpirationSeconds($this->getTimeout());
  208. $session->hash = $this->getHash();
  209. }
  210. /**
  211. * Render CSRF token in form
  212. *
  213. * @param Zend_View_Interface $view
  214. * @return string
  215. */
  216. public function render(Zend_View_Interface $view = null)
  217. {
  218. $this->initCsrfToken();
  219. return parent::render($view);
  220. }
  221. /**
  222. * Generate CSRF token
  223. *
  224. * Generates CSRF token and stores both in {@link $_hash} and element
  225. * value.
  226. *
  227. * @return void
  228. */
  229. protected function _generateHash()
  230. {
  231. $this->_hash = md5(
  232. Zend_Crypt_Math::randBytes(32)
  233. );
  234. $this->setValue($this->_hash);
  235. }
  236. }