Skip to content

Commit

Permalink
Merge pull request #5983 from dodona-edu/enhance/allow-use-for-exercise
Browse files Browse the repository at this point in the history
Add img as allowed tag to sanitizer
  • Loading branch information
jorg-vr authored Dec 3, 2024
2 parents a82c3bb + c930c95 commit 3cad0b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def markdown_unsafe(source)

def sanitize(html)
@tags ||= Rails::Html::SafeListSanitizer.allowed_tags.to_a +
%w[table thead tbody tr td th colgroup col style summary details] +
%w[table thead tbody tr td th colgroup col style summary details img] +
%w[svg g style circle line rect path polygon polyline text defs]
@attributes ||= Rails::Html::SafeListSanitizer.allowed_attributes.to_a +
%w[style target data-bs-toggle data-parent data-tab data-line data-element id] +
Expand Down
16 changes: 16 additions & 0 deletions test/helpers/application_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,17 @@ class ApplicationHelperTest < ActiveSupport::TestCase
dirty_html = <<~HTML
<script>alert(1)</script>
<img src=x onerror=alert(1)>
<svg>
<script>alert(1)</script>
<use xlink:href="javascript:alert(1)"/>
</svg>
<p>Hello
HTML
clean_html = sanitize dirty_html

assert_no_match(/<script>/, clean_html)
assert_no_match(/onerror/, clean_html)
assert_no_match(/<use/, clean_html)
assert_match(/<p>Hello/, clean_html)
end

Expand All @@ -100,6 +105,17 @@ class ApplicationHelperTest < ActiveSupport::TestCase
assert_equal dirty_html, clean_html
end

test 'sanitize helper should allow images' do
# test link image and base64 image
dirty_html = <<~HTML
<img src="https://example.com/image.jpg" alt="Image">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
HTML
clean_html = sanitize dirty_html

assert_equal dirty_html, clean_html
end

test 'sanitize helper should allow a selection of svg tags' do
dirty_html = <<~HTML
<svg viewBox="0 0 100 100" width="300" height="100" version="1.1">
Expand Down

0 comments on commit 3cad0b6

Please sign in to comment.