Skip to content

Commit 826b36c

Browse files
committed
Editors: Added HTML filtering in certain loading conditions
When loaded via ajax for draft revert live in editor, or when loaded into the editor by a different user.
1 parent 3fa1174 commit 826b36c

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

app/Entities/Controllers/PageController.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use BookStack\Http\Controller;
2222
use BookStack\Permissions\Permission;
2323
use BookStack\References\ReferenceFetcher;
24+
use BookStack\Util\HtmlContentFilter;
25+
use BookStack\Util\HtmlContentFilterConfig;
2426
use Exception;
2527
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2628
use Illuminate\Http\Request;
@@ -173,7 +175,7 @@ public function show(string $bookSlug, string $pageSlug)
173175
}
174176

175177
/**
176-
* Get page from an ajax request.
178+
* Get a page from an ajax request.
177179
*
178180
* @throws NotFoundException
179181
*/
@@ -183,6 +185,10 @@ public function getPageAjax(int $pageId)
183185
$page->setHidden(array_diff($page->getHidden(), ['html', 'markdown']));
184186
$page->makeHidden(['book']);
185187

188+
$filterConfig = HtmlContentFilterConfig::fromConfigString(config('app.content_filtering'));
189+
$filter = new HtmlContentFilter($filterConfig);
190+
$page->html = $filter->filterString($page->html);
191+
186192
return response()->json($page);
187193
}
188194

app/Entities/Tools/PageEditorData.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use BookStack\Entities\Tools\Markdown\HtmlToMarkdown;
99
use BookStack\Entities\Tools\Markdown\MarkdownToHtml;
1010
use BookStack\Permissions\Permission;
11+
use BookStack\Util\HtmlContentFilter;
12+
use BookStack\Util\HtmlContentFilterConfig;
1113

1214
class PageEditorData
1315
{
@@ -47,6 +49,7 @@ protected function build(): array
4749
$isDraftRevision = false;
4850
$this->warnings = [];
4951
$editActivity = new PageEditActivity($page);
52+
$lastEditorId = $page->updated_by ?? user()->id;
5053

5154
if ($editActivity->hasActiveEditing()) {
5255
$this->warnings[] = $editActivity->activeEditingMessage();
@@ -58,11 +61,20 @@ protected function build(): array
5861
$page->forceFill($userDraft->only(['name', 'html', 'markdown']));
5962
$isDraftRevision = true;
6063
$this->warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
64+
$lastEditorId = $userDraft->created_by;
6165
}
6266

67+
// Get editor type and handle changes
6368
$editorType = $this->getEditorType($page);
6469
$this->updateContentForEditor($page, $editorType);
6570

71+
// Filter HTML content if required
72+
if ($editorType->isHtmlBased() && !old('html') && $lastEditorId !== user()->id) {
73+
$filterConfig = HtmlContentFilterConfig::fromConfigString(config('app.content_filtering'));
74+
$filter = new HtmlContentFilter($filterConfig);
75+
$page->html = $filter->filterString($page->html);
76+
}
77+
6678
return [
6779
'page' => $page,
6880
'book' => $page->book,

tests/Entity/PageEditorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,21 @@ public function test_editor_type_change_to_wysiwyg_infers_type_from_request_or_u
265265
$this->assertEquals($test['expected'], $page->refresh()->editor, "Failed asserting global editor {$test['setting']} with request editor {$test['request']} results in {$test['expected']} set for the page");
266266
}
267267
}
268+
269+
public function test_editor_html_content_is_filtered_if_loaded_by_a_different_user()
270+
{
271+
$editor = $this->users->editor();
272+
$page = $this->entities->page();
273+
$page->html = '<style>hellotherethisisaturtlemonster</style>';
274+
$page->updated_by = $editor->id;
275+
$page->save();
276+
277+
$resp = $this->asAdmin()->get($page->getUrl('edit'));
278+
$resp->assertOk();
279+
$resp->assertDontSee('hellotherethisisaturtlemonster', false);
280+
281+
$resp = $this->asAdmin()->get("/ajax/page/{$page->id}");
282+
$resp->assertOk();
283+
$resp->assertDontSee('hellotherethisisaturtlemonster', false);
284+
}
268285
}

0 commit comments

Comments
 (0)