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 record encryptor hash values JSON parsing for legacy unencrypted hash values #7496

Merged
merged 1 commit into from
Mar 2, 2021
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
13 changes: 12 additions & 1 deletion decidim-core/lib/decidim/record_encryptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,18 @@ def encrypt_value(value)
def decrypt_hash_values(hash)
return hash unless hash.is_a?(Hash)

hash.transform_values { |value| ActiveSupport::JSON.decode(decrypt_value(value)) }
hash.transform_values do |value|
decrypted_value = decrypt_value(value)

# When handling legacy non-encrypted hash values, the decrypted values
# could not be valid JSON strings. They could be normal strings that
# cannot be JSON decoded.
begin
ActiveSupport::JSON.decode(decrypted_value)
rescue JSON::ParserError
decrypted_value
end
end
end

def encrypt_hash_values(hash)
Expand Down
13 changes: 13 additions & 0 deletions decidim-core/spec/lib/record_encryptor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ module Decidim
# original value is returned instead.
expect(subject.name).to eq("Unencrypted")
end

it "returns the original hash values when the JSON parsing fails for the hash values" do
subject.instance_variable_set(
:@metadata,
"email" => "example001@example.org",
"verification_code" => "123456789"
)

expect(subject.metadata).to eq(
"email" => "example001@example.org",
"verification_code" => 123_456_789
)
end
end

it_behaves_like "encrypted record"
Expand Down