Skip to content

Commit a0a3e8b

Browse files
committed
fix: disallow 'mglyph' and 'malignmark' from safe lists
https://hackerone.com/reports/2519936
1 parent 5104ca9 commit a0a3e8b

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

lib/rails/html/scrubbers.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ def validate!(var, name)
134134
if var && !var.is_a?(Enumerable)
135135
raise ArgumentError, "You should pass :#{name} as an Enumerable"
136136
end
137+
138+
if var && name == :tags
139+
if var.include?("mglyph")
140+
warn("WARNING: 'mglyph' tags cannot be allowed by the PermitScrubber and will be scrubbed")
141+
var.delete("mglyph")
142+
end
143+
144+
if var.include?("malignmark")
145+
warn("WARNING: 'malignmark' tags cannot be allowed by the PermitScrubber and will be scrubbed")
146+
var.delete("malignmark")
147+
end
148+
end
149+
137150
var
138151
end
139152

test/sanitizer_test.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,46 @@ def test_should_sanitize_across_newlines
10261026
assert_equal "", sanitize_css(raw)
10271027
end
10281028

1029+
def test_should_prune_mglyph
1030+
# https://hackerone.com/reports/2519936
1031+
input = "<math><mtext><table><mglyph><style><img src=: onerror=alert(1)>"
1032+
tags = %w(math mtext table mglyph style)
1033+
1034+
actual = nil
1035+
assert_output(nil, /WARNING: 'mglyph' tags cannot be allowed by the PermitScrubber/) do
1036+
actual = safe_list_sanitize(input, tags: tags)
1037+
end
1038+
1039+
acceptable_results = [
1040+
# libxml2
1041+
"<math><mtext><table><style>&lt;img src=: onerror=alert(1)&gt;</style></table></mtext></math>",
1042+
# libgumbo
1043+
"<math><mtext><style><img src=: onerror=alert(1)></style><table></table></mtext></math>",
1044+
]
1045+
1046+
assert_includes(acceptable_results, actual)
1047+
end
1048+
1049+
def test_should_prune_malignmark
1050+
# https://hackerone.com/reports/2519936
1051+
input = "<math><mtext><table><malignmark><style><img src=: onerror=alert(1)>"
1052+
tags = %w(math mtext table malignmark style)
1053+
1054+
actual = nil
1055+
assert_output(nil, /WARNING: 'malignmark' tags cannot be allowed by the PermitScrubber/) do
1056+
actual = safe_list_sanitize(input, tags: tags)
1057+
end
1058+
1059+
acceptable_results = [
1060+
# libxml2
1061+
"<math><mtext><table><style>&lt;img src=: onerror=alert(1)&gt;</style></table></mtext></math>",
1062+
# libgumbo
1063+
"<math><mtext><style><img src=: onerror=alert(1)></style><table></table></mtext></math>",
1064+
]
1065+
1066+
assert_includes(acceptable_results, actual)
1067+
end
1068+
10291069
protected
10301070
def safe_list_sanitize(input, options = {})
10311071
module_under_test::SafeListSanitizer.new.sanitize(input, options)
@@ -1075,5 +1115,37 @@ class HTML4SafeListSanitizerTest < Minitest::Test
10751115
class HTML5SafeListSanitizerTest < Minitest::Test
10761116
@module_under_test = Rails::HTML5
10771117
include SafeListSanitizerTest
1118+
1119+
def test_should_not_be_vulnerable_to_mglyph_namespace_confusion
1120+
# https://hackerone.com/reports/2519936
1121+
input = "<math><mtext><table><mglyph><style><img src=: onerror=alert(1)>"
1122+
tags = %w(math mtext table mglyph style)
1123+
1124+
result = nil
1125+
assert_output(nil, /WARNING/) do
1126+
result = safe_list_sanitize(input, tags: tags)
1127+
end
1128+
1129+
browser = Nokogiri::HTML5::Document.parse(result)
1130+
xss = browser.at_xpath("//img/@onerror")
1131+
1132+
assert_nil(xss)
1133+
end
1134+
1135+
def test_should_not_be_vulnerable_to_malignmark_namespace_confusion
1136+
# https://hackerone.com/reports/2519936
1137+
input = "<math><mtext><table><malignmark><style><img src=: onerror=alert(1)>"
1138+
tags = %w(math mtext table malignmark style)
1139+
1140+
result = nil
1141+
assert_output(nil, /WARNING/) do
1142+
result = safe_list_sanitize(input, tags: tags)
1143+
end
1144+
1145+
browser = Nokogiri::HTML5::Document.parse(result)
1146+
xss = browser.at_xpath("//img/@onerror")
1147+
1148+
assert_nil(xss)
1149+
end
10781150
end if loofah_html5_support?
10791151
end

test/scrubbers_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ def test_leaves_only_supplied_tags_and_attributes
121121
assert_scrubbed html, '<tag></tag><tag cooler=""></tag>'
122122
end
123123

124+
def test_does_not_allow_safelisted_mglyph
125+
# https://hackerone.com/reports/2519936
126+
assert_output(nil, /WARNING: 'mglyph' tags cannot be allowed by the PermitScrubber/) do
127+
@scrubber.tags = ["div", "mglyph", "span"]
128+
end
129+
assert_equal(["div", "span"], @scrubber.tags)
130+
end
131+
124132
def test_leaves_text
125133
assert_scrubbed("some text")
126134
end

0 commit comments

Comments
 (0)