Skip to content

Commit 6fdabdf

Browse files
jhawthornmakmic
andcommitted
Avoid backtracking in ActionMailer block_format
[CVE-2024-47889] Thanks to yuki_osaki and scyoon for reporting this vulnerability Co-authored-by: Michael Leimstaedtner <[email protected]>
1 parent 032a493 commit 6fdabdf

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

actionmailer/lib/action_mailer/mail_helper.rb

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

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

31-
formatted
39+
output
3240
end
3341

3442
# Access the mailer instance.

actionmailer/test/mail_helper_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,17 @@ 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+
assert_equal " *", helper.block_format("* ")
136+
end
124137
end

0 commit comments

Comments
 (0)