|
const | ENTITY_DETECT = 'Detected use of ENTITY in XML, disabled to prevent XXE/XEE attacks' |
|
Definition at line 29 of file Security.php.
◆ detectBom()
static detectBom |
( |
|
$string | ) |
|
|
staticprotected |
Attempt to match a known BOM.
Iterates through the return of getBomMap(), comparing the initial bytes of the provided string to the BOM of each; if a match is determined, it returns the encoding.
- Parameters
-
- Returns
- false|string Returns encoding on success.
Definition at line 223 of file Security.php.
225 foreach (self::getBomMap() as $criteria) {
226 if (0 === strncmp($string, $criteria[
'bom'], $criteria[
'length'])) {
227 return $criteria[
'encoding'];
◆ detectStringEncoding()
static detectStringEncoding |
( |
|
$xml | ) |
|
|
staticprotected |
Determine the string encoding.
Determines string encoding from either a detected BOM or a heuristic.
- Parameters
-
- Returns
- string File encoding
Definition at line 207 of file Security.php.
static detectXmlStringEncoding($xml)
static detectBom($string)
◆ detectXmlEncoding()
static detectXmlEncoding |
( |
|
$xml, |
|
|
|
$fileEncoding |
|
) |
| |
|
staticprotected |
Attempt to detect the specified XML encoding.
Using the file's encoding, determines if an "encoding" attribute is present and well-formed in the XML declaration; if so, it returns a list with both the ASCII representation of that declaration and the original file encoding.
If not, a list containing only the provided file encoding is returned.
- Parameters
-
string | $xml | |
string | $fileEncoding | |
- Returns
- string[] Potential XML encodings
Definition at line 266 of file Security.php.
274 $closePos = strpos($xml, $close);
275 if (
false === $closePos) {
276 return array($fileEncoding);
279 $encPos = strpos($xml, $encAttr);
280 if (
false === $encPos
281 || $encPos > $closePos
283 return array($fileEncoding);
286 $encPos += strlen($encAttr);
287 $quotePos = strpos($xml,
$quote, $encPos);
288 if (
false === $quotePos) {
289 return array($fileEncoding);
295 str_replace(
'\0',
'', $encoding),
static substr($string, $start, $end)
call_user_func($callable, $param)
static getAsciiEncodingMap()
◆ detectXmlStringEncoding()
static detectXmlStringEncoding |
( |
|
$xml | ) |
|
|
staticprotected |
Attempt to detect the string encoding of an XML string.
- Parameters
-
- Returns
- string Encoding
Definition at line 239 of file Security.php.
241 foreach (self::getAsciiEncodingMap() as $encoding =>
$generator) {
call_user_func($callable, $param)
◆ encodeToUTF16BE()
static encodeToUTF16BE |
( |
|
$ascii | ) |
|
|
static |
Encode an ASCII string to UTF-16BE
Definition at line 460 of file Security.php.
462 return preg_replace(
'/(.)/',
"\0\\1", $ascii);
◆ encodeToUTF16LE()
static encodeToUTF16LE |
( |
|
$ascii | ) |
|
|
static |
Encode an ASCII string to UTF-16LE
Definition at line 472 of file Security.php.
474 return preg_replace(
'/(.)/',
"\\1\0", $ascii);
◆ encodeToUTF32BE()
static encodeToUTF32BE |
( |
|
$ascii | ) |
|
|
static |
Encode an ASCII string to UTF-32BE
Definition at line 412 of file Security.php.
414 return preg_replace(
'/(.)/',
"\0\0\0\\1", $ascii);
◆ encodeToUTF32LE()
static encodeToUTF32LE |
( |
|
$ascii | ) |
|
|
static |
Encode an ASCII string to UTF-32LE
Definition at line 424 of file Security.php.
426 return preg_replace(
'/(.)/',
"\\1\0\0\0", $ascii);
◆ encodeToUTF32odd1()
static encodeToUTF32odd1 |
( |
|
$ascii | ) |
|
|
static |
Encode an ASCII string to UTF-32odd1
Definition at line 436 of file Security.php.
438 return preg_replace(
'/(.)/',
"\0\\1\0\0", $ascii);
◆ encodeToUTF32odd2()
static encodeToUTF32odd2 |
( |
|
$ascii | ) |
|
|
static |
Encode an ASCII string to UTF-32odd2
Definition at line 448 of file Security.php.
450 return preg_replace(
'/(.)/',
"\0\0\\1\0", $ascii);
◆ encodeToUTF8()
static encodeToUTF8 |
( |
|
$ascii | ) |
|
|
static |
Encode an ASCII string to UTF-8
Definition at line 484 of file Security.php.
◆ generateEntityComparison()
static generateEntityComparison |
( |
|
$encoding | ) |
|
|
static |
Generate an entity comparison based on the given encoding.
This patch is internal only, and public only so it can be used as a callable to pass to array_map.
Definition at line 398 of file Security.php.
401 $generator = isset($encodingMap[$encoding]) ? $encodingMap[$encoding] : $encodingMap[
'UTF-8'];
call_user_func($callable, $param)
static getAsciiEncodingMap()
◆ getAsciiEncodingMap()
static getAsciiEncodingMap |
( |
| ) |
|
|
staticprotected |
Return a map of encoding => generator pairs.
Returns a map of encoding => generator pairs, where the generator is a callable that accepts a string and returns the appropriate byte order sequence of that string for the encoding.
- Returns
- array
Definition at line 354 of file Security.php.
357 'UTF-32BE' => array(__CLASS__,
'encodeToUTF32BE'),
358 'UTF-32LE' => array(__CLASS__,
'encodeToUTF32LE'),
359 'UTF-32odd1' => array(__CLASS__,
'encodeToUTF32odd1'),
360 'UTF-32odd2' => array(__CLASS__,
'encodeToUTF32odd2'),
361 'UTF-16BE' => array(__CLASS__,
'encodeToUTF16BE'),
362 'UTF-16LE' => array(__CLASS__,
'encodeToUTF16LE'),
363 'UTF-8' => array(__CLASS__,
'encodeToUTF8'),
364 'GB-18030' => array(__CLASS__,
'encodeToUTF8'),
◆ getBomMap()
Return a list of BOM maps.
Returns a list of common encoding -> BOM maps, along with the character length to compare against.
array
Definition at line 309 of file Security.php.
313 'encoding' =>
'UTF-32BE',
314 'bom' => pack(
'CCCC', 0x00, 0x00, 0xfe, 0xff),
318 'encoding' =>
'UTF-32LE',
319 'bom' => pack(
'CCCC', 0xff, 0xfe, 0x00, 0x00),
323 'encoding' =>
'GB-18030',
324 'bom' => pack(
'CCCC', 0x84, 0x31, 0x95, 0x33),
328 'encoding' =>
'UTF-16BE',
329 'bom' => pack(
'CC', 0xfe, 0xff),
333 'encoding' =>
'UTF-16LE',
334 'bom' => pack(
'CC', 0xff, 0xfe),
338 'encoding' =>
'UTF-8',
339 'bom' => pack(
'CCC', 0xef, 0xbb, 0xbf),
◆ getEntityComparison()
static getEntityComparison |
( |
|
$xml | ) |
|
|
staticprotected |
Determine and return the string(s) to use for the <!ENTITY comparison.
- Parameters
-
- Returns
- string[]
Definition at line 189 of file Security.php.
193 array(__CLASS__,
'generateEntityComparison'),
194 self::detectXmlEncoding($xml, self::detectStringEncoding($xml))
static getAsciiEncodingMap()
◆ heuristicScan()
static heuristicScan |
( |
|
$xml | ) |
|
|
staticprotected |
Heuristic scan to detect entity in XML
- Parameters
-
- Exceptions
-
Definition at line 39 of file Security.php.
41 foreach (self::getEntityComparison($xml) as $compare) {
42 if (strpos($xml, $compare) !==
false) {
◆ isPhpFpm()
Return true if PHP is running with PHP-FPM
This method is mainly used to determine whether or not heuristic checks (vs libxml checks) should be made, due to threading issues in libxml; under php-fpm, threading becomes a concern.
However, PHP versions 5.5.22+ and 5.6.6+ contain a patch to the libxml support in PHP that makes the libxml checks viable; in such versions, this method will return false to enforce those checks, which are more strict and accurate than the heuristic checks.
- Returns
- boolean
Definition at line 167 of file Security.php.
169 $isVulnerableVersion = (
170 version_compare(PHP_VERSION,
'5.5.22',
'lt')
172 version_compare(PHP_VERSION,
'5.6',
'gte')
173 && version_compare(PHP_VERSION,
'5.6.6',
'lt')
177 if (
substr(php_sapi_name(), 0, 3) ===
'fpm' && $isVulnerableVersion) {
static substr($string, $start, $end)
◆ loadXmlErrorHandler()
static loadXmlErrorHandler |
( |
|
$errno, |
|
|
|
$errstr, |
|
|
|
$errfile, |
|
|
|
$errline |
|
) |
| |
|
static |
- Parameters
-
integer | $errno | |
string | $errstr | |
string | $errfile | |
integer | $errline | |
- Returns
- bool
Definition at line 55 of file Security.php.
57 if (substr_count($errstr,
'DOMDocument::loadXML()') > 0) {
◆ scan()
static scan |
( |
|
$xml, |
|
|
DOMDocument |
$dom = null |
|
) |
| |
|
static |
Scan XML string for potential XXE and XEE attacks
- Parameters
-
string | $xml | |
DomDocument | $dom | |
- Exceptions
-
- Returns
- SimpleXMLElement|DomDocument|boolean
Definition at line 71 of file Security.php.
76 if (self::isPhpFpm()) {
82 $dom =
new DOMDocument();
85 if (!self::isPhpFpm()) {
86 $loadEntities = libxml_disable_entity_loader(
true);
87 $useInternalXmlErrors = libxml_use_internal_errors(
true);
92 set_error_handler(array(
'Zend_Xml_Security',
'loadXmlErrorHandler'), E_WARNING);
94 $result = $dom->loadXml($xml, LIBXML_NONET);
95 restore_error_handler();
99 if (!self::isPhpFpm()) {
100 libxml_disable_entity_loader($loadEntities);
101 libxml_use_internal_errors($useInternalXmlErrors);
107 if (!self::isPhpFpm()) {
108 foreach ($dom->childNodes as $child) {
109 if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
110 if ($child->entities->length > 0) {
111 #require_once 'Exception.php'; 119 if (!self::isPhpFpm()) {
120 libxml_disable_entity_loader($loadEntities);
121 libxml_use_internal_errors($useInternalXmlErrors);
124 if (isset($simpleXml)) {
125 $result = simplexml_import_dom($dom);
126 if (!
$result instanceof SimpleXMLElement) {
static heuristicScan($xml)
◆ scanFile()
static scanFile |
( |
|
$file, |
|
|
DOMDocument |
$dom = null |
|
) |
| |
|
static |
Scan XML file for potential XXE/XEE attacks
- Parameters
-
string | $file | |
DOMDocument | $dom | |
- Exceptions
-
- Returns
- SimpleXMLElement|DomDocument
Definition at line 142 of file Security.php.
144 if (!file_exists($file)) {
145 #require_once 'Exception.php'; 147 "The file $file specified doesn't exist" static scan($xml, DOMDocument $dom=null)
◆ substr()
static substr |
( |
|
$string, |
|
|
|
$start, |
|
|
|
$end |
|
) |
| |
|
staticprotected |
Binary-safe substr.
substr() is not binary-safe; this method loops by character to ensure multi-byte characters are aggregated correctly.
- Parameters
-
string | $string | |
int | $start | |
int | $end | |
- Returns
- string
Definition at line 379 of file Security.php.
383 $substr .= $string[
$i];
◆ ENTITY_DETECT
const ENTITY_DETECT = 'Detected use of ENTITY in XML, disabled to prevent XXE/XEE attacks' |
The documentation for this class was generated from the following file: