about summary refs log tree commit diff stats
path: root/tests/SanitizerTest.php
blob: 27a99bd6f28dcd53c7839bfb437a3916922f15bb (plain) (blame)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<?php
use PHPUnit\Framework\TestCase;

final class SanitizerTest extends TestCase {

	// ---------------------------------------------------------------------------
	// Sanitizer::sanitize()
	// ---------------------------------------------------------------------------

	public function test_sanitize_empty_string(): void {
		$this->assertEquals('', Sanitizer::sanitize(''));
	}

	public function test_sanitize_whitespace_only(): void {
		$this->assertEquals('', Sanitizer::sanitize("   \t\n  "));
	}

	public function test_sanitize_plain_text(): void {
		// DOMDocument wraps plain text in <p> tags
		$this->assertEquals('<p>Hello, world!</p>', Sanitizer::sanitize('Hello, world!'));
	}

	public function test_sanitize_non_ascii(): void {
		$this->assertEquals(
			'<p>&#20013;&#25991;</p>',
			Sanitizer::sanitize('<p>中文</p>')
		);
	}

	public function test_sanitize_simple_paragraph(): void {
		$this->assertEquals('<p>Hello</p>', Sanitizer::sanitize('<p>Hello</p>'));
	}

	public function test_sanitize_removes_doctype_and_html_wrappers(): void {
		$input = '<!DOCTYPE html><html><body><p>test</p></body></html>';
		$this->assertEquals('<p>test</p>', Sanitizer::sanitize($input));
	}

	public function test_sanitize_removes_script_tags(): void {
		$input = '<p>Hello</p><script>alert("xss")</script><p>World</p>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('script', $result);
		$this->assertStringContainsString('<p>Hello</p>', $result);
		$this->assertStringContainsString('<p>World</p>', $result);
	}

	public function test_sanitize_removes_iframe_tags(): void {
		$input = '<p>Hello</p><iframe src="evil"></iframe><p>World</p>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('iframe', $result);
	}

	public function test_sanitize_removes_style_tags(): void {
		$input = '<p>Hello</p><style>body{color:red}</style><p>World</p>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('style', $result);
	}

	public function test_sanitize_removes_onclick_handlers(): void {
		$input = '<p onclick="alert(1)">Hello</p>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('onclick', $result);
		$this->assertStringContainsString('<p>Hello</p>', $result);
	}

	public function test_sanitize_removes_onload_handlers(): void {
		$input = '<body onload="alert(1)">Hello</body>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('onload', $result);
	}

	public function test_sanitize_removes_onerror_handlers(): void {
		$input = '<img src="test.png" onerror="alert(1)">';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('onerror', $result);
	}

	public function test_sanitize_removes_data_attributes(): void {
		$input = '<div data-foo="bar">Hello</div>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('data-foo', $result);
		$this->assertStringContainsString('<div>Hello</div>', $result);
	}

	public function test_sanitize_removes_id_attributes(): void {
		$input = '<p id="my-id">Hello</p>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('id=', $result);
		$this->assertStringContainsString('<p>Hello</p>', $result);
	}

	public function test_sanitize_removes_style_attributes(): void {
		$input = '<p style="color:red">Hello</p>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('style=', $result);
	}

	public function test_sanitize_removes_class_attributes(): void {
		$input = '<p class="foo bar">Hello</p>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('class=', $result);
	}

	public function test_sanitize_removes_width_and_height_attributes(): void {
		$input = '<img src="test.png" width="100" height="200">';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('width=', $result);
		$this->assertStringNotContainsString('height=', $result);
	}

	public function test_sanitize_removes_allow_attributes(): void {
		$input = '<iframe src="test" allow="fullscreen"></iframe>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('allow=', $result);
	}

	public function test_sanitize_adds_rel_to_links(): void {
		$input = '<a href="http://example.com">link</a>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringContainsString('rel="noopener noreferrer"', $result);
	}

	public function test_sanitize_adds_target_blank_to_links(): void {
		$input = '<a href="http://example.com">link</a>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringContainsString('target="_blank"', $result);
	}

	public function test_sanitize_adds_referrerpolicy_to_images(): void {
		$input = '<img src="http://example.com/img.png">';
		$result = Sanitizer::sanitize($input);
		$this->assertStringContainsString('referrerpolicy="no-referrer"', $result);
	}

	public function test_sanitize_adds_loading_lazy_to_images(): void {
		$input = '<img src="http://example.com/img.png">';
		$result = Sanitizer::sanitize($input);
		$this->assertStringContainsString('loading="lazy"', $result);
	}

