Skip to content

Commit 985f192

Browse files
jhawthornrafaelfranca
authored andcommitted
Avoid backtracking in ActionMailer block_format
[CVE-2024-47889] Thanks to yuki_osaki and scyoon for reporting this vulnerability
1 parent 4f4312b commit 985f192

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

actionmailer/lib/action_mailer/mail_helper.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ def block_format(text)
2323
}.join("\n\n")
2424

2525
# Make list points stand on their own line
26-
formatted.gsub!(/[ ]*([*]+) ([^*]*)/) { " #{$1} #{$2.strip}\n" }
27-
formatted.gsub!(/[ ]*([#]+) ([^#]*)/) { " #{$1} #{$2.strip}\n" }
26+
output = +""
27+
splits = formatted.split(/(\*+|\#+)/)
28+
while line = splits.shift
29+
if line.start_with?("*", "#") && splits[0].start_with?(" ")
30+
output.chomp!(" ") while output.end_with?(" ")
31+
output << " #{line} #{splits.shift.strip}\n"
32+
else
33+
output << line
34+
end
35+
end
2836

29-
formatted
37+
output
3038
end
3139

3240
# Access the mailer instance.

actionmailer/test/mail_helper_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,16 @@ def test_use_cache
121121
assert_equal "Greetings from a cache helper block", mail.body.encoded
122122
end
123123
end
124+
125+
def helper
126+
Object.new.extend(ActionMailer::MailHelper)
127+
end
128+
129+
def test_block_format
130+
assert_equal " * foo\n", helper.block_format(" * foo")
131+
assert_equal " * foo\n", helper.block_format(" * foo")
132+
assert_equal " * foo\n", helper.block_format("* foo")
133+
assert_equal " * foo\n*bar", helper.block_format("* foo*bar")
134+
assert_equal " * foo\n * bar\n", helper.block_format("* foo * bar")
135+
end
124136
end

0 commit comments

Comments
 (0)