Skip to content

Commit

Permalink
Reject non-HTTP schemes in StreamHandler
Browse files Browse the repository at this point in the history
Non-HTTP schemes are effectively not supported, because the HTTP response
headers will only be filled for the `http` and `https` stream wrappers. Also
Guzzle is an HTTP client after all.

Reject non-HTTP schemes early on to improve error messages and to prevent
possible exploits using odd stream wrappers in case an non-fully-trusted URL is
passed to Guzzle.
  • Loading branch information
TimWolla committed Feb 24, 2022
1 parent 5172455 commit d198940
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions 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
4 changes: 2 additions & 2 deletions tests/Handler/StreamHandlerTest.php
Expand Up @@ -739,12 +739,12 @@ public function testHandlesInvalidStatusCodeGracefully()
)->wait();
}

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

$this->expectException(RequestException::class);
$this->expectExceptionMessage('An error was encountered while creating the response');
$this->expectExceptionMessage("The scheme 'file' is not supported.");

$handler(
new Request('GET', 'file:///etc/passwd'),
Expand Down

0 comments on commit d198940

Please sign in to comment.