“An insecure overload of” warning

前端 未结 1 405
北恋
北恋 2021-01-21 08:06

I have the following code:

using (FileStream fs = new FileStream(path_to_xml, FileMode.Open))
{
    using (XmlReader xr = XmlReader.Create(fs))
    {
        //          


        
相关标签:
1条回答
  • 2021-01-21 08:22

    Looking at the documentation of the warning explains the root cause and many possible fixes, but it boils down that the XML being readed can contain DTD references to potentially insecure places, and a carefully crafted document could represent a vulnerability. From the docs:

    If you use insecure DtdProcessing instances or reference external entity sources, the parser may accept untrusted input and disclose sensitive information to attackers.

    The problem lies in that the default settings of both XmlReader and XmlReaderSettings classes allow for such behavior. As the default presents this problem, you need to explictly set to a safe option, that ultimately boils down to setting DtdProcessing to DtdProcessing.Prohibit or XmlResolver to a XmlSecureResolver.

    Back to your code, it can be changed as such:

    using (XmlReader xr = XmlReader.Create(fs, new XmlReaderSettings() { DtdProcessing = DtdProcessing.Prohibit }))
    

    or

    using (XmlReader xr = XmlReader.Create(fs, new XmlReaderSettings() { XmlResolver = new XmlSecureResolver() }))
    
    0 讨论(0)
提交回复
热议问题