Skip to content

Commit e5f4400

Browse files
JulijaRamoskieneTratcher
authored andcommitted
FormFeature -Added exception for invalid content-disposition header (#7525)
1 parent 616a4a3 commit e5f4400

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Http/Http/src/Features/FormFeature.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@ private async Task<IFormCollection> InnerReadFormAsync(CancellationToken cancell
175175
while (section != null)
176176
{
177177
// Parse the content disposition here and pass it further to avoid reparsings
178-
ContentDispositionHeaderValue contentDisposition;
179-
ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);
178+
if (!ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition))
179+
{
180+
throw new InvalidDataException("Form section has invalid Content-Disposition value: " + section.ContentDisposition);
181+
}
180182

181183
if (contentDisposition.IsFileDisposition())
182184
{

src/Http/Http/test/Features/FormFeatureTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ public async Task ReadFormAsync_SimpleData_ReturnsParsedFormCollection(bool buff
115115
"\r\n" +
116116
"Foo\r\n";
117117

118+
private const string InvalidContentDispositionValue = "form-data; name=\"description\" - filename=\"temp.html\"";
119+
120+
private const string MultipartFormFileInvalidContentDispositionValue = "--WebKitFormBoundary5pDRpGheQXaM8k3T\r\n" +
121+
"Content-Disposition: " +
122+
InvalidContentDispositionValue +
123+
"\r\n" +
124+
"\r\n" +
125+
"Foo\r\n";
118126

119127
private const string MultipartFormWithField =
120128
MultipartFormField +
@@ -137,6 +145,10 @@ public async Task ReadFormAsync_SimpleData_ReturnsParsedFormCollection(bool buff
137145
MultipartFormFileSpecialCharacters +
138146
MultipartFormEndWithSpecialCharacters;
139147

148+
private const string MultipartFormWithInvalidContentDispositionValue =
149+
MultipartFormFileInvalidContentDispositionValue +
150+
MultipartFormEnd;
151+
140152
[Theory]
141153
[InlineData(true)]
142154
[InlineData(false)]
@@ -489,6 +501,24 @@ public async Task ReadFormAsync_MultipartWithFieldAndMediumFile_ReturnsParsedFor
489501
await responseFeature.CompleteAsync();
490502
}
491503

504+
[Fact]
505+
public async Task ReadFormAsync_MultipartWithInvalidContentDisposition_Throw()
506+
{
507+
var formContent = Encoding.UTF8.GetBytes(MultipartFormWithInvalidContentDispositionValue);
508+
var context = new DefaultHttpContext();
509+
var responseFeature = new FakeResponseFeature();
510+
context.Features.Set<IHttpResponseFeature>(responseFeature);
511+
context.Request.ContentType = MultipartContentType;
512+
context.Request.Body = new NonSeekableReadStream(formContent);
513+
514+
IFormFeature formFeature = new FormFeature(context.Request, new FormOptions());
515+
context.Features.Set<IFormFeature>(formFeature);
516+
517+
var exception = await Assert.ThrowsAsync<InvalidDataException>(() => context.Request.ReadFormAsync());
518+
519+
Assert.Equal("Form section has invalid Content-Disposition value: " + InvalidContentDispositionValue, exception.Message);
520+
}
521+
492522
private Stream CreateFile(int size)
493523
{
494524
var stream = new MemoryStream(size);

0 commit comments

Comments
 (0)