XML bomb (Entity Injection) is by default taken care in .Net 4.0 but not in .Net 3.5. How? What changed? -


following code

xmldocument xdoc = new xmldocument();             string xml = @"<!doctype lolz [" +                     "<!entity lol \"lol\">" +                     "<!entity lol2 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">" +                     "<!entity lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">" +                     "<!entity lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">" +                     "<!entity lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">" +                     "<!entity lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">" +                     "<!entity lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">" +                     "<!entity lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">" +                     "<!entity lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">" +                     "]>" +                     "<lolz>&lol9;</lolz>";             xdoc.loadxml(xml); 

.net 4.0 code throw exception the input document has exceeded limit set maxcharactersfromentities

.net 2.0/3.5 code not throw exception , keep on growing in xml until memory limit reached

can explain reason of difference?

research done far disassembled system.xml v2.0 , v4.0 , change saw in method registerconsumedcharacters v2.0 definition

private void registerconsumedcharacters(long characters, bool inentityreference) {     if (this.maxcharactersindocument > 0l)     {         long num = this.charactersindocument + characters;         if (num < this.charactersindocument)         {             this.throwwithoutlineinfo("xmlserializeerrordetails", new string[] { "maxcharactersindocument", "" });         }         else         {             this.charactersindocument = num;         }         if (this.charactersindocument > this.maxcharactersindocument)         {             this.throwwithoutlineinfo("xmlserializeerrordetails", new string[] { "maxcharactersindocument", "" });         }     }     if ((this.maxcharactersfromentities > 0l) && inentityreference)     {         long num2 = this.charactersfromentities + characters;         if (num2 < this.charactersfromentities)         {             this.throwwithoutlineinfo("xmlserializeerrordetails", new string[] { "maxcharactersfromentities", "" });         }         else         {             this.charactersfromentities = num2;         }         if ((this.charactersfromentities > this.maxcharactersfromentities) && xmltextreadersection.limitcharactersfromentities)         {             this.throwwithoutlineinfo("xmlserializeerrordetails", new string[] { "maxcharactersfromentities", "" });         }     } } 

v4.0 definition

private void registerconsumedcharacters(long characters, bool inentityreference) {     if (this.maxcharactersindocument > 0l)     {         long num = this.charactersindocument + characters;         if (num < this.charactersindocument)         {             this.throwwithoutlineinfo("xml_limitexceeded", "maxcharactersindocument");         }         else         {             this.charactersindocument = num;         }         if (this.charactersindocument > this.maxcharactersindocument)         {             this.throwwithoutlineinfo("xml_limitexceeded", "maxcharactersindocument");         }     }     if ((this.maxcharactersfromentities > 0l) && inentityreference)     {         long num2 = this.charactersfromentities + characters;         if (num2 < this.charactersfromentities)         {             this.throwwithoutlineinfo("xml_limitexceeded", "maxcharactersfromentities");         }         else         {             this.charactersfromentities = num2;         }         if (this.charactersfromentities > this.maxcharactersfromentities)         {             this.throwwithoutlineinfo("xml_limitexceeded", "maxcharactersfromentities");         }     } } 

only difference see here change in parameters of throwwithoutlineinfo , removal of xmltextreadersection.limitcharactersfromentities in v4.0, not able make out of , have hit block here.

the default value xmlreadersettings.maxcharactersfromentities 0 , means "no limit" msdn documentation says.

but there nasty trick not pointed out documentation, in .net 4 if don't pass xmlreadersettings xmltextreader limit not set 0 10,000,000.

the relevant source code here, comment pointing out breaking change: http://referencesource.microsoft.com/#system.xml/xml/system/xml/core/xmltextreaderimpl.cs#378


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -