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

Keep nil values set on cloaked fields #3

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/erebus.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ defmodule Erebus do
* options - providing backend and options for that backend
"""
def decrypt(struct, fields_to_decrypt, opts \\ []) do
encrypted_dek = struct.dek |> Erebus.EncryptedData.cast_if_needed()
encrypted_dek = Erebus.EncryptedData.cast_if_needed(struct.dek)

decrypted_dek = Erebus.SymmetricKeyStore.get_key(encrypted_dek, opts)

decrypted_fields =
Enum.map(fields_to_decrypt, fn field ->
fields_to_decrypt
|> Enum.map(fn field ->
stringified_field = Atom.to_string(field)

encrypted_field = Map.get(struct, String.to_atom(stringified_field <> "_encrypted"))
Expand Down Expand Up @@ -181,13 +182,18 @@ defmodule Erebus do
nil ->
nil

field ->
field_name ->
aead = :crypto.strong_rand_bytes(16)
iv = :crypto.strong_rand_bytes(16)

stringified = Atom.to_string(field)
stringified = Atom.to_string(field_name)

data_to_encrypt = struct.changes |> Map.get(field) || Map.get(struct.data, field)
data_to_encrypt =
Map.get_lazy(
struct.changes,
field_name,
fn -> Map.get(struct.data, field_name) end
)

if not is_nil(data_to_encrypt) do
{ciphertext, ciphertag} =
Expand Down Expand Up @@ -218,7 +224,6 @@ defmodule Erebus do
]
end
end)
|> Enum.filter(& &1)
|> List.flatten()
|> Enum.into(%{})

Expand Down
29 changes: 20 additions & 9 deletions test/erebus_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
:ok
end

defmodule EncryptedStuff do
defmodule EncryptedModel do
use Ecto.Schema
import Ecto.Changeset
import Erebus.Schema
Expand Down Expand Up @@ -47,11 +47,11 @@
end

test "encrypting and decrypting data with ecto" do
model = %EncryptedStuff{}
model = %EncryptedModel{}

encrypted =
model
|> EncryptedStuff.changeset(%{first: "hello", second: "there"})
|> EncryptedModel.changeset(%{first: "hello", second: "there"})
|> Ecto.Changeset.apply_changes()
# simulate reloading with virtual fields emptied
|> Map.merge(%{first: nil, second: nil})
Expand Down Expand Up @@ -86,7 +86,7 @@

encrypted_2 =
encrypted
|> EncryptedStuff.changeset(%{second: "thereX"})
|> EncryptedModel.changeset(%{second: "thereX"})
|> Ecto.Changeset.apply_changes()

assert hash_before != encrypted_2.first_encrypted
Expand All @@ -96,19 +96,19 @@

encrypted_3 =
encrypted
|> EncryptedStuff.changeset(%{other: "somestring"})
|> EncryptedModel.changeset(%{other: "somestring"})
|> Ecto.Changeset.apply_changes()

assert hash_before == encrypted_3.first_encrypted
assert dek_before == encrypted_3.dek
end

test "encrypting and decrypting data with one field being null" do

Check failure on line 106 in test/erebus_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

test encrypting and decrypting data with one field being null (Erebus.Test)
model = %EncryptedStuff{}
model = %EncryptedModel{}

encrypted =
model
|> EncryptedStuff.changeset(%{first: "hello"})
|> EncryptedModel.changeset(%{first: "hello"})
|> Ecto.Changeset.apply_changes()
# simulate reloading with virtual fields emptied
|> Map.merge(%{first: nil})
Expand All @@ -129,12 +129,12 @@
assert nil == decrypted_first.second
end

test "forcing data reencryption" do

Check failure on line 132 in test/erebus_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

test forcing data reencryption (Erebus.Test)
model = %EncryptedStuff{first: "hello"}
model = %EncryptedModel{first: "hello"}

encrypted =
model
|> EncryptedStuff.changeset(%{}, true)
|> EncryptedModel.changeset(%{}, true)
|> Ecto.Changeset.apply_changes()
# simulate reloading with virtual fields emptied
|> Map.merge(%{first: nil})
Expand All @@ -153,4 +153,15 @@

assert "hello" == decrypted_first.first
end

test "setting a field to nil" do

Check failure on line 157 in test/erebus_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

test setting a field to nil (Erebus.Test)
model = %EncryptedModel{first: "beep", second: "boop"}

encrypted =

Check warning on line 160 in test/erebus_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

variable "encrypted" is unused (if the variable is not meant to be used, prefix it with an underscore)
model
|> EncryptedModel.changeset(%{}, true)
|> Ecto.Changeset.apply_changes()

raise "test not implemented"
end
end
Loading