mirror of
https://github.com/bitwarden/server.git
synced 2025-05-30 15:50:33 -05:00
fix serialization issues
This commit is contained in:
parent
69c30df919
commit
57f41ee7bc
@ -986,31 +986,75 @@ public class CipherService : ICipherService
|
|||||||
// Check if user is a "hidden password" user
|
// Check if user is a "hidden password" user
|
||||||
if (!cipherPermissions.TryGetValue(cipher.Id, out var permission) || !(permission.ViewPassword && permission.Edit))
|
if (!cipherPermissions.TryGetValue(cipher.Id, out var permission) || !(permission.ViewPassword && permission.Edit))
|
||||||
{
|
{
|
||||||
var existingCipherData = JsonSerializer.Deserialize<CipherLoginData>(existingCipher.Data);
|
var existingCipherData = DeserializeCipherData(existingCipher);
|
||||||
var newCipherData = JsonSerializer.Deserialize<CipherLoginData>(cipher.Data);
|
var newCipherData = DeserializeCipherData(cipher);
|
||||||
|
|
||||||
// "hidden password" users may not add cipher key encryption
|
// "hidden password" users may not add cipher key encryption
|
||||||
if (existingCipher.Key == null && cipher.Key != null)
|
if (existingCipher.Key == null && cipher.Key != null)
|
||||||
{
|
{
|
||||||
throw new BadRequestException("You do not have permission to add cipher key encryption.");
|
throw new BadRequestException("You do not have permission to add cipher key encryption.");
|
||||||
}
|
}
|
||||||
if (newCipherData?.Fields != null)
|
// Keep only non-hidden fileds from the new cipher
|
||||||
{
|
var nonHiddenFields = newCipherData.Fields?.Where(f => f.Type != FieldType.Hidden) ?? [];
|
||||||
// Keep only non-hidden fields from the new cipher
|
// Get hidden fields from the existing cipher
|
||||||
var nonHiddenFields = newCipherData.Fields.Where(f => f.Type != FieldType.Hidden).ToList();
|
var hiddenFields = existingCipherData.Fields?.Where(f => f.Type == FieldType.Hidden) ?? [];
|
||||||
// Get hidden fields from the existing cipher
|
// Replace the hidden fields in new cipher data with the existing ones
|
||||||
var hiddenFields = existingCipherData.Fields?.Where(f => f.Type == FieldType.Hidden) ?? [];
|
newCipherData.Fields = nonHiddenFields.Concat(hiddenFields);
|
||||||
// Replace the hidden fields in new cipher data with the existing ones
|
cipher.Data = SerializeCipherData(newCipherData);
|
||||||
newCipherData.Fields = nonHiddenFields.Concat(hiddenFields);
|
if (existingCipherData is CipherLoginData existingLoginData && newCipherData is CipherLoginData newLoginCipherData)
|
||||||
cipher.Data = JsonSerializer.Serialize(newCipherData);
|
|
||||||
}
|
|
||||||
if (cipher.Type == CipherType.Login)
|
|
||||||
{
|
{
|
||||||
// "hidden password" users may not change passwords, TOTP codes, or passkeys, so we need to set them back to the original values
|
// "hidden password" users may not change passwords, TOTP codes, or passkeys, so we need to set them back to the original values
|
||||||
newCipherData.Fido2Credentials = existingCipherData.Fido2Credentials;
|
newLoginCipherData.Fido2Credentials = existingLoginData.Fido2Credentials;
|
||||||
newCipherData.Totp = existingCipherData.Totp;
|
newLoginCipherData.Totp = existingLoginData.Totp;
|
||||||
newCipherData.Password = existingCipherData.Password;
|
newLoginCipherData.Password = existingLoginData.Password;
|
||||||
cipher.Data = JsonSerializer.Serialize(newCipherData);
|
cipher.Data = SerializeCipherData(newLoginCipherData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string SerializeCipherData(CipherData data)
|
||||||
|
{
|
||||||
|
if (data is CipherLoginData cipherLoginData)
|
||||||
|
{
|
||||||
|
return JsonSerializer.Serialize<CipherLoginData>(cipherLoginData);
|
||||||
|
}
|
||||||
|
if (data is CipherIdentityData cipherIdentityData)
|
||||||
|
{
|
||||||
|
return JsonSerializer.Serialize<CipherIdentityData>(cipherIdentityData);
|
||||||
|
}
|
||||||
|
if (data is CipherCardData cipherCardData)
|
||||||
|
{
|
||||||
|
return JsonSerializer.Serialize<CipherCardData>(cipherCardData);
|
||||||
|
}
|
||||||
|
if (data is CipherSecureNoteData cipherSecureNoteData)
|
||||||
|
{
|
||||||
|
return JsonSerializer.Serialize<CipherSecureNoteData>(cipherSecureNoteData);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private CipherData DeserializeCipherData(Cipher cipher)
|
||||||
|
{
|
||||||
|
if (cipher.Type == CipherType.Login)
|
||||||
|
{
|
||||||
|
return JsonSerializer.Deserialize<CipherLoginData>(cipher.Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cipher.Type == CipherType.Identity)
|
||||||
|
{
|
||||||
|
return JsonSerializer.Deserialize<CipherIdentityData>(cipher.Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cipher.Type == CipherType.Card)
|
||||||
|
{
|
||||||
|
return JsonSerializer.Deserialize<CipherCardData>(cipher.Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cipher.Type == CipherType.SecureNote)
|
||||||
|
{
|
||||||
|
return JsonSerializer.Deserialize<CipherSecureNoteData>(cipher.Data);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user