ValidatingArrayLoader.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Package\Loader;
  12. use Composer\Package;
  13. use Composer\Package\BasePackage;
  14. use Composer\Semver\Constraint\Constraint;
  15. use Composer\Package\Version\VersionParser;
  16. use Composer\Repository\PlatformRepository;
  17. /**
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class ValidatingArrayLoader implements LoaderInterface
  21. {
  22. const CHECK_ALL = 3;
  23. const CHECK_UNBOUND_CONSTRAINTS = 1;
  24. const CHECK_STRICT_CONSTRAINTS = 2;
  25. private $loader;
  26. private $versionParser;
  27. private $errors;
  28. private $warnings;
  29. private $config;
  30. private $strictName;
  31. private $flags;
  32. public function __construct(LoaderInterface $loader, $strictName = true, VersionParser $parser = null, $flags = 0)
  33. {
  34. $this->loader = $loader;
  35. $this->versionParser = $parser ?: new VersionParser();
  36. $this->strictName = $strictName;
  37. $this->flags = $flags;
  38. }
  39. public function load(array $config, $class = 'Composer\Package\CompletePackage')
  40. {
  41. $this->errors = array();
  42. $this->warnings = array();
  43. $this->config = $config;
  44. if ($this->strictName) {
  45. $this->validateRegex('name', '[A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*', true);
  46. } else {
  47. $this->validateString('name', true);
  48. }
  49. if (!empty($this->config['version'])) {
  50. try {
  51. $this->versionParser->normalize($this->config['version']);
  52. } catch (\Exception $e) {
  53. $this->errors[] = 'version : invalid value ('.$this->config['version'].'): '.$e->getMessage();
  54. unset($this->config['version']);
  55. }
  56. }
  57. $this->validateRegex('type', '[A-Za-z0-9-]+');
  58. $this->validateString('target-dir');
  59. $this->validateArray('extra');
  60. $this->validateFlatArray('bin');
  61. $this->validateArray('scripts'); // TODO validate event names & listener syntax
  62. $this->validateString('description');
  63. $this->validateUrl('homepage');
  64. $this->validateFlatArray('keywords', '[\p{N}\p{L} ._-]+');
  65. if (isset($this->config['license'])) {
  66. if (is_string($this->config['license'])) {
  67. $this->validateRegex('license', '[A-Za-z0-9+. ()-]+');
  68. } else {
  69. $this->validateFlatArray('license', '[A-Za-z0-9+. ()-]+');
  70. }
  71. }
  72. $this->validateString('time');
  73. if (!empty($this->config['time'])) {
  74. try {
  75. $date = new \DateTime($this->config['time'], new \DateTimeZone('UTC'));
  76. } catch (\Exception $e) {
  77. $this->errors[] = 'time : invalid value ('.$this->config['time'].'): '.$e->getMessage();
  78. unset($this->config['time']);
  79. }
  80. }
  81. if ($this->validateArray('authors') && !empty($this->config['authors'])) {
  82. foreach ($this->config['authors'] as $key => $author) {
  83. if (!is_array($author)) {
  84. $this->errors[] = 'authors.'.$key.' : should be an array, '.gettype($author).' given';
  85. unset($this->config['authors'][$key]);
  86. continue;
  87. }
  88. foreach (array('homepage', 'email', 'name', 'role') as $authorData) {
  89. if (isset($author[$authorData]) && !is_string($author[$authorData])) {
  90. $this->errors[] = 'authors.'.$key.'.'.$authorData.' : invalid value, must be a string';
  91. unset($this->config['authors'][$key][$authorData]);
  92. }
  93. }
  94. if (isset($author['homepage']) && !$this->filterUrl($author['homepage'])) {
  95. $this->warnings[] = 'authors.'.$key.'.homepage : invalid value ('.$author['homepage'].'), must be an http/https URL';
  96. unset($this->config['authors'][$key]['homepage']);
  97. }
  98. if (isset($author['email']) && !filter_var($author['email'], FILTER_VALIDATE_EMAIL)) {
  99. $this->warnings[] = 'authors.'.$key.'.email : invalid value ('.$author['email'].'), must be a valid email address';
  100. unset($this->config['authors'][$key]['email']);
  101. }
  102. if (empty($this->config['authors'][$key])) {
  103. unset($this->config['authors'][$key]);
  104. }
  105. }
  106. if (empty($this->config['authors'])) {
  107. unset($this->config['authors']);
  108. }
  109. }
  110. if ($this->validateArray('support') && !empty($this->config['support'])) {
  111. foreach (array('issues', 'forum', 'wiki', 'source', 'email', 'irc', 'docs') as $key) {
  112. if (isset($this->config['support'][$key]) && !is_string($this->config['support'][$key])) {
  113. $this->errors[] = 'support.'.$key.' : invalid value, must be a string';
  114. unset($this->config['support'][$key]);
  115. }
  116. }
  117. if (isset($this->config['support']['email']) && !filter_var($this->config['support']['email'], FILTER_VALIDATE_EMAIL)) {
  118. $this->warnings[] = 'support.email : invalid value ('.$this->config['support']['email'].'), must be a valid email address';
  119. unset($this->config['support']['email']);
  120. }
  121. if (isset($this->config['support']['irc']) && !$this->filterUrl($this->config['support']['irc'], array('irc'))) {
  122. $this->warnings[] = 'support.irc : invalid value ('.$this->config['support']['irc'].'), must be a irc://<server>/<channel> URL';
  123. unset($this->config['support']['irc']);
  124. }
  125. foreach (array('issues', 'forum', 'wiki', 'source', 'docs') as $key) {
  126. if (isset($this->config['support'][$key]) && !$this->filterUrl($this->config['support'][$key])) {
  127. $this->warnings[] = 'support.'.$key.' : invalid value ('.$this->config['support'][$key].'), must be an http/https URL';
  128. unset($this->config['support'][$key]);
  129. }
  130. }
  131. if (empty($this->config['support'])) {
  132. unset($this->config['support']);
  133. }
  134. }
  135. $unboundConstraint = new Constraint('=', $this->versionParser->normalize('dev-master'));
  136. foreach (array_keys(BasePackage::$supportedLinkTypes) as $linkType) {
  137. if ($this->validateArray($linkType) && isset($this->config[$linkType])) {
  138. foreach ($this->config[$linkType] as $package => $constraint) {
  139. if (!preg_match('{^[A-Za-z0-9_./-]+$}', $package)) {
  140. $this->warnings[] = $linkType.'.'.$package.' : invalid key, package names must be strings containing only [A-Za-z0-9_./-]';
  141. }
  142. if (!is_string($constraint)) {
  143. $this->errors[] = $linkType.'.'.$package.' : invalid value, must be a string containing a version constraint';
  144. unset($this->config[$linkType][$package]);
  145. } elseif ('self.version' !== $constraint) {
  146. try {
  147. $linkConstraint = $this->versionParser->parseConstraints($constraint);
  148. } catch (\Exception $e) {
  149. $this->errors[] = $linkType.'.'.$package.' : invalid version constraint ('.$e->getMessage().')';
  150. unset($this->config[$linkType][$package]);
  151. continue;
  152. }
  153. // check requires for unbound constraints on non-platform packages
  154. if (
  155. ($this->flags & self::CHECK_UNBOUND_CONSTRAINTS)
  156. && 'require' === $linkType
  157. && $linkConstraint->matches($unboundConstraint)
  158. && !preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $package)
  159. ) {
  160. $this->warnings[] = $linkType.'.'.$package.' : unbound version constraints ('.$constraint.') should be avoided';
  161. } elseif (
  162. // check requires for exact constraints
  163. ($this->flags & self::CHECK_STRICT_CONSTRAINTS)
  164. && 'require' === $linkType
  165. && substr($linkConstraint, 0, 1) === '='
  166. ) {
  167. $this->warnings[] = $linkType.'.'.$package.' : exact version constraints ('.$constraint.') should be avoided if the package follows semantic versioning';
  168. }
  169. }
  170. }
  171. }
  172. }
  173. if ($this->validateArray('suggest') && !empty($this->config['suggest'])) {
  174. foreach ($this->config['suggest'] as $package => $description) {
  175. if (!is_string($description)) {
  176. $this->errors[] = 'suggest.'.$package.' : invalid value, must be a string describing why the package is suggested';
  177. unset($this->config['suggest'][$package]);
  178. }
  179. }
  180. }
  181. if ($this->validateString('minimum-stability') && !empty($this->config['minimum-stability'])) {
  182. if (!isset(BasePackage::$stabilities[$this->config['minimum-stability']])) {
  183. $this->errors[] = 'minimum-stability : invalid value ('.$this->config['minimum-stability'].'), must be one of '.implode(', ', array_keys(BasePackage::$stabilities));
  184. unset($this->config['minimum-stability']);
  185. }
  186. }
  187. if ($this->validateArray('autoload') && !empty($this->config['autoload'])) {
  188. $types = array('psr-0', 'psr-4', 'classmap', 'files', 'exclude-from-classmap');
  189. foreach ($this->config['autoload'] as $type => $typeConfig) {
  190. if (!in_array($type, $types)) {
  191. $this->errors[] = 'autoload : invalid value ('.$type.'), must be one of '.implode(', ', $types);
  192. unset($this->config['autoload'][$type]);
  193. }
  194. if ($type === 'psr-4') {
  195. foreach ($typeConfig as $namespace => $dirs) {
  196. if ($namespace !== '' && '\\' !== substr($namespace, -1)) {
  197. $this->errors[] = 'autoload.psr-4 : invalid value ('.$namespace.'), namespaces must end with a namespace separator, should be '.$namespace.'\\\\';
  198. }
  199. }
  200. }
  201. }
  202. }
  203. if (!empty($this->config['autoload']['psr-4']) && !empty($this->config['target-dir'])) {
  204. $this->errors[] = 'target-dir : this can not be used together with the autoload.psr-4 setting, remove target-dir to upgrade to psr-4';
  205. // Unset the psr-4 setting, since unsetting target-dir might
  206. // interfere with other settings.
  207. unset($this->config['autoload']['psr-4']);
  208. }
  209. // TODO validate dist
  210. // TODO validate source
  211. // TODO validate repositories
  212. // TODO validate package repositories' packages using this recursively
  213. $this->validateFlatArray('include-path');
  214. $this->validateArray('transport-options');
  215. // branch alias validation
  216. if (isset($this->config['extra']['branch-alias'])) {
  217. if (!is_array($this->config['extra']['branch-alias'])) {
  218. $this->errors[] = 'extra.branch-alias : must be an array of versions => aliases';
  219. } else {
  220. foreach ($this->config['extra']['branch-alias'] as $sourceBranch => $targetBranch) {
  221. // ensure it is an alias to a -dev package
  222. if ('-dev' !== substr($targetBranch, -4)) {
  223. $this->warnings[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.$targetBranch.') must end in -dev';
  224. unset($this->config['extra']['branch-alias'][$sourceBranch]);
  225. continue;
  226. }
  227. // normalize without -dev and ensure it's a numeric branch that is parseable
  228. $validatedTargetBranch = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4));
  229. if ('-dev' !== substr($validatedTargetBranch, -4)) {
  230. $this->warnings[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.$targetBranch.') must be a parseable number like 2.0-dev';
  231. unset($this->config['extra']['branch-alias'][$sourceBranch]);
  232. continue;
  233. }
  234. // If using numeric aliases ensure the alias is a valid subversion
  235. if (($sourcePrefix = $this->versionParser->parseNumericAliasPrefix($sourceBranch))
  236. && ($targetPrefix = $this->versionParser->parseNumericAliasPrefix($targetBranch))
  237. && (stripos($targetPrefix, $sourcePrefix) !== 0)
  238. ) {
  239. $this->warnings[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.$targetBranch.') is not a valid numeric alias for this version';
  240. unset($this->config['extra']['branch-alias'][$sourceBranch]);
  241. }
  242. }
  243. }
  244. }
  245. if ($this->errors) {
  246. throw new InvalidPackageException($this->errors, $this->warnings, $config);
  247. }
  248. $package = $this->loader->load($this->config, $class);
  249. $this->config = null;
  250. return $package;
  251. }
  252. public function getWarnings()
  253. {
  254. return $this->warnings;
  255. }
  256. public function getErrors()
  257. {
  258. return $this->errors;
  259. }
  260. private function validateRegex($property, $regex, $mandatory = false)
  261. {
  262. if (!$this->validateString($property, $mandatory)) {
  263. return false;
  264. }
  265. if (!preg_match('{^'.$regex.'$}u', $this->config[$property])) {
  266. $message = $property.' : invalid value ('.$this->config[$property].'), must match '.$regex;
  267. if ($mandatory) {
  268. $this->errors[] = $message;
  269. } else {
  270. $this->warnings[] = $message;
  271. }
  272. unset($this->config[$property]);
  273. return false;
  274. }
  275. return true;
  276. }
  277. private function validateString($property, $mandatory = false)
  278. {
  279. if (isset($this->config[$property]) && !is_string($this->config[$property])) {
  280. $this->errors[] = $property.' : should be a string, '.gettype($this->config[$property]).' given';
  281. unset($this->config[$property]);
  282. return false;
  283. }
  284. if (!isset($this->config[$property]) || trim($this->config[$property]) === '') {
  285. if ($mandatory) {
  286. $this->errors[] = $property.' : must be present';
  287. }
  288. unset($this->config[$property]);
  289. return false;
  290. }
  291. return true;
  292. }
  293. private function validateArray($property, $mandatory = false)
  294. {
  295. if (isset($this->config[$property]) && !is_array($this->config[$property])) {
  296. $this->errors[] = $property.' : should be an array, '.gettype($this->config[$property]).' given';
  297. unset($this->config[$property]);
  298. return false;
  299. }
  300. if (!isset($this->config[$property]) || !count($this->config[$property])) {
  301. if ($mandatory) {
  302. $this->errors[] = $property.' : must be present and contain at least one element';
  303. }
  304. unset($this->config[$property]);
  305. return false;
  306. }
  307. return true;
  308. }
  309. private function validateFlatArray($property, $regex = null, $mandatory = false)
  310. {
  311. if (!$this->validateArray($property, $mandatory)) {
  312. return false;
  313. }
  314. $pass = true;
  315. foreach ($this->config[$property] as $key => $value) {
  316. if (!is_string($value) && !is_numeric($value)) {
  317. $this->errors[] = $property.'.'.$key.' : must be a string or int, '.gettype($value).' given';
  318. unset($this->config[$property][$key]);
  319. $pass = false;
  320. continue;
  321. }
  322. if ($regex && !preg_match('{^'.$regex.'$}u', $value)) {
  323. $this->warnings[] = $property.'.'.$key.' : invalid value ('.$value.'), must match '.$regex;
  324. unset($this->config[$property][$key]);
  325. $pass = false;
  326. }
  327. }
  328. return $pass;
  329. }
  330. private function validateUrl($property, $mandatory = false)
  331. {
  332. if (!$this->validateString($property, $mandatory)) {
  333. return false;
  334. }
  335. if (!$this->filterUrl($this->config[$property])) {
  336. $this->warnings[] = $property.' : invalid value ('.$this->config[$property].'), must be an http/https URL';
  337. unset($this->config[$property]);
  338. return false;
  339. }
  340. return true;
  341. }
  342. private function filterUrl($value, array $schemes = array('http', 'https'))
  343. {
  344. if ($value === '') {
  345. return true;
  346. }
  347. $bits = parse_url($value);
  348. if (empty($bits['scheme']) || empty($bits['host'])) {
  349. return false;
  350. }
  351. if (!in_array($bits['scheme'], $schemes, true)) {
  352. return false;
  353. }
  354. return true;
  355. }
  356. }