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
5 changes: 4 additions & 1 deletion Src/Fido2.Models/Base64Url.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ public static byte[] DecodeUtf8(ReadOnlySpan<byte> text)
buffer[encodedLength - 2] = (byte)'=';
}

Base64.DecodeFromUtf8InPlace(buffer.AsSpan(0, encodedLength), out int decodedLength);
if (OperationStatus.Done != Base64.DecodeFromUtf8InPlace(buffer.AsSpan(0, encodedLength), out int decodedLength))
{
throw new FormatException("The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.");
}

var result = buffer.AsSpan(0, decodedLength).ToArray();

Expand Down
11 changes: 11 additions & 0 deletions Test/Base64UrlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,15 @@ public TestDataGenerator()
Add(Array.Empty<byte>());
}
}

[Fact]
public static void Format_BadBase64Char()
{
const string Format_BadBase64Char = "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.";
var ex = Assert.Throws<FormatException>(() => Base64Url.Decode("rCQqQMqKVO/geUyc9aENh85Mt2g1JHAUKUG27WZVE68==="));
Assert.Equal(Format_BadBase64Char, ex.Message);

ex = Assert.Throws<FormatException>(() => Base64Url.DecodeUtf8(Encoding.UTF8.GetBytes("rCQqQMqKVO/geUyc9aENh85Mt2g1JHAUKUG27WZVE68===")));
Assert.Equal(Format_BadBase64Char, ex.Message);
}
}