@@ -306,27 +306,31 @@ static bool EqualsAny (string value, params string[] values)
306
306
internal MimeEntity CreateEntity ( ContentType contentType , IList < Header > headers , bool toplevel , int depth )
307
307
{
308
308
var args = new MimeEntityConstructorArgs ( this , contentType , headers , toplevel ) ;
309
-
310
- if ( depth >= MaxMimeDepth )
311
- return new MimePart ( args ) ;
312
-
313
309
var subtype = contentType . MediaSubtype ;
314
310
var type = contentType . MediaType ;
315
311
316
312
if ( mimeTypes . Count > 0 ) {
317
313
var mimeType = $ "{ type } /{ subtype } ";
318
314
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
+ }
321
320
}
322
321
323
322
if ( type . Equals ( "text" , StringComparison . OrdinalIgnoreCase ) ) {
324
323
// 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 ) )
326
325
return new TextRfc822Headers ( args ) ;
327
326
328
327
return new TextPart ( args ) ;
329
328
} 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
+
330
334
// multipart/alternative
331
335
if ( subtype . Equals ( "alternative" , StringComparison . OrdinalIgnoreCase ) )
332
336
return new MultipartAlternative ( args ) ;
@@ -375,13 +379,13 @@ internal MimeEntity CreateEntity (ContentType contentType, IList<Header> headers
375
379
376
380
// message/rfc822
377
381
if ( EqualsAny ( subtype , "rfc822" , "global" , "news" , "external-body" , "rfc2822" ) ) {
378
- if ( ! IsEncoded ( headers ) )
382
+ if ( depth < MaxMimeDepth && ! IsEncoded ( headers ) )
379
383
return new MessagePart ( args ) ;
380
384
} else if ( subtype . Equals ( "partial" , StringComparison . OrdinalIgnoreCase ) ) {
381
385
if ( ! IsEncoded ( headers ) )
382
386
return new MessagePartial ( args ) ;
383
387
} else if ( subtype . Equals ( "global-headers" , StringComparison . OrdinalIgnoreCase ) ) {
384
- if ( ! IsEncoded ( headers ) )
388
+ if ( depth < MaxMimeDepth && ! IsEncoded ( headers ) )
385
389
return new TextRfc822Headers ( args ) ;
386
390
}
387
391
} else if ( type . Equals ( "application" , StringComparison . OrdinalIgnoreCase ) ) {
0 commit comments