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

[zend-http] Backport of fix for CVE-2021-3007 in Zend_Http_Response_Stream #43

Merged
merged 2 commits into from
Jan 7, 2021
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 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;
}
}