1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Security; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xls; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class XmlScannerTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
protected function setUp() |
13
|
|
|
{ |
14
|
|
|
libxml_disable_entity_loader(false); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @dataProvider providerValidXML |
19
|
|
|
* |
20
|
|
|
* @param mixed $filename |
21
|
|
|
* @param mixed $expectedResult |
22
|
|
|
* @param $libxmlDisableEntityLoader |
23
|
|
|
*/ |
24
|
|
|
public function testValidXML($filename, $expectedResult, $libxmlDisableEntityLoader) |
25
|
|
|
{ |
26
|
|
|
$oldDisableEntityLoaderState = libxml_disable_entity_loader($libxmlDisableEntityLoader); |
27
|
|
|
|
28
|
|
|
$reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml()); |
29
|
|
|
$result = $reader->scanFile($filename); |
30
|
|
|
self::assertEquals($expectedResult, $result); |
31
|
|
|
|
32
|
|
|
libxml_disable_entity_loader($oldDisableEntityLoaderState); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function providerValidXML() |
36
|
|
|
{ |
37
|
|
|
$tests = []; |
38
|
|
|
foreach (glob(__DIR__ . '/../../../data/Reader/Xml/XEETestValid*.xml') as $file) { |
39
|
|
|
$filename = realpath($file); |
40
|
|
|
$expectedResult = file_get_contents($file); |
41
|
|
|
$tests[basename($file) . '_libxml_entity_loader_disabled'] = [$filename, $expectedResult, true]; |
42
|
|
|
$tests[basename($file) . '_libxml_entity_loader_enabled'] = [$filename, $expectedResult, false]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $tests; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider providerInvalidXML |
50
|
|
|
* |
51
|
|
|
* @param mixed $filename |
52
|
|
|
* @param $libxmlDisableEntityLoader |
53
|
|
|
*/ |
54
|
|
|
public function testInvalidXML($filename, $libxmlDisableEntityLoader) |
55
|
|
|
{ |
56
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class); |
57
|
|
|
|
58
|
|
|
libxml_disable_entity_loader($libxmlDisableEntityLoader); |
59
|
|
|
|
60
|
|
|
$reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml()); |
61
|
|
|
$expectedResult = 'FAILURE: Should throw an Exception rather than return a value'; |
62
|
|
|
$result = $reader->scanFile($filename); |
63
|
|
|
self::assertEquals($expectedResult, $result); |
64
|
|
|
self::assertEquals($libxmlDisableEntityLoader, libxml_disable_entity_loader()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function providerInvalidXML() |
68
|
|
|
{ |
69
|
|
|
$tests = []; |
70
|
|
|
foreach (glob(__DIR__ . '/../../../data/Reader/Xml/XEETestInvalidUTF*.xml') as $file) { |
71
|
|
|
$filename = realpath($file); |
72
|
|
|
$tests[basename($file) . '_libxml_entity_loader_disabled'] = [$filename, true]; |
73
|
|
|
$tests[basename($file) . '_libxml_entity_loader_enabled'] = [$filename, false]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $tests; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testGetSecurityScannerForXmlBasedReader() |
80
|
|
|
{ |
81
|
|
|
$fileReader = new Xlsx(); |
82
|
|
|
$scanner = $fileReader->getSecurityScanner(); |
83
|
|
|
|
84
|
|
|
// Must return an object... |
85
|
|
|
$this->assertInternalType('object', $scanner); |
86
|
|
|
// ... of the correct type |
87
|
|
|
$this->assertInstanceOf(XmlScanner::class, $scanner); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testGetSecurityScannerForNonXmlBasedReader() |
91
|
|
|
{ |
92
|
|
|
$fileReader = new Xls(); |
93
|
|
|
$scanner = $fileReader->getSecurityScanner(); |
94
|
|
|
// Must return a null... |
95
|
|
|
$this->assertNull($scanner); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @dataProvider providerValidXMLForCallback |
100
|
|
|
* |
101
|
|
|
* @param mixed $filename |
102
|
|
|
* @param mixed $expectedResult |
103
|
|
|
*/ |
104
|
|
|
public function testSecurityScanWithCallback($filename, $expectedResult) |
105
|
|
|
{ |
106
|
|
|
$fileReader = new Xlsx(); |
107
|
|
|
$scanner = $fileReader->getSecurityScanner(); |
108
|
|
|
$scanner->setAdditionalCallback('strrev'); |
109
|
|
|
$xml = $scanner->scanFile($filename); |
110
|
|
|
|
111
|
|
|
$this->assertEquals(strrev($expectedResult), $xml); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function providerValidXMLForCallback() |
115
|
|
|
{ |
116
|
|
|
$tests = []; |
117
|
|
|
foreach (glob(__DIR__ . '/../../../data/Reader/Xml/SecurityScannerWithCallback*.xml') as $file) { |
118
|
|
|
$tests[basename($file)] = [realpath($file), file_get_contents($file)]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $tests; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testLibxmlDisableEntityLoaderIsRestoredWithoutShutdown() |
125
|
|
|
{ |
126
|
|
|
$reader = new Xlsx(); |
127
|
|
|
unset($reader); |
128
|
|
|
|
129
|
|
|
$reader = new \XMLReader(); |
130
|
|
|
$opened = $reader->open(__DIR__ . '/../../../data/Reader/Xml/SecurityScannerWithCallbackExample.xml'); |
131
|
|
|
$this->assertTrue($opened); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testEncodingAllowsMixedCase() |
135
|
|
|
{ |
136
|
|
|
$scanner = new XmlScanner(); |
137
|
|
|
$output = $scanner->scan($input = '<?xml version="1.0" encoding="utf-8"?><foo>bar</foo>'); |
138
|
|
|
$this->assertSame($input, $output); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|