Skip to content

Commit

Permalink
Merge pull request #48 from weierophinney/security/stream-destruction
Browse files Browse the repository at this point in the history
Security tightening: verify a stream file name is a string before unlinking
  • Loading branch information
weierophinney authored Jan 5, 2021
2 parents 25d7cf0 + eab608e commit 019f31f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Response/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public function __destruct()
if (is_resource($this->stream)) {
$this->stream = null; //Could be listened by others
}
if ($this->cleanup) {
if ($this->cleanup && is_string($this->streamName) && file_exists($this->streamName)) {
ErrorHandler::start(E_WARNING);
unlink($this->streamName);
ErrorHandler::stop();
Expand Down
44 changes: 44 additions & 0 deletions test/Response/ResponseStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@

class ResponseStreamTest extends TestCase
{
/** @var null|string */
private $tempFile;

public function setUp(): void
{
$this->tempFile = null;
}

public function tearDown(): void
{
if (null !== $this->tempFile && file_exists($this->tempFile)) {
unlink($this->tempFile);
}
}

public function testResponseFactoryFromStringCreatesValidResponse()
{
$string = 'HTTP/1.0 200 OK' . "\r\n\r\n" . 'Foo Bar' . "\r\n";
Expand Down Expand Up @@ -78,6 +93,35 @@ public function test300isRedirect()
$this->assertFalse($response->isSuccess(), 'Response is an error, but isSuccess() returned true');
}

/**
* @see https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-3007
*/
public function testDestructionDoesNothingIfStreamIsNotAResourceAndStreamNameIsNotAString(): void
{
$this->tempFile = tempnam(sys_get_temp_dir(), 'lhrs');
$streamObject = new class($this->tempFile) {
private $tempFile;

public function __construct(string $tempFile)
{
$this->tempFile = $tempFile;
}

public function __toString()
{
return $this->tempFile;
}
};

$response = new Stream();
$response->setCleanup(true);
$response->setStreamName($streamObject);

unset($response);

$this->assertFileExists($this->tempFile);
}

/**
* Helper function: read test response from file
*
Expand Down

0 comments on commit 019f31f

Please sign in to comment.