class RuboCop::Cop::Rails::EnvironmentVariableAccess
This cop looks for direct access to environment variables through the `ENV` variable within the application code. This can lead to runtime errors due to misconfiguration that could have been discovered at boot time if the environment variables were loaded as part of initialization and copied into the application's configuration or secrets. The cop can be configured to allow either reads or writes if required.
@example
# good Rails.application.config.foo Rails.application.config.x.foo.bar Rails.application.secrets.foo Rails.application.config.foo = "bar"
@example AllowReads: false (default)
# bad ENV["FOO"] ENV.fetch("FOO")
@example AllowReads: true
# good ENV["FOO"] ENV.fetch("FOO")
@example AllowWrites: false (default)
# bad ENV["FOO"] = "bar"
@example AllowWrites: true
# good ENV["FOO"] = "bar"
Constants
- READ_MSG
- WRITE_MSG
Public Instance Methods
on_const(node)
click to toggle source
# File lib/rubocop/cop/rails/environment_variable_access.rb, line 41 def on_const(node) add_offense(node, message: READ_MSG) if env_read?(node) && !allow_reads? add_offense(node, message: WRITE_MSG) if env_write?(node) && !allow_writes? end
Private Instance Methods
allow_reads?()
click to toggle source
# File lib/rubocop/cop/rails/environment_variable_access.rb, line 57 def allow_reads? cop_config['AllowReads'] == true end
allow_writes?()
click to toggle source
# File lib/rubocop/cop/rails/environment_variable_access.rb, line 61 def allow_writes? cop_config['AllowWrites'] == true end