RemoteFilesystem.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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\Util;
  12. use Composer\Config;
  13. use Composer\IO\IOInterface;
  14. use Composer\Downloader\TransportException;
  15. use Composer\CaBundle\CaBundle;
  16. use Psr\Log\LoggerInterface;
  17. /**
  18. * @author François Pluchino <francois.pluchino@opendisplay.com>
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. * @author Nils Adermann <naderman@naderman.de>
  21. */
  22. class RemoteFilesystem
  23. {
  24. private $io;
  25. private $config;
  26. private $scheme;
  27. private $bytesMax;
  28. private $originUrl;
  29. private $fileUrl;
  30. private $fileName;
  31. private $retry;
  32. private $progress;
  33. private $lastProgress;
  34. private $options = array();
  35. private $peerCertificateMap = array();
  36. private $disableTls = false;
  37. private $retryAuthFailure;
  38. private $lastHeaders;
  39. private $storeAuth;
  40. private $degradedMode = false;
  41. private $redirects;
  42. private $maxRedirects = 20;
  43. /**
  44. * Constructor.
  45. *
  46. * @param IOInterface $io The IO instance
  47. * @param Config $config The config
  48. * @param array $options The options
  49. * @param bool $disableTls
  50. */
  51. public function __construct(IOInterface $io, Config $config = null, array $options = array(), $disableTls = false)
  52. {
  53. $this->io = $io;
  54. // Setup TLS options
  55. // The cafile option can be set via config.json
  56. if ($disableTls === false) {
  57. $this->options = $this->getTlsDefaults($options);
  58. } else {
  59. $this->disableTls = true;
  60. }
  61. // handle the other externally set options normally.
  62. $this->options = array_replace_recursive($this->options, $options);
  63. $this->config = $config;
  64. }
  65. /**
  66. * Copy the remote file in local.
  67. *
  68. * @param string $originUrl The origin URL
  69. * @param string $fileUrl The file URL
  70. * @param string $fileName the local filename
  71. * @param bool $progress Display the progression
  72. * @param array $options Additional context options
  73. *
  74. * @return bool true
  75. */
  76. public function copy($originUrl, $fileUrl, $fileName, $progress = true, $options = array())
  77. {
  78. return $this->get($originUrl, $fileUrl, $options, $fileName, $progress);
  79. }
  80. /**
  81. * Get the content.
  82. *
  83. * @param string $originUrl The origin URL
  84. * @param string $fileUrl The file URL
  85. * @param bool $progress Display the progression
  86. * @param array $options Additional context options
  87. *
  88. * @return bool|string The content
  89. */
  90. public function getContents($originUrl, $fileUrl, $progress = true, $options = array())
  91. {
  92. return $this->get($originUrl, $fileUrl, $options, null, $progress);
  93. }
  94. /**
  95. * Retrieve the options set in the constructor
  96. *
  97. * @return array Options
  98. */
  99. public function getOptions()
  100. {
  101. return $this->options;
  102. }
  103. /**
  104. * Merges new options
  105. *
  106. * @return array $options
  107. */
  108. public function setOptions(array $options)
  109. {
  110. $this->options = array_replace_recursive($this->options, $options);
  111. }
  112. public function isTlsDisabled()
  113. {
  114. return $this->disableTls === true;
  115. }
  116. /**
  117. * Returns the headers of the last request
  118. *
  119. * @return array
  120. */
  121. public function getLastHeaders()
  122. {
  123. return $this->lastHeaders;
  124. }
  125. /**
  126. * @param array $headers array of returned headers like from getLastHeaders()
  127. * @param string $name header name (case insensitive)
  128. * @return string|null
  129. */
  130. public function findHeaderValue(array $headers, $name)
  131. {
  132. $value = null;
  133. foreach ($headers as $header) {
  134. if (preg_match('{^'.$name.':\s*(.+?)\s*$}i', $header, $match)) {
  135. $value = $match[1];
  136. } elseif (preg_match('{^HTTP/}i', $header)) {
  137. // In case of redirects, http_response_headers contains the headers of all responses
  138. // so we reset the flag when a new response is being parsed as we are only interested in the last response
  139. $value = null;
  140. }
  141. }
  142. return $value;
  143. }
  144. /**
  145. * @param array $headers array of returned headers like from getLastHeaders()
  146. * @return int|null
  147. */
  148. public function findStatusCode(array $headers)
  149. {
  150. $value = null;
  151. foreach ($headers as $header) {
  152. if (preg_match('{^HTTP/\S+ (\d+)}i', $header, $match)) {
  153. // In case of redirects, http_response_headers contains the headers of all responses
  154. // so we can not return directly and need to keep iterating
  155. $value = (int) $match[1];
  156. }
  157. }
  158. return $value;
  159. }
  160. /**
  161. * Get file content or copy action.
  162. *
  163. * @param string $originUrl The origin URL
  164. * @param string $fileUrl The file URL
  165. * @param array $additionalOptions context options
  166. * @param string $fileName the local filename
  167. * @param bool $progress Display the progression
  168. *
  169. * @throws TransportException|\Exception
  170. * @throws TransportException When the file could not be downloaded
  171. *
  172. * @return bool|string
  173. */
  174. protected function get($originUrl, $fileUrl, $additionalOptions = array(), $fileName = null, $progress = true)
  175. {
  176. if (strpos($originUrl, '.github.com') === (strlen($originUrl) - 11)) {
  177. $originUrl = 'github.com';
  178. }
  179. $this->scheme = parse_url($fileUrl, PHP_URL_SCHEME);
  180. $this->bytesMax = 0;
  181. $this->originUrl = $originUrl;
  182. $this->fileUrl = $fileUrl;
  183. $this->fileName = $fileName;
  184. $this->progress = $progress;
  185. $this->lastProgress = null;
  186. $this->retryAuthFailure = true;
  187. $this->lastHeaders = array();
  188. $this->redirects = 1; // The first request counts.
  189. // capture username/password from URL if there is one
  190. if (preg_match('{^https?://(.+):(.+)@([^/]+)}i', $fileUrl, $match)) {
  191. $this->io->setAuthentication($originUrl, urldecode($match[1]), urldecode($match[2]));
  192. }
  193. $tempAdditionalOptions = $additionalOptions;
  194. if (isset($tempAdditionalOptions['retry-auth-failure'])) {
  195. $this->retryAuthFailure = (bool) $tempAdditionalOptions['retry-auth-failure'];
  196. unset($tempAdditionalOptions['retry-auth-failure']);
  197. }
  198. $isRedirect = false;
  199. if (isset($tempAdditionalOptions['redirects'])) {
  200. $this->redirects = $tempAdditionalOptions['redirects'];
  201. $isRedirect = true;
  202. unset($tempAdditionalOptions['redirects']);
  203. }
  204. $options = $this->getOptionsForUrl($originUrl, $tempAdditionalOptions);
  205. unset($tempAdditionalOptions);
  206. $userlandFollow = isset($options['http']['follow_location']) && !$options['http']['follow_location'];
  207. $origFileUrl = $fileUrl;
  208. if (isset($options['github-token'])) {
  209. $fileUrl .= (false === strpos($fileUrl, '?') ? '?' : '&') . 'access_token='.$options['github-token'];
  210. unset($options['github-token']);
  211. }
  212. if (isset($options['gitlab-token'])) {
  213. $fileUrl .= (false === strpos($fileUrl, '?') ? '?' : '&') . 'access_token='.$options['gitlab-token'];
  214. unset($options['gitlab-token']);
  215. }
  216. if (isset($options['http'])) {
  217. $options['http']['ignore_errors'] = true;
  218. }
  219. if ($this->degradedMode && substr($fileUrl, 0, 21) === 'http://packagist.org/') {
  220. // access packagist using the resolved IPv4 instead of the hostname to force IPv4 protocol
  221. $fileUrl = 'http://' . gethostbyname('packagist.org') . substr($fileUrl, 20);
  222. }
  223. $ctx = StreamContextFactory::getContext($fileUrl, $options, array('notification' => array($this, 'callbackGet')));
  224. $actualContextOptions = stream_context_get_options($ctx);
  225. $usingProxy = !empty($actualContextOptions['http']['proxy']) ? ' using proxy ' . $actualContextOptions['http']['proxy'] : '';
  226. $this->io->writeError((substr($origFileUrl, 0, 4) === 'http' ? 'Downloading ' : 'Reading ') . $origFileUrl . $usingProxy, true, IOInterface::DEBUG);
  227. unset($origFileUrl, $actualContextOptions);
  228. if ($this->progress && !$isRedirect) {
  229. $this->io->writeError(" Downloading: <comment>Connecting...</comment>", false);
  230. }
  231. // Check for secure HTTP, but allow insecure Packagist calls to $hashed providers as file integrity is verified with sha256
  232. if ((substr($fileUrl, 0, 23) !== 'http://packagist.org/p/' || (false === strpos($fileUrl, '$') && false === strpos($fileUrl, '%24'))) && $this->config) {
  233. $this->config->prohibitUrlByConfig($fileUrl);
  234. }
  235. $errorMessage = '';
  236. $errorCode = 0;
  237. $result = false;
  238. set_error_handler(function ($code, $msg) use (&$errorMessage) {
  239. if ($errorMessage) {
  240. $errorMessage .= "\n";
  241. }
  242. $errorMessage .= preg_replace('{^file_get_contents\(.*?\): }', '', $msg);
  243. });
  244. try {
  245. $result = file_get_contents($fileUrl, false, $ctx);
  246. if (PHP_VERSION_ID < 50600 && !empty($options['ssl']['peer_fingerprint'])) {
  247. // Emulate fingerprint validation on PHP < 5.6
  248. $params = stream_context_get_params($ctx);
  249. $expectedPeerFingerprint = $options['ssl']['peer_fingerprint'];
  250. $peerFingerprint = TlsHelper::getCertificateFingerprint($params['options']['ssl']['peer_certificate']);
  251. // Constant time compare??!
  252. if ($expectedPeerFingerprint !== $peerFingerprint) {
  253. throw new TransportException('Peer fingerprint did not match');
  254. }
  255. }
  256. } catch (\Exception $e) {
  257. if ($e instanceof TransportException && !empty($http_response_header[0])) {
  258. $e->setHeaders($http_response_header);
  259. $e->setStatusCode($this->findStatusCode($http_response_header));
  260. }
  261. if ($e instanceof TransportException && $result !== false) {
  262. $e->setResponse($result);
  263. }
  264. $result = false;
  265. }
  266. if ($errorMessage && !ini_get('allow_url_fopen')) {
  267. $errorMessage = 'allow_url_fopen must be enabled in php.ini ('.$errorMessage.')';
  268. }
  269. restore_error_handler();
  270. if (isset($e) && !$this->retry) {
  271. if (!$this->degradedMode && false !== strpos($e->getMessage(), 'Operation timed out')) {
  272. $this->degradedMode = true;
  273. $this->io->writeError(array(
  274. '<error>'.$e->getMessage().'</error>',
  275. '<error>Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info</error>',
  276. ));
  277. return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress);
  278. }
  279. throw $e;
  280. }
  281. $statusCode = null;
  282. if (!empty($http_response_header[0])) {
  283. $statusCode = $this->findStatusCode($http_response_header);
  284. }
  285. // handle 3xx redirects for php<5.6, 304 Not Modified is excluded
  286. $hasFollowedRedirect = false;
  287. if ($userlandFollow && $statusCode >= 300 && $statusCode <= 399 && $statusCode !== 304 && $this->redirects < $this->maxRedirects) {
  288. $hasFollowedRedirect = true;
  289. $result = $this->handleRedirect($http_response_header, $additionalOptions, $result);
  290. }
  291. // fail 4xx and 5xx responses and capture the response
  292. if ($statusCode && $statusCode >= 400 && $statusCode <= 599) {
  293. if (!$this->retry) {
  294. if ($this->progress && !$this->retry && !$isRedirect) {
  295. $this->io->overwriteError(" Downloading: <error>Failed</error>");
  296. }
  297. $e = new TransportException('The "'.$this->fileUrl.'" file could not be downloaded ('.$http_response_header[0].')', $statusCode);
  298. $e->setHeaders($http_response_header);
  299. $e->setResponse($result);
  300. $e->setStatusCode($statusCode);
  301. throw $e;
  302. }
  303. $result = false;
  304. }
  305. if ($this->progress && !$this->retry && !$isRedirect) {
  306. $this->io->overwriteError(" Downloading: ".($result === false ? '<error>Failed</error>' : '<comment>100%</comment>'));
  307. }
  308. // decode gzip
  309. if ($result && extension_loaded('zlib') && substr($fileUrl, 0, 4) === 'http' && !$hasFollowedRedirect) {
  310. $decode = 'gzip' === strtolower($this->findHeaderValue($http_response_header, 'content-encoding'));
  311. if ($decode) {
  312. try {
  313. if (PHP_VERSION_ID >= 50400) {
  314. $result = zlib_decode($result);
  315. } else {
  316. // work around issue with gzuncompress & co that do not work with all gzip checksums
  317. $result = file_get_contents('compress.zlib://data:application/octet-stream;base64,'.base64_encode($result));
  318. }
  319. if (!$result) {
  320. throw new TransportException('Failed to decode zlib stream');
  321. }
  322. } catch (\Exception $e) {
  323. if ($this->degradedMode) {
  324. throw $e;
  325. }
  326. $this->degradedMode = true;
  327. $this->io->writeError(array(
  328. '<error>Failed to decode response: '.$e->getMessage().'</error>',
  329. '<error>Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info</error>',
  330. ));
  331. return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress);
  332. }
  333. }
  334. }
  335. // handle copy command if download was successful
  336. if (false !== $result && null !== $fileName && !$isRedirect) {
  337. if ('' === $result) {
  338. throw new TransportException('"'.$this->fileUrl.'" appears broken, and returned an empty 200 response');
  339. }
  340. $errorMessage = '';
  341. set_error_handler(function ($code, $msg) use (&$errorMessage) {
  342. if ($errorMessage) {
  343. $errorMessage .= "\n";
  344. }
  345. $errorMessage .= preg_replace('{^file_put_contents\(.*?\): }', '', $msg);
  346. });
  347. $result = (bool) file_put_contents($fileName, $result);
  348. restore_error_handler();
  349. if (false === $result) {
  350. throw new TransportException('The "'.$this->fileUrl.'" file could not be written to '.$fileName.': '.$errorMessage);
  351. }
  352. }
  353. // Handle SSL cert match issues
  354. if (false === $result && false !== strpos($errorMessage, 'Peer certificate') && PHP_VERSION_ID < 50600) {
  355. // Certificate name error, PHP doesn't support subjectAltName on PHP < 5.6
  356. // The procedure to handle sAN for older PHP's is:
  357. //
  358. // 1. Open socket to remote server and fetch certificate (disabling peer
  359. // validation because PHP errors without giving up the certificate.)
  360. //
  361. // 2. Verifying the domain in the URL against the names in the sAN field.
  362. // If there is a match record the authority [host/port], certificate
  363. // common name, and certificate fingerprint.
  364. //
  365. // 3. Retry the original request but changing the CN_match parameter to
  366. // the common name extracted from the certificate in step 2.
  367. //
  368. // 4. To prevent any attempt at being hoodwinked by switching the
  369. // certificate between steps 2 and 3 the fingerprint of the certificate
  370. // presented in step 3 is compared against the one recorded in step 2.
  371. if (CaBundle::isOpensslParseSafe()) {
  372. $certDetails = $this->getCertificateCnAndFp($this->fileUrl, $options);
  373. if ($certDetails) {
  374. $this->peerCertificateMap[$this->getUrlAuthority($this->fileUrl)] = $certDetails;
  375. $this->retry = true;
  376. }
  377. } else {
  378. $this->io->writeError(sprintf(
  379. '<error>Your version of PHP, %s, is affected by CVE-2013-6420 and cannot safely perform certificate validation, we strongly suggest you upgrade.</error>',
  380. PHP_VERSION
  381. ));
  382. }
  383. }
  384. if ($this->retry) {
  385. $this->retry = false;
  386. $result = $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress);
  387. if ($this->storeAuth && $this->config) {
  388. $authHelper = new AuthHelper($this->io, $this->config);
  389. $authHelper->storeAuth($this->originUrl, $this->storeAuth);
  390. $this->storeAuth = false;
  391. }
  392. return $result;
  393. }
  394. if (false === $result) {
  395. $e = new TransportException('The "'.$this->fileUrl.'" file could not be downloaded: '.$errorMessage, $errorCode);
  396. if (!empty($http_response_header[0])) {
  397. $e->setHeaders($http_response_header);
  398. }
  399. if (!$this->degradedMode && false !== strpos($e->getMessage(), 'Operation timed out')) {
  400. $this->degradedMode = true;
  401. $this->io->writeError(array(
  402. '<error>'.$e->getMessage().'</error>',
  403. '<error>Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info</error>',
  404. ));
  405. return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress);
  406. }
  407. throw $e;
  408. }
  409. if (!empty($http_response_header[0])) {
  410. $this->lastHeaders = $http_response_header;
  411. }
  412. return $result;
  413. }
  414. /**
  415. * Get notification action.
  416. *
  417. * @param int $notificationCode The notification code
  418. * @param int $severity The severity level
  419. * @param string $message The message
  420. * @param int $messageCode The message code
  421. * @param int $bytesTransferred The loaded size
  422. * @param int $bytesMax The total size
  423. * @throws TransportException
  424. */
  425. protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax)
  426. {
  427. switch ($notificationCode) {
  428. case STREAM_NOTIFY_FAILURE:
  429. if (400 === $messageCode) {
  430. // This might happen if your host is secured by ssl client certificate authentication
  431. // but you do not send an appropriate certificate
  432. throw new TransportException("The '" . $this->fileUrl . "' URL could not be accessed: " . $message, $messageCode);
  433. }
  434. // intentional fallthrough to the next case as the notificationCode
  435. // isn't always consistent and we should inspect the messageCode for 401s
  436. case STREAM_NOTIFY_AUTH_REQUIRED:
  437. if (401 === $messageCode) {
  438. // Bail if the caller is going to handle authentication failures itself.
  439. if (!$this->retryAuthFailure) {
  440. break;
  441. }
  442. $this->promptAuthAndRetry($messageCode);
  443. }
  444. break;
  445. case STREAM_NOTIFY_AUTH_RESULT:
  446. if (403 === $messageCode) {
  447. // Bail if the caller is going to handle authentication failures itself.
  448. if (!$this->retryAuthFailure) {
  449. break;
  450. }
  451. $this->promptAuthAndRetry($messageCode, $message);
  452. }
  453. break;
  454. case STREAM_NOTIFY_FILE_SIZE_IS:
  455. if ($this->bytesMax < $bytesMax) {
  456. $this->bytesMax = $bytesMax;
  457. }
  458. break;
  459. case STREAM_NOTIFY_PROGRESS:
  460. if ($this->bytesMax > 0 && $this->progress) {
  461. $progression = round($bytesTransferred / $this->bytesMax * 100);
  462. if ((0 === $progression % 5) && 100 !== $progression && $progression !== $this->lastProgress) {
  463. $this->lastProgress = $progression;
  464. $this->io->overwriteError(" Downloading: <comment>$progression%</comment>", false);
  465. }
  466. }
  467. break;
  468. default:
  469. break;
  470. }
  471. }
  472. protected function promptAuthAndRetry($httpStatus, $reason = null)
  473. {
  474. if ($this->config && in_array($this->originUrl, $this->config->get('github-domains'), true)) {
  475. $message = "\n".'Could not fetch '.$this->fileUrl.', please create a GitHub OAuth token '.($httpStatus === 404 ? 'to access private repos' : 'to go over the API rate limit');
  476. $gitHubUtil = new GitHub($this->io, $this->config, null);
  477. if (!$gitHubUtil->authorizeOAuth($this->originUrl)
  478. && (!$this->io->isInteractive() || !$gitHubUtil->authorizeOAuthInteractively($this->originUrl, $message))
  479. ) {
  480. throw new TransportException('Could not authenticate against '.$this->originUrl, 401);
  481. }
  482. } elseif ($this->config && in_array($this->originUrl, $this->config->get('gitlab-domains'), true)) {
  483. $message = "\n".'Could not fetch '.$this->fileUrl.', enter your ' . $this->originUrl . ' credentials ' .($httpStatus === 401 ? 'to access private repos' : 'to go over the API rate limit');
  484. $gitLabUtil = new GitLab($this->io, $this->config, null);
  485. if (!$gitLabUtil->authorizeOAuth($this->originUrl)
  486. && (!$this->io->isInteractive() || !$gitLabUtil->authorizeOAuthInteractively($this->scheme, $this->originUrl, $message))
  487. ) {
  488. throw new TransportException('Could not authenticate against '.$this->originUrl, 401);
  489. }
  490. } else {
  491. // 404s are only handled for github
  492. if ($httpStatus === 404) {
  493. return;
  494. }
  495. // fail if the console is not interactive
  496. if (!$this->io->isInteractive()) {
  497. if ($httpStatus === 401) {
  498. $message = "The '" . $this->fileUrl . "' URL required authentication.\nYou must be using the interactive console to authenticate";
  499. }
  500. if ($httpStatus === 403) {
  501. $message = "The '" . $this->fileUrl . "' URL could not be accessed: " . $reason;
  502. }
  503. throw new TransportException($message, $httpStatus);
  504. }
  505. // fail if we already have auth
  506. if ($this->io->hasAuthentication($this->originUrl)) {
  507. throw new TransportException("Invalid credentials for '" . $this->fileUrl . "', aborting.", $httpStatus);
  508. }
  509. $this->io->overwriteError(' Authentication required (<info>'.parse_url($this->fileUrl, PHP_URL_HOST).'</info>):');
  510. $username = $this->io->ask(' Username: ');
  511. $password = $this->io->askAndHideAnswer(' Password: ');
  512. $this->io->setAuthentication($this->originUrl, $username, $password);
  513. $this->storeAuth = $this->config->get('store-auths');
  514. }
  515. $this->retry = true;
  516. throw new TransportException('RETRY');
  517. }
  518. protected function getOptionsForUrl($originUrl, $additionalOptions)
  519. {
  520. $tlsOptions = array();
  521. // Setup remaining TLS options - the matching may need monitoring, esp. www vs none in CN
  522. if ($this->disableTls === false && PHP_VERSION_ID < 50600 && !stream_is_local($this->fileUrl)) {
  523. $host = parse_url($this->fileUrl, PHP_URL_HOST);
  524. if (PHP_VERSION_ID >= 50304) {
  525. // Must manually follow when setting CN_match because this causes all
  526. // redirects to be validated against the same CN_match value.
  527. $userlandFollow = true;
  528. } else {
  529. // PHP < 5.3.4 does not support follow_location, for those people
  530. // do some really nasty hard coded transformations. These will
  531. // still breakdown if the site redirects to a domain we don't
  532. // expect.
  533. if ($host === 'github.com' || $host === 'api.github.com') {
  534. $host = '*.github.com';
  535. }
  536. }
  537. $tlsOptions['ssl']['CN_match'] = $host;
  538. $tlsOptions['ssl']['SNI_server_name'] = $host;
  539. $urlAuthority = $this->getUrlAuthority($this->fileUrl);
  540. if (isset($this->peerCertificateMap[$urlAuthority])) {
  541. // Handle subjectAltName on lesser PHP's.
  542. $certMap = $this->peerCertificateMap[$urlAuthority];
  543. $this->io->writeError(sprintf(
  544. 'Using <info>%s</info> as CN for subjectAltName enabled host <info>%s</info>',
  545. $certMap['cn'],
  546. $urlAuthority
  547. ), true, IOInterface::DEBUG);
  548. $tlsOptions['ssl']['CN_match'] = $certMap['cn'];
  549. $tlsOptions['ssl']['peer_fingerprint'] = $certMap['fp'];
  550. }
  551. }
  552. $headers = array();
  553. if (extension_loaded('zlib')) {
  554. $headers[] = 'Accept-Encoding: gzip';
  555. }
  556. $options = array_replace_recursive($this->options, $tlsOptions, $additionalOptions);
  557. if (!$this->degradedMode) {
  558. // degraded mode disables HTTP/1.1 which causes issues with some bad
  559. // proxies/software due to the use of chunked encoding
  560. $options['http']['protocol_version'] = 1.1;
  561. $headers[] = 'Connection: close';
  562. }
  563. if (isset($userlandFollow)) {
  564. $options['http']['follow_location'] = 0;
  565. }
  566. if ($this->io->hasAuthentication($originUrl)) {
  567. $auth = $this->io->getAuthentication($originUrl);
  568. if ('github.com' === $originUrl && 'x-oauth-basic' === $auth['password']) {
  569. $options['github-token'] = $auth['username'];
  570. } elseif ($this->config && in_array($originUrl, $this->config->get('gitlab-domains'), true)) {
  571. if ($auth['password'] === 'oauth2') {
  572. $headers[] = 'Authorization: Bearer '.$auth['username'];
  573. }
  574. } else {
  575. $authStr = base64_encode($auth['username'] . ':' . $auth['password']);
  576. $headers[] = 'Authorization: Basic '.$authStr;
  577. }
  578. }
  579. if (isset($options['http']['header']) && !is_array($options['http']['header'])) {
  580. $options['http']['header'] = explode("\r\n", trim($options['http']['header'], "\r\n"));
  581. }
  582. foreach ($headers as $header) {
  583. $options['http']['header'][] = $header;
  584. }
  585. return $options;
  586. }
  587. private function handleRedirect(array $http_response_header, array $additionalOptions, $result)
  588. {
  589. if ($locationHeader = $this->findHeaderValue($http_response_header, 'location')) {
  590. if (parse_url($locationHeader, PHP_URL_SCHEME)) {
  591. // Absolute URL; e.g. https://example.com/composer
  592. $targetUrl = $locationHeader;
  593. } elseif (parse_url($locationHeader, PHP_URL_HOST)) {
  594. // Scheme relative; e.g. //example.com/foo
  595. $targetUrl = $this->scheme.':'.$locationHeader;
  596. } elseif ('/' === $locationHeader[0]) {
  597. // Absolute path; e.g. /foo
  598. $urlHost = parse_url($this->fileUrl, PHP_URL_HOST);
  599. // Replace path using hostname as an anchor.
  600. $targetUrl = preg_replace('{^(.+(?://|@)'.preg_quote($urlHost).'(?::\d+)?)(?:[/\?].*)?$}', '\1'.$locationHeader, $this->fileUrl);
  601. } else {
  602. // Relative path; e.g. foo
  603. // This actually differs from PHP which seems to add duplicate slashes.
  604. $targetUrl = preg_replace('{^(.+/)[^/?]*(?:\?.*)?$}', '\1'.$locationHeader, $this->fileUrl);
  605. }
  606. }
  607. if (!empty($targetUrl)) {
  608. $this->redirects++;
  609. $this->io->writeError(sprintf('Following redirect (%u) %s', $this->redirects, $targetUrl), true, IOInterface::DEBUG);
  610. $additionalOptions['redirects'] = $this->redirects;
  611. return $this->get($this->originUrl, $targetUrl, $additionalOptions, $this->fileName, $this->progress);
  612. }
  613. if (!$this->retry) {
  614. $e = new TransportException('The "'.$this->fileUrl.'" file could not be downloaded, got redirect without Location ('.$http_response_header[0].')');
  615. $e->setHeaders($http_response_header);
  616. $e->setResponse($result);
  617. throw $e;
  618. }
  619. return false;
  620. }
  621. /**
  622. * @param array $options
  623. *
  624. * @return array
  625. */
  626. private function getTlsDefaults(array $options)
  627. {
  628. $ciphers = implode(':', array(
  629. 'ECDHE-RSA-AES128-GCM-SHA256',
  630. 'ECDHE-ECDSA-AES128-GCM-SHA256',
  631. 'ECDHE-RSA-AES256-GCM-SHA384',
  632. 'ECDHE-ECDSA-AES256-GCM-SHA384',
  633. 'DHE-RSA-AES128-GCM-SHA256',
  634. 'DHE-DSS-AES128-GCM-SHA256',
  635. 'kEDH+AESGCM',
  636. 'ECDHE-RSA-AES128-SHA256',
  637. 'ECDHE-ECDSA-AES128-SHA256',
  638. 'ECDHE-RSA-AES128-SHA',
  639. 'ECDHE-ECDSA-AES128-SHA',
  640. 'ECDHE-RSA-AES256-SHA384',
  641. 'ECDHE-ECDSA-AES256-SHA384',
  642. 'ECDHE-RSA-AES256-SHA',
  643. 'ECDHE-ECDSA-AES256-SHA',
  644. 'DHE-RSA-AES128-SHA256',
  645. 'DHE-RSA-AES128-SHA',
  646. 'DHE-DSS-AES128-SHA256',
  647. 'DHE-RSA-AES256-SHA256',
  648. 'DHE-DSS-AES256-SHA',
  649. 'DHE-RSA-AES256-SHA',
  650. 'AES128-GCM-SHA256',
  651. 'AES256-GCM-SHA384',
  652. 'ECDHE-RSA-RC4-SHA',
  653. 'ECDHE-ECDSA-RC4-SHA',
  654. 'AES128',
  655. 'AES256',
  656. 'RC4-SHA',
  657. 'HIGH',
  658. '!aNULL',
  659. '!eNULL',
  660. '!EXPORT',
  661. '!DES',
  662. '!3DES',
  663. '!MD5',
  664. '!PSK',
  665. ));
  666. /**
  667. * CN_match and SNI_server_name are only known once a URL is passed.
  668. * They will be set in the getOptionsForUrl() method which receives a URL.
  669. *
  670. * cafile or capath can be overridden by passing in those options to constructor.
  671. */
  672. $defaults = array(
  673. 'ssl' => array(
  674. 'ciphers' => $ciphers,
  675. 'verify_peer' => true,
  676. 'verify_depth' => 7,
  677. 'SNI_enabled' => true,
  678. 'capture_peer_cert' => true,
  679. ),
  680. );
  681. if (isset($options['ssl'])) {
  682. $defaults['ssl'] = array_replace_recursive($defaults['ssl'], $options['ssl']);
  683. }
  684. $caBundleLogger = $this->io instanceof LoggerInterface ? $this->io : null;
  685. /**
  686. * Attempt to find a local cafile or throw an exception if none pre-set
  687. * The user may go download one if this occurs.
  688. */
  689. if (!isset($defaults['ssl']['cafile']) && !isset($defaults['ssl']['capath'])) {
  690. $result = CaBundle::getSystemCaRootBundlePath($caBundleLogger);
  691. if (preg_match('{^phar://}', $result)) {
  692. $hash = hash_file('sha256', $result);
  693. $targetPath = rtrim(sys_get_temp_dir(), '\\/') . '/composer-cacert-' . $hash . '.pem';
  694. if (!file_exists($targetPath) || $hash !== hash_file('sha256', $targetPath)) {
  695. $this->streamCopy($result, $targetPath);
  696. chmod($targetPath, 0666);
  697. }
  698. $defaults['ssl']['cafile'] = $targetPath;
  699. } elseif (is_dir($result)) {
  700. $defaults['ssl']['capath'] = $result;
  701. } else {
  702. $defaults['ssl']['cafile'] = $result;
  703. }
  704. }
  705. if (isset($defaults['ssl']['cafile']) && (!is_readable($defaults['ssl']['cafile']) || !CaBundle::validateCaFile($defaults['ssl']['cafile'], $caBundleLogger))) {
  706. throw new TransportException('The configured cafile was not valid or could not be read.');
  707. }
  708. if (isset($defaults['ssl']['capath']) && (!is_dir($defaults['ssl']['capath']) || !is_readable($defaults['ssl']['capath']))) {
  709. throw new TransportException('The configured capath was not valid or could not be read.');
  710. }
  711. /**
  712. * Disable TLS compression to prevent CRIME attacks where supported.
  713. */
  714. if (PHP_VERSION_ID >= 50413) {
  715. $defaults['ssl']['disable_compression'] = true;
  716. }
  717. return $defaults;
  718. }
  719. /**
  720. * Uses stream_copy_to_stream instead of copy to work around https://bugs.php.net/bug.php?id=64634
  721. *
  722. * @param string $source
  723. * @param string $target
  724. */
  725. private function streamCopy($source, $target)
  726. {
  727. $source = fopen($source, 'r');
  728. $target = fopen($target, 'w+');
  729. stream_copy_to_stream($source, $target);
  730. fclose($source);
  731. fclose($target);
  732. unset($source, $target);
  733. }
  734. /**
  735. * Fetch certificate common name and fingerprint for validation of SAN.
  736. *
  737. * @todo Remove when PHP 5.6 is minimum supported version.
  738. */
  739. private function getCertificateCnAndFp($url, $options)
  740. {
  741. if (PHP_VERSION_ID >= 50600) {
  742. throw new \BadMethodCallException(sprintf(
  743. '%s must not be used on PHP >= 5.6',
  744. __METHOD__
  745. ));
  746. }
  747. $context = StreamContextFactory::getContext($url, $options, array('options' => array(
  748. 'ssl' => array(
  749. 'capture_peer_cert' => true,
  750. 'verify_peer' => false, // Yes this is fucking insane! But PHP is lame.
  751. ), ),
  752. ));
  753. // Ideally this would just use stream_socket_client() to avoid sending a
  754. // HTTP request but that does not capture the certificate.
  755. if (false === $handle = @fopen($url, 'rb', false, $context)) {
  756. return;
  757. }
  758. // Close non authenticated connection without reading any content.
  759. fclose($handle);
  760. $handle = null;
  761. $params = stream_context_get_params($context);
  762. if (!empty($params['options']['ssl']['peer_certificate'])) {
  763. $peerCertificate = $params['options']['ssl']['peer_certificate'];
  764. if (TlsHelper::checkCertificateHost($peerCertificate, parse_url($url, PHP_URL_HOST), $commonName)) {
  765. return array(
  766. 'cn' => $commonName,
  767. 'fp' => TlsHelper::getCertificateFingerprint($peerCertificate),
  768. );
  769. }
  770. }
  771. }
  772. private function getUrlAuthority($url)
  773. {
  774. $defaultPorts = array(
  775. 'ftp' => 21,
  776. 'http' => 80,
  777. 'https' => 443,
  778. 'ssh2.sftp' => 22,
  779. 'ssh2.scp' => 22,
  780. );
  781. $scheme = parse_url($url, PHP_URL_SCHEME);
  782. if (!isset($defaultPorts[$scheme])) {
  783. throw new \InvalidArgumentException(sprintf(
  784. 'Could not get default port for unknown scheme: %s',
  785. $scheme
  786. ));
  787. }
  788. $defaultPort = $defaultPorts[$scheme];
  789. $port = parse_url($url, PHP_URL_PORT) ?: $defaultPort;
  790. return parse_url($url, PHP_URL_HOST).':'.$port;
  791. }
  792. }