Skip to content

Commit

Permalink
Merge pull request #48984 from shouichi/sandbox-by-default
Browse files Browse the repository at this point in the history
Add an option to start rails console in sandbox mode by default
  • Loading branch information
rafaelfranca committed Aug 22, 2023
2 parents a4d02fb + b8decf7 commit 9b6dee0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
4 changes: 4 additions & 0 deletions guides/source/configuring.md
Expand Up @@ -311,6 +311,10 @@ Sets the format used in responses when errors occur in the development environme

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`.

#### `config.sandbox_by_default`

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`.

#### `config.dom_testing_default_html_version`

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`.
Expand Down
11 changes: 11 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,14 @@
* Add an option to start rails console in sandbox mode by default

`sandbox_by_default` option is added to start rails console in sandbox
mode by default. With this option turned on, `--no-sandbox` must be
specified to start rails in non-sandbox mode.

Note that this option is ignored when rails environment is development
or test.

*Shouichi Kamiya*

* Omit `webdrivers` gem dependency from `Gemfile` template

*Sean Doyle*
Expand Down
6 changes: 4 additions & 2 deletions railties/lib/rails/application/configuration.rb
Expand Up @@ -21,8 +21,9 @@ class Configuration < ::Rails::Engine::Configuration
:beginning_of_week, :filter_redirect, :x,
:read_encrypted_secrets, :log_level, :content_security_policy_report_only,
:content_security_policy_nonce_generator, :content_security_policy_nonce_directives,
:require_master_key, :credentials, :disable_sandbox, :add_autoload_paths_to_load_path,
:rake_eager_load, :server_timing, :log_file_size, :dom_testing_default_html_version
:require_master_key, :credentials, :disable_sandbox, :sandbox_by_default,
:add_autoload_paths_to_load_path, :rake_eager_load, :server_timing, :log_file_size,
:dom_testing_default_html_version

attr_reader :encoding, :api_only, :loaded_config_version

Expand Down Expand Up @@ -76,6 +77,7 @@ def initialize(*)
@loaded_config_version = nil
@credentials = ActiveSupport::InheritableOptions.new(credentials_defaults)
@disable_sandbox = false
@sandbox_by_default = false
@add_autoload_paths_to_load_path = true
@permissions_policy = nil
@rake_eager_load = false
Expand Down
8 changes: 6 additions & 2 deletions railties/lib/rails/commands/console/console_command.rb
Expand Up @@ -46,7 +46,11 @@ def initialize(app, options = {})
end

def sandbox?
options[:sandbox]
return options[:sandbox] if !options[:sandbox].nil?

return false if Rails.env.local?

app.config.sandbox_by_default
end

def environment
Expand Down Expand Up @@ -79,7 +83,7 @@ module Command
class ConsoleCommand < Base # :nodoc:
include EnvironmentArgument

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

def initialize(args = [], local_options = {}, config = {})
Expand Down
36 changes: 36 additions & 0 deletions railties/test/application/console_test.rb
Expand Up @@ -165,6 +165,42 @@ def test_sandbox_when_sandbox_is_disabled
assert_equal 1, $?.exitstatus
end

def test_sandbox_by_default
add_to_config <<-RUBY
config.sandbox_by_default = true
RUBY

options = "-e production -- --verbose --nocolorize"
spawn_console(options)

write_prompt "puts Rails.application.sandbox", "puts Rails.application.sandbox\r\ntrue"
@primary.puts "quit"
end

def test_sandbox_by_default_with_no_sandbox
add_to_config <<-RUBY
config.sandbox_by_default = true
RUBY

options = "-e production --no-sandbox -- --verbose --nocolorize"
spawn_console(options)

write_prompt "puts Rails.application.sandbox", "puts Rails.application.sandbox\r\nfalse"
@primary.puts "quit"
end

def test_sandbox_by_default_with_development_environment
add_to_config <<-RUBY
config.sandbox_by_default = true
RUBY

options = "-- --verbose --nocolorize"
spawn_console(options)

write_prompt "puts Rails.application.sandbox", "puts Rails.application.sandbox\r\nfalse"
@primary.puts "quit"
end

def test_environment_option_and_irb_option
options = "-e test -- --verbose --nocolorize"
spawn_console(options)
Expand Down
2 changes: 1 addition & 1 deletion railties/test/commands/console_test.rb
Expand Up @@ -169,7 +169,7 @@ def app
def build_app(console)
mocked_console = Class.new do
attr_accessor :sandbox
attr_reader :console, :disable_sandbox
attr_reader :console, :disable_sandbox, :sandbox_by_default

def initialize(console)
@console = console
Expand Down

0 comments on commit 9b6dee0

Please sign in to comment.