Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] A new cop to prefer fetch for Rails.application.secrets #42

Closed
gfx opened this issue Feb 25, 2019 · 4 comments
Closed

[Proposal] A new cop to prefer fetch for Rails.application.secrets #42

gfx opened this issue Feb 25, 2019 · 4 comments
Labels
feature request Request for new functionality

Comments

@gfx
Copy link

gfx commented Feb 25, 2019

Rails.application.secrets.foo returns nil if the entry does not exist. However, accessing a missing entry is likely to a typo.

Fortunately, it has fetch() method that raises errors in a missing entry, so it's a good idea to prefer fetch() instead of the use of getter.

e.g.

# good
foo = Rails.application.secrets.fetch(:foo)

# bad
foo = Rails.application.secrets.foo
@mikegee
Copy link
Contributor

mikegee commented Feb 25, 2019

Good idea. Let's include Rails.application.credentials too, since Rails 5.2 replaces the secrets stuff with credentials.

@Drenmi Drenmi added the feature request Request for new functionality label Feb 26, 2019
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue May 31, 2019
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue May 31, 2019
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue Jun 6, 2019
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue Jun 6, 2019
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue Jun 6, 2019
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue Jun 7, 2019
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue Jun 7, 2019
tejasbubane added a commit to tejasbubane/rubocop-rails that referenced this issue Jun 7, 2019
@koic
Copy link
Member

koic commented Jun 16, 2019

I think that a bang method should be used instead of fetch method because the Rails Guides shows a bang method.
https://guides.rubyonrails.org/security.html#custom-credentials

Rails.application.secrets has the same behavior.

% bin/rails c
Loading development environment (Rails 5.2.3)
[1] pry(main)> Rails.application.credentials.some_api_key
=> nil
[2] pry(main)> Rails.application.credentials.some_api_key!
KeyError: :some_api_key is blank
from /Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/ordered_options.rb:49:in `method_missing'
[3] pry(main)> Rails.application.secrets.some_api_key
=> nil
[4] pry(main)> Rails.application.secrets.some_api_key!
KeyError: :some_api_key is blank
from /Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activesupport-5.2.3/lib/active_support/ordered_options.rb:49:in `method_missing'

No bang method is not a deprecation. If no bang is deprecated or a strongly recommended document (e.g. Rails Guides) with bang is accepted by Rails team (rails/rails repo), I think it is worthwhile to make cop with bang method a good case.

@koic koic closed this as completed Jun 16, 2019
@mikegee
Copy link
Contributor

mikegee commented Jun 16, 2019

Thanks @koic! I didn’t know about that.

@tejasbubane
Copy link
Contributor

If no bang is deprecated or a strongly recommended document (e.g. Rails Guides) with bang is accepted by Rails team (rails/rails repo), I think it is worthwhile to make cop with bang method a good case.

I think it is worthwhile regardless to make a cop that suggests some form of better way to access credentials than direct access.

koic added a commit to rubocop/rubocop that referenced this issue Sep 15, 2023
This PR resolves the following error when running `rake changelog:merge` in RuboCop Rails.
So it will prevent the same error from occurring in this repository.

```conosle
$ bundle exec rake changelog:merge
rake aborted!
NoMethodError: undefined method `[]' for nil
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:139:in `block in contributors'
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:138:in `each'
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:138:in `flat_map'
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:138:in `contributors'
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:132:in `new_contributor_lines'
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:126:in `merge_content'
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:115:in `merge!'
tasks/changelog.rake:21:in `block (2 levels) in <top (required)>'
/Users/koic/.rbenv/versions/3.3.0-dev/bin/bundle:25:in `load'
/Users/koic/.rbenv/versions/3.3.0-dev/bin/bundle:25:in `<main>'
Tasks: TOP => changelog:merge
(See full trace by running task with --trace)
```

This error was caused by the absence of a space after the period:

```diff
-* [#42](rubocop/rubocop-rails#42): Fix an error for `Rails/RedundantActiveRecordAllMethod`.([contributor_name])
+* [#42](rubocop/rubocop-rails#42): Fix an error for `Rails/RedundantActiveRecordAllMethod`. ([contributor_name])
```

The added test verifies that there is a single space between the period and the parentheses.  And tweaks changelog entries

NOTE: For compatibility with outdated formats, if there's no contributor name, it checks that the line ends with a period.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request for new functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants