Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a notice when tags and attributes are rejected #1203

Merged
merged 5 commits into from Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/assets/javascripts/posts.js
Expand Up @@ -6,6 +6,19 @@ const ALLOWED_ATTR = ['id', 'class', 'href', 'title', 'src', 'height', 'width',
'start', 'dir'];

$(() => {
DOMPurify.addHook("uponSanitizeAttribute", (node, event) => {
const rowspan = node.getAttribute("rowspan");
const colspan = node.getAttribute("colspan");

if (rowspan && Number.isNaN(+rowspan)) {
event.keepAttr = false;
}

if (colspan && Number.isNaN(+colspan)) {
event.keepAttr = false;
}
});

Oaphi marked this conversation as resolved.
Show resolved Hide resolved
const $uploadForm = $('.js-upload-form');

const stringInsert = (str, idx, insert) => str.slice(0, idx) + insert + str.slice(idx);
Expand Down Expand Up @@ -149,6 +162,27 @@ $(() => {
ALLOWED_TAGS,
ALLOWED_ATTR
});

const removedElements = [...new Set(DOMPurify.removed
.filter(entry => entry.element && !(entry.element instanceof HTMLBodyElement))
.map(entry => entry.element.localName))];

const removedAttributes = [...new Set(DOMPurify.removed
.filter(entry => entry.attribute)
.map(entry => [
entry.attribute.name + (entry.attribute.value ? `='${entry.attribute.value}'` : ''),
entry.from.localName
]))]

$tgt.parents('form')
.find('.rejected-elements')
.toggleClass('hide', removedElements.length === 0 && removedAttributes.length === 0)
Oaphi marked this conversation as resolved.
Show resolved Hide resolved
.find('ul')
.empty()
.append(
removedElements.map(name => $(`<li><code>&lt;${name}&gt;</code></li>`)),
removedAttributes.map(([attr, elName]) => $(`<li><code>${attr}</code> (in <code>&lt;${elName}&gt;</code>)</li>`)));

$tgt.parents('.form-group').siblings('.post-preview').html(html);
$tgt.parents('form').find('.js-post-html[name="__html"]').val(html + '<!-- g: js, mdit -->');
}, 0);
Expand Down
8 changes: 8 additions & 0 deletions app/views/posts/_form.html.erb
Expand Up @@ -57,6 +57,14 @@
<%= render 'shared/body_field', f: f, field_name: :body_markdown, field_label: t('posts.body_label'), post: post,
min_length: min_body_length(category), max_length: max_body_length(category) %>

<div class="rejected-elements notice is-warning hide">
<h3>Unsupported HTML detected</h3>
<p>The following HTML tags and attributes are unsupported and will be removed from the final post:</p>
<ul>
</ul>
<p>For a list of allowed HTML, see <a href="/help/advanced-formatting">this help article</a>.
If you meant to display the tags as code in the post, please enclose them in a code block.</p>
</div>
<div class="post-preview"></div>

<% unless post_type.has_parent? %>
Expand Down