Repository navigation
Expand file tree
/
Copy pathSecurityHelperTest.php
More file actions
346 lines (310 loc) · 16.2 KB
/
Copy pathSecurityHelperTest.php
File metadata and controls
346 lines (310 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Helpers;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockSecurity;
use Config\Security as SecurityConfig;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Config\Services;
/**
* @internal
*/
#[Group('Others')]
final class SecurityHelperTest extends CIUnitTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->resetServices();
helper('security');
}
public function testSanitizeFilenameSimpleSuccess(): void
{
Services::injectMock('security', new MockSecurity(new SecurityConfig()));
$this->assertSame('hello.doc', sanitize_filename('hello.doc'));
}
public function testSanitizeFilenameStripsExtras(): void
{
$filename = './<!--foo -->';
$this->assertSame('foo ', sanitize_filename($filename));
}
#[DataProvider('provideSanitizeFilenamePaths')]
public function testSanitizeFilenamePaths(string $filename, bool $relativePath, string $expected): void
{
$this->assertSame($expected, sanitize_filename($filename, $relativePath));
}
/**
* @return iterable<string, array{string, bool, string}>
*/
public static function provideSanitizeFilenamePaths(): iterable
{
$backslash = chr(92);
$doubleBackslash = str_repeat($backslash, 2);
$tripleBackslash = str_repeat($backslash, 3);
$fourBackslashes = str_repeat($backslash, 4);
$eightBackslashes = str_repeat($backslash, 8);
return [
'empty filename' => ['', false, ''],
'empty relative path' => ['', true, ''],
'absolute path in relative mode' => ['/etc/passwd', true, 'etc/passwd'],
'multiple leading slashes' => ['////etc/passwd', true, 'etc/passwd'],
'double-slash traversal' => ['..//..//etc/passwd', true, 'etc/passwd'],
'triple-slash traversal' => ['..///..///etc/passwd', true, 'etc/passwd'],
'leading slash exposed by filters' => ['</etc/passwd', true, 'etc/passwd'],
'leading slash exposed by backslash' => [$backslash . '/etc/passwd', true, 'etc/passwd'],
'only forward slashes' => ['////', true, ''],
'only traversal and slashes' => ['..//..///', true, ''],
'preserve internal slashes' => ['approved//folder/file.txt', true, 'approved//folder/file.txt'],
'preserve trailing slash' => ['approved/folder/', true, 'approved/folder/'],
'absolute path in default mode' => ['/etc/passwd', false, 'etcpasswd'],
'double-slash traversal in default mode' => ['..//..//etc/passwd', false, 'etcpasswd'],
'escaped NUL in filename' => ['a' . $backslash . '0b', false, 'a0b'],
'escaped NUL in relative path' => ['a' . $backslash . '0b', true, 'a0b'],
'literal NUL in filename' => ['a' . chr(0) . 'b', false, 'ab'],
'literal NUL in relative path' => ['a' . chr(0) . 'b', true, 'ab'],
'strip forward path' => [
'approved/folder/file.txt',
false,
'approvedfolderfile.txt',
],
'preserve forward relative path' => [
'approved/folder/file.txt',
true,
'approved/folder/file.txt',
],
'strip forward traversal' => ['../../public/file.txt', false, 'publicfile.txt'],
'relative forward traversal' => ['../../public/file.txt', true, 'public/file.txt'],
'forward traversal exposed by filters' => ['.<.>/public/file.txt', true, 'public/file.txt'],
'forward traversal split by backslash' => ['.' . $backslash . './file.txt', true, 'file.txt'],
'backslash traversal split by backslash' => [
'.' . $backslash . '.' . $doubleBackslash . 'file.txt',
true,
'..file.txt',
],
'mixed separators' => [
'..' . $doubleBackslash . '../public' . $doubleBackslash . 'file.txt',
true,
'..publicfile.txt',
],
'backslashes exposed by filters' => [
'folder' . $backslash . '<' . $backslash . 'file.txt',
true,
'folderfile.txt',
],
'filename traversal with single backslash' => [
'..' . $backslash . '..' . $backslash . 'public' . $backslash . 'file.txt',
false,
'....publicfile.txt',
],
'filename path with single backslash' => [
'folder' . $backslash . 'file.txt',
false,
'folderfile.txt',
],
'relative traversal with single backslash' => [
'..' . $backslash . '..' . $backslash . 'public' . $backslash . 'file.txt',
true,
'....publicfile.txt',
],
'relative path with single backslash' => [
'folder' . $backslash . 'file.txt',
true,
'folderfile.txt',
],
'filename traversal with double backslashes' => [
'..' . $doubleBackslash . '..' . $doubleBackslash . 'public' . $doubleBackslash . 'file.txt',
false,
'....publicfile.txt',
],
'filename path with double backslashes' => [
'folder' . $doubleBackslash . 'file.txt',
false,
'folderfile.txt',
],
'relative traversal with double backslashes' => [
'..' . $doubleBackslash . '..' . $doubleBackslash . 'public' . $doubleBackslash . 'file.txt',
true,
'....publicfile.txt',
],
'relative path with double backslashes' => [
'folder' . $doubleBackslash . 'file.txt',
true,
'folderfile.txt',
],
'filename traversal with triple backslashes' => [
'..' . $tripleBackslash . '..' . $tripleBackslash . 'public' . $tripleBackslash . 'file.txt',
false,
'....publicfile.txt',
],
'filename path with triple backslashes' => [
'folder' . $tripleBackslash . 'file.txt',
false,
'folderfile.txt',
],
'relative traversal with triple backslashes' => [
'..' . $tripleBackslash . '..' . $tripleBackslash . 'public' . $tripleBackslash . 'file.txt',
true,
'....publicfile.txt',
],
'relative path with triple backslashes' => [
'folder' . $tripleBackslash . 'file.txt',
true,
'folderfile.txt',
],
'filename traversal with four backslashes' => [
'..' . $fourBackslashes . '..' . $fourBackslashes . 'public' . $fourBackslashes . 'file.txt',
false,
'....publicfile.txt',
],
'filename path with four backslashes' => [
'folder' . $fourBackslashes . 'file.txt',
false,
'folderfile.txt',
],
'relative traversal with four backslashes' => [
'..' . $fourBackslashes . '..' . $fourBackslashes . 'public' . $fourBackslashes . 'file.txt',
true,
'....publicfile.txt',
],
'relative path with four backslashes' => [
'folder' . $fourBackslashes . 'file.txt',
true,
'folderfile.txt',
],
'filename traversal with eight backslashes' => [
'..' . $eightBackslashes . '..' . $eightBackslashes . 'public' . $eightBackslashes . 'file.txt',
false,
'....publicfile.txt',
],
'filename path with eight backslashes' => [
'folder' . $eightBackslashes . 'file.txt',
false,
'folderfile.txt',
],
'relative traversal with eight backslashes' => [
'..' . $eightBackslashes . '..' . $eightBackslashes . 'public' . $eightBackslashes . 'file.txt',
true,
'....publicfile.txt',
],
'relative path with eight backslashes' => [
'folder' . $eightBackslashes . 'file.txt',
true,
'folderfile.txt',
],
];
}
public function testStripImageTags(): void
{
$this->assertSame('http://example.com/spacer.gif', strip_image_tags('http://example.com/spacer.gif'));
$this->assertSame('http://example.com/spacer.gif', strip_image_tags('<img src="http://example.com/spacer.gif" alt="Who needs CSS when you have a spacer.gif?" />'));
$this->assertSame('http://example.com/spacer.gif', strip_image_tags('<img src=\'http://example.com/spacer.gif\' alt="test" />'));
$this->assertSame('http://example.com/spacer.gif', strip_image_tags('<img src=http://example.com/spacer.gif alt="test" />'));
$this->assertSame('http://example.com/spacer.gif', strip_image_tags('<img src = "http://example.com/spacer.gif" />'));
$this->assertSame('http://example.com/test.png', strip_image_tags('<img class="my-class" id="my-img" src="http://example.com/test.png">'));
$this->assertSame('http://example.com/test.jpg', strip_image_tags('<img alt="test\'s photo" src="http://example.com/test.jpg">'));
$this->assertSame('http://example.com/test.jpg', strip_image_tags('<img alt=\'test "quoted" photo\' src=\'http://example.com/test.jpg\'>'));
// Empty src or tags without src should be stripped
$this->assertSame('', strip_image_tags('<img src="" onerror="alert(1)">'));
$this->assertSame('', strip_image_tags('<img src=\'\' onerror=\'alert(1)\'>'));
$this->assertSame('', strip_image_tags('<img onerror="alert(1)">'));
$this->assertSame('', strip_image_tags('<img alt="no image">'));
$this->assertSame('Hello World', strip_image_tags('Hello <img> World'));
$this->assertSame('Hello World', strip_image_tags('Hello <img/> World'));
$this->assertSame('Hello World', strip_image_tags('Hello <img > World'));
// Multiline img tag
$this->assertSame('http://example.com/test.png', strip_image_tags("<img\n class=\"foo\"\n src=\"http://example.com/test.png\"\n alt=\"bar\">"));
// Intervening text between multiple img tags must not be swallowed
$this->assertSame('Text1 Text2 bar.jpg Text3', strip_image_tags('Text1 <img alt="foo"> Text2 <img src="bar.jpg"> Text3'));
$this->assertSame('Start 1.jpg Middle 2.jpg End', strip_image_tags('Start <img src="1.jpg"> Middle <img src="2.jpg"> End'));
// Attribute injection and quote handling
$this->assertSame('test', strip_image_tags('<img src="test" onmouseover="alert(1)">'));
$this->assertSame('test', strip_image_tags('<img src=\'test\' onmouseover=\'alert(1)\'>'));
$this->assertSame('test', strip_image_tags('<img src=\'test\' onmouseover="alert(1)">'));
$this->assertSame('test', strip_image_tags('<img src="test" onmouseover=\'alert(1)\'>'));
// Nested img tag bypass
$this->assertSame('x', strip_image_tags('<im<img>g src=x onerror=alert(1)>'));
$this->assertSame('x', strip_image_tags('<im<im<img>g>g src=x onerror=alert(1)>'));
// Only actual src attribute should be matched, not attributes ending with src (e.g. data-src)
$this->assertSame('', strip_image_tags('<img data-src="http://example.com/fake.gif">'));
$this->assertSame('http://example.com/real.gif', strip_image_tags('<img data-src="http://example.com/fake.gif" src="http://example.com/real.gif">'));
$this->assertSame('http://example.com/real.gif', strip_image_tags('<img src="http://example.com/real.gif" data-src="http://example.com/fake.gif">'));
}
public function testStripImageTagsGreaterThanSignInsideQuotedAttribute(): void
{
$this->assertSame('photo.jpg', strip_image_tags('<img alt="><svg onload=alert(1)>" src="photo.jpg">'));
$this->assertSame('photo.jpg', strip_image_tags("<img alt='><svg onload=alert(1)>' src='photo.jpg'>"));
$this->assertSame('photo.jpg', strip_image_tags('<img src="photo.jpg" alt="><svg onload=alert(1)>">'));
$this->assertSame('', strip_image_tags('<img alt="><svg onload=alert(1)>">'));
$this->assertSame('Before photo.jpg After', strip_image_tags('Before <img alt="a>b" src="photo.jpg"> After'));
}
public function testStripImageTagsNestedTags(): void
{
$this->assertSame('x', strip_image_tags('<im<img>g src=x onerror=alert(1)>'));
$this->assertSame('x', strip_image_tags('<im<im<img>g>g src=x onerror=alert(1)>'));
$this->assertStringNotContainsString('<img', strip_image_tags('<im<img>g src=x onerror=alert(1)>'));
$this->assertStringNotContainsString('onerror', strip_image_tags('<im<img>g src=x onerror=alert(1)>'));
}
public function testStripImageTagsIgnoresDataSrcAttribute(): void
{
$this->assertSame('real.gif', strip_image_tags('<img data-src="fake.gif" src="real.gif">'));
$this->assertSame('real.gif', strip_image_tags('<img src="real.gif" data-src="fake.gif">'));
$this->assertSame('', strip_image_tags('<img data-src="fake.gif">'));
}
public function testStripImageTagsWithoutSrcAttribute(): void
{
$this->assertSame('', strip_image_tags('<img>'));
$this->assertSame('', strip_image_tags('<img alt="test">'));
$this->assertSame('', strip_image_tags('<IMG ALT="test">'));
}
public function testStripImageTagsIsCaseInsensitive(): void
{
$this->assertSame('photo.jpg', strip_image_tags('<IMG SRC="photo.jpg">'));
}
public function testStripImageTagsFallbackWhenPcreFails(): void
{
$oldBacktrack = ini_get('pcre.backtrack_limit');
$oldJit = ini_get('pcre.jit');
try {
// Force PCRE failure: JIT does not honour backtrack_limit.
ini_set('pcre.jit', '0');
ini_set('pcre.backtrack_limit', '1');
// Make sure PCRE really fails for this input, so the fallback is exercised.
// This also keeps the test independent of the test order: a pattern
// already compiled and JIT-cached by earlier tests still honours
// backtrack_limit once JIT is disabled, and the assertion below turns
// any silent PCRE success into an explicit failure.
$input = '<img src="photo.jpg" onerror="alert(1)">';
$result = strip_image_tags($input);
$this->assertStringNotContainsString('<img', $result);
$this->assertStringContainsString('<img', $result);
$result = strip_image_tags('Hello <img alt="a>b" onerror=alert(1)> World');
$this->assertSame('Hello <img alt="a>b" onerror=alert(1)> World', $result);
} finally {
ini_set('pcre.backtrack_limit', (string) $oldBacktrack);
ini_set('pcre.jit', (string) $oldJit);
}
}
public function testStripImageTagsExceedingIterationLimitEscapesPrefix(): void
{
$input = str_repeat('<im', 26)
. '<img>'
. str_repeat('g>', 25)
. 'g src=x onerror=alert(1)>';
$output = strip_image_tags($input);
$this->assertStringNotContainsString('<img', $output);
$this->assertStringContainsString('<img', $output);
}
public function testEncodePhpTags(): void
{
$this->assertSame('<? echo $foo; ?>', encode_php_tags('<? echo $foo; ?>'));
}
}