Skip to content

Commit

Permalink
Merge pull request #43 from glensc/CVE-2021-3007
Browse files Browse the repository at this point in the history
Backport of fix for CVE-2021-3007 in Zend_Http_Response_Stream
  • Loading branch information
falkenhawk authored Jan 7, 2021
2 parents a476428 + 5ed35fb commit d04c889
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/zend-http/library/Zend/Http/Response/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function __destruct()
fclose($this->stream);
$this->stream = null;
}
if($this->_cleanup) {
if($this->_cleanup && is_string($this->stream_name) && file_exists($this->stream_name)) {
@unlink($this->stream_name);
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Zend/Http/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
* @version $Id$
*/

use Zend\Http\StreamObject;

/**
* Zend_Http_Response
*/
// require_once 'Zend/Http/Response.php';

require_once __DIR__ . '/StreamObject.php';

/**
* Zend_Http_Response unit tests
*
Expand All @@ -38,9 +42,19 @@
*/
class Zend_Http_ResponseTest extends PHPUnit_Framework_TestCase
{
/** @var null|string */
private $tempFile;

public function setUp()
{ }

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

public function testGzipResponse ()
{
$response_text = file_get_contents(dirname(__FILE__) . '/_files/response_gzip');
Expand Down Expand Up @@ -173,6 +187,23 @@ public function test300isRedirect()
$this->assertFalse($response->isSuccessful(), 'Response is a redirection, but isSuccessful() returned true');
}

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

$response = new Zend_Http_Response_Stream(200, array());
$response->setCleanup(true);
$response->setStreamName($streamObject);

unset($response);

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

public function test200Ok()
{
$response = Zend_Http_Response::fromString($this->readResponse('response_deflate'));
Expand Down
18 changes: 18 additions & 0 deletions tests/Zend/Http/StreamObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Zend\Http;

class StreamObject
{
private $tempFile;

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

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

0 comments on commit d04c889

Please sign in to comment.