【问题标题】:XML injections in Python using xml.dom.minidom在 Python 中使用 xml.dom.minidom 进行 XML 注入
【发布时间】: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


    【解决方案1】:

    ElementTree 无法防止恶意构造的数据。见list of vulnerabilities。考虑改用defusedxml

    【讨论】:

    • 我已经使用了 defusedxml.cElementTree.parse()。但是模块 defusedxml.cElementTree 不提供类 SubElement、Element、ElementTree。
    • 那是另一个问题。
    猜你喜欢
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 2012-08-17
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    相关资源
    最近更新 更多