-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes to the xml security scanner to use libxml_disable_entity_loader() when cleanly supported and thread-safe, and to handle UTF-7 charset which otherwise permits an XXE exploit
- Loading branch information
Mark Baker
authored
Nov 20, 2018
1 parent
3bea6f5
commit 0f8f071
Showing
11 changed files
with
229 additions
and
147 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheet\Reader\Security; | ||
|
||
use PhpOffice\PhpSpreadsheet\Reader\Exception; | ||
|
||
class XmlScanner | ||
{ | ||
/** | ||
* Identifies whether the thread-safe libxmlDisableEntityLoader() function is available. | ||
* | ||
* @var bool | ||
*/ | ||
private $libxmlDisableEntityLoader = false; | ||
|
||
private $pattern; | ||
|
||
public function __construct($pattern = '<!DOCTYPE') | ||
{ | ||
$this->pattern = $pattern; | ||
$this->libxmlDisableEntityLoader = $this->identifyLibxmlDisableEntityLoaderAvailability(); | ||
|
||
if ($this->libxmlDisableEntityLoader) { | ||
libxml_disable_entity_loader(true); | ||
} | ||
} | ||
|
||
private function identifyLibxmlDisableEntityLoaderAvailability() | ||
{ | ||
if (PHP_MAJOR_VERSION == 7) { | ||
switch (PHP_MINOR_VERSION) { | ||
case 2: | ||
return PHP_RELEASE_VERSION >= 1; | ||
case 1: | ||
return PHP_RELEASE_VERSION >= 13; | ||
case 0: | ||
return PHP_RELEASE_VERSION >= 27; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Scan the XML for use of <!ENTITY to prevent XXE/XEE attacks. | ||
* | ||
* @param mixed $xml | ||
* | ||
* @throws Exception | ||
* | ||
* @return string | ||
*/ | ||
public function scan($xml) | ||
{ | ||
$pattern = '/encoding="(.*?)"/'; | ||
$result = preg_match($pattern, $xml, $matches); | ||
$charset = $result ? $matches[1] : 'UTF-8'; | ||
|
||
if ($charset !== 'UTF-8') { | ||
$xml = mb_convert_encoding($xml, 'UTF-8', $charset); | ||
} | ||
|
||
// Don't rely purely on libxml_disable_entity_loader() | ||
$pattern = '/\\0?' . implode('\\0?', str_split($this->pattern)) . '\\0?/'; | ||
if (preg_match($pattern, $xml)) { | ||
throw new Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks'); | ||
} | ||
|
||
return $xml; | ||
} | ||
|
||
/** | ||
* Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks. | ||
* | ||
* @param string $filestream | ||
* | ||
* @throws Exception | ||
* | ||
* @return string | ||
*/ | ||
public function scanFile($filestream) | ||
{ | ||
return $this->scan(file_get_contents($filestream)); | ||
} | ||
} |
Oops, something went wrong.