DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Convert XLS to XLSX in Java
  • Start Coding With Google Cloud Workstations
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Why I Started Using Dependency Injection in Python

Trending

  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • A Modern Stack for Building Scalable Systems
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  1. DZone
  2. Coding
  3. Languages
  4. How to Mitigate XXE Vulnerabilities in Python

How to Mitigate XXE Vulnerabilities in Python

Want to learn more about the XML External Entity (XXE)? Check out this post to learn more about how to mitigate XXE vulnerabilities in Python.

By 
Juxhin Dyrmishi Brigjaj user avatar
Juxhin Dyrmishi Brigjaj
·
Sep. 07, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
9.8K Views

Join the DZone community and get the full member experience.

Join For Free

What Is an XML External Entity (XXE)?

XML External Entity Injection is often referred to as a variant of Server-side Request Forgery (SSRF). XXE leverages language parsers parse the widely used data format, XML, used in a number of common scenarios, such as SOAP and REST web services and file formats, such as PDF, DOCX, HTML.

The main issue lies in XML parsers and how, by default, they handle outbound network calls as well as other XML-specific features, which we will get into later. For a broader and more in-depth explanation of XXE, I would highly recommend going over our 2-part series first.

Python XML Libraries

In the Python ecosystem (2.X & 3.X), most — if not all — XML parsing is handled by the standard libraries:

  • minidom
  • etree
  • sax
  • pulldom

And, in some cases, even beautifulsoup, since as we said HTML is a subset of XML, we can parse XML using it. The good news is that minidom and etree are not vulnerable to XXE by default. As a result, we’ll focus on pulldom as it’s tightly knit to sax.

Examples

The following example leverages the pulldom module as well as bottle to create a very minimal web service. It has a single endpoint, POST  /pulldom that receives the request body as XML, parses it, and returns it back.

import bottle

from xml.dom.pulldom import START_ELEMENT, parse

@bottle.post('/pulldom')
def pulldom():
    doc = parse(bottle.request.body)
    for event, node in doc:
        doc.expandNode(node)
    return(str(doc))

if __name__ == '__main__':
    bottle.run(host='0.0.0.0', port=5050)


The scanner can detect this by leveraging our AcuMonitor service by transmitting and receiving the following request and response during the scan:

Request

POST http://localhost:5050/lxmlnet HTTP/1.1
Content-type: text/xml
Host: localhost:5050


<!ENTITY dteyybzent SYSTEM "http://hitWP5ElLuA1m.bxss.me/">
]>
&dteyybzent;


In bold, you can see the !ENTITY expansion pointing to a remote URL, which, in this case, routes to the AcuMonitor (under bxss.me).

Response

HTTP/1.0 500 Internal Server Error
Server: WSGIServer/0.1 Python/2.7.14

...
<body>
<h1>Error: 500 Internal Server Error</h1>
<p>Sorry, the requested URL <tt>'http://localhost:5050/lxmlnet'</tt>
caused an error:</p>
<pre>Internal Server Error</pre>
</body>
...


The above is an HTTP Response truncated for clarity.

The server tried processing the payload internally and sent the request, which raised a  SAXParseException:

SAXParseException: http://hituxSWuqFxmy.bxss.me/:2:0: error in processing external entity reference


Conclusion

With security, the first question when receiving an input is along the lines of, “where is this data source coming from?” Given that the two most popular libraries, minidom and etree, are safe from XXE attacks (though vulnerable to others), you are generally good to go from the standard library.

The alternative libraries, pulldom and sax, are, by design, required to pull XML from remote locations, which means that you should avoid using them for handling untrusted user XMLs in any form. Should you still opt to use the aforementioned libraries, you should wrap them with your own safe implementation that sanitizes input prior to processing it.

Python (language) XML

Published at DZone with permission of Juxhin Dyrmishi Brigjaj, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Start Coding With Google Cloud Workstations
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Why I Started Using Dependency Injection in Python

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!