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

[SG-10] Refactor Cipher data model #76

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/Api/Models/Request/CipherRequestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -86,7 +87,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(Data, JsonHelpers.IgnoreWritingNull);
break;
Comment on lines 89 to +91
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: This default case allows for custom cipher types, but ensure that proper validation and type checking are implemented to prevent misuse or data corruption.

}

existingCipher.Reprompt = Reprompt;
Expand Down Expand Up @@ -227,7 +229,6 @@ private CipherSecureNoteData ToCipherSecureNoteData()
Notes = Notes,
Fields = Fields?.Select(f => f.ToCipherFieldData()),
PasswordHistory = PasswordHistory?.Select(ph => ph.ToCipherPasswordHistoryData()),

Type = SecureNote.Type,
};
}
Expand Down
16 changes: 11 additions & 5 deletions src/Api/Models/Response/CipherResponseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ public CipherMiniResponseModel(Cipher cipher, IGlobalSettings globalSettings, bo
Identity = new CipherIdentityModel(identityData);
break;
default:
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
var customData = JsonSerializer.Deserialize<EncObject>(cipher.Data);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Ensure that JsonSerializer.Deserialize can handle null cipher.Data

Data = 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));
}
Comment on lines +61 to +67
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: This block may leave Name, Notes, Fields, and PasswordHistory uninitialized for custom types

RevisionDate = cipher.RevisionDate;
OrganizationId = cipher.OrganizationId?.ToString();
Attachments = AttachmentResponseModel.FromCipher(cipher, globalSettings);
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Enums/CipherType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public enum CipherType : byte
Login = 1,
SecureNote = 2,
Card = 3,
Identity = 4
Identity = 4,
}
}
12 changes: 12 additions & 0 deletions src/Core/Models/Data/EncObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Bit.Core.Enums;

namespace Bit.Core.Models.Data
{
public class EncObject
{
public EncryptionType Type { get; set; }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: consider adding XML documentation for each property

public string Data { get; set; }
public string Iv { get; set; }
public string Mac { get; set; }
}
}
Loading