Skip to content

Commit 51cd033

Browse files
committed
Fix unit tests
1 parent 858ba20 commit 51cd033

2 files changed

Lines changed: 13 additions & 16 deletions

File tree

‎tests/XML/DOMDocumentFactoryTest.php‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use DOMException;
99
use PHPUnit\Framework\Attributes\CoversClass;
1010
use PHPUnit\Framework\Attributes\Group;
11+
use PHPUnit\Framework\Attributes\RequiresOperatingSystemFamily;
1112
use PHPUnit\Framework\TestCase;
1213
use RuntimeException;
1314
use SimpleSAML\Assert\AssertionFailedException;
@@ -19,6 +20,7 @@
1920
use function is_file;
2021
use function mb_convert_encoding;
2122
use function random_bytes;
23+
use function str_replace;
2224
use function strval;
2325
use function sys_get_temp_dir;
2426

@@ -37,7 +39,7 @@ final class DOMDocumentFactoryTest extends TestCase
3739
protected function setUp(): void
3840
{
3941
$this->marker = 'XXE_TEST_MARKER_' . bin2hex(random_bytes(16));
40-
$this->secretFile = sys_get_temp_dir() . '/xml-common-xxe-test-' . getmypid() . '.txt';
42+
$this->secretFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'xml-common-xxe-test-' . getmypid() . '.txt';
4143
file_put_contents($this->secretFile, $this->marker);
4244
}
4345

@@ -54,20 +56,19 @@ protected function tearDown(): void
5456
* Classic external-entity payload aimed at a file we control.
5557
* The factory must reject the document; the marker must never appear.
5658
*/
59+
#[RequiresOperatingSystemFamily('Linux')]
5760
public function testExternalEntityCannotReadLocalFileUsingDefaultOptions(): void
5861
{
5962
// file:// URI pointing at our temporary secret
60-
$uri = 'file://' . $this->secretFile;
61-
63+
$uri = 'file:///' . str_replace(DIRECTORY_SEPARATOR, '/', $this->secretFile);
6264
$payload = <<<XML
63-
<?xml version="1.0" encoding="UTF-8"?>
6465
<!DOCTYPE foo [
65-
<!ELEMENT foo ANY>
6666
<!ENTITY xxe SYSTEM "{$uri}">
6767
]>
6868
<foo>&xxe;</foo>
6969
XML;
7070

71+
var_dump($payload);
7172
$doc = Dom\XMLDocument::createFromString($payload, DOMDocumentFactory::getDefaultOptions());
7273
// If we reach here the protection failed – check that the secret did not leak into the document.
7374
$xml = $doc->saveXml();
@@ -83,15 +84,13 @@ public function testExternalEntityCannotReadLocalFileUsingDefaultOptions(): void
8384
* Classic external-entity payload aimed at a file we control.
8485
* The factory must reject the document; the marker must never appear.
8586
*/
87+
#[RequiresOperatingSystemFamily('Linux')]
8688
public function testExternalEntityCannotReadLocalFileUsingNonDefaultOptions(): void
8789
{
8890
// file:// URI pointing at our temporary secret
89-
$uri = 'file://' . $this->secretFile;
90-
91+
$uri = 'file://' . str_replace(DIRECTORY_SEPARATOR, '/', $this->secretFile);
9192
$payload = <<<XML
92-
<?xml version="1.0" encoding="UTF-8"?>
9393
<!DOCTYPE foo [
94-
<!ELEMENT foo ANY>
9594
<!ENTITY xxe SYSTEM "{$uri}">
9695
]>
9796
<foo>&xxe;</foo>
@@ -111,14 +110,13 @@ public function testExternalEntityCannotReadLocalFileUsingNonDefaultOptions(): v
111110
/**
112111
* Same idea with a UTF-16LE encoded payload.
113112
*/
113+
#[RequiresOperatingSystemFamily('Linux')]
114114
public function testUtf16ExternalEntityCannotReadLocalFileUsingDefaultOptions(): void
115115
{
116116
$uri = 'file://' . $this->secretFile;
117117

118118
$utf8 = <<<XML
119-
<?xml version="1.0" encoding="UTF-16"?>
120119
<!DOCTYPE foo [
121-
<!ELEMENT foo ANY>
122120
<!ENTITY xxe SYSTEM "{$uri}">
123121
]>
124122
<foo>&xxe;</foo>
@@ -140,14 +138,13 @@ public function testUtf16ExternalEntityCannotReadLocalFileUsingDefaultOptions():
140138
/**
141139
* Same idea with a UTF-16LE encoded payload.
142140
*/
141+
#[RequiresOperatingSystemFamily('Linux')]
143142
public function testUtf16ExternalEntityCannotReadLocalFileUsingNonDefaultOptions(): void
144143
{
145144
$uri = 'file://' . $this->secretFile;
146145

147146
$utf8 = <<<XML
148-
<?xml version="1.0" encoding="UTF-16"?>
149147
<!DOCTYPE foo [
150-
<!ELEMENT foo ANY>
151148
<!ENTITY xxe SYSTEM "{$uri}">
152149
]>
153150
<foo>&xxe;</foo>

‎tests/XPath/XPathTest.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function testSkipsDefaultNamespaceDeclarationDoesNotCreateUsableXmlnsPref
220220
XPath::xpQuery($context, 'xmlns:b', $xp);
221221
} finally {
222222
$errors = libxml_get_errors();
223-
$this->assertEquals("Undefined namespace prefix\n", $errors[0]->message);
223+
$this->assertStringStartsWith("Undefined namespace prefix", $errors[0]->message);
224224
libxml_clear_errors();
225225
libxml_use_internal_errors(false);
226226
}
@@ -251,7 +251,7 @@ public function testSkipsEmptyUriNamespaceDeclaration(): void
251251
} finally {
252252
$errors = libxml_get_errors();
253253
$this->assertEquals("xmlns:empty: Empty XML namespace is not allowed\n", $errors[0]->message);
254-
$this->assertEquals("Undefined namespace prefix\n", $errors[1]->message);
254+
$this->assertStringStartsWith("Undefined namespace prefix", $errors[1]->message);
255255
libxml_clear_errors();
256256
libxml_use_internal_errors(false);
257257
}
@@ -352,7 +352,7 @@ public function testAbsoluteXPathFindsTopLevelSlatePerson(
352352
} finally {
353353
$errors = libxml_get_errors();
354354
$this->assertNotEmpty($errors);
355-
$this->assertSame("Undefined namespace prefix\n", $errors[0]->message);
355+
$this->assertStringStartsWith("Undefined namespace prefix", $errors[0]->message);
356356
libxml_clear_errors();
357357
libxml_use_internal_errors(false);
358358
}

0 commit comments

Comments
 (0)