Skip to content

Commit 4315487

Browse files
committed
Fixed MaxMimeDepth logic to still use MimePart subclasses
When the MaxMimeDepth is reached by the parser, before this fix, the parser would only instantiate MimePart (and not Multipart or MessagePart subclasses) in order to avoid recursing even further. With this fix, it will instaniate any/all MimePart subclasses as well (such as TextPart). Fixes issue #1006
1 parent 1223aaa commit 4315487

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

MimeKit/ParserOptions.cs

+13-9
Original file line numberDiff line numberDiff line change
@@ -306,27 +306,31 @@ static bool EqualsAny (string value, params string[] values)
306306
internal MimeEntity CreateEntity (ContentType contentType, IList<Header> headers, bool toplevel, int depth)
307307
{
308308
var args = new MimeEntityConstructorArgs (this, contentType, headers, toplevel);
309-
310-
if (depth >= MaxMimeDepth)
311-
return new MimePart (args);
312-
313309
var subtype = contentType.MediaSubtype;
314310
var type = contentType.MediaType;
315311

316312
if (mimeTypes.Count > 0) {
317313
var mimeType = $"{type}/{subtype}";
318314

319-
if (mimeTypes.TryGetValue (mimeType, out var ctor))
320-
return (MimeEntity) ctor.Invoke (new object[] { args });
315+
if (mimeTypes.TryGetValue (mimeType, out var ctor)) {
316+
// Instantiate the custom type if-and-only-if the current parser depth is < MaxMimeDepth -or- the custom type is a MimePart subclass.
317+
if (depth < MaxMimeDepth || typeof (MimePart).IsAssignableFrom (ctor.DeclaringType))
318+
return (MimeEntity) ctor.Invoke (new object[] { args });
319+
}
321320
}
322321

323322
if (type.Equals ("text", StringComparison.OrdinalIgnoreCase)) {
324323
// text/rfc822-headers
325-
if (subtype.Equals ("rfc822-headers", StringComparison.OrdinalIgnoreCase) && !IsEncoded (headers))
324+
if (depth < MaxMimeDepth && subtype.Equals ("rfc822-headers", StringComparison.OrdinalIgnoreCase) && !IsEncoded (headers))
326325
return new TextRfc822Headers (args);
327326

328327
return new TextPart (args);
329328
} else if (type.Equals ("multipart", StringComparison.OrdinalIgnoreCase)) {
329+
if (depth >= MaxMimeDepth) {
330+
// We don't want to recurse any further, so treat this as a leaf node.
331+
return new MimePart (args);
332+
}
333+
330334
// multipart/alternative
331335
if (subtype.Equals ("alternative", StringComparison.OrdinalIgnoreCase))
332336
return new MultipartAlternative (args);
@@ -375,13 +379,13 @@ internal MimeEntity CreateEntity (ContentType contentType, IList<Header> headers
375379

376380
// message/rfc822
377381
if (EqualsAny (subtype, "rfc822", "global", "news", "external-body", "rfc2822")) {
378-
if (!IsEncoded (headers))
382+
if (depth < MaxMimeDepth && !IsEncoded (headers))
379383
return new MessagePart (args);
380384
} else if (subtype.Equals ("partial", StringComparison.OrdinalIgnoreCase)) {
381385
if (!IsEncoded (headers))
382386
return new MessagePartial (args);
383387
} else if (subtype.Equals ("global-headers", StringComparison.OrdinalIgnoreCase)) {
384-
if (!IsEncoded (headers))
388+
if (depth < MaxMimeDepth && !IsEncoded (headers))
385389
return new TextRfc822Headers (args);
386390
}
387391
} else if (type.Equals ("application", StringComparison.OrdinalIgnoreCase)) {

0 commit comments

Comments
 (0)