ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
CustomIconTempUploadPathTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use org\bovigo\vfs;
23use PHPUnit\Framework\TestCase;
24use PHPUnit\Framework\Attributes\DataProvider;
25
26class CustomIconTempUploadPathTest extends TestCase
27{
28 private function vfsStreamIsAvailable(): bool
29 {
30 return class_exists(vfs\vfsStreamWrapper::class);
31 }
32
33 private function skipIfVfsStreamNotAvailable(): void
34 {
35 if (!$this->vfsStreamIsAvailable()) {
36 $this->markTestSkipped(
37 'vfsStream (https://github.com/bovigo/vfsStream) is required for virtual filesystem tests.'
38 );
39 }
40 }
41
42 private function buildTestingObject(
43 string $temp_file_name,
44 string $ilias_data_dir
46 return new class (
47 $temp_file_name,
48 $ilias_data_dir
49 ) extends CustomIconTempUploadPath {
50 #[\Override]
51 protected function getRealPath(
52 string $path
53 ): string|false {
54 $normalized = str_replace('\\', '/', $path);
55 if (is_dir($path)) {
56 return rtrim($normalized, '/');
57 }
58
59 if (is_file($path)) {
60 return $normalized;
61 }
62
63 return false;
64 }
65 };
66 }
67
69 {
70 $this->skipIfVfsStreamNotAvailable();
71
72 vfs\vfsStream::setup();
73 vfs\vfsStream::create([
74 'data' => [
75 'temp' => [
76 'icon_upload.svg' => '<svg xmlns="http://www.w3.org/2000/svg"/>',
77 ],
78 ],
79 ]);
80
81 $data_dir = vfs\vfsStream::url('root/data');
82 $expected_file = vfs\vfsStream::url('root/data/temp/icon_upload.svg');
83
84 $path = $this->buildTestingObject(
85 'icon_upload.svg',
87 );
88
89 self::assertSame($expected_file, $path->getAbsolutePath());
90 }
91
93 {
94 $this->skipIfVfsStreamNotAvailable();
95
96 vfs\vfsStream::setup();
97 vfs\vfsStream::create([
98 'data' => [
99 'secret.txt' => 'sensitive',
100 'temp' => [],
101 ],
102 ]);
103
104 $data_dir = vfs\vfsStream::url('root/data');
105
106 $this->expectException(InvalidArgumentException::class);
107 $this->expectExceptionMessage('Temporary upload file not found.');
108
109 $this->buildTestingObject(
110 '../secret.txt',
112 );
113 }
114
116 {
117 $this->skipIfVfsStreamNotAvailable();
118
119 vfs\vfsStream::setup();
120 vfs\vfsStream::create([
121 'data' => [
122 'temp' => [],
123 ],
124 ]);
125
126 $data_dir = vfs\vfsStream::url('root/data');
127
128 $this->expectException(InvalidArgumentException::class);
129 $this->expectExceptionMessage('Temporary upload file not found.');
130
131 $this->buildTestingObject(
132 'missing.svg',
134 );
135 }
136
137 #[DataProvider('invalidTemporaryFileNameProvider')]
139 string $malicious_input
140 ): void {
141 $this->expectException(InvalidArgumentException::class);
142 $this->expectExceptionMessage('Invalid temporary upload file name.');
143
144 $this->buildTestingObject(
145 $malicious_input,
146 '/does/not/matter'
147 );
148 }
149
153 public static function invalidTemporaryFileNameProvider(): Generator
154 {
155 yield 'parent directory segment' => ['..'];
156 yield 'current directory segment' => ['.'];
157 }
158}
testInvalidTemporaryFileNameIsRejected(string $malicious_input)
buildTestingObject(string $temp_file_name, string $ilias_data_dir)
Resolves a user-supplied temp file identifier to an absolute path that is guaranteed to refer to a re...
return['3gp', '7z', 'ai', 'aif', 'aifc', 'aiff', 'au', 'arw', 'avi', 'backup', 'bak', 'bas', 'bpmn', 'bpmn2', 'bmp', 'bib', 'bibtex', 'bz', 'bz2', 'c', 'c++', 'cc', 'cct', 'cdf', 'cer', 'class', 'cls', 'conf', 'cpp', 'crt', 'crs', 'crw', 'cr2', 'css', 'cst', 'csv', 'cur', 'db', 'dcr', 'des', 'dng', 'doc', 'docx', 'dot', 'dotx', 'dtd', 'dvi', 'el', 'eps', 'epub', 'f', 'f77', 'f90', 'flv', 'for', 'g3', 'gif', 'gl', 'gan', 'ggb', 'gsd', 'gsm', 'gtar', 'gz', 'gzip', 'h', 'hpp', 'htm', 'html', 'htmls', 'ibooks', 'ico', 'ics', 'ini', 'ipynb', 'java', 'jbf', 'jpeg', 'jpg', 'js', 'jsf', 'jso', 'json', 'latex', 'lang', 'less', 'log', 'lsp', 'ltx', 'm1v', 'm2a', 'm2v', 'm3u', 'm4a', 'm4v', 'markdown', 'm', 'mat', 'md', 'mdl', 'mdown', 'mid', 'min', 'midi', 'mobi', 'mod', 'mov', 'movie', 'mp2', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'mph', 'mpga', 'mpp', 'mpt', 'mpv', 'mpx', 'mv', 'mw', 'mv4', 'nb', 'nbp', 'nef', 'nif', 'niff', 'obj', 'obm', 'odt', 'ods', 'odp', 'odg', 'odf', 'oga', 'ogg', 'ogv', 'old', 'p', 'pas', 'pbm', 'pcl', 'pct', 'pcx', 'pdf', 'pgm', 'pic', 'pict', 'png', 'por', 'pov', 'project', 'properties', 'ppa', 'ppm', 'pps', 'ppsx', 'ppt', 'pptx', 'ppz', 'ps', 'psd', 'pwz', 'qt', 'qtc', 'qti', 'qtif', 'r', 'ra', 'ram', 'rar', 'rast', 'rda', 'rev', 'rexx', 'ris', 'rf', 'rgb', 'rm', 'rmd', 'rmi', 'rmm', 'rmp', 'rt', 'rtf', 'rtx', 'rv', 's', 's3m', 'sav', 'sbs', 'sec', 'sdml', 'sgm', 'sgml', 'smi', 'smil', 'srt', 'sps', 'spv', 'stl', 'svg', 'swa', 'swf', 'swz', 'tar', 'tex', 'texi', 'texinfo', 'text', 'tgz', 'tif', 'tiff', 'ttf', 'txt', 'tmp', 'uvproj', 'vdf', 'vimeo', 'viv', 'vivo', 'vrml', 'vsdx', 'vtt', 'wav', 'webm', 'wmv', 'wmx', 'wmz', 'woff', 'wwd', 'xhtml', 'xif', 'xls', 'xlsx', 'xmind', 'xml', 'xsl', 'xsd', 'zip']
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...