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