Skip to content

Commit 829b0df

Browse files
committed
Fix #71592: External entity processing never fails
If the callback set via `xml_set_external_entity_ref_handler()` returns a falsy value, parsing is supposed to stop and the error number set to `XML_ERROR_EXTERNAL_ENTITY_HANDLING`. This is already correctly done by the libexpat binding, but the libxml2 binding ignores the return value. We fix this by calling `xmlStopParser()` which is available as of libxml 2.1.0[1] (PHP-7.1 requires at least libxml 2.6.11 anyway), and setting the desired `errNo` ourselves. [1] <http://xmlsoft.org/news.html>
1 parent bca0a7e commit 829b0df

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ PHP NEWS
99
. Fixed bug #50675 (SoapClient can't handle object references correctly).
1010
(Cameron Porter)
1111

12+
- XML:
13+
. Fixed bug 71592 (External entity processing never fails). (cmb)
14+
1215
25 Oct 2018, PHP 7.3.0RC4
1316

1417
- Core:

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ PCRE:
482482
supported transparently. Since tidyp offers no API to get the release date,
483483
tidy_get_release() and tidy::getRelease() return 'unknown' in this case.
484484

485+
XML:
486+
. The return value of the `xml_set_external_entity_ref_handler()` callback is
487+
now also heeded if the extension has been built against libxml. Formerly,
488+
the return value has been ignored, and parsing did never stop.
489+
485490
Zip:
486491
. Building against the bundled libzip is discouraged, but still possible by
487492
adding `--without-libzip` to the configuration.

ext/xml/compat.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,10 @@ _external_entity_ref_handler(void *user, const xmlChar *names, int type, const x
359359
return;
360360
}
361361

362-
parser->h_external_entity_ref(parser, names, (XML_Char *) "", sys_id, pub_id);
362+
if (!parser->h_external_entity_ref(parser, names, (XML_Char *) "", sys_id, pub_id)) {
363+
xmlStopParser(parser->parser);
364+
parser->parser->errNo = XML_ERROR_EXTERNAL_ENTITY_HANDLING;
365+
};
363366
}
364367

365368
static xmlEntityPtr

ext/xml/tests/bug71592.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Bug #71592 (External entity processing never fails)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('xml')) die('skip xml extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$xml = <<<XML
10+
<?xml version="1.0" encoding="UTF-8"?>
11+
<!DOCTYPE p [
12+
<!ENTITY pic PUBLIC "image.gif" "http://example.org/image.gif">
13+
]>
14+
<root>
15+
<p>&pic;</p>
16+
<p></nop>
17+
</root>
18+
XML;
19+
20+
$parser = xml_parser_create_ns('UTF-8');
21+
xml_set_external_entity_ref_handler($parser, function () {
22+
return false;
23+
});
24+
xml_parse($parser, $xml);
25+
var_dump(xml_get_error_code($parser));
26+
?>
27+
===DONE===
28+
--EXPECT--
29+
int(21)
30+
===DONE===

0 commit comments

Comments
 (0)