Skip to content

Commit f3d1e9e

Browse files
stephanknikic
andcommitted
Make tests compatible with libxml2 2.9.12
This version of libxml introduced quite a few changes. Most of them are differences in error reporting, while some also change behavior, e.g. null bytes are no longer supported and xinclude recursion is limited. Closes GH-7030. Closes GH-7046. Co-authored-by: Nikita Popov <nikic@php.net>
1 parent ee9e075 commit f3d1e9e

9 files changed

+150
-7
lines changed

ext/dom/tests/DOMDocument_loadXML_error1.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ domdocumentloadxml_test_method.inc
2121
--EXPECTF--
2222
Warning: DOMDocument::load%r(XML){0,1}%r(): Opening and ending tag mismatch: title line 5 and book %s
2323

24-
Warning: DOMDocument::load%r(XML){0,1}%r(): %rexpected '>'|Opening and ending tag mismatch: book line 5 and books%r %s
24+
Warning: DOMDocument::load%r(XML){0,1}%r(): %rexpected '>'|Opening and ending tag mismatch: book line (4|5) and books%r %s
2525

2626
Warning: DOMDocument::load%r(XML){0,1}%r(): %rPremature end of data in tag books|EndTag: '<\/' not found in Entity, line: 13%r %s

ext/dom/tests/DOMDocument_load_error1.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ domdocumentload_test_method.inc
2121
--EXPECTF--
2222
Warning: DOMDocument::load%r(XML){0,1}%r(): Opening and ending tag mismatch: title line 5 and book %s
2323

24-
Warning: DOMDocument::load%r(XML){0,1}%r(): %rexpected '>'|Opening and ending tag mismatch: book line 5 and books%r %s
24+
Warning: DOMDocument::load%r(XML){0,1}%r(): %rexpected '>'|Opening and ending tag mismatch: book line (4|5) and books%r %s
2525

2626
Warning: DOMDocument::load%r(XML){0,1}%r(): %rPremature end of data in tag books|EndTag: '<\/' not found%r %s

ext/dom/tests/bug43364.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ $doc->xinclude();
3636

3737
$count = loopElements(array($doc->documentElement));
3838

39-
var_dump($count);
39+
var_dump($count == 13 || $count == 11);
4040
?>
4141
--EXPECT--
42-
int(13)
42+
bool(true)

ext/dom/tests/bug80268.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
--TEST--
22
Bug #80268 (loadHTML() truncates at NUL bytes)
33
--SKIPIF--
4-
<?php require_once('skipif.inc'); ?>
4+
<?php
5+
require_once('skipif.inc');
6+
if (LIBXML_VERSION >= 20912) die('skip For libxml2 < 2.9.12 only');
7+
?>
58
--FILE--
69
<?php
710
$doc = new DOMDocument;

ext/dom/tests/bug80268_2.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Bug #80268 (loadHTML() truncates at NUL bytes)
3+
--SKIPIF--
4+
<?php
5+
require_once('skipif.inc');
6+
if (LIBXML_VERSION < 20912) die('skip For libxml2 >= 2.9.12 only');
7+
?>
8+
--FILE--
9+
<?php
10+
$doc = new DOMDocument;
11+
$doc->loadHTML("<p>foo\0bar</p>");
12+
$html = $doc->saveHTML();
13+
var_dump(strpos($html, '<p>foo</p>') !== false);
14+
15+
file_put_contents(__DIR__ . '/80268.html', "<p>foo\0bar</p>");
16+
$doc = new DOMDocument;
17+
$doc->loadHTMLFile(__DIR__ . '/80268.html');
18+
$html = $doc->saveHTML();
19+
var_dump(strpos($html, '<p>foo</p>') !== false);
20+
?>
21+
--CLEAN--
22+
<?php
23+
unlink(__DIR__ . '/80268.html');
24+
?>
25+
--EXPECTF--
26+
Warning: DOMDocument::loadHTML(): Char 0x0 out of allowed range in Entity, line: 1 in %s on line %d
27+
bool(false)
28+
29+
Warning: DOMDocument::loadHTMLFile(): Char 0x0 out of allowed range in %s on line %d
30+
bool(false)

ext/libxml/tests/bug61367-read.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
--TEST--
22
Bug #61367: open_basedir bypass in libxml RSHUTDOWN: read test
33
--SKIPIF--
4-
<?php if(!extension_loaded('dom')) echo 'skip'; ?>
4+
<?php
5+
if(!extension_loaded('dom')) echo 'skip dom extension not available';
6+
if (LIBXML_VERSION >= 20912) die('skip For libxml2 < 2.9.12 only');
7+
?>
58
--INI--
69
open_basedir=.
710
error_reporting=E_ALL & ~E_NOTICE

