PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/reference/validation/BaseValidationRule.php

http://owasp-esapi-php.googlecode.com/
PHP | 275 lines | 114 code | 25 blank | 136 comment | 19 complexity | 9096f94eb8e1ab22197827d3cc103925 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. <?php
  2. /**
  3. * OWASP Enterprise Security API (ESAPI)
  4. *
  5. * This file is part of the Open Web Application Security Project (OWASP)
  6. * Enterprise Security API (ESAPI) project.
  7. *
  8. * PHP version 5.2
  9. *
  10. * LICENSE: This source file is subject to the New BSD license. You should read
  11. * and accept the LICENSE before you use, modify, and/or redistribute this
  12. * software.
  13. *
  14. * @category OWASP
  15. * @package ESAPI_Reference_Validation
  16. * @author Johannes B. Ullrich <jullrich@sans.edu>
  17. * @author jah <jah@jahboite.co.uk>
  18. * @author Mike Boberski <boberski_michael@bah.com>
  19. * @copyright 2009-2010 The OWASP Foundation
  20. * @license http://www.opensource.org/licenses/bsd-license.php New BSD license
  21. * @version SVN: $Id$
  22. * @link http://www.owasp.org/index.php/ESAPI
  23. */
  24. /**
  25. * BaseValidationRule requires the ValidationRule Interface and DefaultEncoder
  26. * for canonicalization.
  27. */
  28. require_once dirname(__FILE__) . '/../../ValidationRule.php';
  29. require_once dirname(__FILE__) . '/../DefaultEncoder.php';
  30. /**
  31. * Reference Implementation of the ValidationRule interface.
  32. *
  33. * @category OWASP
  34. * @package ESAPI_Reference_Validation
  35. * @author Johannes B. Ullrich <jullrich@sans.edu>
  36. * @author jah <jah@jahboite.co.uk>
  37. * @author Mike Boberski <boberski_michael@bah.com>
  38. * @copyright 2009-2010 The OWASP Foundation
  39. * @license http://www.opensource.org/licenses/bsd-license.php New BSD license
  40. * @version Release: @package_version@
  41. * @link http://www.owasp.org/index.php/ESAPI
  42. */
  43. abstract class BaseValidationRule implements ValidationRule
  44. {
  45. protected $typeName = null;
  46. protected $encoder = null;
  47. protected $allowNull = false;
  48. /**
  49. * Stores an instance of an Encoder implementation (e.g. DefaultEncoder) to
  50. * be used for canonicalization and a name for the type of Input to be
  51. * validated (e.g. 'Date' or 'CreditCardNumber').
  52. *
  53. * @param string $typeName type name of the input to be validated.
  54. * @param Encoder $encoder instance of an Encoder implementation.
  55. *
  56. * @return does not return a value.
  57. */
  58. protected function __construct($typeName, $encoder)
  59. {
  60. global $ESAPI;
  61. if ($encoder instanceof Encoder) {
  62. $this->encoder = $encoder;
  63. } else {
  64. $this->encoder = new DefaultEncoder();
  65. }
  66. $this->typeName = $typeName;
  67. }
  68. /**
  69. * Sets the boolean allowNull property which, if set true, will allow empty
  70. * inputs to validate as true.
  71. *
  72. * @param bool $flag set to true if empty inputs should validate as true.
  73. *
  74. * @return does not return a value.
  75. */
  76. public function setAllowNull($flag)
  77. {
  78. if ($flag === true) {
  79. $this->allowNull = true;
  80. } else {
  81. $this->allowNull = false;
  82. }
  83. }
  84. /**
  85. * Gets the boolean allowNull property which, if set true, will allow empty
  86. * inputs to validate as true.
  87. *
  88. * @return bool returns true if empty inputs should validate as true, false
  89. * otherwise.
  90. */
  91. public function getAllowNull()
  92. {
  93. return $this->allowNull;
  94. }
  95. /**
  96. * Sets a descriptive name for the validator e.g. CreditCardNumber.
  97. * If $typeName is empty or not a string then a default value will be set.
  98. *
  99. * @param string $typeName name describing the validator.
  100. *
  101. * @return does not return a value.
  102. */
  103. public function setTypeName($typeName)
  104. {
  105. if (! is_string($typeName) || $typeName == '') {
  106. $typeName = 'GenericValidator';
  107. }
  108. $this->typeName = $typeName;
  109. }
  110. /**
  111. * Gets the descriptive name for the validator.
  112. *
  113. * @return string name describing the validator.
  114. */
  115. public function getTypeName()
  116. {
  117. return $this->typeName;
  118. }
  119. /**
  120. * Sets an instance of an encoder class which should provide a
  121. * canonicalize method.
  122. * TODO should ensure that a canonicalize method is available or should
  123. * only allow instances of Encoder implementations...
  124. *
  125. * @param Encoder $encoder object which provides a canonicalize method.
  126. *
  127. * @return does not return a value.
  128. * @throws InvalidArgumentException if encoder is missing a canonicalize method
  129. */
  130. final public function setEncoder($encoder)
  131. {
  132. if ( ! is_object($encoder)
  133. || ! method_exists($encoder, 'canonicalize')
  134. ) {
  135. throw new InvalidArgumentException(
  136. 'expected $encoder to be an object providing a canonicalize method'
  137. );
  138. }
  139. $this->encoder = $encoder;
  140. }
  141. /**
  142. * Asserts that the supplied $input is valid after canonicalization. Invalid
  143. * Inputs will cause a descriptive ValidationException to be thrown. Inputs
  144. * that are obviously an attack will cause an IntrusionException.
  145. *
  146. * @param string $context A descriptive name of the parameter that you are
  147. * validating (e.g., LoginPage_UsernameField). This
  148. * value is used by any logging or error handling that
  149. * is done with respect to the value passed in.
  150. * @param string $input The actual user input data to validate.
  151. *
  152. * @return does not return a value.
  153. */
  154. public function assertValid($context, $input)
  155. {
  156. $this->getValid($context, $input);
  157. }
  158. /**
  159. * Attempts to return valid canonicalized input. If a ValidationException
  160. * is thrown, this method will return sanitized input which may or may not
  161. * have any similarity to the original input.
  162. *
  163. * @param string $context A descriptive name of the parameter that you are
  164. * validating (e.g., LoginPage_UsernameField). This
  165. * value is used by any logging or error handling that
  166. * is done with respect to the value passed in.
  167. * @param string $input The actual user input data to validate.
  168. *
  169. * @return string valid, canonicalized input or sanitized input or a default
  170. * value.
  171. */
  172. public function getSafe($context, $input)
  173. {
  174. $safe = null;
  175. try
  176. {
  177. $safe = $this->getValid($context, $input);
  178. }
  179. catch (ValidationException$e )
  180. {
  181. $safe = $this->sanitize($context, $input);
  182. }
  183. return $safe;
  184. }
  185. /**
  186. * Returns boolean true if the input is valid, false otherwise.
  187. *
  188. * @param string $context A descriptive name of the parameter that you are
  189. * validating (e.g., LoginPage_UsernameField). This
  190. * value is used by any logging or error handling that
  191. * is done with respect to the value passed in.
  192. * @param string $input The actual user input data to validate.
  193. *
  194. * @return bool true if the input is valid, false otherwise.
  195. */
  196. public function isValid($context, $input)
  197. {
  198. try
  199. {
  200. $this->getValid($context, $input);
  201. return true;
  202. }
  203. catch (Exception $e)
  204. {
  205. return false;
  206. }
  207. }
  208. /**
  209. * Returns the supplied input string after removing any characters not
  210. * present in the supplied whitelist.
  211. *
  212. * @param string $input string input to be filtered.
  213. * @param string $whitelist array or string of whitelist characters.
  214. *
  215. * @return string returns characters from $input that are present in $whitelist.
  216. */
  217. public function whitelist($input, $whitelist)
  218. {
  219. // Sanity check
  220. if (! is_string($input) || $input == '') {
  221. $input = '';
  222. }
  223. if (is_string($whitelist)) {
  224. $charEnc = Codec::detectEncoding($whitelist);
  225. $limit = mb_strlen($whitelist, $charEnc);
  226. $ary = array();
  227. for ($i = 0; $i < $limit; $i++) {
  228. $ary[] = mb_substr($whitelist, $i, 1, $charEnc);
  229. }
  230. $whitelist = $ary;
  231. }
  232. $filtered = '';
  233. $initialCharEnc = Codec::detectEncoding($input);
  234. $_4ByteCharacterString = Codec::normalizeEncoding($input);
  235. $limit = mb_strlen($_4ByteCharacterString, 'UTF-32');
  236. for ($i = 0; $i < $limit; $i++) {
  237. $c = mb_substr($_4ByteCharacterString, $i, 1, 'UTF-32');
  238. if (Codec::containsCharacter($c, $whitelist)) {
  239. $filtered .= $c;
  240. }
  241. }
  242. if ($filtered != '') {
  243. $filtered = mb_convert_encoding($filtered, $initialCharEnc, 'UTF-32');
  244. }
  245. if (! is_string($filtered)) {
  246. $filtered = '';
  247. }
  248. return $filtered;
  249. }
  250. }