【发布时间】:2019-02-08 11:39:58
【问题描述】:
我使用AppScan 扫描了 Python 源代码,它说代码包含潜在的漏洞(XML 注入)。例如:
import xml.dom.minidom
...
dom = xml.dom.minidom.parse(filename)
...
document = xml.dom.minidom.parseString(xmlStr)
...
我安装了 defusedxml 并将使用标准 Python xml 包的所有解析替换为来自 defusedxml.minidom 和 defusedxml.cElementTree 的 parse/parseString:
import defusedxml.minidom
...
dom = defusedxml.minidom.parse(filename)
...
document = defusedxml.minidom.parseString(xmlStr)
...
这些漏洞已从扫描报告中消失。但是 AppScan 仍然会通知我有关从标准 xml 包导入任何函数/类的漏洞。例如来自 ElementTree 的类来修改/构建 xml 树:
from xml.etree.cElementTree import ( # vulnerability here
SubElement, Element, ElementTree)
import defusedxml.cElementTree as et
...
template = et.parse(template_filename) # safe parsing
root = template.getroot()
email_list_el = root.find('emails').find('list')
for email_address in to_list:
SubElement(email_list_el , 'string').text = email_address
root.find('subject')[0].text = subject
root.find('body')[0].text = body
...
如果 xml.dom.minidom 仅用于编写 XML,这是否可以视为漏洞?
【问题讨论】:
标签: python xml xml-parsing