Skip to content

Commit 6b6df4f

Browse files
committed
Merge pull request rails#53490 from rails/rm-regexp-timeout-2
Default Regexp.timeout to 1s
1 parent bb0ba7d commit 6b6df4f

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

guides/source/8_0_release_notes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Please refer to the [Changelog][railties] for detailed changes.
4040

4141
### Notable changes
4242

43+
* Set `Regexp.timeout` to `1`s by default to improve security over Regexp Denial-of-Service attacks.
44+
4345
Action Cable
4446
------------
4547

guides/source/configuring.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Below are the default values associated with each target version. In cases of co
6262

6363
#### Default Values for Target Version 8.0
6464

65+
- [`Regexp.timeout`](#regexp-timeout): `1`
6566
- [`config.action_dispatch.strict_freshness`](#config-action-dispatch-strict-freshness): `true`
6667
- [`config.active_support.to_time_preserves_timezone`](#config-active-support-to-time-preserves-timezone): `:zone`
6768

@@ -3164,6 +3165,11 @@ Configures the HTML sanitizer used by Action Text by setting `ActionText::Conten
31643165
31653166
NOTE: `Rails::HTML5::Sanitizer` is not supported on JRuby, so on JRuby platforms Rails will fall back to `Rails::HTML4::Sanitizer`.
31663167
3168+
#### `Regexp.timeout`
3169+
3170+
3171+
See Ruby's documentation for [`Regexp.timeout=`](https://docs.ruby-lang.org/en/3.3/Regexp.html#method-c-timeout-3D).
3172+
31673173
### Configuring a Database
31683174

31693175
Just about every Rails application will interact with a database. You can connect to the database by setting an environment variable `ENV['DATABASE_URL']` or by using a configuration file called `config/database.yml`.

railties/lib/rails/application/configuration.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ def load_defaults(target_version)
344344
if respond_to?(:action_dispatch)
345345
action_dispatch.strict_freshness = true
346346
end
347+
348+
Regexp.timeout ||= 1
347349
when "8.1"
348350
load_defaults "8.0"
349351
else

railties/test/application/configuration_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4815,6 +4815,17 @@ class Foo < ApplicationRecord
48154815
assert_instance_of ActiveJob::QueueAdapters::TestAdapter, adapter
48164816
end
48174817

4818+
test "Regexp.timeout is set to 1s by default" do
4819+
app "development"
4820+
assert_equal 1, Regexp.timeout
4821+
end
4822+
4823+
test "Regexp.timeout can be configured" do
4824+
add_to_config "Regexp.timeout = 5"
4825+
app "development"
4826+
assert_equal 5, Regexp.timeout
4827+
end
4828+
48184829
private
48194830
def set_custom_config(contents, config_source = "custom".inspect)
48204831
app_file "config/custom.yml", contents

tools/rail_inspector/lib/rail_inspector/visitor/framework_default.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ def visit_assign(node)
8686
end
8787
@configs[target] = value
8888
end
89+
90+
def visit_opassign(node)
91+
if node.operator.name == :"||="
92+
visit_assign(node)
93+
else
94+
super
95+
end
96+
end
8997
end
9098

9199
private

0 commit comments

Comments
 (0)