Skip to content

Commit 94fe766

Browse files
authored
Pass buffer.End to AdvanceTo #27585 (#27586)
1 parent 1e1293a commit 94fe766

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Http/WebUtilities/src/FormPipeReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public async Task<Dictionary<string, StringValues>> ReadFormAsync(CancellationTo
101101
}
102102
catch
103103
{
104-
_pipeReader.AdvanceTo(buffer.Start);
104+
_pipeReader.AdvanceTo(buffer.Start, buffer.End);
105105
throw;
106106
}
107107
}

src/Http/WebUtilities/test/FormPipeReaderTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,31 @@ public async Task ReadFormAsync_ValueLengthLimitExceeded_Throw()
190190
Assert.Equal(Encoding.UTF8.GetBytes(content), readResult.Buffer.ToArray());
191191
}
192192

193+
[Fact]
194+
public async Task ReadFormAsync_ValueLengthLimitExceededAcrossBufferBoundary_Throw()
195+
{
196+
Pipe bodyPipe = new Pipe();
197+
198+
var content1 = "foo=1&baz=1234567890";
199+
var content2 = "1";
200+
201+
await bodyPipe.Writer.WriteAsync(Encoding.UTF8.GetBytes(content1));
202+
await bodyPipe.Writer.FlushAsync();
203+
204+
var readTask = Assert.ThrowsAsync<InvalidDataException>(
205+
() => ReadFormAsync(new FormPipeReader(bodyPipe.Reader) { ValueLengthLimit = 10 }));
206+
207+
await bodyPipe.Writer.WriteAsync(Encoding.UTF8.GetBytes(content2));
208+
bodyPipe.Writer.Complete();
209+
210+
var exception = await readTask;
211+
Assert.Equal("Form value length limit 10 exceeded.", exception.Message);
212+
213+
// The body pipe is still readable and has not advanced.
214+
var readResult = await bodyPipe.Reader.ReadAsync();
215+
Assert.Equal(Encoding.UTF8.GetBytes("baz=12345678901"), readResult.Buffer.ToArray());
216+
}
217+
193218
// https://en.wikipedia.org/wiki/Percent-encoding
194219
[Theory]
195220
[InlineData("++=hello", " ", "hello")]

0 commit comments

Comments
 (0)