Skip to content

Commit 65fb72f

Browse files
committed
Merge branch 'h1-2519936-mglyph-foster-parenting' into flavorjones-2024-security-fixes
* h1-2519936-mglyph-foster-parenting: fix: disallow 'mglyph' and 'malignmark' from safe lists
2 parents 3fe22a8 + a0a3e8b commit 65fb72f

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

lib/rails/html/scrubbers.rb

+13
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ def validate!(var, name)
138138
if var && !var.is_a?(Enumerable)
139139
raise ArgumentError, "You should pass :#{name} as an Enumerable"
140140
end
141+
142+
if var && name == :tags
143+
if var.include?("mglyph")
144+
warn("WARNING: 'mglyph' tags cannot be allowed by the PermitScrubber and will be scrubbed")
145+
var.delete("mglyph")
146+
end
147+
148+
if var.include?("malignmark")
149+
warn("WARNING: 'malignmark' tags cannot be allowed by the PermitScrubber and will be scrubbed")
150+
var.delete("malignmark")
151+
end
152+
end
153+
141154
var
142155
end
143156

test/sanitizer_test.rb

+72
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,46 @@ def test_should_sanitize_across_newlines
10961096
assert_equal "", sanitize_css(raw)
10971097
end
10981098

1099+
def test_should_prune_mglyph
1100+
# https://hackerone.com/reports/2519936
1101+
input = "<math><mtext><table><mglyph><style><img src=: onerror=alert(1)>"
1102+
tags = %w(math mtext table mglyph style)
1103+
1104+
actual = nil
1105+
assert_output(nil, /WARNING: 'mglyph' tags cannot be allowed by the PermitScrubber/) do
1106+
actual = safe_list_sanitize(input, tags: tags)
1107+
end
1108+
1109+
acceptable_results = [
1110+
# libxml2
1111+
"<math><mtext><table><style>&lt;img src=: onerror=alert(1)&gt;</style></table></mtext></math>",
1112+
# libgumbo
1113+
"<math><mtext><style><img src=: onerror=alert(1)></style><table></table></mtext></math>",
1114+
]
1115+
1116+
assert_includes(acceptable_results, actual)
1117+
end
1118+
1119+
def test_should_prune_malignmark
1120+
# https://hackerone.com/reports/2519936
1121+
input = "<math><mtext><table><malignmark><style><img src=: onerror=alert(1)>"
1122+
tags = %w(math mtext table malignmark style)
1123+
1124+
actual = nil
1125+
assert_output(nil, /WARNING: 'malignmark' tags cannot be allowed by the PermitScrubber/) do
1126+
actual = safe_list_sanitize(input, tags: tags)
1127+
end
1128+
1129+
acceptable_results = [
1130+
# libxml2
1131+
"<math><mtext><table><style>&lt;img src=: onerror=alert(1)&gt;</style></table></mtext></math>",
1132+
# libgumbo
1133+
"<math><mtext><style><img src=: onerror=alert(1)></style><table></table></mtext></math>",
1134+
]
1135+
1136+
assert_includes(acceptable_results, actual)
1137+
end
1138+
10991139
protected
11001140
def safe_list_sanitize(input, options = {})
11011141
module_under_test::SafeListSanitizer.new.sanitize(input, options)
@@ -1175,5 +1215,37 @@ def test_should_not_be_vulnerable_to_ns_confusion_2519941
11751215

11761216
assert_nil(xss)
11771217
end
1218+
1219+
def test_should_not_be_vulnerable_to_mglyph_namespace_confusion
1220+
# https://hackerone.com/reports/2519936
1221+
input = "<math><mtext><table><mglyph><style><img src=: onerror=alert(1)>"
1222+
tags = %w(math mtext table mglyph style)
1223+
1224+
result = nil
1225+
assert_output(nil, /WARNING/) do
1226+
result = safe_list_sanitize(input, tags: tags)
1227+
end
1228+
1229+
browser = Nokogiri::HTML5::Document.parse(result)
1230+
xss = browser.at_xpath("//img/@onerror")
1231+
1232+
assert_nil(xss)
1233+
end
1234+
1235+
def test_should_not_be_vulnerable_to_malignmark_namespace_confusion
1236+
# https://hackerone.com/reports/2519936
1237+
input = "<math><mtext><table><malignmark><style><img src=: onerror=alert(1)>"
1238+
tags = %w(math mtext table malignmark style)
1239+
1240+
result = nil
1241+
assert_output(nil, /WARNING/) do
1242+
result = safe_list_sanitize(input, tags: tags)
1243+
end
1244+
1245+
browser = Nokogiri::HTML5::Document.parse(result)
1246+
xss = browser.at_xpath("//img/@onerror")
1247+
1248+
assert_nil(xss)
1249+
end
11781250
end if loofah_html5_support?
11791251
end

test/scrubbers_test.rb

+8
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)