Commit 1a34ac03 authored by Jon Skeet's avatar Jon Skeet

Throw a better exception when invalid base64 is detected in JSON

parent 730c38ad
......@@ -821,6 +821,15 @@ namespace Google.Protobuf
Assert.Throws<InvalidProtocolBufferException>(() => parser63.Parse<TestRecursiveMessage>(data64));
}
[Test]
[TestCase("AQI")]
[TestCase("_-==")]
public void Bytes_InvalidBase64(string badBase64)
{
string json = "{ \"singleBytes\": \"" + badBase64 + "\" }";
Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
}
[Test]
[TestCase("\"FOREIGN_BAR\"")]
[TestCase("5")]
......
......@@ -30,6 +30,7 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using System.IO;
namespace Google.Protobuf
......@@ -45,6 +46,11 @@ namespace Google.Protobuf
{
}
internal InvalidProtocolBufferException(string message, Exception innerException)
: base(message, innerException)
{
}
internal static InvalidProtocolBufferException MoreDataAvailable()
{
return new InvalidProtocolBufferException(
......@@ -82,6 +88,11 @@ namespace Google.Protobuf
"Protocol message contained an invalid tag (zero).");
}
internal static InvalidProtocolBufferException InvalidBase64(Exception innerException)
{
return new InvalidProtocolBufferException("Invalid base64 data", innerException);
}
internal static InvalidProtocolBufferException InvalidEndTag()
{
return new InvalidProtocolBufferException(
......
......@@ -647,7 +647,14 @@ namespace Google.Protobuf
case FieldType.String:
return text;
case FieldType.Bytes:
try
{
return ByteString.FromBase64(text);
}
catch (FormatException e)
{
throw InvalidProtocolBufferException.InvalidBase64(e);
}
case FieldType.Int32:
case FieldType.SInt32:
case FieldType.SFixed32:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment