Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject non-HTTP schemes in StreamHandler #2989

Merged
merged 2 commits into from Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Handler/StreamHandler.php
Expand Up @@ -266,6 +266,10 @@ private function createStream(RequestInterface $request, array $options)
$methods = \array_flip(\get_class_methods(__CLASS__));
}

if (!\in_array($request->getUri()->getScheme(), ['http', 'https'])) {
throw new RequestException(\sprintf("The scheme '%s' is not supported.", $request->getUri()->getScheme()), $request);
}

// HTTP/1.1 streams using the PHP stream wrapper require a
// Connection: close header
if ($request->getProtocolVersion() == '1.1'
Expand Down Expand Up @@ -318,7 +322,7 @@ static function () use ($context, $params) {
return $this->createResource(
function () use ($uri, &$http_response_header, $contextResource, $context, $options, $request) {
$resource = @\fopen((string) $uri, 'r', false, $contextResource);
$this->lastHeaders = $http_response_header;
$this->lastHeaders = $http_response_header ?? [];

if (false === $resource) {
throw new ConnectException(sprintf('Connection refused for URI %s', $uri), $request, null, $context);
Expand Down
15 changes: 15 additions & 0 deletions tests/Handler/StreamHandlerTest.php
Expand Up @@ -738,4 +738,19 @@ public function testHandlesInvalidStatusCodeGracefully()
]
)->wait();
}

public function testRejectsNonHttpSchemes()
{
$handler = new StreamHandler();

$this->expectException(RequestException::class);
$this->expectExceptionMessage("The scheme 'file' is not supported.");

$handler(
new Request('GET', 'file:///etc/passwd'),
[
RequestOptions::STREAM => true,
]
)->wait();
}
}