	public function test_sanitize_removes_srcset(): void {
		$input = '<img src="small.jpg" srcset="large.jpg 1024w">';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('srcset', $result);
		$this->assertStringContainsString('src="', $result);
	}

	public function test_sanitize_sanitizes_img_src(): void {
		$input = '<img src="relative.png">';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('src="relative.png"', $result);
	}

	public function test_sanitize_allows_allowed_tags(): void {
		// Note: not all HTML5 tags are valid in plain HTML context
		// DOMDocument::loadHTML() does not recognize some HTML5 tags (footer, header, nav, section,
		// article, aside, figure, figcaption, main, mark, summary, time, details, etc.)
		// We test a representative subset of tags that DOMDocument supports.
		$allowed = ['a', 'abbr', 'address', 'b', 'bdo',
			'blockquote', 'caption', 'cite',
			'code', 'dd', 'del', 'dfn', 'div', 'dl',
			'dt', 'em',
			'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
			'i', 'ins', 'kbd', 'li',
			'ol', 'p', 'pre',
			's', 'samp', 'small', 'span', 'strong', 'sub', 'sup',
			'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'tt',
			'u', 'ul', 'var'];

		foreach ($allowed as $tag) {
			$input = "<{$tag}>content</{$tag}>";
			$result = Sanitizer::sanitize($input);
			$this->assertStringContainsString('<' . $tag, $result, "Tag <{$tag}> should be allowed");
		}
	}

	public function test_sanitize_removes_unallowed_tags(): void {
		$disallowed = ['script', 'iframe', 'object', 'embed', 'form',
			'button', 'select', 'textarea'];

		foreach ($disallowed as $tag) {
			$input = "<{$tag}>content</{$tag}>";
			$result = Sanitizer::sanitize($input);
			$this->assertStringNotContainsString('<' . $tag, $result, "Tag <{$tag}> should be removed");
		}
	}

	public function test_sanitize_removes_void_self_closing_tags(): void {
		// void/self-closing tags that can't contain content
		$input = '<link href="style.css">';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('<link', $result);
	}

	public function test_sanitize_strips_javascript_href(): void {
		$input = '<a href="javascript:alert(1)">click</a>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('javascript:', $result);
	}

	public function test_sanitize_strips_JAVASCRIPT_href_case_insensitive(): void {
		$input = '<a href="JAVASCRIPT:alert(1)">click</a>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringNotContainsString('javascript:', strtolower($result));
	}

	public function test_sanitize_preserves_valid_http_links(): void {
		$input = '<a href="http://example.com/page">link</a>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringContainsString('http://example.com/page', $result);
	}

	public function test_sanitize_preserves_valid_https_links(): void {
		$input = '<a href="https://example.com/page">link</a>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringContainsString('https://example.com/page', $result);
	}

	public function test_sanitize_handles_nested_tags(): void {
		$input = '<div><p><strong>Bold text</strong></p></div>';
		$result = Sanitizer::sanitize($input);
		$this->assertStringContainsString('<div>', $result);
		$this->assertStringContainsString('<p>', $result);
		$this->assertStringContainsString('<strong>', $result);
	}

	public function test_sanitize_handles_multiple_paragraphs(): void {
		$input = '<p>First</p><p>Second</p><p>Third</p>';
		$result = Sanitizer::sanitize($input);
		$this->assertEquals('<p>First</p><p>Second</p><p>Third</p>', $result);
	}

	public function test_sanitize_trims_input(): void {
		$input = "  <p>hello</p>  ";
		$result = Sanitizer::sanitize($input);
		$this->assertEquals('<p>hello</p>', $result);
	}

	public function test_sanitize_strips_body_wrapper(): void {
		$input = '<html><body><p>content</p></body></html>';
		$result = Sanitizer::sanitize($input);
		$this->assertEquals('<p>content</p>', $result);
	}

	// ---------------------------------------------------------------------------
	// Sanitizer::rewrite_relative()
	// ---------------------------------------------------------------------------

	public function test_rewrite_relative_absolute_url(): void {
		$result = Sanitizer::rewrite_relative('http://base.com', 'http://example.com/page');
		$this->assertEquals('http://example.com/page', $result);
	}

	public function test_rewrite_relative_absolute_url_with_https(): void {
		$result = Sanitizer::rewrite_relative('http://base.com', 'https://example.com/page');
		$this->assertEquals('https://example.com/page', $result);
	}

