Skip to content

Commit

Permalink
Raise an error when host header is empty.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Jan 13, 2025
1 parent 6e344f3 commit dc07f55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/OpenSearch/Aws/SigningClientDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function sendRequest(RequestInterface $request): ResponseInterface
$request = $request->withHeader($name, $value);
}

if (empty($request->getHeaderLine('Host'))) {
throw new \RuntimeException('Missing Host header.');
}

$request = $request->withHeader('x-amz-content-sha256', hash('sha256', (string) $request->getBody()));
$request = $this->signer->signRequest($request, $this->credentials);
return $this->inner->sendRequest($request);
Expand Down
24 changes: 24 additions & 0 deletions tests/Aws/SigningClientDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,28 @@ public function testSendRequest()
$request = new Request('GET', 'http://localhost:9200/_search');
$decorator->sendRequest($request);
}

public function testSendRequestWithNullHostHeader(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Missing Host header.');
$client = $this->createMock(ClientInterface::class);
$credentials = $this->createMock(CredentialsInterface::class);
$signer = $this->createMock(SignatureV4::class);
$decorator = new SigningClientDecorator($client, $credentials, $signer, []);
$request = new Request('GET', 'http://localhost:9200/_search', ['Host' => null]);

Check failure on line 63 in tests/Aws/SigningClientDecoratorTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #3 $headers of class GuzzleHttp\Psr7\Request constructor expects array<array<string>|string>, array<string, null> given.
$decorator->sendRequest($request);
}

public function testSendRequestWithEmptyHostHeader(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Missing Host header.');
$client = $this->createMock(ClientInterface::class);
$credentials = $this->createMock(CredentialsInterface::class);
$signer = $this->createMock(SignatureV4::class);
$decorator = new SigningClientDecorator($client, $credentials, $signer, []);
$request = new Request('GET', 'http://localhost:9200/_search', ['Host' => '']);
$decorator->sendRequest($request);
}
}

0 comments on commit dc07f55

Please sign in to comment.