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 @@ public EncryptionConverter(IEncryptionProvider encryptionProvider, StorageFormat
{
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 @@
Name = name,
NameAsBytes = name,
ExtraData = bytes,
ExtraDataAsBytes = bytes
ExtraDataAsBytes = bytes,
EmptyString = ""
};

using var contextFactory = new DatabaseContextFactory();
Expand All @@ -52,6 +53,7 @@
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 @@
Assert.Equal(name, u.NameAsBytes);
Assert.Equal(bytes, u.ExtraData);
Assert.Equal(bytes, u.ExtraDataAsBytes);
Assert.Null(u.EmptyString);
}
}

Expand All @@ -73,21 +76,21 @@
{
Assert.NotNull(property);

IAnnotation encryptedAnnotation = property.FindAnnotation(PropertyAnnotations.IsEncrypted);

Check warning on line 79 in test/EntityFrameworkCore.DataEncryption.Test/PropertyBuilderExtensionsTest.cs

View workflow job for this annotation

GitHub Actions / build-library

Microsoft.EntityFrameworkCore.DataEncryption.Internal.PropertyAnnotations is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release.

Check warning on line 79 in test/EntityFrameworkCore.DataEncryption.Test/PropertyBuilderExtensionsTest.cs

View workflow job for this annotation

GitHub Actions / build-library

Microsoft.EntityFrameworkCore.DataEncryption.Internal.PropertyAnnotations is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release.

if (shouldBeEncrypted)
{
Assert.NotNull(encryptedAnnotation);
Assert.True((bool)encryptedAnnotation.Value);

IAnnotation formatAnnotation = property.FindAnnotation(PropertyAnnotations.StorageFormat);

Check warning on line 86 in test/EntityFrameworkCore.DataEncryption.Test/PropertyBuilderExtensionsTest.cs

View workflow job for this annotation

GitHub Actions / build-library

Microsoft.EntityFrameworkCore.DataEncryption.Internal.PropertyAnnotations is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release.

Check warning on line 86 in test/EntityFrameworkCore.DataEncryption.Test/PropertyBuilderExtensionsTest.cs

View workflow job for this annotation

GitHub Actions / build-library

Microsoft.EntityFrameworkCore.DataEncryption.Internal.PropertyAnnotations is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release.
Assert.NotNull(formatAnnotation);
Assert.Equal(expectedStorageFormat, formatAnnotation.Value);
}
else
{
Assert.Null(encryptedAnnotation);
Assert.Null(property.FindAnnotation(PropertyAnnotations.StorageFormat));

Check warning on line 93 in test/EntityFrameworkCore.DataEncryption.Test/PropertyBuilderExtensionsTest.cs

View workflow job for this annotation

GitHub Actions / build-library

Microsoft.EntityFrameworkCore.DataEncryption.Internal.PropertyAnnotations is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release.

Check warning on line 93 in test/EntityFrameworkCore.DataEncryption.Test/PropertyBuilderExtensionsTest.cs

View workflow job for this annotation

GitHub Actions / build-library

Microsoft.EntityFrameworkCore.DataEncryption.Internal.PropertyAnnotations is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release.
}
}

Expand All @@ -106,6 +109,9 @@

// 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 @@
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
Loading