From ebbaf6e2a17577d00dab24eb2d67581d856d1657 Mon Sep 17 00:00:00 2001
From: Filipe GP <4021025+Eastrall@users.noreply.github.com>
Date: Sat, 22 Apr 2023 10:52:33 +0200
Subject: [PATCH 1/2] Fix null or empty string encryption
---
.../EntityFrameworkCore.DataEncryption.csproj | 4 ++--
.../Internal/EncryptionConverter.cs | 7 ++++++-
2 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/src/EntityFrameworkCore.DataEncryption/EntityFrameworkCore.DataEncryption.csproj b/src/EntityFrameworkCore.DataEncryption/EntityFrameworkCore.DataEncryption.csproj
index 3807410..bad5761 100644
--- a/src/EntityFrameworkCore.DataEncryption/EntityFrameworkCore.DataEncryption.csproj
+++ b/src/EntityFrameworkCore.DataEncryption/EntityFrameworkCore.DataEncryption.csproj
@@ -7,7 +7,7 @@
EntityFrameworkCore.DataEncryption
Microsoft.EntityFrameworkCore.DataEncryption
true
- 4.0.0
+ 4.0.1
Filipe GOMES PEIXOTO
EntityFrameworkCore.DataEncryption
https://github.com/Eastrall/EntityFrameworkCore.DataEncryption
@@ -20,7 +20,7 @@
Filipe GOMES PEIXOTO © 2019 - 2023
A plugin for Microsoft.EntityFrameworkCore to add support of encrypted fields using built-in or custom encryption providers.
LICENSE
- https://github.com/Eastrall/EntityFrameworkCore.DataEncryption/releases/tag/v4.0.0
+ https://github.com/Eastrall/EntityFrameworkCore.DataEncryption/releases/tag/v4.0.1
README.md
diff --git a/src/EntityFrameworkCore.DataEncryption/Internal/EncryptionConverter.cs b/src/EntityFrameworkCore.DataEncryption/Internal/EncryptionConverter.cs
index 57ad58a..92c9bab 100644
--- a/src/EntityFrameworkCore.DataEncryption/Internal/EncryptionConverter.cs
+++ b/src/EntityFrameworkCore.DataEncryption/Internal/EncryptionConverter.cs
@@ -30,13 +30,18 @@ private static TOutput Encrypt(TInput input, IEncryptionProvide
{
byte[] inputData = input switch
{
- string => Encoding.UTF8.GetBytes(input.ToString()),
+ string => !string.IsNullOrEmpty(input.ToString()) ? Encoding.UTF8.GetBytes(input.ToString()) : null,
byte[] => input as byte[],
_ => null,
};
byte[] encryptedRawBytes = encryptionProvider.Encrypt(inputData);
+ if (encryptedRawBytes is null)
+ {
+ return default;
+ }
+
object encryptedData = storageFormat switch
{
StorageFormat.Default or StorageFormat.Base64 => Convert.ToBase64String(encryptedRawBytes),
From 9e15a88f4ae19e5b8658545439499bc26276d81a Mon Sep 17 00:00:00 2001
From: Filipe GP <4021025+Eastrall@users.noreply.github.com>
Date: Fri, 12 May 2023 09:40:22 +0200
Subject: [PATCH 2/2] Add unit test case
---
.../PropertyBuilderExtensionsTest.cs | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/test/EntityFrameworkCore.DataEncryption.Test/PropertyBuilderExtensionsTest.cs b/test/EntityFrameworkCore.DataEncryption.Test/PropertyBuilderExtensionsTest.cs
index d9a5b8f..11c1f20 100644
--- a/test/EntityFrameworkCore.DataEncryption.Test/PropertyBuilderExtensionsTest.cs
+++ b/test/EntityFrameworkCore.DataEncryption.Test/PropertyBuilderExtensionsTest.cs
@@ -36,7 +36,8 @@ public void PropertyShouldHaveEncryptionAnnotationsTest()
Name = name,
NameAsBytes = name,
ExtraData = bytes,
- ExtraDataAsBytes = bytes
+ ExtraDataAsBytes = bytes,
+ EmptyString = ""
};
using var contextFactory = new DatabaseContextFactory();
@@ -52,6 +53,7 @@ public void PropertyShouldHaveEncryptionAnnotationsTest()
AssertPropertyAnnotations(entityType.GetProperty(nameof(UserEntity.ExtraData)), true, StorageFormat.Base64);
AssertPropertyAnnotations(entityType.GetProperty(nameof(UserEntity.ExtraDataAsBytes)), true, StorageFormat.Binary);
AssertPropertyAnnotations(entityType.GetProperty(nameof(UserEntity.Id)), false, StorageFormat.Default);
+ AssertPropertyAnnotations(entityType.GetProperty(nameof(UserEntity.EmptyString)), true, StorageFormat.Base64);
context.Users.Add(user);
context.SaveChanges();
@@ -66,6 +68,7 @@ public void PropertyShouldHaveEncryptionAnnotationsTest()
Assert.Equal(name, u.NameAsBytes);
Assert.Equal(bytes, u.ExtraData);
Assert.Equal(bytes, u.ExtraDataAsBytes);
+ Assert.Null(u.EmptyString);
}
}
@@ -106,6 +109,9 @@ private class UserEntity
// Encrypted as raw byte array.
public byte[] ExtraDataAsBytes { get; set; }
+
+ // Encrypt as Base64 string, but will be empty.
+ public string EmptyString { get; set; }
}
private class FluentDbContext : DbContext
@@ -134,6 +140,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
userEntityBuilder.Property(x => x.NameAsBytes).IsRequired().HasColumnType("BLOB").IsEncrypted(StorageFormat.Binary);
userEntityBuilder.Property(x => x.ExtraData).IsRequired().HasColumnType("TEXT").IsEncrypted(StorageFormat.Base64);
userEntityBuilder.Property(x => x.ExtraDataAsBytes).IsRequired().HasColumnType("BLOB").IsEncrypted(StorageFormat.Binary);
+ userEntityBuilder.Property(x => x.EmptyString).IsRequired(false).HasColumnType("TEXT").IsEncrypted(StorageFormat.Base64);
modelBuilder.UseEncryption(_encryptionProvider);
}