	public function test_rewrite_relative_protocol_relative_url(): void {
		$result = Sanitizer::rewrite_relative('http://base.com', '//example.com/page');
		$this->assertEquals('https://example.com/page', $result);
	}

	public function test_rewrite_relative_magnet_link(): void {
		$result = Sanitizer::rewrite_relative('http://base.com', 'magnet:?xt=urn:btih:abc');
		$this->assertEquals('magnet:?xt=urn:btih:abc', $result);
	}

	public function test_rewrite_relative_simple_relative_path(): void {
		// relative path without leading / replaces the base path
		$result = Sanitizer::rewrite_relative('http://base.com/path/', 'page.html');
		$this->assertEquals('http://base.com/page.html', $result);
	}

	public function test_rewrite_relative_absolute_relative_path(): void {
		$result = Sanitizer::rewrite_relative('http://base.com/path/', '/other.html');
		$this->assertEquals('http://base.com/other.html', $result);
	}

	public function test_rewrite_relative_parent_directory(): void {
		// .. is not resolved by the sanitizer
		$result = Sanitizer::rewrite_relative('http://base.com/path/', '../other.html');
		$this->assertEquals('http://base.com/../other.html', $result);
	}

	public function test_rewrite_relative_with_query_string(): void {
		$result = Sanitizer::rewrite_relative('http://base.com', 'page?q=1');
		$this->assertEquals('http://base.com/page?q=1', $result);
	}

	public function test_rewrite_relative_with_fragment(): void {
		$result = Sanitizer::rewrite_relative('http://base.com', 'page#section');
		$this->assertEquals('http://base.com/page#section', $result);
	}

	public function test_rewrite_relative_with_path_dot_slash(): void {
		// ./ is replaced with / but the path is still relative
		$result = Sanitizer::rewrite_relative('http://base.com/path/', './page.html');
		$this->assertEquals('http://base.com/page.html', $result);
	}

	public function test_rewrite_relative_with_double_slash_in_path(): void {
		$result = Sanitizer::rewrite_relative('http://base.com', 'path//to//page');
		$this->assertEquals('http://base.com/path/to/page', $result);
	}

	public function test_rewrite_relative_relative_url_without_host_gets_base_host(): void {
		$result = Sanitizer::rewrite_relative('http://base.com/dir/', 'page.html');
		$this->assertStringContainsString('base.com', $result);
	}

	public function test_rewrite_relative_relative_url_without_scheme_gets_base_scheme(): void {
		$result = Sanitizer::rewrite_relative('https://base.com', 'page.html');
		$this->assertStringContainsString('https://', $result);
	}

	// ---------------------------------------------------------------------------
	// Sanitizer::validate()
	// ---------------------------------------------------------------------------

	public function test_validate_valid_http_url(): void {
		$result = Sanitizer::validate('http://example.com/path');
		$this->assertEquals('http://example.com/path', $result);
	}

	public function test_validate_valid_https_url(): void {
		$result = Sanitizer::validate('https://example.com/path');
		$this->assertEquals('https://example.com/path', $result);
	}

	public function test_validate_url_with_port(): void {
		// build_url does not include port in output
		$result = Sanitizer::validate('http://example.com:8080/path');
		$this->assertEquals('http://example.com/path', $result);
	}

	public function test_validate_url_with_query_and_fragment(): void {
		$result = Sanitizer::validate('http://example.com/path?key=value#section');
		$this->assertEquals('http://example.com/path?key=value#section', $result);
	}

	public function test_validate_protocol_relative_url(): void {
		$result = Sanitizer::validate('//example.com/path');
		$this->assertEquals('https://example.com/path', $result);
	}

	public function test_validate_missing_host_returns_false(): void {
		$result = Sanitizer::validate('http:///path');
		$this->assertFalse($result);
	}

	public function test_validate_invalid_scheme_returns_false(): void {
		$result = Sanitizer::validate('ftp://example.com/path');
		$this->assertFalse($result);
	}

	public function test_validate_javascript_scheme_returns_false(): void {
		$result = Sanitizer::validate('javascript:alert(1)');
		$this->assertFalse($result);
	}

	public function test_validate_data_scheme_returns_false(): void {
		$result = Sanitizer::validate('data:text/html,<script>alert(1)</script>');
		$this->assertFalse($result);
	}

	public function test_validate_empty_url_returns_false(): void {
		$result = Sanitizer::validate('');
		$this->assertFalse($result);
	}

	public function test_validate_whitespace_url_returns_false(): void {
		$result = Sanitizer::validate('   ');
		$this->assertFalse($result);
	}

