Skip to content

Commit 49c4d81

Browse files
committed
Reject embedded NUL in SimpleXMLElement path/URL constructor
When dataIsURL is true, __construct parsed the path as a plain string and passed it to xmlReadFile, so an embedded NUL truncated the path. simplexml_load_file already rejects NULs via the path parameter type. Match that check before xmlReadFile.
1 parent 9366c61 commit 49c4d81

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ PHP NEWS
4242
. Fixed bug GH-23043 (broken session id code can cause zend_mm_heap
4343
corrupted). (ndossche)
4444

45+
- SimpleXML:
46+
. Fixed SimpleXMLElement::__construct() accepting embedded null bytes in
47+
URL/path mode. (iliaal)
48+
4549
- Sockets:
4650
. Fixed various memory related issues in ext/sockets. (David Carlier)
4751

ext/simplexml/simplexml.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,11 @@ PHP_METHOD(SimpleXMLElement, __construct)
23342334
RETURN_THROWS();
23352335
}
23362336

2337+
if (is_url && CHECK_NULL_PATH(data, data_len)) {
2338+
zend_argument_value_error(1, "must not contain any null bytes");
2339+
RETURN_THROWS();
2340+
}
2341+
23372342
PHP_LIBXML_SANITIZE_GLOBALS(read_file_or_memory);
23382343
docp = is_url ? xmlReadFile(data, NULL, (int)options) : xmlReadMemory(data, (int)data_len, NULL, NULL, (int)options);
23392344
PHP_LIBXML_RESTORE_GLOBALS(read_file_or_memory);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
SimpleXMLElement constructor rejects embedded NUL in URL/path mode
3+
--EXTENSIONS--
4+
simplexml
5+
--FILE--
6+
<?php
7+
$tmp = tempnam(sys_get_temp_dir(), 'sxe');
8+
file_put_contents($tmp, '<r/>');
9+
$path = $tmp . "\0evil";
10+
try {
11+
new SimpleXMLElement($path, 0, true);
12+
echo "ctor: loaded\n";
13+
} catch (Throwable $e) {
14+
echo $e::class, ": ", $e->getMessage(), "\n";
15+
}
16+
try {
17+
simplexml_load_file($path);
18+
echo "load_file: loaded\n";
19+
} catch (Throwable $e) {
20+
echo $e::class, ": ", $e->getMessage(), "\n";
21+
}
22+
unlink($tmp);
23+
?>
24+
--EXPECT--
25+
ValueError: SimpleXMLElement::__construct(): Argument #1 ($data) must not contain any null bytes
26+
ValueError: simplexml_load_file(): Argument #1 ($filename) must not contain any null bytes

0 commit comments

Comments
 (0)