-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12470 from mkllnk/description-html-test
Sanitise HTML in long description of enterprise [read-only]
- Loading branch information
Showing
4 changed files
with
74 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
# Keeps only allowed HTML. | ||
# | ||
# We store some rich text as HTML in attributes of models like Enterprise. | ||
# We offer an editor which supports certain tags but you can't insert just any | ||
# HTML, which would be dangerous. | ||
class HtmlSanitizer | ||
def self.sanitize(html) | ||
@sanitizer ||= Rails::HTML5::SafeListSanitizer.new | ||
@sanitizer.sanitize( | ||
html, tags: %w[h1 h2 h3 h4 p br b i u a], attributes: %w[href target], | ||
) | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe HtmlSanitizer do | ||
subject { described_class } | ||
|
||
it "removes dangerous tags" do | ||
html = "Hello <script>alert</script>!" | ||
expect(subject.sanitize(html)) | ||
.to eq "Hello alert!" | ||
end | ||
|
||
it "keeps supported tags" do | ||
html = "Hello <b>alert</b>! <br>How are you?" | ||
expect(subject.sanitize(html)) | ||
.to eq "Hello <b>alert</b>! <br>How are you?" | ||
end | ||
|
||
it "keeps supported attributes" do | ||
html = 'Hello <a href="#focus">alert</a>!' | ||
expect(subject.sanitize(html)) | ||
.to eq 'Hello <a href="#focus">alert</a>!' | ||
end | ||
|
||
it "removes unsupported attributes" do | ||
html = 'Hello <a href="#focus" onclick="alert">alert</a>!' | ||
expect(subject.sanitize(html)) | ||
.to eq 'Hello <a href="#focus">alert</a>!' | ||
end | ||
|
||
it "removes dangerous attribute values" do | ||
html = 'Hello <a href="javascript:alert(\"boo!\")">you</a>!' | ||
expect(subject.sanitize(html)) | ||
.to eq 'Hello <a>you</a>!' | ||
end | ||
end |