Skip to content

Commit f15f8fc

Browse files
cmb69smalyshev
authored andcommitted
Fix #79971: special character is breaking the path in xml function
The libxml based XML functions accepting a filename actually accept URIs with possibly percent-encoded characters. Percent-encoded NUL bytes lead to truncation, like non-encoded NUL bytes would. We catch those, and let the functions fail with a respective warning.
1 parent 88f99c9 commit f15f8fc

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

ext/dom/domimplementation.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ PHP_METHOD(domimplementation, createDocumentType)
112112
pch2 = (xmlChar *) systemid;
113113
}
114114

115+
if (strstr(name, "%00")) {
116+
php_error_docref(NULL, E_WARNING, "URI must not contain percent-encoded NUL bytes");
117+
RETURN_FALSE;
118+
}
119+
115120
uri = xmlParseURI(name);
116121
if (uri != NULL && uri->opaque != NULL) {
117122
localname = xmlStrdup((xmlChar *) uri->opaque);

ext/dom/tests/bug79971_2.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Bug #79971 (special character is breaking the path in xml function)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('dom')) die('skip dom extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$imp = new DOMImplementation;
10+
if (PHP_OS_FAMILY === 'Windows') {
11+
$path = '/' . str_replace('\\', '/', __DIR__);
12+
} else {
13+
$path = __DIR__;
14+
}
15+
$uri = "file://$path/bug79971_2.xml";
16+
var_dump($imp->createDocumentType("$uri%00foo"));
17+
?>
18+
--EXPECTF--
19+
Warning: DOMImplementation::createDocumentType(): URI must not contain percent-encoded NUL bytes in %s on line %d
20+
bool(false)

ext/libxml/libxml.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ static void *php_libxml_streams_IO_open_wrapper(const char *filename, const char
306306
int isescaped=0;
307307
xmlURI *uri;
308308

309+
if (strstr(filename, "%00")) {
310+
php_error_docref(NULL, E_WARNING, "URI must not contain percent-encoded NUL bytes");
311+
return NULL;
312+
}
309313

310314
uri = xmlParseURI(filename);
311315
if (uri && (uri->scheme == NULL ||
@@ -437,6 +441,11 @@ php_libxml_output_buffer_create_filename(const char *URI,
437441
if (URI == NULL)
438442
return(NULL);
439443

444+
if (strstr(URI, "%00")) {
445+
php_error_docref(NULL, E_WARNING, "URI must not contain percent-encoded NUL bytes");
446+
return NULL;
447+
}
448+
440449
puri = xmlParseURI(URI);
441450
if (puri != NULL) {
442451
if (puri->scheme != NULL)

ext/simplexml/tests/bug79971_1.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Bug #79971 (special character is breaking the path in xml function)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('simplexml')) die('skip simplexml extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
if (PHP_OS_FAMILY === 'Windows') {
10+
$path = '/' . str_replace('\\', '/', __DIR__);
11+
} else {
12+
$path = __DIR__;
13+
}
14+
$uri = "file://$path/bug79971_1.xml";
15+
var_dump(simplexml_load_file("$uri%00foo"));
16+
17+
$sxe = simplexml_load_file($uri);
18+
var_dump($sxe->asXML("$uri.out%00foo"));
19+
?>
20+
--EXPECTF--
21+
Warning: simplexml_load_file(): URI must not contain percent-encoded NUL bytes in %s on line %d
22+
23+
Warning: simplexml_load_file(): I/O warning : failed to load external entity "%s/bug79971_1.xml%00foo" in %s on line %d
24+
bool(false)
25+
26+
Warning: SimpleXMLElement::asXML(): URI must not contain percent-encoded NUL bytes in %s on line %d
27+
bool(false)

ext/simplexml/tests/bug79971_1.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0"?>
2+
<root></root>

0 commit comments

Comments
 (0)