Skip to content

Commit d7a94c1

Browse files
committed
Merge branch 'h1-2503220-nokogiri-serialization' into flavorjones-2024-security-fixes
* h1-2503220-nokogiri-serialization: dep: bump Nokogiri dependency to address the foreign style issue test: Nokogiri's HTML5 "foreign style serialization" issue
2 parents 3fd6e65 + b0220b8 commit d7a94c1

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

Gemfile

-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,3 @@ group :rubocop do
1414
gem "rubocop-performance", require: false
1515
gem "rubocop-rails", require: false
1616
end
17-
18-
# specify gem versions for old rubies
19-
gem "nokogiri", ">= 1.7"
20-
gem "activesupport", ">= 5"

Gemfile.lock

+6-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PATH
33
specs:
44
rails-html-sanitizer (1.6.0)
55
loofah (~> 2.21)
6-
nokogiri (~> 1.14)
6+
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
77

88
GEM
99
remote: https://rubygems.org/
@@ -37,18 +37,10 @@ GEM
3737
loofah (2.23.1)
3838
crass (~> 1.0.2)
3939
nokogiri (>= 1.12.0)
40+
mini_portile2 (2.8.8)
4041
minitest (5.25.2)
41-
nokogiri (1.16.7-aarch64-linux)
42-
racc (~> 1.4)
43-
nokogiri (1.16.7-arm-linux)
44-
racc (~> 1.4)
45-
nokogiri (1.16.7-arm64-darwin)
46-
racc (~> 1.4)
47-
nokogiri (1.16.7-x86-linux)
48-
racc (~> 1.4)
49-
nokogiri (1.16.7-x86_64-darwin)
50-
racc (~> 1.4)
51-
nokogiri (1.16.7-x86_64-linux)
42+
nokogiri (1.16.8)
43+
mini_portile2 (~> 2.8.2)
5244
racc (~> 1.4)
5345
parallel (1.26.3)
5446
parser (3.3.6.0)
@@ -94,17 +86,11 @@ GEM
9486
uri (1.0.2)
9587

9688
PLATFORMS
97-
aarch64-linux
98-
arm-linux
99-
arm64-darwin
100-
x86-linux
101-
x86_64-darwin
89+
ruby
10290
x86_64-linux
10391

10492
DEPENDENCIES
105-
activesupport (>= 5)
10693
minitest
107-
nokogiri (>= 1.7)
10894
rails-html-sanitizer!
10995
rake
11096
rubocop (>= 1.25.1)
@@ -114,4 +100,4 @@ DEPENDENCIES
114100
rubocop-rails
115101

116102
BUNDLED WITH
117-
2.5.4
103+
2.5.23

rails-html-sanitizer.gemspec

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ Gem::Specification.new do |spec|
2626
spec.test_files = Dir["test/**/*"]
2727
spec.require_paths = ["lib"]
2828

29-
# NOTE: There's no need to update dependencies for CVEs in minor releases
30-
# when users can simply run `bundle update loofah`.
3129
spec.add_dependency "loofah", "~> 2.21"
32-
spec.add_dependency "nokogiri", "~> 1.14"
30+
31+
# A fix was shipped in nokogiri v1.15.7 and v1.16.8 without which there is a vulnerability in this gem.
32+
spec.add_dependency "nokogiri", [">=1.15.7",
33+
"!=1.16.0", "!=1.16.0.rc1", "!=1.16.1", "!=1.16.2", "!=1.16.3",
34+
"!=1.16.4", "!=1.16.5", "!=1.16.6", "!=1.16.7"]
3335
end

test/sanitizer_test.rb

+38
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,34 @@ def test_combination_of_svg_and_style_with_img_payload_2
976976
assert_includes(acceptable_results, actual)
977977
end
978978

979+
def test_combination_of_svg_and_style_with_escaped_img_payload
980+
# https://hackerone.com/reports/2503220
981+
input, tags = "<svg><style>&lt;img src onerror=alert(1)>", ["svg", "style"]
982+
actual = safe_list_sanitize(input, tags: tags)
983+
acceptable_results = [
984+
# libxml2
985+
"<svg><style>&amp;lt;img src onerror=alert(1)&gt;</style></svg>",
986+
# libgumbo
987+
"<svg><style>&lt;img src onerror=alert(1)&gt;</style></svg>",
988+
]
989+
990+
assert_includes(acceptable_results, actual)
991+
end
992+
993+
def test_combination_of_math_and_style_with_escaped_img_payload
994+
# https://hackerone.com/reports/2503220
995+
input, tags = "<math><style>&lt;img src onerror=alert(1)>", ["math", "style"]
996+
actual = safe_list_sanitize(input, tags: tags)
997+
acceptable_results = [
998+
# libxml2
999+
"<math><style>&amp;lt;img src onerror=alert(1)&gt;</style></math>",
1000+
# libgumbo
1001+
"<math><style>&lt;img src onerror=alert(1)&gt;</style></math>",
1002+
]
1003+
1004+
assert_includes(acceptable_results, actual)
1005+
end
1006+
9791007
def test_should_sanitize_illegal_style_properties
9801008
raw = %(display:block; position:absolute; left:0; top:0; width:100%; height:100%; z-index:1; background-color:black; background-image:url(http://www.ragingplatypus.com/i/cam-full.jpg); background-x:center; background-y:center; background-repeat:repeat;)
9811009
expected = %(display:block;width:100%;height:100%;background-color:black;background-x:center;background-y:center;)
@@ -1075,5 +1103,15 @@ class HTML4SafeListSanitizerTest < Minitest::Test
10751103
class HTML5SafeListSanitizerTest < Minitest::Test
10761104
@module_under_test = Rails::HTML5
10771105
include SafeListSanitizerTest
1106+
1107+
def test_should_not_be_vulnerable_to_nokogiri_foreign_style_serialization_bug
1108+
# https://hackerone.com/reports/2503220
1109+
input = "<svg><style>&lt;img src onerror=alert(1)>"
1110+
result = Rails::HTML5::SafeListSanitizer.new.sanitize(input, tags: ["svg", "style"])
1111+
browser = Nokogiri::HTML5::Document.parse(result)
1112+
xss = browser.at_xpath("//img/@onerror")
1113+
1114+
assert_nil(xss)
1115+
end
10781116
end if loofah_html5_support?
10791117
end

0 commit comments

Comments
 (0)