mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 00:22:50 -05:00
Address PR feedback pt.1
This commit is contained in:
@ -11,7 +11,7 @@ namespace Bit.Core.Utilities;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class EncryptedStringAttribute : ValidationAttribute
|
public class EncryptedStringAttribute : ValidationAttribute
|
||||||
{
|
{
|
||||||
internal static readonly Dictionary<EncryptionType, int> _encryptionTypeMap = new()
|
internal static readonly Dictionary<EncryptionType, int> _encryptionTypeToRequiredPiecesMap = new()
|
||||||
{
|
{
|
||||||
[EncryptionType.AesCbc256_B64] = 2, // iv|ct
|
[EncryptionType.AesCbc256_B64] = 2, // iv|ct
|
||||||
[EncryptionType.AesCbc128_HmacSha256_B64] = 3, // iv|ct|mac
|
[EncryptionType.AesCbc128_HmacSha256_B64] = 3, // iv|ct|mac
|
||||||
@ -54,7 +54,7 @@ public class EncryptedStringAttribute : ValidationAttribute
|
|||||||
{
|
{
|
||||||
if (!value.TrySplitBy('.', out var headerChunk, out var rest))
|
if (!value.TrySplitBy('.', out var headerChunk, out var rest))
|
||||||
{
|
{
|
||||||
// We coundn't find a header part, this is the slow path, because we have to do two loops over
|
// We couldn't find a header part, this is the slow path, because we have to do two loops over
|
||||||
// the data.
|
// the data.
|
||||||
// If it has 3 encryption parts that means it is AesCbc128_HmacSha256_B64
|
// If it has 3 encryption parts that means it is AesCbc128_HmacSha256_B64
|
||||||
// else we assume it is AesCbc256_B64
|
// else we assume it is AesCbc256_B64
|
||||||
@ -72,11 +72,11 @@ public class EncryptedStringAttribute : ValidationAttribute
|
|||||||
|
|
||||||
if (pieces == 3)
|
if (pieces == 3)
|
||||||
{
|
{
|
||||||
return ValidatePieces(rest, _encryptionTypeMap[EncryptionType.AesCbc128_HmacSha256_B64]);
|
return ValidatePieces(rest, _encryptionTypeToRequiredPiecesMap[EncryptionType.AesCbc128_HmacSha256_B64]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return ValidatePieces(rest, _encryptionTypeMap[EncryptionType.AesCbc256_B64]);
|
return ValidatePieces(rest, _encryptionTypeToRequiredPiecesMap[EncryptionType.AesCbc256_B64]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,7 +94,7 @@ public class EncryptedStringAttribute : ValidationAttribute
|
|||||||
|
|
||||||
// Since this value came from Enum.TryParse we know it is an enumerated object and we can therefore
|
// Since this value came from Enum.TryParse we know it is an enumerated object and we can therefore
|
||||||
// just access the dictionary
|
// just access the dictionary
|
||||||
return ValidatePieces(rest, _encryptionTypeMap[encryptionType]);
|
return ValidatePieces(rest, _encryptionTypeToRequiredPiecesMap[encryptionType]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simply cast the number to the enum, this could be a value that doesn't actually have a backing enum
|
// Simply cast the number to the enum, this could be a value that doesn't actually have a backing enum
|
||||||
@ -102,7 +102,7 @@ public class EncryptedStringAttribute : ValidationAttribute
|
|||||||
// numbers will be filtered out there.
|
// numbers will be filtered out there.
|
||||||
encryptionType = (EncryptionType)encryptionTypeNumber;
|
encryptionType = (EncryptionType)encryptionTypeNumber;
|
||||||
|
|
||||||
if (!_encryptionTypeMap.TryGetValue(encryptionType, out var encryptionPieces))
|
if (!_encryptionTypeToRequiredPiecesMap.TryGetValue(encryptionType, out var encryptionPieces))
|
||||||
{
|
{
|
||||||
// Could not find a configuration map for the given header piece. This is an invalid string
|
// Could not find a configuration map for the given header piece. This is an invalid string
|
||||||
return false;
|
return false;
|
||||||
@ -170,7 +170,7 @@ public class EncryptedStringAttribute : ValidationAttribute
|
|||||||
// Check if we rented the pool and if so, return it.
|
// Check if we rented the pool and if so, return it.
|
||||||
if (pooledChunks != null)
|
if (pooledChunks != null)
|
||||||
{
|
{
|
||||||
ArrayPool<byte>.Shared.Return(pooledChunks);
|
ArrayPool<byte>.Shared.Return(pooledChunks, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ public static class SpanExtensions
|
|||||||
{
|
{
|
||||||
var splitIndex = input.IndexOf(splitChar);
|
var splitIndex = input.IndexOf(splitChar);
|
||||||
|
|
||||||
if (splitIndex < 1)
|
if (splitIndex == -1)
|
||||||
{
|
{
|
||||||
chunk = default;
|
chunk = default;
|
||||||
rest = input;
|
rest = input;
|
||||||
|
@ -75,13 +75,13 @@ public class EncryptedStringAttributeTests
|
|||||||
public void EncryptionTypeMap_HasEntry_ForEachEnumValue()
|
public void EncryptionTypeMap_HasEntry_ForEachEnumValue()
|
||||||
{
|
{
|
||||||
var enumValues = Enum.GetValues<EncryptionType>();
|
var enumValues = Enum.GetValues<EncryptionType>();
|
||||||
Assert.Equal(enumValues.Length, EncryptedStringAttribute._encryptionTypeMap.Count);
|
Assert.Equal(enumValues.Length, EncryptedStringAttribute._encryptionTypeToRequiredPiecesMap.Count);
|
||||||
|
|
||||||
foreach (var enumValue in enumValues)
|
foreach (var enumValue in enumValues)
|
||||||
{
|
{
|
||||||
// Go a step further and ensure that the map contains a value for each value instead of just casting
|
// Go a step further and ensure that the map contains a value for each value instead of just casting
|
||||||
// a random number for one of the keys.
|
// a random number for one of the keys.
|
||||||
Assert.True(EncryptedStringAttribute._encryptionTypeMap.ContainsKey(enumValue));
|
Assert.True(EncryptedStringAttribute._encryptionTypeToRequiredPiecesMap.ContainsKey(enumValue));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user