ext/libxml/tests/bug61367-read_2.phpt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
Bug #61367: open_basedir bypass in libxml RSHUTDOWN: read test
3+
--SKIPIF--
4+
<?php
5+
if(!extension_loaded('dom')) echo 'skip dom extension not available';
6+
if (LIBXML_VERSION < 20912) die('skip For libxml2 >= 2.9.12 only');
7+
?>
8+
--INI--
9+
open_basedir=.
10+
--FILE--
11+
<?php
12+
/*
13+
* Note: Using error_reporting=E_ALL & ~E_NOTICE to suppress "Trying to get property of non-object" notices.
14+
*/
15+
class StreamExploiter {
16+
public function stream_close ( ) {
17+
$doc = new DOMDocument;
18+
$doc->resolveExternals = true;
19+
$doc->substituteEntities = true;
20+
$dir = htmlspecialchars(dirname(getcwd()));
21+
$dir = str_replace('\\', '/', $dir); // fix for windows
22+
$doc->loadXML( <<<XML
23+
<!DOCTYPE doc [
24+
<!ENTITY file SYSTEM "file:///$dir/bad">
25+
]>
26+
<doc>&file;</doc>
27+
XML
28+
);
29+
print $doc->documentElement->firstChild->nodeValue;
30+
}
31+
32+
public function stream_open ( $path , $mode , $options , &$opened_path ) {
33+
return true;
34+
}
35+
}
36+
37+
var_dump(mkdir('test_bug_61367-read'));
38+
var_dump(mkdir('test_bug_61367-read/base'));
39+
var_dump(file_put_contents('test_bug_61367-read/bad', 'blah'));
40+
var_dump(chdir('test_bug_61367-read/base'));
41+
42+
stream_wrapper_register( 'exploit', 'StreamExploiter' );
43+
$s = fopen( 'exploit://', 'r' );
44+
45+
?>
46+
--CLEAN--
47+
<?php
48+
unlink('test_bug_61367-read/bad');
49+
rmdir('test_bug_61367-read/base');
50+
rmdir('test_bug_61367-read');
51+
?>
52+
--EXPECTF--
53+
bool(true)
54+
bool(true)
55+
int(4)
56+
bool(true)
57+
58+
Warning: DOMDocument::loadXML(): I/O warning : failed to load external entity "file:///%s/test_bug_61367-read/bad" in %s on line %d
59+
60+
Warning: Attempt to read property "nodeValue" on null in %s on line %d

ext/libxml/tests/libxml_disable_entity_loader.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
--TEST--
22
libxml_disable_entity_loader()
33
--SKIPIF--
4-
<?php if (!extension_loaded('libxml') || !extension_loaded('dom')) die('skip'); ?>
4+
<?php
5+
if (!extension_loaded('libxml')) die('skip libxml extension not available');
6+
if (!extension_loaded('dom')) die('skip dom extension not available');
7+
if (LIBXML_VERSION >= 20912) die('skip For libxml2 < 2.9.12 only');
8+
?>
59
--FILE--
610
<?php
711

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
libxml_disable_entity_loader()
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('libxml')) die('skip libxml extension not available');
6+
if (!extension_loaded('dom')) die('skip dom extension not available');
7+
if (LIBXML_VERSION < 20912) die('skip For libxml2 >= 2.9.12 only');
8+
--FILE--
9+
<?php
10+
11+
$xml = <<<EOT
12+
<?xml version="1.0" encoding="UTF-8"?>
13+
<!DOCTYPE test [<!ENTITY xxe SYSTEM "XXE_URI">]>
14+
<foo>&xxe;</foo>
15+
EOT;
16+
17+
$dir = str_replace('\\', '/', __DIR__);
18+
$xml = str_replace('XXE_URI', $dir . '/libxml_disable_entity_loader_payload.txt', $xml);
19+
20+
function parseXML($xml) {
21+
$doc = new DOMDocument();
22+
$doc->resolveExternals = true;
23+
$doc->substituteEntities = true;
24+
$doc->validateOnParse = false;
25+
$doc->loadXML($xml, 0);
26+
return $doc->saveXML();
27+
}
28+
29+
var_dump(strpos(parseXML($xml), 'SECRET_DATA') !== false);
30+
var_dump(libxml_disable_entity_loader(true));
31+
var_dump(strpos(parseXML($xml), 'SECRET_DATA') === false);
32+
33+
echo "Done\n";
34+
?>
35+
--EXPECTF--
36+
bool(true)
37+
38+
Deprecated: Function libxml_disable_entity_loader() is deprecated in %s on line %d
39+
bool(false)
40+
41+
Warning: DOMDocument::loadXML(): I/O warning : failed to load external entity "%s" in %s on line %d
42+
bool(true)
43+
Done

0 commit comments

Comments
 (0)