Skip to content

Commit

Permalink
[Fix #8035] Fix a false positive for Lint/DeprecatedOpenSSLConstant
Browse files Browse the repository at this point in the history
Fixes #8035.

This PR fixes the following false positive for `Lint/DeprecatedOpenSSLConstant`
when using double quoted string argument.

```console
% cat example.rb
OpenSSL::Cipher::AES256.new('cbc')
OpenSSL::Cipher::AES256.new("cbc")

% bundle exec rubocop --only Lint/DeprecatedOpenSSLConstant -a
(snip)

Offenses:

example.rb:1:1: W: [Corrected] Lint/DeprecatedOpenSSLConstant: Use
OpenSSL::Cipher.new('AES-256-cbc') instead of OpenSSL::Cipher::AES256.new('cbc').
OpenSSL::Cipher::AES256.new('cbc')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.rb:2:1: W: [Corrected] Lint/DeprecatedOpenSSLConstant: Use
OpenSSL::Cipher.new('AES-256-"cbc"') instead of OpenSSL::Cipher::AES256.new("cbc").
OpenSSL::Cipher::AES256.new("cbc")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

% cat example.rb
OpenSSL::Cipher.new('AES-256-cbc')
OpenSSL::Cipher.new('AES-256-"cbc"')
```

#8035 has another different false positive, which resolves as another PR.
  • Loading branch information
koic authored and bbatsov committed May 26, 2020
1 parent c7a4e54 commit 52dc41c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@
* [#8012](https://github.com/rubocop-hq/rubocop/issues/8012): Fix an incorrect autocorrect for `Lint/DeprecatedOpenSSLConstant` when deprecated OpenSSL constant is used in a block. ([@koic][])
* [#8017](https://github.com/rubocop-hq/rubocop/pull/8017): Fix a false positive for `Lint/SuppressedException` when empty rescue with comment in `def`. ([@koic][])
* [#7990](https://github.com/rubocop-hq/rubocop/issues/7990): Fix resolving `inherit_gem` in remote configs. ([@CvX][])
* [#8035](https://github.com/rubocop-hq/rubocop/issues/8035): Fix the following false positive for `Lint/DeprecatedOpenSSLConstant` when using double quoted string argument. ([@koic][])

## 0.84.0 (2020-05-21)

Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb
Expand Up @@ -106,7 +106,11 @@ def algorithm_name(node)
end

def sanitize_arguments(arguments)
arguments.flat_map { |arg| arg.source.tr(":'", '').split('-') }
arguments.flat_map do |arg|
argument = arg.str_type? ? arg.value : arg.source

argument.tr(":'", '').split('-')
end
end

def replacement_args(node)
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/lint/deprecated_open_ssl_constant_spec.rb
Expand Up @@ -27,6 +27,17 @@
RUBY
end

it 'registers an offense with cipher constant and double quoted string argument and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Cipher::AES128.new("GCM")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-GCM')` instead of `OpenSSL::Cipher::AES128.new("GCM")`.
RUBY

expect_correction(<<~RUBY)
OpenSSL::Cipher.new('AES-128-GCM')
RUBY
end

it 'registers an offense with AES + blocksize constant and mode argument and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Cipher::AES128.new(:GCM)
Expand Down

0 comments on commit 52dc41c

Please sign in to comment.