[CVE-2020-25817] Prevent loading of xml entities

This commit is contained in:
Steve Boyd 2021-02-11 13:05:10 +13:00
parent 472fc4ebb4
commit 3bb435c241
3 changed files with 22 additions and 0 deletions

View File

@ -17,3 +17,6 @@ SilverStripe\Dev\DevelopmentAdmin:
controller: Silverstripe\Dev\DevConfigController
links:
config: 'View the current config, useful for debugging'
SilverStripe\Dev\CSSContentParser:
disable_xml_external_entities: true

View File

@ -2,6 +2,7 @@
namespace SilverStripe\Dev;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SimpleXMLElement;
use tidy;
@ -25,6 +26,7 @@ use Exception;
class CSSContentParser
{
use Injectable;
use Configurable;
protected $simpleXML = null;
@ -56,6 +58,13 @@ class CSSContentParser
$tidy = $content;
}
// Prevent loading of external entities to prevent XXE attacks
// Note: as of libxml 2.9.0 entity substitution is disabled by default so this won't be required
if ($this->config()->get('disable_xml_external_entities')) {
libxml_set_external_entity_loader(function () {
return null;
});
}
$this->simpleXML = @simplexml_load_string($tidy, 'SimpleXMLElement', LIBXML_NOWARNING);
if (!$this->simpleXML) {
throw new Exception('CSSContentParser::__construct(): Could not parse content.'

View File

@ -50,4 +50,14 @@ HTML
$result = $parser->getBySelector('#A .other');
$this->assertEquals("result", $result[0] . '');
}
public function testXmlEntitiesDisabled()
{
// CSSContentParser uses simplexml to parse html
// Ensure XML entities are not substituted in to prevent XXE attacks
$xml = '<!DOCTYPE html [<!ENTITY myentity "World">]><html><div>Hello &myentity;</div></html>';
$parser = new CSSContentParser($xml);
$div = $parser->getBySelector('div')[0]->asXML();
$this->assertEquals('<div>Hello &amp;myentity;</div>', $div);
}
}