Convert InvalidOperationException to InvalidDataException for form reader (#20138)

This commit is contained in:
Roman Marusyk 2020-03-26 18:42:22 +02:00 committed by GitHub
parent cfac5edd15
commit d0cc04f957
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 4 deletions

View File

@ -377,10 +377,17 @@ namespace Microsoft.AspNetCore.WebUtilities
// We will also create a string from it by the end of the function.
var span = MemoryMarshal.CreateSpan(ref Unsafe.AsRef(readOnlySpan[0]), readOnlySpan.Length);
var bytes = UrlDecoder.DecodeInPlace(span, isFormEncoding: true);
span = span.Slice(0, bytes);
try
{
var bytes = UrlDecoder.DecodeInPlace(span, isFormEncoding: true);
span = span.Slice(0, bytes);
return _encoding.GetString(span);
return _encoding.GetString(span);
}
catch (InvalidOperationException ex)
{
throw new InvalidDataException("The form value contains invalid characters.", ex);
}
}
else
{

View File

@ -78,6 +78,18 @@ namespace Microsoft.AspNetCore.WebUtilities
Assert.Equal("2", formCollection["baz"].ToString());
}
[Fact]
public async Task ReadFormAsync_ValueContainsInvalidCharacters_Throw()
{
var bodyPipe = await MakePipeReader("%00");
var exception = await Assert.ThrowsAsync<InvalidDataException>(
() => ReadFormAsync(new FormPipeReader(bodyPipe)));
Assert.Equal("The form value contains invalid characters.", exception.Message);
Assert.IsType<InvalidOperationException>(exception.InnerException);
}
[Fact]
public async Task ReadFormAsync_ValueCountLimitMet_Success()
{

View File

@ -294,7 +294,7 @@ namespace Microsoft.AspNetCore.Internal
/// Read the next char and convert it into hexadecimal value.
///
/// The <paramref name="scan"/> index will be moved to the next
/// byte no matter no matter whether the operation successes.
/// byte no matter whether the operation successes.
/// </summary>
/// <param name="scan">The index of the byte in the buffer to read</param>
/// <param name="buffer">The byte span from which the hex to be read</param>