Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'weierophinney/hotfix/xmlrpc-disable-ext…
Browse files Browse the repository at this point in the history
…ernal-entities'
  • Loading branch information
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,16 @@ public function loadXml($request)
return false;
}

// @see ZF-12293 - disable external entities for security purposes
$loadEntities = libxml_disable_entity_loader(true);
try {
$xml = new \SimpleXMLElement($request);
libxml_disable_entity_loader($loadEntities);
} catch (\Exception $e) {
// Not valid XML
$this->_fault = new Fault(631);
$this->_fault->setEncoding($this->getEncoding());
libxml_disable_entity_loader($loadEntities);
return false;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ public function loadXml($response)
return false;
}

// @see ZF-12293 - disable external entities for security purposes
$loadEntities = libxml_disable_entity_loader(true);
$useInternalXmlErrors = libxml_use_internal_errors(true);
try {
$useInternalXmlErrors = libxml_use_internal_errors(true);
$xml = new \SimpleXMLElement($response);
libxml_disable_entity_loader($loadEntities);
libxml_use_internal_errors($useInternalXmlErrors);
} catch (\Exception $e) {
libxml_disable_entity_loader($loadEntities);
libxml_use_internal_errors($useInternalXmlErrors);
// Not valid XML
$this->_fault = new Fault(651);
Expand Down
15 changes: 15 additions & 0 deletions test/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,19 @@ public function testSetGetEncoding()
$this->assertEquals('ISO-8859-1', $this->_request->getEncoding());
$this->assertEquals('ISO-8859-1', Value::getGenerator()->getEncoding());
}

/**
* @group ZF-12293
*/
public function testDoesNotAllowExternalEntities()
{
$payload = file_get_contents(dirname(__FILE__) . '/_files/ZF12293-request.xml');
$payload = sprintf($payload, 'file://' . realpath(dirname(__FILE__) . '/_files/ZF12293-payload.txt'));
$this->_request->loadXml($payload);
$method = $this->_request->getMethod();
$this->assertTrue(empty($method));
if (is_string($method)) {
$this->assertNotContains('Local file inclusion', $method);
}
}
}
15 changes: 15 additions & 0 deletions test/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,19 @@ public function trackError($error)
{
$this->_errorOccured = true;
}

/**
* @group ZF-12293
*/
public function testDoesNotAllowExternalEntities()
{
$payload = file_get_contents(dirname(__FILE__) . '/_files/ZF12293-response.xml');
$payload = sprintf($payload, 'file://' . realpath(dirname(__FILE__) . '/_files/ZF12293-payload.txt'));
$this->_response->loadXml($payload);
$value = $this->_response->getReturnValue();
$this->assertTrue(empty($value));
if (is_string($value)) {
$this->assertNotContains('Local file inclusion', $value);
}
}
}
1 change: 1 addition & 0 deletions test/_files/ZF12293-payload.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Local file inclusion
8 changes: 8 additions & 0 deletions test/_files/ZF12293-request.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<!DOCTYPE foo [
<!ELEMENT methodName ANY >
<!ENTITY xxe SYSTEM "%s" >
]>
<methodCall>
<methodName>&xxe;</methodName>
</methodCall>
10 changes: 10 additions & 0 deletions test/_files/ZF12293-response.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!DOCTYPE foo [
<!ELEMENT methodResponse ANY >
<!ENTITY xxe SYSTEM "%s" >
]>
<methodResponse>
<params>
<param><value><string>&xxe;</string></value></param>
</params>
</methodResponse>

0 comments on commit 9881dbb

Please sign in to comment.