Skip to content

Commit f0f945c

Browse files
committed
Validate the data when attempting to determine multi-part boundary
Resolves #104
1 parent 77bca0e commit f0f945c

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

Source/HttpMultipartParser/StreamingMultipartFormDataParser.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,14 @@ private static string DetectBoundary(RebufferableBinaryReader reader)
279279
{
280280
// Presumably the boundary is --|||||||||||||| where -- is the stuff added on to
281281
// the front as per the protocol and ||||||||||||| is the part we care about.
282-
string boundary = string.Concat(reader.ReadLine().Skip(2));
282+
var line = reader.ReadLine();
283+
284+
// The line must not be empty and must starts with "--".
285+
if (string.IsNullOrEmpty(line)) throw new MultipartParseException("Unable to determine boundary: unexpected end of stream");
286+
else if (!line.StartsWith("--")) throw new MultipartParseException("Unable to determine boundary: content does not start with a valid multipart boundary");
287+
288+
// Remove the two dashes
289+
string boundary = line.Substring(2);
283290

284291
// If the string ends with '--' it means that we found the "end" boundary and we
285292
// need to trim the two dashes to get the actual boundary
@@ -315,7 +322,13 @@ private static async Task<string> DetectBoundaryAsync(RebufferableBinaryReader r
315322
// Presumably the boundary is --|||||||||||||| where -- is the stuff added on to
316323
// the front as per the protocol and ||||||||||||| is the part we care about.
317324
var line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false);
318-
string boundary = string.Concat(line.Skip(2));
325+
326+
// The line must not be empty and must starts with "--".
327+
if (string.IsNullOrEmpty(line)) throw new MultipartParseException("Unable to determine boundary: either the stream is empty or we reached the end of the stream");
328+
else if (!line.StartsWith("--")) throw new MultipartParseException("Unable to determine boundary: content is not a valid multipart boundary");
329+
330+
// Remove the two dashes
331+
string boundary = line.Substring(2);
319332

320333
// If the string ends with '--' it means that we found the "end" boundary and we
321334
// need to trim the two dashes to get the actual boundary.

0 commit comments

Comments
 (0)