Skip to content

Commit

Permalink
Update invalid json test value, fix issue in Base64Url validation
Browse files Browse the repository at this point in the history
  • Loading branch information
buyaa-n committed Jul 22, 2024
1 parent 9efa0b9 commit a6739dc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ public void BasicDecodingWithNonZeroUnusedBits(string inputString)
Span<byte> decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)];

Assert.False(Base64.IsValid(inputString));
Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int _));
Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8(source, decodedBytes, out int _, out int _));
Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8InPlace(source, out int _));
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,12 @@ public void BasicDecodingWithFinalBlockTrueKnownInputDone(string inputString, in
[InlineData("AQIDBAUHCAkKCwwNDxD")]
public void BasicDecodingWithNonZeroUnusedBits(string inputString)
{
Span<byte> source = Encoding.ASCII.GetBytes(inputString);
byte[] source = Encoding.ASCII.GetBytes(inputString);
Span<byte> decodedBytes = new byte[Base64.GetMaxDecodedFromUtf8Length(source.Length)];

Assert.False(Base64.IsValid(inputString));
Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8(source, decodedBytes, out int consumed, out int _));
Assert.False(Base64Url.IsValid(inputString));
Assert.Equal(OperationStatus.InvalidData, Base64Url.DecodeFromUtf8(source, decodedBytes, out int _, out int _));
Assert.Throws<FormatException>(() => Base64Url.DecodeFromUtf8InPlace(source));
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,19 @@ internal static bool IsValid<T, TBase64Validatable>(TBase64Validatable validatab
}
}

int decoded = validatable.DecodeValue(lastChar);
if (paddingCount == 1 && (decoded & 0x03) != 0 ||
paddingCount == 2 && (decoded & 0x0F) != 0)
{
// unused lower bits are not 0, reject input
decodedLength = 0;
return false;
}

length += paddingCount;
break;
}

int decoded = validatable.DecodeValue(lastChar);
if (paddingCount == 1 && (decoded & 0x03) != 0 ||
paddingCount == 2 && (decoded & 0x0F) != 0)
{
// unused lower bits are not 0, reject input
decodedLength = 0;
return false;
}

if (!validatable.ValidateAndDecodeLength(length, paddingCount, out decodedLength))
{
goto Fail;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class JsonBase64TestData
{
public static IEnumerable<object[]> ValidBase64Tests()
{
yield return new object[] { "\"ABC=\"" };
yield return new object[] { "\"ABA=\"" };
yield return new object[] { "\"AB+D\"" };
yield return new object[] { "\"ABCD\"" };
yield return new object[] { "\"ABC/\"" };
Expand All @@ -19,9 +19,9 @@ public static IEnumerable<object[]> ValidBase64Tests()

public static IEnumerable<object[]> InvalidBase64Tests()
{
yield return new object[] { "\"ABC===\"" };
yield return new object[] { "\"ABC\"" };
yield return new object[] { "\"ABC!\"" };
yield return new object[] { "\"ABA===\"" };
yield return new object[] { "\"ABA\"" };
yield return new object[] { "\"ABA!\"" };
yield return new object[] { GenerateRandomInvalidLargeString(includeEscapedCharacter: true) };
yield return new object[] { GenerateRandomInvalidLargeString(includeEscapedCharacter: false) };
}
Expand Down

0 comments on commit a6739dc

Please sign in to comment.