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

Fix Lint/DeprecatedOpenSSLConstant auto-correction of OpenSSL::Cipher to use lower case #8262

Merged
merged 1 commit into from Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@
* [#8257](https://github.com/rubocop-hq/rubocop/issues/8257): Fix an error for `Style/BisectedAttrAccessor` when using `attr_reader` and `attr_writer` with splat arguments. ([@fatkodima][])
* [#8239](https://github.com/rubocop-hq/rubocop/pull/8239): Don't load `.rubocop.yml` from personal folders to check for exclusions if given a custom configuration file. ([@deivid-rodriguez][])
* [#8256](https://github.com/rubocop-hq/rubocop/issues/8256): Fix an error for `--auto-gen-config` when running a cop who do not support auto-correction. ([@koic][])
* [#8262](https://github.com/rubocop-hq/rubocop/pull/8262): Fix `Lint/DeprecatedOpenSSLConstant` auto-correction of `OpenSSL::Cipher` to use lower case, as some Linux-based systems do not accept upper cased cipher names. ([@bdewater][])

## 0.87.0 (2020-07-06)

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/cops_lint.adoc
Expand Up @@ -507,7 +507,7 @@ instead.
OpenSSL::Cipher::AES.new(128, :GCM)

# good
OpenSSL::Cipher.new('AES-128-GCM')
OpenSSL::Cipher.new('aes-128-gcm')
----

[source,ruby]
Expand Down
8 changes: 4 additions & 4 deletions lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb
Expand Up @@ -15,7 +15,7 @@ module Lint
# OpenSSL::Cipher::AES.new(128, :GCM)
#
# # good
# OpenSSL::Cipher.new('AES-128-GCM')
# OpenSSL::Cipher.new('aes-128-gcm')
#
# @example
#
Expand Down Expand Up @@ -127,9 +127,9 @@ def replacement_args(node)
end

def build_cipher_arguments(node, algorithm_name)
algorithm_parts = algorithm_name.split('-')
size_and_mode = sanitize_arguments(node.arguments)
"'#{(algorithm_parts + size_and_mode + ['CBC']).take(3).join('-')}'"
algorithm_parts = algorithm_name.downcase.split('-')
size_and_mode = sanitize_arguments(node.arguments).map(&:downcase)
"'#{(algorithm_parts + size_and_mode + ['cbc']).take(3).join('-')}'"
end
end
end
Expand Down
22 changes: 11 additions & 11 deletions spec/rubocop/cop/lint/deprecated_open_ssl_constant_spec.rb
Expand Up @@ -8,61 +8,61 @@
it 'registers an offense with cipher constant and two arguments and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Cipher::AES.new(128, :GCM)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-GCM')` instead of `OpenSSL::Cipher::AES.new(128, :GCM)`.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('aes-128-gcm')` instead of `OpenSSL::Cipher::AES.new(128, :GCM)`.
RUBY

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

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

expect_correction(<<~RUBY)
OpenSSL::Cipher.new('AES-128-GCM')
OpenSSL::Cipher.new('aes-128-gcm')
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")`.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('aes-128-gcm')` instead of `OpenSSL::Cipher::AES128.new("GCM")`.
RUBY

expect_correction(<<~RUBY)
OpenSSL::Cipher.new('AES-128-GCM')
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)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-GCM')` instead of `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')
OpenSSL::Cipher.new('aes-128-gcm')
RUBY
end

it 'registers an offense with AES + blocksize constant and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Cipher::AES128.new
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-CBC')` instead of `OpenSSL::Cipher::AES128.new`.
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('aes-128-cbc')` instead of `OpenSSL::Cipher::AES128.new`.
RUBY

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

it 'does not register an offense when using cipher with a string' do
expect_no_offenses(<<~RUBY)
OpenSSL::Cipher.new('AES-128-GCM')
OpenSSL::Cipher.new('aes-128-gcm')
RUBY
end

Expand Down