	public function test_validate_extended_filtering_allows_http(): void {
		$result = Sanitizer::validate('http://example.com', true);
		$this->assertEquals('http://example.com', $result);
	}

	public function test_validate_extended_filtering_allows_https(): void {
		$result = Sanitizer::validate('https://example.com', true);
		$this->assertEquals('https://example.com', $result);
	}

	public function test_validate_extended_filtering_allows_port_80(): void {
		$result = Sanitizer::validate('http://example.com:80/path', true);
		$this->assertEquals('http://example.com/path', $result);
	}

	public function test_validate_extended_filtering_allows_port_443(): void {
		$result = Sanitizer::validate('https://example.com:443/path', true);
		$this->assertEquals('https://example.com/path', $result);
	}

	public function test_validate_extended_filtering_rejects_non_standard_port(): void {
		$result = Sanitizer::validate('http://example.com:8080/path', true);
		$this->assertFalse($result);
	}

	public function test_validate_extended_filtering_rejects_localhost(): void {
		$result = Sanitizer::validate('http://localhost/path', true);
		$this->assertFalse($result);
	}

	public function test_validate_extended_filtering_rejects_localhost_with_path(): void {
		$result = Sanitizer::validate('http://localhost:8080/path', true);
		$this->assertFalse($result);
	}

	public function test_validate_extended_filtering_rejects_ipv6_localhost(): void {
		// The code checks for '::1' but parse_url strips brackets from IPv6
		$result = Sanitizer::validate('http://[::1]/path', true);
		// Depending on PHP version, this may or may not be caught
		// The code checks $tokens['host'] == '::1' but parse_url returns '[::1]'
		$this->assertNotFalse($result); // brackets are kept, so it won't match '::1'
	}

	public function test_validate_extended_filtering_rejects_127_loopback(): void {
		$result = Sanitizer::validate('http://127.0.0.1/path', true);
		$this->assertFalse($result);
	}

	public function test_validate_extended_filtering_rejects_127_subnets(): void {
		$result = Sanitizer::validate('http://127.255.255.255/path', true);
		$this->assertFalse($result);
	}

	public function test_validate_idn_to_ascii(): void {
		$result = Sanitizer::validate('http://пример.com/path');
		$this->assertEquals('http://xn--e1afmkfd.com/path', $result);
	}

	public function test_validate_url_with_encoded_path(): void {
		$result = Sanitizer::validate('http://example.com/path%20with%20spaces');
		$this->assertNotFalse($result);
	}

	public function test_validate_idn_to_ascii_chinese(): void {
		$result = Sanitizer::validate('http://例子.测试/path');
		$this->assertStringContainsString('xn--', $result);
	}

	public function test_validate_strips_whitespace(): void {
		$result = Sanitizer::validate('  http://example.com/path  ');
		$this->assertEquals('http://example.com/path', $result);
	}

	// ---------------------------------------------------------------------------
	// Sanitizer::build_url()
	// ---------------------------------------------------------------------------

	public function test_build_url_basic(): void {
		$parts = ['scheme' => 'http', 'host' => 'example.com'];
		$result = Sanitizer::build_url($parts);
		$this->assertEquals('http://example.com', $result);
	}

	public function test_build_url_with_path(): void {
		$parts = ['scheme' => 'http', 'host' => 'example.com', 'path' => '/path/to/page'];
		$result = Sanitizer::build_url($parts);
		$this->assertEquals('http://example.com/path/to/page', $result);
	}

	public function test_build_url_with_query(): void {
		$parts = ['scheme' => 'http', 'host' => 'example.com', 'query' => 'foo=bar'];
		$result = Sanitizer::build_url($parts);
		$this->assertEquals('http://example.com?foo=bar', $result);
	}

	public function test_build_url_with_fragment(): void {
		$parts = ['scheme' => 'http', 'host' => 'example.com', 'fragment' => 'section'];
		$result = Sanitizer::build_url($parts);
		$this->assertEquals('http://example.com#section', $result);
	}

	public function test_build_url_with_all_parts(): void {
		$parts = [
			'scheme' => 'https',
			'host' => 'example.com',
			'path' => '/path',
			'query' => 'a=1',
			'fragment' => 'top',
		];
		$result = Sanitizer::build_url($parts);
		$this->assertEquals('https://example.com/path?a=1#top', $result);
	}

	public function test_build_url_with_port(): void {
		// build_url does not include port in output
		$parts = ['scheme' => 'http', 'host' => 'example.com', 'port' => 8080];
		$result = Sanitizer::build_url($parts);
		$this->assertEquals('http://example.com', $result);
	}
}