Skip to content

Commit

Permalink
fix: evaluated page title content (#3684)
Browse files Browse the repository at this point in the history
* fix: evaluated page title content
* chore: add comment
* chore: use DOMParser instead
* fix: use `innerHTML` for the actual value

Signed-off-by: Sami Mazouz <sychocouldy@gmail.com>
Co-authored-by: David Wheatley <hi@davwheat.dev>
  • Loading branch information
SychO9 and davwheat committed Nov 18, 2022
1 parent b5f324a commit ed0cee9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions framework/core/js/src/common/Application.tsx
Expand Up @@ -410,16 +410,22 @@ export default class Application {
pageNumber: 1,
};

const title =
let title =
onHomepage || !this.title
? extractText(app.translator.trans('core.lib.meta_titles.without_page_title', params))
: extractText(app.translator.trans('core.lib.meta_titles.with_page_title', params));

const tempEl = document.createElement('div');
tempEl.innerHTML = title;
const decodedTitle = tempEl.innerText;
title = count + title;

document.title = count + decodedTitle;
// We pass the title through a DOMParser to allow HTML entities
// to be rendered correctly, while still preventing XSS attacks
// from user input by using a script-disabled environment.
// https://github.com/flarum/framework/issues/3514
// https://github.com/flarum/framework/pull/3684
const parser = new DOMParser();
const safeTitle = parser.parseFromString(title, 'text/html').body.innerHTML;

document.title = safeTitle;
}

protected transformRequestOptions<ResponseType>(flarumOptions: FlarumRequestOptions<ResponseType>): InternalFlarumRequestOptions<ResponseType> {
Expand Down
2 changes: 1 addition & 1 deletion framework/core/views/frontend/app.blade.php
Expand Up @@ -3,7 +3,7 @@
@if ($language) lang="{{ $language }}" @endif>
<head>
<meta charset="utf-8">
<title>{!! $title !!}</title>
<title>{{ $title }}</title>

{!! $head !!}
</head>
Expand Down

0 comments on commit ed0cee9

Please sign in to comment.