@@ -61,12 +61,12 @@ final public function __construct(ServerRequestInterface $serverRequest)
6161 return $ result ;
6262 });
6363
64- $ this -> registerMediaTypeParser ( ' application/xml ' , function ($ input ) {
65- $ backup = libxml_disable_entity_loader (true );
64+ $ xmlParserCallable = function ($ input ) {
65+ $ backup = self :: disableXmlEntityLoader (true );
6666 $ backup_errors = libxml_use_internal_errors (true );
6767 $ result = simplexml_load_string ($ input );
6868
69- libxml_disable_entity_loader ($ backup );
69+ self :: disableXmlEntityLoader ($ backup );
7070 libxml_clear_errors ();
7171 libxml_use_internal_errors ($ backup_errors );
7272
@@ -75,23 +75,10 @@ final public function __construct(ServerRequestInterface $serverRequest)
7575 }
7676
7777 return $ result ;
78- });
79-
80- $ this ->registerMediaTypeParser ('text/xml ' , function ($ input ) {
81- $ backup = libxml_disable_entity_loader (true );
82- $ backup_errors = libxml_use_internal_errors (true );
83- $ result = simplexml_load_string ($ input );
84-
85- libxml_disable_entity_loader ($ backup );
86- libxml_clear_errors ();
87- libxml_use_internal_errors ($ backup_errors );
88-
89- if ($ result === false ) {
90- return null ;
91- }
78+ };
9279
93- return $ result ;
94- } );
80+ $ this -> registerMediaTypeParser ( ' application/xml ' , $ xmlParserCallable ) ;
81+ $ this -> registerMediaTypeParser ( ' text/xml ' , $ xmlParserCallable );
9582
9683 $ this ->registerMediaTypeParser ('application/x-www-form-urlencoded ' , function ($ input ) {
9784 parse_str ($ input , $ data );
@@ -781,4 +768,17 @@ public function isXhr(): bool
781768 {
782769 return $ this ->serverRequest ->getHeaderLine ('X-Requested-With ' ) === 'XMLHttpRequest ' ;
783770 }
771+
772+ private static function disableXmlEntityLoader (bool $ disable ): bool
773+ {
774+ if (\LIBXML_VERSION >= 20900 ) {
775+ // libxml >= 2.9.0 disables entity loading by default, so it is
776+ // safe to skip the real call (deprecated in PHP 8).
777+ return true ;
778+ }
779+
780+ // @codeCoverageIgnoreStart
781+ return libxml_disable_entity_loader ($ disable );
782+ // @codeCoverageIgnoreEnd
783+ }
784784}
0 commit comments