-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a small PHP 8.0 fix #3
Changes from all commits
0998c41
55716b8
1a267d5
c1b12d1
e393636
d0b105b
895415a
ab35cfd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/.phpunit.result.cache | ||
/clover.xml | ||
/composer.lock | ||
/coveralls-upload.json | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true"> | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use the local XML schema definition, otherwise we have to update it with each version.. |
||
bootstrap="vendor/autoload.php" | ||
colors="true"> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</coverage> | ||
|
||
<testsuites> | ||
<testsuite name="laminas-xml Test Suite"> | ||
<directory>./test</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
namespace LaminasTest\Xml; | ||
|
||
use Laminas\Xml\Exception; | ||
use Laminas\Xml\Exception\RuntimeException; | ||
use Laminas\Xml\Security; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionMethod; | ||
|
||
|
@@ -17,7 +19,10 @@ | |
*/ | ||
class MultibyteTest extends TestCase | ||
{ | ||
public function multibyteEncodings() | ||
/** | ||
* @psalm-return array<array-key, array{0: string, 1: string, 2: int}> | ||
*/ | ||
public function multibyteEncodings(): array | ||
{ | ||
return [ | ||
'UTF-16LE' => ['UTF-16LE', pack('CC', 0xff, 0xfe), 3], | ||
|
@@ -27,76 +32,79 @@ public function multibyteEncodings() | |
]; | ||
} | ||
|
||
public function getXmlWithXXE() | ||
public function getXmlWithXXE(): string | ||
{ | ||
return <<<XML | ||
<?xml version="1.0" encoding="{ENCODING}"?> | ||
<!DOCTYPE methodCall [ | ||
<!ENTITY pocdata SYSTEM "file:///etc/passwd"> | ||
]> | ||
<methodCall> | ||
<methodName>retrieved: &pocdata;</methodName> | ||
</methodCall> | ||
XML; | ||
<?xml version="1.0" encoding="{ENCODING}"?> | ||
<!DOCTYPE methodCall [ | ||
<!ENTITY pocdata SYSTEM "file:///etc/passwd"> | ||
]> | ||
<methodCall> | ||
<methodName>retrieved: &pocdata;</methodName> | ||
</methodCall> | ||
XML; | ||
Comment on lines
+38
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a note, in case anybody wonders why I did this: PHP 7.3 adds the ability to indent heredocs and nowdocs. In each case, any leading whitespace before the closing marker is considered indentation, and stripped from each line of the string. This makes them far more readable when declared in indented code, which was the whole point of the change. |
||
} | ||
|
||
/** | ||
* Invoke Laminas\Xml\Security::heuristicScan with the provided XML. | ||
* | ||
* @param string $xml | ||
* @return void | ||
* @throws Exception\RuntimeException | ||
*/ | ||
public function invokeHeuristicScan($xml) | ||
public function invokeHeuristicScan(string $xml): void | ||
{ | ||
$r = new ReflectionMethod('Laminas\Xml\Security', 'heuristicScan'); | ||
$r = new ReflectionMethod(Security::class, 'heuristicScan'); | ||
$r->setAccessible(true); | ||
return $r->invoke(null, $xml); | ||
$r->invoke(null, $xml); | ||
} | ||
|
||
/** | ||
* @dataProvider multibyteEncodings | ||
* @group heuristicDetection | ||
*/ | ||
public function testDetectsMultibyteXXEVectorsUnderFPMWithEncodedStringMissingBOM($encoding, $bom, $bomLength) | ||
{ | ||
public function testDetectsMultibyteXXEVectorsUnderFPMWithEncodedStringMissingBOM( | ||
string $encoding, | ||
string $bom, | ||
int $bomLength | ||
): void { | ||
$xml = $this->getXmlWithXXE(); | ||
$xml = str_replace('{ENCODING}', $encoding, $xml); | ||
$xml = iconv('UTF-8', $encoding, $xml); | ||
$this->assertNotSame(0, strncmp($xml, $bom, $bomLength)); | ||
$this->expectException('Laminas\Xml\Exception\RuntimeException'); | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('ENTITY'); | ||
$this->invokeHeuristicScan($xml); | ||
} | ||
|
||
/** | ||
* @dataProvider multibyteEncodings | ||
*/ | ||
public function testDetectsMultibyteXXEVectorsUnderFPMWithEncodedStringUsingBOM($encoding, $bom) | ||
{ | ||
public function testDetectsMultibyteXXEVectorsUnderFPMWithEncodedStringUsingBOM( | ||
string $encoding, | ||
string $bom | ||
): void { | ||
$xml = $this->getXmlWithXXE(); | ||
$xml = str_replace('{ENCODING}', $encoding, $xml); | ||
$orig = iconv('UTF-8', $encoding, $xml); | ||
$xml = $bom . $orig; | ||
$this->expectException('Laminas\Xml\Exception\RuntimeException'); | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('ENTITY'); | ||
$this->invokeHeuristicScan($xml); | ||
} | ||
|
||
public function getXmlWithoutXXE() | ||
public function getXmlWithoutXXE(): string | ||
{ | ||
return <<<XML | ||
<?xml version="1.0" encoding="{ENCODING}"?> | ||
<methodCall> | ||
<methodName>retrieved: &pocdata;</methodName> | ||
</methodCall> | ||
XML; | ||
<?xml version="1.0" encoding="{ENCODING}"?> | ||
<methodCall> | ||
<methodName>retrieved: &pocdata;</methodName> | ||
</methodCall> | ||
XML; | ||
} | ||
|
||
/** | ||
* @dataProvider multibyteEncodings | ||
*/ | ||
public function testDoesNotFlagValidMultibyteXmlAsInvalidUnderFPM($encoding) | ||
public function testDoesNotFlagValidMultibyteXmlAsInvalidUnderFPM(string $encoding): void | ||
{ | ||
$xml = $this->getXmlWithoutXXE(); | ||
$xml = str_replace('{ENCODING}', $encoding, $xml); | ||
|
@@ -113,13 +121,15 @@ public function testDoesNotFlagValidMultibyteXmlAsInvalidUnderFPM($encoding) | |
* @dataProvider multibyteEncodings | ||
* @group mixedEncoding | ||
*/ | ||
public function testDetectsXXEWhenXMLDocumentEncodingDiffersFromFileEncoding($encoding, $bom) | ||
{ | ||
public function testDetectsXXEWhenXMLDocumentEncodingDiffersFromFileEncoding( | ||
string $encoding, | ||
string $bom | ||
): void { | ||
$xml = $this->getXmlWithXXE(); | ||
$xml = str_replace('{ENCODING}', 'UTF-8', $xml); | ||
$xml = iconv('UTF-8', $encoding, $xml); | ||
$xml = $bom . $xml; | ||
$this->expectException('Laminas\Xml\Exception\RuntimeException'); | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('ENTITY'); | ||
$this->invokeHeuristicScan($xml); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By bumping phpunit, you definitely have to change almost all tests contain
setUp
andtearDown
aswell as have to check for some assertion methods.Did you execute tests locally? You might want to fix those tests even without travis. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did run the tests on PHP 8 and via
./vendor/bin/phpunit
so not sure what happened there if they would be incompatible. I will double-check and see if I did something wrong 👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@boesing There are two test cases in the suite, and neither use
setUp
ortearDown
, so tests actually do run!@xvilo I'm doing updates and testing manually, and will push to your branch for final review shortly. Thanks for getting this started!