forked from publiclab/plots2
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD: Comment HTML sanitization and tests for it (publiclab#7282)
Unit tests: - "sanitizing comment body for XSS" - "should close incomplete tags" Resolves publiclab#3630 publiclab#3553
- Loading branch information
1 parent
678b4ab
commit 2fc1586
Showing
3 changed files
with
49 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -260,12 +260,15 @@ def setup | |
comment = Comment.new({ | ||
comment: "Thank you! On Tuesday, 3 July 2018, 11:20:57 PM IST, RP <[email protected]> wrote: Here you go." | ||
}) | ||
|
||
parsed = comment.parse_quoted_text | ||
output = comment.render_body | ||
|
||
assert_equal "Thank you! ", parsed[:body] | ||
assert_equal "On Tuesday, 3 July 2018, 11:20:57 PM IST, RP <[email protected]> wrote:", parsed[:boundary] | ||
assert_equal " Here you go.", parsed[:quote] | ||
assert_equal "Thank you! ", comment.scrub_quoted_text | ||
assert_equal "Thank you! <!-- @@$$%% Trimmed Content @@$$%% -->On Tuesday, 3 July 2018, 11:20:57 PM IST, RP <[email protected]> wrote: Here you go.", comment.render_body | ||
assert_equal output, "Thank you! On Tuesday, 3 July 2018, 11:20:57 PM IST, RP wrote: Here you go." | ||
end | ||
|
||
test 'should give the domain of gmail correctly' do | ||
|
@@ -313,4 +316,38 @@ def setup | |
test 'find comments using tagname and user id' do | ||
assert_equal('Admin comment', Comment.find_by_tag_and_author("awesome", 5).first.comment) | ||
end | ||
|
||
test 'sanitizing comment body for XSS' do | ||
comment = Comment.new | ||
comment.comment = "<img src=x onerror=prompt(133)>" # inserting executable javascript into a comment | ||
assert comment.save | ||
|
||
output = comment.render_body | ||
|
||
# Ensure that malicious attributes have been removed | ||
assert_equal [], output.scan('src=x') | ||
assert_equal [], output.scan('onerror=prompt') | ||
|
||
# Ensure all the OK attributes are preserved, for a wide range of comment types: | ||
comment.comment = "<iframe src='/hello' width='100' height='100' border='0'></iframe><p style='color:red;' class='nice' id='cool' title='sweet'></p>" | ||
assert comment.save | ||
output = comment.render_body | ||
|
||
assert_equal [], output.scan("src='/hello' width='100' height='100' border='0'") | ||
assert_equal [], output.scan("class='nice' id='cool' title='sweet'") | ||
end | ||
|
||
test 'should close incomplete tags' do | ||
comment = Comment.new | ||
|
||
# <iframe> is not closed (</iframe>) | ||
comment.comment = "Let’s see how this works with images or an embedded video.\n\n \n\n![](cid:[email protected])\n\n \n\n\\<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/Kt\\_MSMpxy7Y\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen\\>\\\n\n \n\nIs \\ ***markdown** \\* parsed?\n\n \n\n1. 1. Number 1\n\n2. 4. Number 4\n\n3. 3. Number 3\n\n " | ||
|
||
assert comment.save | ||
output = comment.render_body | ||
|
||
# Make sure that <iframe> is closed (</iframe>) | ||
assert_not_equal [], output.scan('<iframe width="560" height="315" src="https://www.youtube.com/embed/Kt%5C_MSMpxy7Y">\</iframe>') | ||
end | ||
|
||
end |