Drupal investigation

UuidValidator.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\ConstraintValidator;
  14. use Symfony\Component\Validator\Constraints\Deprecated\UuidValidator as Deprecated;
  15. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  16. /**
  17. * Validates whether the value is a valid UUID per RFC 4122.
  18. *
  19. * @author Colin O'Dell <colinodell@gmail.com>
  20. * @author Bernhard Schussek <bschussek@gmail.com>
  21. *
  22. * @see http://tools.ietf.org/html/rfc4122
  23. * @see https://en.wikipedia.org/wiki/Universally_unique_identifier
  24. */
  25. class UuidValidator extends ConstraintValidator
  26. {
  27. // The strict pattern matches UUIDs like this:
  28. // xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
  29. // Roughly speaking:
  30. // x = any hexadecimal character
  31. // M = any allowed version {1..5}
  32. // N = any allowed variant {8, 9, a, b}
  33. const STRICT_LENGTH = 36;
  34. const STRICT_FIRST_HYPHEN_POSITION = 8;
  35. const STRICT_LAST_HYPHEN_POSITION = 23;
  36. const STRICT_VERSION_POSITION = 14;
  37. const STRICT_VARIANT_POSITION = 19;
  38. // The loose pattern validates similar yet non-compliant UUIDs.
  39. // Hyphens are completely optional. If present, they should only appear
  40. // between every fourth character:
  41. // xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx
  42. // xxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxx
  43. // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  44. // The value can also be wrapped with characters like []{}:
  45. // {xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx}
  46. // Neither the version nor the variant is validated by this pattern.
  47. const LOOSE_MAX_LENGTH = 39;
  48. const LOOSE_FIRST_HYPHEN_POSITION = 4;
  49. /**
  50. * @deprecated since version 2.6, to be removed in 3.0
  51. */
  52. const STRICT_PATTERN = '/^[a-f0-9]{8}-[a-f0-9]{4}-[%s][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i';
  53. /**
  54. * @deprecated since version 2.6, to be removed in 3.0
  55. */
  56. const LOOSE_PATTERN = '/^[a-f0-9]{4}(?:-?[a-f0-9]{4}){7}$/i';
  57. /**
  58. * @deprecated since version 2.6, to be removed in 3.0
  59. */
  60. const STRICT_UUID_LENGTH = 36;
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function validate($value, Constraint $constraint)
  65. {
  66. if (null === $value || '' === $value) {
  67. return;
  68. }
  69. if (!$constraint instanceof Uuid) {
  70. throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Uuid');
  71. }
  72. if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
  73. throw new UnexpectedTypeException($value, 'string');
  74. }
  75. $value = (string) $value;
  76. if ($constraint->strict) {
  77. $this->validateStrict($value, $constraint);
  78. return;
  79. }
  80. $this->validateLoose($value, $constraint);
  81. }
  82. private function validateLoose($value, Uuid $constraint)
  83. {
  84. // Error priority:
  85. // 1. ERROR_INVALID_CHARACTERS
  86. // 2. ERROR_INVALID_HYPHEN_PLACEMENT
  87. // 3. ERROR_TOO_SHORT/ERROR_TOO_LONG
  88. // Trim any wrapping characters like [] or {} used by some legacy systems
  89. $trimmed = trim($value, '[]{}');
  90. // Position of the next expected hyphen
  91. $h = self::LOOSE_FIRST_HYPHEN_POSITION;
  92. // Expected length
  93. $l = self::LOOSE_MAX_LENGTH;
  94. for ($i = 0; $i < $l; ++$i) {
  95. // Check length
  96. if (!isset($trimmed[$i])) {
  97. if ($this->context instanceof ExecutionContextInterface) {
  98. $this->context->buildViolation($constraint->message)
  99. ->setParameter('{{ value }}', $this->formatValue($value))
  100. ->setCode(Uuid::TOO_SHORT_ERROR)
  101. ->addViolation();
  102. } else {
  103. $this->buildViolation($constraint->message)
  104. ->setParameter('{{ value }}', $this->formatValue($value))
  105. ->setCode(Uuid::TOO_SHORT_ERROR)
  106. ->addViolation();
  107. }
  108. return;
  109. }
  110. // Hyphens must occur every fifth position
  111. // xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx
  112. // ^ ^ ^ ^ ^ ^ ^
  113. if ('-' === $trimmed[$i]) {
  114. if ($i !== $h) {
  115. if ($this->context instanceof ExecutionContextInterface) {
  116. $this->context->buildViolation($constraint->message)
  117. ->setParameter('{{ value }}', $this->formatValue($value))
  118. ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
  119. ->addViolation();
  120. } else {
  121. $this->buildViolation($constraint->message)
  122. ->setParameter('{{ value }}', $this->formatValue($value))
  123. ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
  124. ->addViolation();
  125. }
  126. return;
  127. }
  128. $h += 5;
  129. continue;
  130. }
  131. // Missing hyphens are ignored
  132. if ($i === $h) {
  133. $h += 4;
  134. --$l;
  135. }
  136. // Check characters
  137. if (!ctype_xdigit($trimmed[$i])) {
  138. if ($this->context instanceof ExecutionContextInterface) {
  139. $this->context->buildViolation($constraint->message)
  140. ->setParameter('{{ value }}', $this->formatValue($value))
  141. ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
  142. ->addViolation();
  143. } else {
  144. $this->buildViolation($constraint->message)
  145. ->setParameter('{{ value }}', $this->formatValue($value))
  146. ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
  147. ->addViolation();
  148. }
  149. return;
  150. }
  151. }
  152. // Check length again
  153. if (isset($trimmed[$i])) {
  154. if ($this->context instanceof ExecutionContextInterface) {
  155. $this->context->buildViolation($constraint->message)
  156. ->setParameter('{{ value }}', $this->formatValue($value))
  157. ->setCode(Uuid::TOO_LONG_ERROR)
  158. ->addViolation();
  159. } else {
  160. $this->buildViolation($constraint->message)
  161. ->setParameter('{{ value }}', $this->formatValue($value))
  162. ->setCode(Uuid::TOO_LONG_ERROR)
  163. ->addViolation();
  164. }
  165. }
  166. }
  167. private function validateStrict($value, Uuid $constraint)
  168. {
  169. // Error priority:
  170. // 1. ERROR_INVALID_CHARACTERS
  171. // 2. ERROR_INVALID_HYPHEN_PLACEMENT
  172. // 3. ERROR_TOO_SHORT/ERROR_TOO_LONG
  173. // 4. ERROR_INVALID_VERSION
  174. // 5. ERROR_INVALID_VARIANT
  175. // Position of the next expected hyphen
  176. $h = self::STRICT_FIRST_HYPHEN_POSITION;
  177. for ($i = 0; $i < self::STRICT_LENGTH; ++$i) {
  178. // Check length
  179. if (!isset($value[$i])) {
  180. if ($this->context instanceof ExecutionContextInterface) {
  181. $this->context->buildViolation($constraint->message)
  182. ->setParameter('{{ value }}', $this->formatValue($value))
  183. ->setCode(Uuid::TOO_SHORT_ERROR)
  184. ->addViolation();
  185. } else {
  186. $this->buildViolation($constraint->message)
  187. ->setParameter('{{ value }}', $this->formatValue($value))
  188. ->setCode(Uuid::TOO_SHORT_ERROR)
  189. ->addViolation();
  190. }
  191. return;
  192. }
  193. // Check hyphen placement
  194. // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  195. // ^ ^ ^ ^
  196. if ('-' === $value[$i]) {
  197. if ($i !== $h) {
  198. if ($this->context instanceof ExecutionContextInterface) {
  199. $this->context->buildViolation($constraint->message)
  200. ->setParameter('{{ value }}', $this->formatValue($value))
  201. ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
  202. ->addViolation();
  203. } else {
  204. $this->buildViolation($constraint->message)
  205. ->setParameter('{{ value }}', $this->formatValue($value))
  206. ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
  207. ->addViolation();
  208. }
  209. return;
  210. }
  211. // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  212. // ^
  213. if ($h < self::STRICT_LAST_HYPHEN_POSITION) {
  214. $h += 5;
  215. }
  216. continue;
  217. }
  218. // Check characters
  219. if (!ctype_xdigit($value[$i])) {
  220. if ($this->context instanceof ExecutionContextInterface) {
  221. $this->context->buildViolation($constraint->message)
  222. ->setParameter('{{ value }}', $this->formatValue($value))
  223. ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
  224. ->addViolation();
  225. } else {
  226. $this->buildViolation($constraint->message)
  227. ->setParameter('{{ value }}', $this->formatValue($value))
  228. ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
  229. ->addViolation();
  230. }
  231. return;
  232. }
  233. // Missing hyphen
  234. if ($i === $h) {
  235. if ($this->context instanceof ExecutionContextInterface) {
  236. $this->context->buildViolation($constraint->message)
  237. ->setParameter('{{ value }}', $this->formatValue($value))
  238. ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
  239. ->addViolation();
  240. } else {
  241. $this->buildViolation($constraint->message)
  242. ->setParameter('{{ value }}', $this->formatValue($value))
  243. ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
  244. ->addViolation();
  245. }
  246. return;
  247. }
  248. }
  249. // Check length again
  250. if (isset($value[$i])) {
  251. if ($this->context instanceof ExecutionContextInterface) {
  252. $this->context->buildViolation($constraint->message)
  253. ->setParameter('{{ value }}', $this->formatValue($value))
  254. ->setCode(Uuid::TOO_LONG_ERROR)
  255. ->addViolation();
  256. } else {
  257. $this->buildViolation($constraint->message)
  258. ->setParameter('{{ value }}', $this->formatValue($value))
  259. ->setCode(Uuid::TOO_LONG_ERROR)
  260. ->addViolation();
  261. }
  262. }
  263. // Check version
  264. if (!in_array($value[self::STRICT_VERSION_POSITION], $constraint->versions)) {
  265. if ($this->context instanceof ExecutionContextInterface) {
  266. $this->context->buildViolation($constraint->message)
  267. ->setParameter('{{ value }}', $this->formatValue($value))
  268. ->setCode(Uuid::INVALID_VERSION_ERROR)
  269. ->addViolation();
  270. } else {
  271. $this->buildViolation($constraint->message)
  272. ->setParameter('{{ value }}', $this->formatValue($value))
  273. ->setCode(Uuid::INVALID_VERSION_ERROR)
  274. ->addViolation();
  275. }
  276. }
  277. // Check variant - first two bits must equal "10"
  278. // 0b10xx
  279. // & 0b1100 (12)
  280. // = 0b1000 (8)
  281. if ((hexdec($value[self::STRICT_VARIANT_POSITION]) & 12) !== 8) {
  282. if ($this->context instanceof ExecutionContextInterface) {
  283. $this->context->buildViolation($constraint->message)
  284. ->setParameter('{{ value }}', $this->formatValue($value))
  285. ->setCode(Uuid::INVALID_VARIANT_ERROR)
  286. ->addViolation();
  287. } else {
  288. $this->buildViolation($constraint->message)
  289. ->setParameter('{{ value }}', $this->formatValue($value))
  290. ->setCode(Uuid::INVALID_VARIANT_ERROR)
  291. ->addViolation();
  292. }
  293. }
  294. }
  295. }