Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 1eedf89

Browse files
committed
Make sure we check offset and length bounds
1 parent 1145b82 commit 1eedf89

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/Microsoft.AspNetCore.Http.Abstractions/HttpResponse.cs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ public virtual Task SendFileAsync(IFileInfo file)
141141
{
142142
using (var readStream = file.CreateReadStream())
143143
{
144-
readStream.Seek(offset, SeekOrigin.Begin); // TODO: What if !CanSeek?
145-
146-
await StreamCopyOperation.CopyToAsync(readStream, Body, count, cancellationToken);
144+
await SendFileAsync(Body, readStream, offset, count, cancellationToken);
147145
return;
148146
}
149147
}
@@ -159,23 +157,8 @@ public virtual Task SendFileAsync(IFileInfo file)
159157
await sendFile.SendFileAsync(file.PhysicalPath, offset, count, cancellationToken);
160158
}
161159

162-
// Not safe for overlapped writes.
163-
private static async Task SendFileAsync(Stream outputStream, string fileName, long offset, long? length, CancellationToken cancellationToken)
160+
private static async Task SendFileAsync(Stream outputStream, string fileName, long offset, long? count, CancellationToken cancellationToken)
164161
{
165-
cancellationToken.ThrowIfCancellationRequested();
166-
167-
var fileInfo = new FileInfo(fileName);
168-
if (offset < 0 || offset > fileInfo.Length)
169-
{
170-
throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
171-
}
172-
173-
if (length.HasValue &&
174-
(length.Value < 0 || length.Value > fileInfo.Length - offset))
175-
{
176-
throw new ArgumentOutOfRangeException(nameof(length), length, string.Empty);
177-
}
178-
179162
int bufferSize = 1024 * 16;
180163

181164
var fileStream = new FileStream(
@@ -188,10 +171,29 @@ private static async Task SendFileAsync(Stream outputStream, string fileName, lo
188171

189172
using (fileStream)
190173
{
191-
fileStream.Seek(offset, SeekOrigin.Begin);
174+
await SendFileAsync(outputStream, fileStream, offset, count, cancellationToken);
175+
}
176+
}
177+
178+
// Not safe for overlapped writes.
179+
private static async Task SendFileAsync(Stream outputStream, Stream readStream, long offset, long? length, CancellationToken cancellationToken)
180+
{
181+
cancellationToken.ThrowIfCancellationRequested();
182+
183+
if (offset < 0 || offset > readStream.Length)
184+
{
185+
throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
186+
}
192187

193-
await StreamCopyOperation.CopyToAsync(fileStream, outputStream, length, cancellationToken);
188+
if (length.HasValue &&
189+
(length.Value < 0 || length.Value > readStream.Length - offset))
190+
{
191+
throw new ArgumentOutOfRangeException(nameof(length), length, string.Empty);
194192
}
193+
194+
readStream.Seek(offset, SeekOrigin.Begin); // TODO: What if !CanSeek?
195+
196+
await StreamCopyOperation.CopyToAsync(readStream, outputStream, length, cancellationToken);
195197
}
196198
}
197199
}

0 commit comments

Comments
 (0)