Skip to content

Commit 9b6dee0

Browse files
authored
Merge pull request rails#48984 from shouichi/sandbox-by-default
Add an option to start rails console in sandbox mode by default
2 parents a4d02fb + b8decf7 commit 9b6dee0

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed

guides/source/configuring.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,10 @@ Sets the format used in responses when errors occur in the development environme
311311

312312
Controls whether or not someone can start a console in sandbox mode. This is helpful to avoid a long running session of sandbox console, that could lead a database server to run out of memory. Defaults to `false`.
313313

314+
#### `config.sandbox_by_default`
315+
316+
When `true`, rails console starts in sandbox mode. To start rails console in non-sandbox mode, `--no-sandbox` must be specified. This is helpful to avoid accidental writing to the production database. Defaults to `false`.
317+
314318
#### `config.dom_testing_default_html_version`
315319

316320
Controls whether an HTML4 parser or an HTML5 parser is used by default by the test helpers in Action View, Action Dispatch, and `rails-dom-testing`.

railties/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
* Add an option to start rails console in sandbox mode by default
2+
3+
`sandbox_by_default` option is added to start rails console in sandbox
4+
mode by default. With this option turned on, `--no-sandbox` must be
5+
specified to start rails in non-sandbox mode.
6+
7+
Note that this option is ignored when rails environment is development
8+
or test.
9+
10+
*Shouichi Kamiya*
11+
112
* Omit `webdrivers` gem dependency from `Gemfile` template
213

314
*Sean Doyle*

railties/lib/rails/application/configuration.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ class Configuration < ::Rails::Engine::Configuration
2121
:beginning_of_week, :filter_redirect, :x,
2222
:read_encrypted_secrets, :log_level, :content_security_policy_report_only,
2323
:content_security_policy_nonce_generator, :content_security_policy_nonce_directives,
24-
:require_master_key, :credentials, :disable_sandbox, :add_autoload_paths_to_load_path,
25-
:rake_eager_load, :server_timing, :log_file_size, :dom_testing_default_html_version
24+
:require_master_key, :credentials, :disable_sandbox, :sandbox_by_default,
25+
:add_autoload_paths_to_load_path, :rake_eager_load, :server_timing, :log_file_size,
26+
:dom_testing_default_html_version
2627

2728
attr_reader :encoding, :api_only, :loaded_config_version
2829

@@ -76,6 +77,7 @@ def initialize(*)
7677
@loaded_config_version = nil
7778
@credentials = ActiveSupport::InheritableOptions.new(credentials_defaults)
7879
@disable_sandbox = false
80+
@sandbox_by_default = false
7981
@add_autoload_paths_to_load_path = true
8082
@permissions_policy = nil
8183
@rake_eager_load = false

railties/lib/rails/commands/console/console_command.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ def initialize(app, options = {})
4646
end
4747

4848
def sandbox?
49-
options[:sandbox]
49+
return options[:sandbox] if !options[:sandbox].nil?
50+
51+
return false if Rails.env.local?
52+
53+
app.config.sandbox_by_default
5054
end
5155

5256
def environment
@@ -79,7 +83,7 @@ module Command
7983
class ConsoleCommand < Base # :nodoc:
8084
include EnvironmentArgument
8185

82-
class_option :sandbox, aliases: "-s", type: :boolean, default: false,
86+
class_option :sandbox, aliases: "-s", type: :boolean, default: nil,
8387
desc: "Rollback database modifications on exit."
8488

8589
def initialize(args = [], local_options = {}, config = {})

railties/test/application/console_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,42 @@ def test_sandbox_when_sandbox_is_disabled
165165
assert_equal 1, $?.exitstatus
166166
end
167167

168+
def test_sandbox_by_default
169+
add_to_config <<-RUBY
170+
config.sandbox_by_default = true
171+
RUBY
172+
173+
options = "-e production -- --verbose --nocolorize"
174+
spawn_console(options)
175+
176+
write_prompt "puts Rails.application.sandbox", "puts Rails.application.sandbox\r\ntrue"
177+
@primary.puts "quit"
178+
end
179+
180+
def test_sandbox_by_default_with_no_sandbox
181+
add_to_config <<-RUBY
182+
config.sandbox_by_default = true
183+
RUBY
184+
185+
options = "-e production --no-sandbox -- --verbose --nocolorize"
186+
spawn_console(options)
187+
188+
write_prompt "puts Rails.application.sandbox", "puts Rails.application.sandbox\r\nfalse"
189+
@primary.puts "quit"
190+
end
191+
192+
def test_sandbox_by_default_with_development_environment
193+
add_to_config <<-RUBY
194+
config.sandbox_by_default = true
195+
RUBY
196+
197+
options = "-- --verbose --nocolorize"
198+
spawn_console(options)
199+
200+
write_prompt "puts Rails.application.sandbox", "puts Rails.application.sandbox\r\nfalse"
201+
@primary.puts "quit"
202+
end
203+
168204
def test_environment_option_and_irb_option
169205
options = "-e test -- --verbose --nocolorize"
170206
spawn_console(options)

railties/test/commands/console_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def app
169169
def build_app(console)
170170
mocked_console = Class.new do
171171
attr_accessor :sandbox
172-
attr_reader :console, :disable_sandbox
172+
attr_reader :console, :disable_sandbox, :sandbox_by_default
173173

174174
def initialize(console)
175175
@console = console

0 commit comments

Comments
 (0)