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 null or empty string encryption #52

Merged
merged 2 commits into from
May 12, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<AssemblyName>EntityFrameworkCore.DataEncryption</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore.DataEncryption</RootNamespace>
<IsPackable>true</IsPackable>
<Version>4.0.0</Version>
<Version>4.0.1</Version>
<Authors>Filipe GOMES PEIXOTO</Authors>
<PackageId>EntityFrameworkCore.DataEncryption</PackageId>
<PackageProjectUrl>https://github.com/Eastrall/EntityFrameworkCore.DataEncryption</PackageProjectUrl>
Expand All @@ -20,7 +20,7 @@
<Copyright>Filipe GOMES PEIXOTO © 2019 - 2023</Copyright>
<Description>A plugin for Microsoft.EntityFrameworkCore to add support of encrypted fields using built-in or custom encryption providers.</Description>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReleaseNotes>https://github.com/Eastrall/EntityFrameworkCore.DataEncryption/releases/tag/v4.0.0</PackageReleaseNotes>
<PackageReleaseNotes>https://github.com/Eastrall/EntityFrameworkCore.DataEncryption/releases/tag/v4.0.1</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ private static TOutput Encrypt<TInput, TOutput>(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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public void PropertyShouldHaveEncryptionAnnotationsTest()
Name = name,
NameAsBytes = name,
ExtraData = bytes,
ExtraDataAsBytes = bytes
ExtraDataAsBytes = bytes,
EmptyString = ""
};

using var contextFactory = new DatabaseContextFactory();
Expand All @@ -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();
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down