Skip to content
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
],
"require": {
"php": "^7.2",
"php": "^7.2 || ^8.0",
"ext-SimpleXML": "*",
"ext-fileinfo": "*",
"ext-json": "*",
Expand All @@ -42,7 +42,7 @@
"nyholm/psr7": "^1.3",
"php-http/psr7-integration-tests": "dev-master",
"phpstan/phpstan": "^0.12.52",
"phpunit/phpunit": "^8.5",
"phpunit/phpunit": "^8.5 || ^9.3",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload": {
Expand Down
38 changes: 19 additions & 19 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ final public function __construct(ServerRequestInterface $serverRequest)
return $result;
});

$this->registerMediaTypeParser('application/xml', function ($input) {
$backup = libxml_disable_entity_loader(true);
$xmlParserCallable = function ($input) {
$backup = self::disableXmlEntityLoader(true);
$backup_errors = libxml_use_internal_errors(true);
$result = simplexml_load_string($input);

libxml_disable_entity_loader($backup);
self::disableXmlEntityLoader($backup);
libxml_clear_errors();
libxml_use_internal_errors($backup_errors);

Expand All @@ -75,23 +75,10 @@ final public function __construct(ServerRequestInterface $serverRequest)
}

return $result;
});

$this->registerMediaTypeParser('text/xml', function ($input) {
$backup = libxml_disable_entity_loader(true);
$backup_errors = libxml_use_internal_errors(true);
$result = simplexml_load_string($input);

libxml_disable_entity_loader($backup);
libxml_clear_errors();
libxml_use_internal_errors($backup_errors);

if ($result === false) {
return null;
}
};

return $result;
});
$this->registerMediaTypeParser('application/xml', $xmlParserCallable);
$this->registerMediaTypeParser('text/xml', $xmlParserCallable);

$this->registerMediaTypeParser('application/x-www-form-urlencoded', function ($input) {
parse_str($input, $data);
Expand Down Expand Up @@ -781,4 +768,17 @@ public function isXhr(): bool
{
return $this->serverRequest->getHeaderLine('X-Requested-With') === 'XMLHttpRequest';
}

private static function disableXmlEntityLoader(bool $disable): bool
{
if (\LIBXML_VERSION >= 20900) {
// libxml >= 2.9.0 disables entity loading by default, so it is
// safe to skip the real call (deprecated in PHP 8).
return true;
}

// @codeCoverageIgnoreStart
return libxml_disable_entity_loader($disable);
// @codeCoverageIgnoreEnd
}
}