Skip to content

Commit

Permalink
HttpDownloader: add option to prevent access to private network (#11895)
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubinix committed Mar 19, 2024
1 parent 8321211 commit d36cd30
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Composer/Util/Http/CurlDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
use Composer\Util\Url;
use Composer\Util\HttpDownloader;
use React\Promise\Promise;
use Symfony\Component\HttpFoundation\IpUtils;

/**
* @internal
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Nicolas Grekas <p@tchwork.com>
* @phpstan-type Attributes array{retryAuthFailure: bool, redirects: int<0, max>, retries: int<0, max>, storeAuth: 'prompt'|bool, ipResolve: 4|6|null}
* @phpstan-type Job array{url: non-empty-string, origin: string, attributes: Attributes, options: mixed[], progress: mixed[], curlHandle: \CurlHandle, filename: string|null, headerHandle: resource, bodyHandle: resource, resolve: callable, reject: callable}
* @phpstan-type Job array{url: non-empty-string, origin: string, attributes: Attributes, options: mixed[], progress: mixed[], curlHandle: \CurlHandle, filename: string|null, headerHandle: resource, bodyHandle: resource, resolve: callable, reject: callable, primaryIp: string}
*/
class CurlDownloader
{
Expand Down Expand Up @@ -279,6 +280,7 @@ private function initDownload(callable $resolve, callable $reject, string $origi
'bodyHandle' => $bodyHandle,
'resolve' => $resolve,
'reject' => $reject,
'primaryIp' => '',
];

$usingProxy = $proxy->getFormattedUrl(' using proxy (%s)');
Expand Down Expand Up @@ -505,6 +507,18 @@ public function tick(): void
}
}

if (isset($progress['primary_ip']) && $progress['primary_ip'] !== $this->jobs[$i]['primaryIp']) {
if (
isset($this->jobs[$i]['options']['prevent_ip_access_callable']) &&
is_callable($this->jobs[$i]['options']['prevent_ip_access_callable']) &&
$this->jobs[$i]['options']['prevent_ip_access_callable']($progress['primary_ip'])
) {
throw new TransportException(sprintf('IP "%s" is blocked for "%s".', $progress['primary_ip'], $progress['url']));
}

$this->jobs[$i]['primaryIp'] = (string) $progress['primary_ip'];
}

// TODO progress
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Composer/Util/RemoteFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ protected function get(string $originUrl, string $fileUrl, array $additionalOpti

$origFileUrl = $fileUrl;

if (isset($options['prevent_ip_access_callable'])) {
throw new \RuntimeException("RemoteFilesystem doesn't support the 'prevent_ip_access_callable' config.");
}

if (isset($options['gitlab-token'])) {
$fileUrl .= (false === strpos($fileUrl, '?') ? '?' : '&') . 'access_token='.$options['gitlab-token'];
unset($options['gitlab-token']);
Expand Down

0 comments on commit d36cd30

Please sign in to comment.