From dd25a575fa2b1048705a6af0bd7325a1a0091edd Mon Sep 17 00:00:00 2001 From: Hinton Date: Tue, 29 Mar 2022 11:27:24 +0200 Subject: [PATCH 1/4] Add support for "unknown item types" --- src/Api/Models/Request/CipherRequestModel.cs | 15 +++++++++++++-- src/Api/Models/Response/CipherResponseModel.cs | 5 +++++ .../Models/Data/{ => Cipher}/CipherAttachment.cs | 0 .../Models/Data/{ => Cipher}/CipherCardData.cs | 0 src/Core/Models/Data/Cipher/CipherCustomData.cs | 6 ++++++ src/Core/Models/Data/{ => Cipher}/CipherData.cs | 0 .../Models/Data/{ => Cipher}/CipherDetails.cs | 0 .../Models/Data/{ => Cipher}/CipherFieldData.cs | 0 .../Data/{ => Cipher}/CipherIdentityData.cs | 0 .../Models/Data/{ => Cipher}/CipherLoginData.cs | 0 .../{ => Cipher}/CipherOrganizationDetails.cs | 0 .../{ => Cipher}/CipherPasswordHistoryData.cs | 0 .../Data/{ => Cipher}/CipherSecureNoteData.cs | 0 13 files changed, 24 insertions(+), 2 deletions(-) rename src/Core/Models/Data/{ => Cipher}/CipherAttachment.cs (100%) rename src/Core/Models/Data/{ => Cipher}/CipherCardData.cs (100%) create mode 100644 src/Core/Models/Data/Cipher/CipherCustomData.cs rename src/Core/Models/Data/{ => Cipher}/CipherData.cs (100%) rename src/Core/Models/Data/{ => Cipher}/CipherDetails.cs (100%) rename src/Core/Models/Data/{ => Cipher}/CipherFieldData.cs (100%) rename src/Core/Models/Data/{ => Cipher}/CipherIdentityData.cs (100%) rename src/Core/Models/Data/{ => Cipher}/CipherLoginData.cs (100%) rename src/Core/Models/Data/{ => Cipher}/CipherOrganizationDetails.cs (100%) rename src/Core/Models/Data/{ => Cipher}/CipherPasswordHistoryData.cs (100%) rename src/Core/Models/Data/{ => Cipher}/CipherSecureNoteData.cs (100%) diff --git a/src/Api/Models/Request/CipherRequestModel.cs b/src/Api/Models/Request/CipherRequestModel.cs index 417fb4497137..2298a9ec2b25 100644 --- a/src/Api/Models/Request/CipherRequestModel.cs +++ b/src/Api/Models/Request/CipherRequestModel.cs @@ -86,7 +86,8 @@ public Cipher ToCipher(Cipher existingCipher) existingCipher.Data = JsonSerializer.Serialize(ToCipherSecureNoteData(), JsonHelpers.IgnoreWritingNull); break; default: - throw new ArgumentException("Unsupported type: " + nameof(Type) + "."); + existingCipher.Data = JsonSerializer.Serialize(ToCipherCustomData(), JsonHelpers.IgnoreWritingNull); + break; } existingCipher.Reprompt = Reprompt; @@ -227,10 +228,20 @@ private CipherSecureNoteData ToCipherSecureNoteData() Notes = Notes, Fields = Fields?.Select(f => f.ToCipherFieldData()), PasswordHistory = PasswordHistory?.Select(ph => ph.ToCipherPasswordHistoryData()), - Type = SecureNote.Type, }; } + + private CipherCustomData ToCipherCustomData() + { + return new CipherCustomData + { + Name = Name, + Notes = Notes, + Fields = Fields?.Select(f => f.ToCipherFieldData()), + PasswordHistory = PasswordHistory?.Select(ph => ph.ToCipherPasswordHistoryData()), + }; + } } public class CipherWithIdRequestModel : CipherRequestModel diff --git a/src/Api/Models/Response/CipherResponseModel.cs b/src/Api/Models/Response/CipherResponseModel.cs index 6ebf671e6e5d..a1f9fd2510c8 100644 --- a/src/Api/Models/Response/CipherResponseModel.cs +++ b/src/Api/Models/Response/CipherResponseModel.cs @@ -51,6 +51,11 @@ public CipherMiniResponseModel(Cipher cipher, IGlobalSettings globalSettings, bo cipherData = identityData; Identity = new CipherIdentityModel(identityData); break; + case CipherType.Custom: + var customData = JsonSerializer.Deserialize(cipher.Data); + Data = customData; + cipherData = customData; + break; default: throw new ArgumentException("Unsupported " + nameof(Type) + "."); } diff --git a/src/Core/Models/Data/CipherAttachment.cs b/src/Core/Models/Data/Cipher/CipherAttachment.cs similarity index 100% rename from src/Core/Models/Data/CipherAttachment.cs rename to src/Core/Models/Data/Cipher/CipherAttachment.cs diff --git a/src/Core/Models/Data/CipherCardData.cs b/src/Core/Models/Data/Cipher/CipherCardData.cs similarity index 100% rename from src/Core/Models/Data/CipherCardData.cs rename to src/Core/Models/Data/Cipher/CipherCardData.cs diff --git a/src/Core/Models/Data/Cipher/CipherCustomData.cs b/src/Core/Models/Data/Cipher/CipherCustomData.cs new file mode 100644 index 000000000000..55502a781b13 --- /dev/null +++ b/src/Core/Models/Data/Cipher/CipherCustomData.cs @@ -0,0 +1,6 @@ +namespace Bit.Core.Models.Data +{ + public class CipherCustomData : CipherData + { + } +} diff --git a/src/Core/Models/Data/CipherData.cs b/src/Core/Models/Data/Cipher/CipherData.cs similarity index 100% rename from src/Core/Models/Data/CipherData.cs rename to src/Core/Models/Data/Cipher/CipherData.cs diff --git a/src/Core/Models/Data/CipherDetails.cs b/src/Core/Models/Data/Cipher/CipherDetails.cs similarity index 100% rename from src/Core/Models/Data/CipherDetails.cs rename to src/Core/Models/Data/Cipher/CipherDetails.cs diff --git a/src/Core/Models/Data/CipherFieldData.cs b/src/Core/Models/Data/Cipher/CipherFieldData.cs similarity index 100% rename from src/Core/Models/Data/CipherFieldData.cs rename to src/Core/Models/Data/Cipher/CipherFieldData.cs diff --git a/src/Core/Models/Data/CipherIdentityData.cs b/src/Core/Models/Data/Cipher/CipherIdentityData.cs similarity index 100% rename from src/Core/Models/Data/CipherIdentityData.cs rename to src/Core/Models/Data/Cipher/CipherIdentityData.cs diff --git a/src/Core/Models/Data/CipherLoginData.cs b/src/Core/Models/Data/Cipher/CipherLoginData.cs similarity index 100% rename from src/Core/Models/Data/CipherLoginData.cs rename to src/Core/Models/Data/Cipher/CipherLoginData.cs diff --git a/src/Core/Models/Data/CipherOrganizationDetails.cs b/src/Core/Models/Data/Cipher/CipherOrganizationDetails.cs similarity index 100% rename from src/Core/Models/Data/CipherOrganizationDetails.cs rename to src/Core/Models/Data/Cipher/CipherOrganizationDetails.cs diff --git a/src/Core/Models/Data/CipherPasswordHistoryData.cs b/src/Core/Models/Data/Cipher/CipherPasswordHistoryData.cs similarity index 100% rename from src/Core/Models/Data/CipherPasswordHistoryData.cs rename to src/Core/Models/Data/Cipher/CipherPasswordHistoryData.cs diff --git a/src/Core/Models/Data/CipherSecureNoteData.cs b/src/Core/Models/Data/Cipher/CipherSecureNoteData.cs similarity index 100% rename from src/Core/Models/Data/CipherSecureNoteData.cs rename to src/Core/Models/Data/Cipher/CipherSecureNoteData.cs From 3fcef25deeb2007486568f5d28603ab389ac62cc Mon Sep 17 00:00:00 2001 From: Hinton Date: Fri, 1 Apr 2022 11:33:14 +0200 Subject: [PATCH 2/4] Improve cipher response on unknown type --- src/Api/Models/Response/CipherResponseModel.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Api/Models/Response/CipherResponseModel.cs b/src/Api/Models/Response/CipherResponseModel.cs index a1f9fd2510c8..43023ed8b4f1 100644 --- a/src/Api/Models/Response/CipherResponseModel.cs +++ b/src/Api/Models/Response/CipherResponseModel.cs @@ -51,13 +51,11 @@ public CipherMiniResponseModel(Cipher cipher, IGlobalSettings globalSettings, bo cipherData = identityData; Identity = new CipherIdentityModel(identityData); break; - case CipherType.Custom: + default: var customData = JsonSerializer.Deserialize(cipher.Data); Data = customData; cipherData = customData; break; - default: - throw new ArgumentException("Unsupported " + nameof(Type) + "."); } Name = cipherData.Name; From 84b44456f78dc9d3364eec912bbce4cf5f86b8fc Mon Sep 17 00:00:00 2001 From: Hinton Date: Fri, 13 May 2022 14:35:25 +0200 Subject: [PATCH 3/4] Add EncObject --- src/Api/Models/Request/CipherRequestModel.cs | 13 +------------ src/Api/Models/Response/CipherResponseModel.cs | 15 +++++++++------ src/Core/Enums/CipherType.cs | 2 +- src/Core/Models/Data/Cipher/CipherCustomData.cs | 6 ------ src/Core/Models/Data/EncObject.cs | 12 ++++++++++++ 5 files changed, 23 insertions(+), 25 deletions(-) delete mode 100644 src/Core/Models/Data/Cipher/CipherCustomData.cs create mode 100644 src/Core/Models/Data/EncObject.cs diff --git a/src/Api/Models/Request/CipherRequestModel.cs b/src/Api/Models/Request/CipherRequestModel.cs index 2298a9ec2b25..0854b5ebc3e4 100644 --- a/src/Api/Models/Request/CipherRequestModel.cs +++ b/src/Api/Models/Request/CipherRequestModel.cs @@ -86,7 +86,7 @@ public Cipher ToCipher(Cipher existingCipher) existingCipher.Data = JsonSerializer.Serialize(ToCipherSecureNoteData(), JsonHelpers.IgnoreWritingNull); break; default: - existingCipher.Data = JsonSerializer.Serialize(ToCipherCustomData(), JsonHelpers.IgnoreWritingNull); + //existingCipher.Data = JsonSerializer.Serialize(ToCipherCustomData(), JsonHelpers.IgnoreWritingNull); break; } @@ -231,17 +231,6 @@ private CipherSecureNoteData ToCipherSecureNoteData() Type = SecureNote.Type, }; } - - private CipherCustomData ToCipherCustomData() - { - return new CipherCustomData - { - Name = Name, - Notes = Notes, - Fields = Fields?.Select(f => f.ToCipherFieldData()), - PasswordHistory = PasswordHistory?.Select(ph => ph.ToCipherPasswordHistoryData()), - }; - } } public class CipherWithIdRequestModel : CipherRequestModel diff --git a/src/Api/Models/Response/CipherResponseModel.cs b/src/Api/Models/Response/CipherResponseModel.cs index 43023ed8b4f1..1bcb721f53e4 100644 --- a/src/Api/Models/Response/CipherResponseModel.cs +++ b/src/Api/Models/Response/CipherResponseModel.cs @@ -52,16 +52,19 @@ public CipherMiniResponseModel(Cipher cipher, IGlobalSettings globalSettings, bo Identity = new CipherIdentityModel(identityData); break; default: - var customData = JsonSerializer.Deserialize(cipher.Data); + var customData = JsonSerializer.Deserialize(cipher.Data); Data = customData; - cipherData = customData; + cipherData = null; break; } - Name = cipherData.Name; - Notes = cipherData.Notes; - Fields = cipherData.Fields?.Select(f => new CipherFieldModel(f)); - PasswordHistory = cipherData.PasswordHistory?.Select(ph => new CipherPasswordHistoryModel(ph)); + if (cipherData != null) + { + Name = cipherData.Name; + Notes = cipherData.Notes; + Fields = cipherData.Fields?.Select(f => new CipherFieldModel(f)); + PasswordHistory = cipherData.PasswordHistory?.Select(ph => new CipherPasswordHistoryModel(ph)); + } RevisionDate = cipher.RevisionDate; OrganizationId = cipher.OrganizationId?.ToString(); Attachments = AttachmentResponseModel.FromCipher(cipher, globalSettings); diff --git a/src/Core/Enums/CipherType.cs b/src/Core/Enums/CipherType.cs index 0aca94864088..3e56a37c7615 100644 --- a/src/Core/Enums/CipherType.cs +++ b/src/Core/Enums/CipherType.cs @@ -7,6 +7,6 @@ public enum CipherType : byte Login = 1, SecureNote = 2, Card = 3, - Identity = 4 + Identity = 4, } } diff --git a/src/Core/Models/Data/Cipher/CipherCustomData.cs b/src/Core/Models/Data/Cipher/CipherCustomData.cs deleted file mode 100644 index 55502a781b13..000000000000 --- a/src/Core/Models/Data/Cipher/CipherCustomData.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Bit.Core.Models.Data -{ - public class CipherCustomData : CipherData - { - } -} diff --git a/src/Core/Models/Data/EncObject.cs b/src/Core/Models/Data/EncObject.cs new file mode 100644 index 000000000000..8525e3794b36 --- /dev/null +++ b/src/Core/Models/Data/EncObject.cs @@ -0,0 +1,12 @@ +using Bit.Core.Enums; + +namespace Bit.Core.Models.Data +{ + public class EncObject + { + public EncryptionType Type { get; set; } + public string Data { get; set; } + public string Iv { get; set; } + public string Mac { get; set; } + } +} From b6bfc9f8ab6b6bcab9198fdc4b111882946e5fbe Mon Sep 17 00:00:00 2001 From: Hinton Date: Fri, 13 May 2022 15:31:11 +0200 Subject: [PATCH 4/4] Use EncObject to serialize --- src/Api/Models/Request/CipherRequestModel.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Api/Models/Request/CipherRequestModel.cs b/src/Api/Models/Request/CipherRequestModel.cs index 0854b5ebc3e4..7d7e0fa0b766 100644 --- a/src/Api/Models/Request/CipherRequestModel.cs +++ b/src/Api/Models/Request/CipherRequestModel.cs @@ -22,6 +22,7 @@ public class CipherRequestModel public string FolderId { get; set; } public bool Favorite { get; set; } public CipherRepromptType Reprompt { get; set; } + public EncObject Data { get; set; } [Required] [EncryptedString] [EncryptedStringLength(1000)] @@ -86,7 +87,7 @@ public Cipher ToCipher(Cipher existingCipher) existingCipher.Data = JsonSerializer.Serialize(ToCipherSecureNoteData(), JsonHelpers.IgnoreWritingNull); break; default: - //existingCipher.Data = JsonSerializer.Serialize(ToCipherCustomData(), JsonHelpers.IgnoreWritingNull); + existingCipher.Data = JsonSerializer.Serialize(Data, JsonHelpers.IgnoreWritingNull); break; }