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

Security tightening: verify a stream file name is a string before unlinking #48

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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