Merge pull request #42370 from sunny/better-error-message-when-encrypted-file-key-is-blank

Raise missing key error when master key env variable is blank
This commit is contained in:
Rafael França 2021-06-07 16:59:35 -04:00 committed by GitHub
commit 5c09cfd96f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 2 deletions

View File

@ -1,3 +1,8 @@
* Raise `ActiveSupport::EncryptedFile::MissingKeyError` when the
`RAILS_MASTER_KEY` environment variable is blank (e.g. `""`).
*Sunny Ripert*
* The `from:` option is added to `ActiveSupport::TestCase#assert_no_changes`.
It permits asserting on the initial value that is expected not to change.

View File

@ -98,7 +98,7 @@ module ActiveSupport
def read_env_key
ENV[env_key]
ENV[env_key].presence
end
def read_key_file

View File

@ -55,6 +55,22 @@ class EncryptedFileTest < ActiveSupport::TestCase
end
end
test "raise MissingKeyError when env key is blank" do
FileUtils.rm_rf @key_path
begin
ENV["CONTENT_KEY"] = ""
raised = assert_raise ActiveSupport::EncryptedFile::MissingKeyError do
@encrypted_file.write @content
@encrypted_file.read
end
assert_match(/Missing encryption key to decrypt file/, raised.message)
ensure
ENV["CONTENT_KEY"] = nil
end
end
test "raise InvalidKeyLengthError when key is too short" do
File.write(@key_path, ActiveSupport::EncryptedFile.generate_key[0..-2])