From 14956f6383c9a811dc531d5cdcba18085e37cf09 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 2 Aug 2018 08:57:32 -0400 Subject: [PATCH] encrypted string length attr --- src/Core/Models/Api/CipherCardModel.cs | 10 +++--- src/Core/Models/Api/CipherFieldModel.cs | 5 +-- src/Core/Models/Api/CipherIdentityModel.cs | 34 +++++++++---------- src/Core/Models/Api/CipherLoginModel.cs | 10 +++--- .../Models/Api/CipherPasswordHistoryModel.cs | 4 ++- .../Models/Api/Request/CipherRequestModel.cs | 4 +-- .../Api/Request/CollectionRequestModel.cs | 2 +- .../Models/Api/Request/FolderRequestModel.cs | 2 +- .../OrganizationCreateLicenseRequestModel.cs | 2 +- .../OrganizationCreateRequestModel.cs | 2 +- .../EncryptedStringLengthAttribute.cs | 17 ++++++++++ 11 files changed, 56 insertions(+), 36 deletions(-) create mode 100644 src/Core/Utilities/EncryptedStringLengthAttribute.cs diff --git a/src/Core/Models/Api/CipherCardModel.cs b/src/Core/Models/Api/CipherCardModel.cs index 9a928df10d..5b81fc02ac 100644 --- a/src/Core/Models/Api/CipherCardModel.cs +++ b/src/Core/Models/Api/CipherCardModel.cs @@ -19,22 +19,22 @@ namespace Bit.Core.Models.Api } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string CardholderName { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Brand { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Number { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string ExpMonth { get; set; } [EncryptedString] [StringLength(1000)] public string ExpYear { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Code { get; set; } } } diff --git a/src/Core/Models/Api/CipherFieldModel.cs b/src/Core/Models/Api/CipherFieldModel.cs index 8bf4aee4e0..655cd02a93 100644 --- a/src/Core/Models/Api/CipherFieldModel.cs +++ b/src/Core/Models/Api/CipherFieldModel.cs @@ -1,6 +1,7 @@ using System.ComponentModel.DataAnnotations; using Bit.Core.Enums; using Bit.Core.Models.Data; +using Bit.Core.Utilities; namespace Bit.Core.Models.Api { @@ -16,9 +17,9 @@ namespace Bit.Core.Models.Api } public FieldType Type { get; set; } - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Name { get; set; } - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Value { get; set; } } } diff --git a/src/Core/Models/Api/CipherIdentityModel.cs b/src/Core/Models/Api/CipherIdentityModel.cs index 3e4afb69d6..f8909647da 100644 --- a/src/Core/Models/Api/CipherIdentityModel.cs +++ b/src/Core/Models/Api/CipherIdentityModel.cs @@ -31,58 +31,58 @@ namespace Bit.Core.Models.Api } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Title { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string FirstName { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string MiddleName { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string LastName { get; set; } [EncryptedString] [StringLength(1000)] public string Address1 { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Address2 { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Address3 { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string City { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string State { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string PostalCode { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Country { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Company { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Email { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Phone { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string SSN { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Username { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string PassportNumber { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string LicenseNumber { get; set; } } } diff --git a/src/Core/Models/Api/CipherLoginModel.cs b/src/Core/Models/Api/CipherLoginModel.cs index 71067e5394..c50b4ea7a3 100644 --- a/src/Core/Models/Api/CipherLoginModel.cs +++ b/src/Core/Models/Api/CipherLoginModel.cs @@ -27,7 +27,7 @@ namespace Bit.Core.Models.Api } [EncryptedString] - [StringLength(10000)] + [EncryptedStringLength(10000)] public string Uri { get => Uris?.FirstOrDefault()?.Uri; @@ -48,14 +48,14 @@ namespace Bit.Core.Models.Api } public List Uris { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Username { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Password { get; set; } public DateTime? PasswordRevisionDate { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Totp { get; set; } public class CipherLoginUriModel @@ -74,7 +74,7 @@ namespace Bit.Core.Models.Api } [EncryptedString] - [StringLength(10000)] + [EncryptedStringLength(10000)] public string Uri { get; set; } public UriMatchType? Match { get; set; } = null; } diff --git a/src/Core/Models/Api/CipherPasswordHistoryModel.cs b/src/Core/Models/Api/CipherPasswordHistoryModel.cs index e6dc4a8afa..9f2d42802c 100644 --- a/src/Core/Models/Api/CipherPasswordHistoryModel.cs +++ b/src/Core/Models/Api/CipherPasswordHistoryModel.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel.DataAnnotations; using Bit.Core.Models.Data; +using Bit.Core.Utilities; namespace Bit.Core.Models.Api { @@ -14,7 +15,8 @@ namespace Bit.Core.Models.Api LastUsedDate = data.LastUsedDate; } - [StringLength(2000)] + [EncryptedString] + [EncryptedStringLength(2000)] [Required] public string Password { get; set; } [Required] diff --git a/src/Core/Models/Api/Request/CipherRequestModel.cs b/src/Core/Models/Api/Request/CipherRequestModel.cs index 95abdbc3b5..c167211d33 100644 --- a/src/Core/Models/Api/Request/CipherRequestModel.cs +++ b/src/Core/Models/Api/Request/CipherRequestModel.cs @@ -22,10 +22,10 @@ namespace Bit.Core.Models.Api public bool Favorite { get; set; } [Required] [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Name { get; set; } [EncryptedString] - [StringLength(10000)] + [EncryptedStringLength(10000)] public string Notes { get; set; } public IEnumerable Fields { get; set; } public IEnumerable PasswordHistory { get; set; } diff --git a/src/Core/Models/Api/Request/CollectionRequestModel.cs b/src/Core/Models/Api/Request/CollectionRequestModel.cs index 7878ed3608..b912927dd5 100644 --- a/src/Core/Models/Api/Request/CollectionRequestModel.cs +++ b/src/Core/Models/Api/Request/CollectionRequestModel.cs @@ -11,7 +11,7 @@ namespace Bit.Core.Models.Api { [Required] [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Name { get; set; } public IEnumerable Groups { get; set; } diff --git a/src/Core/Models/Api/Request/FolderRequestModel.cs b/src/Core/Models/Api/Request/FolderRequestModel.cs index 9212a3dc9a..6d6d56fd24 100644 --- a/src/Core/Models/Api/Request/FolderRequestModel.cs +++ b/src/Core/Models/Api/Request/FolderRequestModel.cs @@ -10,7 +10,7 @@ namespace Bit.Core.Models.Api { [Required] [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string Name { get; set; } public Folder ToFolder(Guid userId) diff --git a/src/Core/Models/Api/Request/Organizations/OrganizationCreateLicenseRequestModel.cs b/src/Core/Models/Api/Request/Organizations/OrganizationCreateLicenseRequestModel.cs index 808695f06b..97cff0a6a6 100644 --- a/src/Core/Models/Api/Request/Organizations/OrganizationCreateLicenseRequestModel.cs +++ b/src/Core/Models/Api/Request/Organizations/OrganizationCreateLicenseRequestModel.cs @@ -8,7 +8,7 @@ namespace Bit.Core.Models.Api [Required] public string Key { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string CollectionName { get; set; } } } diff --git a/src/Core/Models/Api/Request/Organizations/OrganizationCreateRequestModel.cs b/src/Core/Models/Api/Request/Organizations/OrganizationCreateRequestModel.cs index d25f911438..5504333d05 100644 --- a/src/Core/Models/Api/Request/Organizations/OrganizationCreateRequestModel.cs +++ b/src/Core/Models/Api/Request/Organizations/OrganizationCreateRequestModel.cs @@ -27,7 +27,7 @@ namespace Bit.Core.Models.Api [Range(0, 99)] public short? AdditionalStorageGb { get; set; } [EncryptedString] - [StringLength(1000)] + [EncryptedStringLength(1000)] public string CollectionName { get; set; } public string Country { get; set; } diff --git a/src/Core/Utilities/EncryptedStringLengthAttribute.cs b/src/Core/Utilities/EncryptedStringLengthAttribute.cs new file mode 100644 index 0000000000..46170487d9 --- /dev/null +++ b/src/Core/Utilities/EncryptedStringLengthAttribute.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace Bit.Core.Utilities +{ + public class EncryptedStringLengthAttribute : StringLengthAttribute + { + public EncryptedStringLengthAttribute(int maximumLength) + : base(maximumLength) + { } + + public override string FormatErrorMessage(string name) + { + return string.Format("The field {0} exceeds the maximum encrypted value length of {1} characters.", + name, MaximumLength); + } + } +}