]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageContentTest.php
Add WKHTMLTOPDF to .env.example.complete
[bookstack] / tests / Entity / PageContentTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Page;
6 use BookStack\Entities\Tools\PageContent;
7 use Tests\TestCase;
8
9 class PageContentTest extends TestCase
10 {
11     protected $base64Jpeg = '/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=';
12
13     public function test_page_includes()
14     {
15         $page = $this->entities->page();
16         $secondPage = $this->entities->page();
17
18         $secondPage->html = "<p id='section1'>Hello, This is a test</p><p id='section2'>This is a second block of content</p>";
19         $secondPage->save();
20
21         $this->asEditor();
22
23         $pageContent = $this->get($page->getUrl());
24         $pageContent->assertDontSee('Hello, This is a test');
25
26         $originalHtml = $page->html;
27         $page->html .= "{{@{$secondPage->id}}}";
28         $page->save();
29
30         $pageContent = $this->get($page->getUrl());
31         $pageContent->assertSee('Hello, This is a test');
32         $pageContent->assertSee('This is a second block of content');
33
34         $page->html = $originalHtml . " Well {{@{$secondPage->id}#section2}}";
35         $page->save();
36
37         $pageContent = $this->get($page->getUrl());
38         $pageContent->assertDontSee('Hello, This is a test');
39         $pageContent->assertSee('Well This is a second block of content');
40     }
41
42     public function test_saving_page_with_includes()
43     {
44         $page = $this->entities->page();
45         $secondPage = $this->entities->page();
46
47         $this->asEditor();
48         $includeTag = '{{@' . $secondPage->id . '}}';
49         $page->html = '<p>' . $includeTag . '</p>';
50
51         $resp = $this->put($page->getUrl(), ['name' => $page->name, 'html' => $page->html, 'summary' => '']);
52
53         $resp->assertStatus(302);
54
55         $page = Page::find($page->id);
56         $this->assertStringContainsString($includeTag, $page->html);
57         $this->assertEquals('', $page->text);
58     }
59
60     public function test_page_includes_do_not_break_tables()
61     {
62         $page = $this->entities->page();
63         $secondPage = $this->entities->page();
64
65         $content = '<table id="table"><tbody><tr><td>test</td></tr></tbody></table>';
66         $secondPage->html = $content;
67         $secondPage->save();
68
69         $page->html = "{{@{$secondPage->id}#table}}";
70         $page->save();
71
72         $pageResp = $this->asEditor()->get($page->getUrl());
73         $pageResp->assertSee($content, false);
74     }
75
76     public function test_page_includes_do_not_break_code()
77     {
78         $page = $this->entities->page();
79         $secondPage = $this->entities->page();
80
81         $content = '<pre id="bkmrk-code"><code>var cat = null;</code></pre>';
82         $secondPage->html = $content;
83         $secondPage->save();
84
85         $page->html = "{{@{$secondPage->id}#bkmrk-code}}";
86         $page->save();
87
88         $pageResp = $this->asEditor()->get($page->getUrl());
89         $pageResp->assertSee($content, false);
90     }
91
92     public function test_page_includes_rendered_on_book_export()
93     {
94         $page = $this->entities->page();
95         $secondPage = Page::query()
96             ->where('book_id', '!=', $page->book_id)
97             ->first();
98
99         $content = '<p id="bkmrk-meow">my cat is awesome and scratchy</p>';
100         $secondPage->html = $content;
101         $secondPage->save();
102
103         $page->html = "{{@{$secondPage->id}#bkmrk-meow}}";
104         $page->save();
105
106         $this->asEditor();
107         $htmlContent = $this->get($page->book->getUrl('/export/html'));
108         $htmlContent->assertSee('my cat is awesome and scratchy');
109     }
110
111     public function test_page_content_scripts_removed_by_default()
112     {
113         $this->asEditor();
114         $page = $this->entities->page();
115         $script = 'abc123<script>console.log("hello-test")</script>abc123';
116         $page->html = "escape {$script}";
117         $page->save();
118
119         $pageView = $this->get($page->getUrl());
120         $pageView->assertStatus(200);
121         $pageView->assertDontSee($script, false);
122         $pageView->assertSee('abc123abc123');
123     }
124
125     public function test_more_complex_content_script_escaping_scenarios()
126     {
127         $checks = [
128             "<p>Some script</p><script>alert('cat')</script>",
129             "<div><div><div><div><p>Some script</p><script>alert('cat')</script></div></div></div></div>",
130             "<p>Some script<script>alert('cat')</script></p>",
131             "<p>Some script <div><script>alert('cat')</script></div></p>",
132             "<p>Some script <script><div>alert('cat')</script></div></p>",
133             "<p>Some script <script><div>alert('cat')</script><script><div>alert('cat')</script></p><script><div>alert('cat')</script>",
134         ];
135
136         $this->asEditor();
137         $page = $this->entities->page();
138
139         foreach ($checks as $check) {
140             $page->html = $check;
141             $page->save();
142
143             $pageView = $this->get($page->getUrl());
144             $pageView->assertStatus(200);
145             $this->withHtml($pageView)->assertElementNotContains('.page-content', '<script>');
146             $this->withHtml($pageView)->assertElementNotContains('.page-content', '</script>');
147         }
148     }
149
150     public function test_js_and_base64_src_urls_are_removed()
151     {
152         $checks = [
153             '<iframe src="javascript:alert(document.cookie)"></iframe>',
154             '<iframe src="JavAScRipT:alert(document.cookie)"></iframe>',
155             '<iframe src="JavAScRipT:alert(document.cookie)"></iframe>',
156             '<iframe SRC=" javascript: alert(document.cookie)"></iframe>',
157             '<iframe src="data:text/html;base64,PHNjcmlwdD5hbGVydCgnaGVsbG8nKTwvc2NyaXB0Pg==" frameborder="0"></iframe>',
158             '<iframe src="DaTa:text/html;base64,PHNjcmlwdD5hbGVydCgnaGVsbG8nKTwvc2NyaXB0Pg==" frameborder="0"></iframe>',
159             '<iframe src=" data:text/html;base64,PHNjcmlwdD5hbGVydCgnaGVsbG8nKTwvc2NyaXB0Pg==" frameborder="0"></iframe>',
160             '<img src="javascript:alert(document.cookie)"/>',
161             '<img src="JavAScRipT:alert(document.cookie)"/>',
162             '<img src="JavAScRipT:alert(document.cookie)"/>',
163             '<img SRC=" javascript: alert(document.cookie)"/>',
164             '<img src="data:text/html;base64,PHNjcmlwdD5hbGVydCgnaGVsbG8nKTwvc2NyaXB0Pg=="/>',
165             '<img src="DaTa:text/html;base64,PHNjcmlwdD5hbGVydCgnaGVsbG8nKTwvc2NyaXB0Pg=="/>',
166             '<img src=" data:text/html;base64,PHNjcmlwdD5hbGVydCgnaGVsbG8nKTwvc2NyaXB0Pg=="/>',
167             '<iframe srcdoc="<script>window.alert(document.cookie)</script>"></iframe>',
168             '<iframe SRCdoc="<script>window.alert(document.cookie)</script>"></iframe>',
169             '<IMG SRC=`javascript:alert("RSnake says, \'XSS\'")`>',
170         ];
171
172         $this->asEditor();
173         $page = $this->entities->page();
174
175         foreach ($checks as $check) {
176             $page->html = $check;
177             $page->save();
178
179             $pageView = $this->get($page->getUrl());
180             $pageView->assertStatus(200);
181             $html = $this->withHtml($pageView);
182             $html->assertElementNotContains('.page-content', '<iframe>');
183             $html->assertElementNotContains('.page-content', '<img');
184             $html->assertElementNotContains('.page-content', '</iframe>');
185             $html->assertElementNotContains('.page-content', 'src=');
186             $html->assertElementNotContains('.page-content', 'javascript:');
187             $html->assertElementNotContains('.page-content', 'data:');
188             $html->assertElementNotContains('.page-content', 'base64');
189         }
190     }
191
192     public function test_javascript_uri_links_are_removed()
193     {
194         $checks = [
195             '<a id="xss" href="javascript:alert(document.cookie)>Click me</a>',
196             '<a id="xss" href="javascript: alert(document.cookie)>Click me</a>',
197             '<a id="xss" href="JaVaScRiPt: alert(document.cookie)>Click me</a>',
198             '<a id="xss" href=" JaVaScRiPt: alert(document.cookie)>Click me</a>',
199         ];
200
201         $this->asEditor();
202         $page = $this->entities->page();
203
204         foreach ($checks as $check) {
205             $page->html = $check;
206             $page->save();
207
208             $pageView = $this->get($page->getUrl());
209             $pageView->assertStatus(200);
210             $this->withHtml($pageView)->assertElementNotContains('.page-content', '<a id="xss"');
211             $this->withHtml($pageView)->assertElementNotContains('.page-content', 'href=javascript:');
212         }
213     }
214
215     public function test_form_actions_with_javascript_are_removed()
216     {
217         $checks = [
218             '<form><input id="xss" type=submit formaction=javascript:alert(document.domain) value=Submit><input></form>',
219             '<form ><button id="xss" formaction="JaVaScRiPt:alert(document.domain)">Click me</button></form>',
220             '<form ><button id="xss" formaction=javascript:alert(document.domain)>Click me</button></form>',
221             '<form id="xss" action=javascript:alert(document.domain)><input type=submit value=Submit></form>',
222             '<form id="xss" action="JaVaScRiPt:alert(document.domain)"><input type=submit value=Submit></form>',
223         ];
224
225         $this->asEditor();
226         $page = $this->entities->page();
227
228         foreach ($checks as $check) {
229             $page->html = $check;
230             $page->save();
231
232             $pageView = $this->get($page->getUrl());
233             $pageView->assertStatus(200);
234             $this->withHtml($pageView)->assertElementNotContains('.page-content', '<button id="xss"');
235             $this->withHtml($pageView)->assertElementNotContains('.page-content', '<input id="xss"');
236             $this->withHtml($pageView)->assertElementNotContains('.page-content', '<form id="xss"');
237             $this->withHtml($pageView)->assertElementNotContains('.page-content', 'action=javascript:');
238             $this->withHtml($pageView)->assertElementNotContains('.page-content', 'formaction=javascript:');
239         }
240     }
241
242     public function test_metadata_redirects_are_removed()
243     {
244         $checks = [
245             '<meta http-equiv="refresh" content="0; url=//external_url">',
246             '<meta http-equiv="refresh" ConTeNt="0; url=//external_url">',
247             '<meta http-equiv="refresh" content="0; UrL=//external_url">',
248         ];
249
250         $this->asEditor();
251         $page = $this->entities->page();
252
253         foreach ($checks as $check) {
254             $page->html = $check;
255             $page->save();
256
257             $pageView = $this->get($page->getUrl());
258             $pageView->assertStatus(200);
259             $this->withHtml($pageView)->assertElementNotContains('.page-content', '<meta>');
260             $this->withHtml($pageView)->assertElementNotContains('.page-content', '</meta>');
261             $this->withHtml($pageView)->assertElementNotContains('.page-content', 'content=');
262             $this->withHtml($pageView)->assertElementNotContains('.page-content', 'external_url');
263         }
264     }
265
266     public function test_page_inline_on_attributes_removed_by_default()
267     {
268         $this->asEditor();
269         $page = $this->entities->page();
270         $script = '<p onmouseenter="console.log(\'test\')">Hello</p>';
271         $page->html = "escape {$script}";
272         $page->save();
273
274         $pageView = $this->get($page->getUrl());
275         $pageView->assertStatus(200);
276         $pageView->assertDontSee($script, false);
277         $pageView->assertSee('<p>Hello</p>', false);
278     }
279
280     public function test_more_complex_inline_on_attributes_escaping_scenarios()
281     {
282         $checks = [
283             '<p onclick="console.log(\'test\')">Hello</p>',
284             '<p OnCliCk="console.log(\'test\')">Hello</p>',
285             '<div>Lorem ipsum dolor sit amet.</div><p onclick="console.log(\'test\')">Hello</p>',
286             '<div>Lorem ipsum dolor sit amet.<p onclick="console.log(\'test\')">Hello</p></div>',
287             '<div><div><div><div>Lorem ipsum dolor sit amet.<p onclick="console.log(\'test\')">Hello</p></div></div></div></div>',
288             '<div onclick="console.log(\'test\')">Lorem ipsum dolor sit amet.</div><p onclick="console.log(\'test\')">Hello</p><div></div>',
289             '<a a="<img src=1 onerror=\'alert(1)\'> ',
290             '\<a onclick="alert(document.cookie)"\>xss link\</a\>',
291         ];
292
293         $this->asEditor();
294         $page = $this->entities->page();
295
296         foreach ($checks as $check) {
297             $page->html = $check;
298             $page->save();
299
300             $pageView = $this->get($page->getUrl());
301             $pageView->assertStatus(200);
302             $this->withHtml($pageView)->assertElementNotContains('.page-content', 'onclick');
303         }
304     }
305
306     public function test_page_content_scripts_show_when_configured()
307     {
308         $this->asEditor();
309         $page = $this->entities->page();
310         config()->set('app.allow_content_scripts', 'true');
311
312         $script = 'abc123<script>console.log("hello-test")</script>abc123';
313         $page->html = "no escape {$script}";
314         $page->save();
315
316         $pageView = $this->get($page->getUrl());
317         $pageView->assertSee($script, false);
318         $pageView->assertDontSee('abc123abc123');
319     }
320
321     public function test_svg_script_usage_is_removed()
322     {
323         $checks = [
324             '<svg id="test" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100"><a xlink:href="javascript:alert(document.domain)"><rect x="0" y="0" width="100" height="100" /></a></svg>',
325             '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><use xlink:href="data:application/xml;base64 ,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIj4KPGRlZnM+CjxjaXJjbGUgaWQ9InRlc3QiIHI9IjAiIGN4PSIwIiBjeT0iMCIgc3R5bGU9ImZpbGw6ICNGMDAiPgo8c2V0IGF0dHJpYnV0ZU5hbWU9ImZpbGwiIGF0dHJpYnV0ZVR5cGU9IkNTUyIgb25iZWdpbj0nYWxlcnQoZG9jdW1lbnQuZG9tYWluKScKb25lbmQ9J2FsZXJ0KCJvbmVuZCIpJyB0bz0iIzAwRiIgYmVnaW49IjBzIiBkdXI9Ijk5OXMiIC8+CjwvY2lyY2xlPgo8L2RlZnM+Cjx1c2UgeGxpbms6aHJlZj0iI3Rlc3QiLz4KPC9zdmc+#test"/></svg>',
326             '<svg><animate href=#xss attributeName=href values=javascript:alert(1) /></svg>',
327             '<svg><animate href="#xss" attributeName="href" values="a;javascript:alert(1)" /></svg>',
328             '<svg><animate href="#xss" attributeName="href" values="a;data:alert(1)" /></svg>',
329             '<svg><animate href=#xss attributeName=href from=javascript:alert(1) to=1 /><a id=xss><text x=20 y=20>XSS</text></a>',
330             '<svg><set href=#xss attributeName=href from=? to=javascript:alert(1) /><a id=xss><text x=20 y=20>XSS</text></a>',
331             '<svg><g><g><g><animate href=#xss attributeName=href values=javascript:alert(1) /></g></g></g></svg>',
332         ];
333
334         $this->asEditor();
335         $page = $this->entities->page();
336
337         foreach ($checks as $check) {
338             $page->html = $check;
339             $page->save();
340
341             $pageView = $this->get($page->getUrl());
342             $pageView->assertStatus(200);
343             $html = $this->withHtml($pageView);
344             $html->assertElementNotContains('.page-content', 'alert');
345             $html->assertElementNotContains('.page-content', 'xlink:href');
346             $html->assertElementNotContains('.page-content', 'application/xml');
347             $html->assertElementNotContains('.page-content', 'javascript');
348         }
349     }
350
351     public function test_page_inline_on_attributes_show_if_configured()
352     {
353         $this->asEditor();
354         $page = $this->entities->page();
355         config()->set('app.allow_content_scripts', 'true');
356
357         $script = '<p onmouseenter="console.log(\'test\')">Hello</p>';
358         $page->html = "escape {$script}";
359         $page->save();
360
361         $pageView = $this->get($page->getUrl());
362         $pageView->assertSee($script, false);
363         $pageView->assertDontSee('<p>Hello</p>', false);
364     }
365
366     public function test_duplicate_ids_does_not_break_page_render()
367     {
368         $this->asEditor();
369         $pageA = Page::query()->first();
370         $pageB = Page::query()->where('id', '!=', $pageA->id)->first();
371
372         $content = '<ul id="bkmrk-xxx-%28"></ul> <ul id="bkmrk-xxx-%28"></ul>';
373         $pageA->html = $content;
374         $pageA->save();
375
376         $pageB->html = '<ul id="bkmrk-xxx-%28"></ul> <p>{{@' . $pageA->id . '#test}}</p>';
377         $pageB->save();
378
379         $pageView = $this->get($pageB->getUrl());
380         $pageView->assertSuccessful();
381     }
382
383     public function test_duplicate_ids_fixed_on_page_save()
384     {
385         $this->asEditor();
386         $page = $this->entities->page();
387
388         $content = '<ul id="bkmrk-test"><li>test a</li><li><ul id="bkmrk-test"><li>test b</li></ul></li></ul>';
389         $pageSave = $this->put($page->getUrl(), [
390             'name'    => $page->name,
391             'html'    => $content,
392             'summary' => '',
393         ]);
394         $pageSave->assertRedirect();
395
396         $updatedPage = Page::query()->where('id', '=', $page->id)->first();
397         $this->assertEquals(substr_count($updatedPage->html, 'bkmrk-test"'), 1);
398     }
399
400     public function test_anchors_referencing_non_bkmrk_ids_rewritten_after_save()
401     {
402         $this->asEditor();
403         $page = $this->entities->page();
404
405         $content = '<h1 id="non-standard-id">test</h1><p><a href="#non-standard-id">link</a></p>';
406         $this->put($page->getUrl(), [
407             'name'    => $page->name,
408             'html'    => $content,
409             'summary' => '',
410         ]);
411
412         $updatedPage = Page::query()->where('id', '=', $page->id)->first();
413         $this->assertStringContainsString('id="bkmrk-test"', $updatedPage->html);
414         $this->assertStringContainsString('href="#bkmrk-test"', $updatedPage->html);
415     }
416
417     public function test_get_page_nav_sets_correct_properties()
418     {
419         $content = '<h1 id="testa">Hello</h1><h2 id="testb">There</h2><h3 id="testc">Donkey</h3>';
420         $pageContent = new PageContent(new Page(['html' => $content]));
421         $navMap = $pageContent->getNavigation($content);
422
423         $this->assertCount(3, $navMap);
424         $this->assertArrayMapIncludes([
425             'nodeName' => 'h1',
426             'link'     => '#testa',
427             'text'     => 'Hello',
428             'level'    => 1,
429         ], $navMap[0]);
430         $this->assertArrayMapIncludes([
431             'nodeName' => 'h2',
432             'link'     => '#testb',
433             'text'     => 'There',
434             'level'    => 2,
435         ], $navMap[1]);
436         $this->assertArrayMapIncludes([
437             'nodeName' => 'h3',
438             'link'     => '#testc',
439             'text'     => 'Donkey',
440             'level'    => 3,
441         ], $navMap[2]);
442     }
443
444     public function test_get_page_nav_does_not_show_empty_titles()
445     {
446         $content = '<h1 id="testa">Hello</h1><h2 id="testb">&nbsp;</h2><h3 id="testc"></h3>';
447         $pageContent = new PageContent(new Page(['html' => $content]));
448         $navMap = $pageContent->getNavigation($content);
449
450         $this->assertCount(1, $navMap);
451         $this->assertArrayMapIncludes([
452             'nodeName' => 'h1',
453             'link'     => '#testa',
454             'text'     => 'Hello',
455         ], $navMap[0]);
456     }
457
458     public function test_get_page_nav_shifts_headers_if_only_smaller_ones_are_used()
459     {
460         $content = '<h4 id="testa">Hello</h4><h5 id="testb">There</h5><h6 id="testc">Donkey</h6>';
461         $pageContent = new PageContent(new Page(['html' => $content]));
462         $navMap = $pageContent->getNavigation($content);
463
464         $this->assertCount(3, $navMap);
465         $this->assertArrayMapIncludes([
466             'nodeName' => 'h4',
467             'level'    => 1,
468         ], $navMap[0]);
469         $this->assertArrayMapIncludes([
470             'nodeName' => 'h5',
471             'level'    => 2,
472         ], $navMap[1]);
473         $this->assertArrayMapIncludes([
474             'nodeName' => 'h6',
475             'level'    => 3,
476         ], $navMap[2]);
477     }
478
479     public function test_page_text_decodes_html_entities()
480     {
481         $page = $this->entities->page();
482
483         $this->actingAs($this->users->admin())
484             ->put($page->getUrl(''), [
485                 'name' => 'Testing',
486                 'html' => '<p>&quot;Hello &amp; welcome&quot;</p>',
487             ]);
488
489         $page->refresh();
490         $this->assertEquals('"Hello & welcome"', $page->text);
491     }
492
493     public function test_page_markdown_table_rendering()
494     {
495         $this->asEditor();
496         $page = $this->entities->page();
497
498         $content = '| Syntax      | Description |
499 | ----------- | ----------- |
500 | Header      | Title       |
501 | Paragraph   | Text        |';
502         $this->put($page->getUrl(), [
503             'name' => $page->name,  'markdown' => $content,
504             'html' => '', 'summary' => '',
505         ]);
506
507         $page->refresh();
508         $this->assertStringContainsString('</tbody>', $page->html);
509
510         $pageView = $this->get($page->getUrl());
511         $this->withHtml($pageView)->assertElementExists('.page-content table tbody td');
512     }
513
514     public function test_page_markdown_task_list_rendering()
515     {
516         $this->asEditor();
517         $page = $this->entities->page();
518
519         $content = '- [ ] Item a
520 - [x] Item b';
521         $this->put($page->getUrl(), [
522             'name' => $page->name,  'markdown' => $content,
523             'html' => '', 'summary' => '',
524         ]);
525
526         $page->refresh();
527         $this->assertStringContainsString('input', $page->html);
528         $this->assertStringContainsString('type="checkbox"', $page->html);
529
530         $pageView = $this->get($page->getUrl());
531         $this->withHtml($pageView)->assertElementExists('.page-content li.task-list-item input[type=checkbox]');
532         $this->withHtml($pageView)->assertElementExists('.page-content li.task-list-item input[type=checkbox][checked]');
533     }
534
535     public function test_page_markdown_strikethrough_rendering()
536     {
537         $this->asEditor();
538         $page = $this->entities->page();
539
540         $content = '~~some crossed out text~~';
541         $this->put($page->getUrl(), [
542             'name' => $page->name,  'markdown' => $content,
543             'html' => '', 'summary' => '',
544         ]);
545
546         $page->refresh();
547         $this->assertStringMatchesFormat('%A<s%A>some crossed out text</s>%A', $page->html);
548
549         $pageView = $this->get($page->getUrl());
550         $this->withHtml($pageView)->assertElementExists('.page-content p > s');
551     }
552
553     public function test_page_markdown_single_html_comment_saving()
554     {
555         $this->asEditor();
556         $page = $this->entities->page();
557
558         $content = '<!-- Test Comment -->';
559         $this->put($page->getUrl(), [
560             'name' => $page->name,  'markdown' => $content,
561             'html' => '', 'summary' => '',
562         ]);
563
564         $page->refresh();
565         $this->assertStringMatchesFormat($content, $page->html);
566
567         $pageView = $this->get($page->getUrl());
568         $pageView->assertStatus(200);
569         $pageView->assertSee($content, false);
570     }
571
572     public function test_base64_images_get_extracted_from_page_content()
573     {
574         $this->asEditor();
575         $page = $this->entities->page();
576
577         $this->put($page->getUrl(), [
578             'name' => $page->name, 'summary' => '',
579             'html' => '<p>test<img src="data:image/jpeg;base64,' . $this->base64Jpeg . '"/></p>',
580         ]);
581
582         $page->refresh();
583         $this->assertStringMatchesFormat('%A<p%A>test<img src="http://localhost/uploads/images/gallery/%A.jpeg">%A</p>%A', $page->html);
584
585         $matches = [];
586         preg_match('/src="https:\/\/p.rizon.top:443\/http\/localhost(.*?)"/', $page->html, $matches);
587         $imagePath = $matches[1];
588         $imageFile = public_path($imagePath);
589         $this->assertEquals(base64_decode($this->base64Jpeg), file_get_contents($imageFile));
590
591         $this->files->deleteAtRelativePath($imagePath);
592     }
593
594     public function test_base64_images_get_extracted_when_containing_whitespace()
595     {
596         $this->asEditor();
597         $page = $this->entities->page();
598
599         $base64PngWithWhitespace = "iVBORw0KGg\noAAAANSUhE\tUgAAAAEAAAA BCA   YAAAAfFcSJAAA\n\t ACklEQVR4nGMAAQAABQAB";
600         $base64PngWithoutWhitespace = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQAB';
601         $this->put($page->getUrl(), [
602             'name' => $page->name, 'summary' => '',
603             'html' => '<p>test<img src="data:image/png;base64,' . $base64PngWithWhitespace . '"/></p>',
604         ]);
605
606         $page->refresh();
607         $this->assertStringMatchesFormat('%A<p%A>test<img src="http://localhost/uploads/images/gallery/%A.png">%A</p>%A', $page->html);
608
609         $matches = [];
610         preg_match('/src="https:\/\/p.rizon.top:443\/http\/localhost(.*?)"/', $page->html, $matches);
611         $imagePath = $matches[1];
612         $imageFile = public_path($imagePath);
613         $this->assertEquals(base64_decode($base64PngWithoutWhitespace), file_get_contents($imageFile));
614
615         $this->files->deleteAtRelativePath($imagePath);
616     }
617
618     public function test_base64_images_within_html_blanked_if_not_supported_extension_for_extract()
619     {
620         // Relevant to https://github.com/BookStackApp/BookStack/issues/3010 and other cases
621         $extensions = [
622             'jiff', 'pngr', 'png ', ' png', '.png', 'png.', 'p.ng', ',png',
623             'data:image/png', ',data:image/png',
624         ];
625
626         foreach ($extensions as $extension) {
627             $this->asEditor();
628             $page = $this->entities->page();
629
630             $this->put($page->getUrl(), [
631                 'name' => $page->name, 'summary' => '',
632                 'html' => '<p>test<img src="data:image/' . $extension . ';base64,' . $this->base64Jpeg . '"/></p>',
633             ]);
634
635             $page->refresh();
636             $this->assertStringContainsString('<img src=""', $page->html);
637         }
638     }
639
640     public function test_base64_images_get_extracted_from_markdown_page_content()
641     {
642         $this->asEditor();
643         $page = $this->entities->page();
644
645         $this->put($page->getUrl(), [
646             'name'     => $page->name, 'summary' => '',
647             'markdown' => 'test ![test](data:image/jpeg;base64,' . $this->base64Jpeg . ')',
648         ]);
649
650         $page->refresh();
651         $this->assertStringMatchesFormat('%A<p%A>test <img src="http://localhost/uploads/images/gallery/%A.jpeg" alt="test">%A</p>%A', $page->html);
652
653         $matches = [];
654         preg_match('/src="https:\/\/p.rizon.top:443\/http\/localhost(.*?)"/', $page->html, $matches);
655         $imagePath = $matches[1];
656         $imageFile = public_path($imagePath);
657         $this->assertEquals(base64_decode($this->base64Jpeg), file_get_contents($imageFile));
658
659         $this->files->deleteAtRelativePath($imagePath);
660     }
661
662     public function test_markdown_base64_extract_not_limited_by_pcre_limits()
663     {
664         $pcreBacktrackLimit = ini_get('pcre.backtrack_limit');
665         $pcreRecursionLimit = ini_get('pcre.recursion_limit');
666
667         $this->asEditor();
668         $page = $this->entities->page();
669
670         ini_set('pcre.backtrack_limit', '500');
671         ini_set('pcre.recursion_limit', '500');
672
673         $content = str_repeat('a', 5000);
674         $base64Content = base64_encode($content);
675
676         $this->put($page->getUrl(), [
677             'name'     => $page->name, 'summary' => '',
678             'markdown' => 'test ![test](data:image/jpeg;base64,' . $base64Content . ') ![test](data:image/jpeg;base64,' . $base64Content . ')',
679         ]);
680
681         $page->refresh();
682         $this->assertStringMatchesFormat('<p%A>test <img src="http://localhost/uploads/images/gallery/%A.jpeg" alt="test"> <img src="http://localhost/uploads/images/gallery/%A.jpeg" alt="test">%A</p>%A', $page->html);
683
684         $matches = [];
685         preg_match('/src="https:\/\/p.rizon.top:443\/http\/localhost(.*?)"/', $page->html, $matches);
686         $imagePath = $matches[1];
687         $imageFile = public_path($imagePath);
688         $this->assertEquals($content, file_get_contents($imageFile));
689
690         $this->files->deleteAtRelativePath($imagePath);
691         ini_set('pcre.backtrack_limit', $pcreBacktrackLimit);
692         ini_set('pcre.recursion_limit', $pcreRecursionLimit);
693     }
694
695     public function test_base64_images_within_markdown_blanked_if_not_supported_extension_for_extract()
696     {
697         $page = $this->entities->page();
698
699         $this->asEditor()->put($page->getUrl(), [
700             'name'     => $page->name, 'summary' => '',
701             'markdown' => 'test ![test](data:image/jiff;base64,' . $this->base64Jpeg . ')',
702         ]);
703
704         $this->assertStringContainsString('<img src=""', $page->refresh()->html);
705     }
706
707     public function test_nested_headers_gets_assigned_an_id()
708     {
709         $page = $this->entities->page();
710
711         $content = '<table><tbody><tr><td><h5>Simple Test</h5></td></tr></tbody></table>';
712         $this->asEditor()->put($page->getUrl(), [
713             'name'    => $page->name,
714             'html'    => $content,
715         ]);
716
717         // The top level <table> node will get assign the bkmrk-simple-test id because the system will
718         // take the node value of h5
719         // So the h5 should get the bkmrk-simple-test-1 id
720         $this->assertStringContainsString('<h5 id="bkmrk-simple-test-1">Simple Test</h5>', $page->refresh()->html);
721     }
722
723     public function test_non_breaking_spaces_are_preserved()
724     {
725         $page = $this->entities->page();
726
727         $content = '<p>&nbsp;</p>';
728         $this->asEditor()->put($page->getUrl(), [
729             'name'    => $page->name,
730             'html'    => $content,
731         ]);
732
733         $this->assertStringContainsString('<p id="bkmrk-%C2%A0">&nbsp;</p>', $page->refresh()->html);
734     }
735
736     public function test_page_save_with_many_headers_and_links_is_reasonable()
737     {
738         $page = $this->entities->page();
739
740         $content = '';
741         for ($i = 0; $i < 500; $i++) {
742             $content .= "<table><tbody><tr><td><h5 id='header-{$i}'>Simple Test</h5><a href='#header-{$i}'></a></td></tr></tbody></table>";
743         }
744
745         $time = time();
746         $this->asEditor()->put($page->getUrl(), [
747             'name'    => $page->name,
748             'html'    => $content,
749         ])->assertRedirect();
750
751         $timeElapsed = time() - $time;
752         $this->assertLessThan(3, $timeElapsed);
753     }
754 }