Skip to content
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
18 changes: 16 additions & 2 deletions src/Api/Vault/Models/Request/CipherRequestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Bit.Api.Vault.Models.Request;

public class CipherRequestModel
public class CipherRequestModel : IValidatableObject
{
/// <summary>
/// The Id of the user that encrypted the cipher. It should always represent a UserId.
Expand All @@ -36,7 +36,6 @@ public class CipherRequestModel
public bool Favorite { get; set; }
public CipherRepromptType Reprompt { get; set; }
public string Key { get; set; }
[Required]
[EncryptedString]
[EncryptedStringLength(1000)]
public string Name { get; set; }
Expand Down Expand Up @@ -85,6 +84,21 @@ public class CipherRequestModel
public KeyId GetEncryptedByKeyId() =>
KeyId.FromHexEncodedString(string.IsNullOrEmpty(EncryptedByKeyId) ? null : EncryptedByKeyId);

/// <summary>
/// Blob-encrypted ciphers carry all their content in <see cref="Data"/> and leave Name unused.
/// Every other format still stores Name as a structured field, so it stays required there.
/// </summary>
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var isBlobEncrypted = new Cipher { Data = Data }.IsDataBlobEncrypted();

if (!isBlobEncrypted && string.IsNullOrWhiteSpace(Name))
{
yield return new ValidationResult(
"The Name field is required.", new[] { nameof(Name) });
}
}

/// <summary>
/// True when this cipher is owned by an organization, and so is encrypted with the organization
/// key rather than the acting user's key.
Expand Down
65 changes: 64 additions & 1 deletion test/Api.Test/Vault/Models/Request/CipherRequestModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using Bit.Api.Vault.Models;
using Bit.Api.Vault.Models.Request;
using Bit.Core.Vault.Entities;
Expand Down Expand Up @@ -263,4 +264,66 @@ public void ToCipher_Passport_WithDataField_UsesDataDirectly()

Assert.Equal(expectedData, cipher.Data);
}

[Fact]
public void Validate_BlobEncrypted_NameOmitted_Passes()
{
var request = new CipherRequestModel
{
Type = CipherType.Login,
Data = "{\"format_version\":1}",
};

var results = ValidateModel(request);

Assert.Empty(results);
}

[Fact]
public void Validate_NonBlobData_NameOmitted_Fails()
{
var request = new CipherRequestModel
{
Type = CipherType.Login,
Data = "{\"username\":\"2.aGVsbG8=|aGVsbG8=|aGVsbG8=\"}",
};

var results = ValidateModel(request);

Assert.Contains(results, r => r.MemberNames.Contains(nameof(CipherRequestModel.Name)));
}

[Fact]
public void Validate_NeitherDataNorNameProvided_Fails()
{
var request = new CipherRequestModel
{
Type = CipherType.Login,
};

var results = ValidateModel(request);

Assert.Contains(results, r => r.MemberNames.Contains(nameof(CipherRequestModel.Name)));
}

[Fact]
public void Validate_DataOmitted_NameProvided_Passes()
{
var request = new CipherRequestModel
{
Type = CipherType.Login,
Name = "2.aGVsbG8=|aGVsbG8=|aGVsbG8=",
};

var results = ValidateModel(request);

Assert.Empty(results);
}

private static List<ValidationResult> ValidateModel(CipherRequestModel request)
{
var results = new List<ValidationResult>();
Validator.TryValidateObject(request, new ValidationContext(request), results, validateAllProperties: true);
return results;
}
}
Loading