<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Iroro Chadere</title>
    <description>The latest articles on DEV Community by Iroro Chadere (@irorochad).</description>
    <link>https://dev.to/irorochad</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F917028%2F044d776a-3fdb-4a50-860a-4e78e2a03228.png</url>
      <title>DEV Community: Iroro Chadere</title>
      <link>https://dev.to/irorochad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/irorochad"/>
    <language>en</language>
    <item>
      <title>XML External Entity (XXE) Injection: A Complete Guide for Developers</title>
      <dc:creator>Iroro Chadere</dc:creator>
      <pubDate>Sun, 04 Jan 2026 17:35:33 +0000</pubDate>
      <link>https://dev.to/irorochad/xml-external-entity-xxe-injection-a-complete-guide-for-developers-17cg</link>
      <guid>https://dev.to/irorochad/xml-external-entity-xxe-injection-a-complete-guide-for-developers-17cg</guid>
      <description>&lt;p&gt;XXE (XML External Entity) injection is a vulnerability that turns standard XML features into security nightmares. Imagine three weeks after adding XML support to your API, you discover your application has been leaking AWS credentials to attackers. The culprit? A seemingly innocent XML parser doing exactly what it was designed to do.&lt;/p&gt;

&lt;p&gt;Let's break down exactly how it works and how to prevent it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why XML Parsers Are Different
&lt;/h2&gt;

&lt;p&gt;If you've worked with JSON APIs, you know the parser's job is straightforward: read the data structure and deserialize it. The JSON itself can't tell the parser to fetch external files or make network requests.&lt;/p&gt;

&lt;p&gt;XML operates differently. XML includes a feature called Document Type Definitions (DTDs), a system originally designed to define structure and validation rules for documents. DTDs support entities, which work like variables you can define and reference throughout your document.&lt;/p&gt;

&lt;p&gt;Here's a simple internal entity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE note [
  &amp;lt;!ENTITY company "TechCorp"&amp;gt;&lt;/span&gt;
]&amp;gt;
&lt;span class="nt"&gt;&amp;lt;note&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;message&amp;gt;&lt;/span&gt;Welcome to &lt;span class="ni"&gt;&amp;amp;company;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/message&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/note&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When parsed, &lt;code&gt;&amp;amp;company;&lt;/code&gt; gets replaced with "TechCorp". This is completely safe because the entity value is defined right there in the document—there's no external data source involved.&lt;/p&gt;

&lt;p&gt;But XML also supports external entity references to files on the filesystem or URLs on the network:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE data [
  &amp;lt;!ENTITY external SYSTEM "file:///etc/passwd"&amp;gt;&lt;/span&gt;
]&amp;gt;
&lt;span class="nt"&gt;&amp;lt;data&amp;gt;&lt;/span&gt;&lt;span class="ni"&gt;&amp;amp;external;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/data&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where things get dangerous. The &lt;code&gt;SYSTEM&lt;/code&gt; keyword tells the parser to go fetch content from an external source. By default, most XML parsers will resolve this external entity without question. They'll read &lt;code&gt;/etc/passwd&lt;/code&gt; from the filesystem and insert its contents into your document.&lt;/p&gt;

&lt;p&gt;The parser doesn't distinguish between "content the developer intended to include" and "content an attacker specified in a malicious payload." It just follows instructions. If your application reflects that parsed content back to users in an API response, displays it in an admin panel, or logs it somewhere accessible, an attacker just read arbitrary files from your server.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Three Main Attack Vectors
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Direct File Disclosure
&lt;/h3&gt;

&lt;p&gt;The most straightforward attack. An attacker sends XML with an external entity pointing to a sensitive file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE order [
  &amp;lt;!ENTITY xxe SYSTEM "file:///var/www/app/config/database.yml"&amp;gt;&lt;/span&gt;
]&amp;gt;
&lt;span class="nt"&gt;&amp;lt;order&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;&lt;span class="ni"&gt;&amp;amp;xxe;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/order&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what happens step by step. Your application receives this XML, maybe via an API endpoint that accepts XML data. The XML parser encounters the &lt;code&gt;&amp;lt;!DOCTYPE&amp;gt;&lt;/code&gt; declaration and processes the DTD. The parser sees the entity definition and reads that file from disk. When the parser reaches &lt;code&gt;&amp;amp;xxe;&lt;/code&gt; in the document body, it replaces it with the file's contents. Your application processes the parsed XML, maybe it stores the details field in a database or returns it in an API response. The attacker receives your database credentials.&lt;/p&gt;

&lt;p&gt;Real-world target files include &lt;code&gt;/etc/passwd&lt;/code&gt; for user account information on Linux systems, &lt;code&gt;/var/www/app/.env&lt;/code&gt; for environment variables and secrets, &lt;code&gt;/proc/self/environ&lt;/code&gt; for environment variables of the current process which often contains secrets on cloud platforms, &lt;code&gt;C:\Windows\System32\drivers\etc\hosts&lt;/code&gt; for system configuration on Windows, and &lt;code&gt;~/.ssh/id_rsa&lt;/code&gt; for private SSH keys.&lt;/p&gt;

&lt;p&gt;The key requirement for this attack to work: your application must echo back or expose the parsed content somehow. This happens more often than you'd think, debugging endpoints that return parsed data, error messages that include XML values, or admin dashboards that display imported data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Server-Side Request Forgery via XXE
&lt;/h3&gt;

&lt;p&gt;Instead of reading local files, attackers can make your server send HTTP requests to arbitrary URLs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE data [
  &amp;lt;!ENTITY xxe SYSTEM "http://169.254.169.254/latest/meta-data/iam/security-credentials/admin-role"&amp;gt;&lt;/span&gt;
]&amp;gt;
&lt;span class="nt"&gt;&amp;lt;data&amp;gt;&lt;/span&gt;&lt;span class="ni"&gt;&amp;amp;xxe;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/data&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example targets AWS's metadata service, an internal HTTP endpoint available to EC2 instances that returns sensitive information about the instance, including IAM credentials.&lt;/p&gt;

&lt;p&gt;Here's why this is so dangerous. The metadata service at 169.254.169.254 is only accessible from inside the AWS network. You can't reach it from your laptop or from the internet. But when your server's XML parser encounters &lt;code&gt;SYSTEM "http://169.254.169.254/..."&lt;/code&gt;, it makes an HTTP request from inside your infrastructure. The parser fetches the response which contains AWS credentials and inserts it into the parsed document.&lt;/p&gt;

&lt;p&gt;If your application returns this parsed content, the attacker now has credentials to read from and write to S3 buckets, query and modify databases, spin up new EC2 instances, and access any resource the role has permissions for.&lt;/p&gt;

&lt;p&gt;This works because your server can reach internal services that should never be internet-accessible. The attacker is using your server as a proxy into your private network. They could target &lt;code&gt;http://internal-api.company.local/admin/users&lt;/code&gt; for internal admin APIs, &lt;code&gt;http://localhost:8080/actuator/health&lt;/code&gt; for Spring Boot management endpoints, &lt;code&gt;http://192.168.1.50:9200/_cat/indices&lt;/code&gt; for Elasticsearch clusters on your internal network, or &lt;code&gt;http://consul.service.consul:8500/v1/kv/&lt;/code&gt; for service discovery systems.&lt;/p&gt;

&lt;p&gt;The XML parser doesn't care that these are internal resources. It just follows the URL and returns whatever it finds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Denial of Service Through Entity Expansion
&lt;/h3&gt;

&lt;p&gt;This attack is called "Billion Laughs" and it's devastatingly simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE data [
  &amp;lt;!ENTITY lol "lol"&amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;!ENTITY lol2 "&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;"&amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;!ENTITY lol3 "&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;"&amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;!ENTITY lol4 "&amp;amp;lol3;&amp;amp;lol3;&amp;amp;lol3;&amp;amp;lol3;&amp;amp;lol3;&amp;amp;lol3;&amp;amp;lol3;&amp;amp;lol3;&amp;amp;lol3;&amp;amp;lol3;"&amp;gt;&lt;/span&gt;
]&amp;gt;
&lt;span class="nt"&gt;&amp;lt;data&amp;gt;&lt;/span&gt;&lt;span class="ni"&gt;&amp;amp;lol4;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/data&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's trace what happens when the parser expands &lt;code&gt;&amp;amp;lol4;&lt;/code&gt;. The entity lol4 contains 10 references to lol3. Each lol3 contains 10 references to lol2, so that's 10 times 10 equals 100 lol2 references. Each lol2 contains 10 references to lol, so that's 100 times 10 equals 1,000 lol references. Each lol contains the string "lol", so that's 1,000 times 3 characters equals 3,000 characters.&lt;/p&gt;

&lt;p&gt;Now imagine adding more levels. With just a few more entity definitions, you can create billions of repetitions. By the time the parser finishes expanding, it's trying to hold billions of strings in memory. Your application crashes. Your server runs out of memory and the operating system kills the process. If this happens repeatedly, you've got a full denial of service.&lt;/p&gt;

&lt;p&gt;The attack works because entity expansion happens during parsing before your application code even sees the data. You can't validate or limit it at the application layer because the parser consumes resources before you get a chance to intervene.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where XXE Hides in Modern Applications
&lt;/h2&gt;

&lt;p&gt;"Nobody uses XML anymore" is a dangerous assumption. XML processing happens in places you might not expect. SAML authentication systems use XML to pass authentication assertions between identity providers and applications. SOAP APIs are still common in enterprise integrations, especially with older systems. Office documents like .docx, .xlsx, and .pptx are ZIP files containing XML files that define document structure. SVG images are XML-based. RSS and Atom feeds for blogs and podcasts use XML. Configuration files like Maven POMs, Android layouts, and Spring configuration files are all XML.&lt;/p&gt;

&lt;h3&gt;
  
  
  The File Upload Problem
&lt;/h3&gt;

&lt;p&gt;The most dangerous scenario is file uploads. This deserves special attention because it's often overlooked.&lt;/p&gt;

&lt;p&gt;Here's why file uploads are particularly vulnerable. First, developers don't realize they're parsing XML. When you add a feature to let users upload profile pictures and accept SVG files, you might not think "I'm processing XML." But SVGs are XML documents. If you validate them, extract metadata, or resize them using a library that parses the XML structure, you're running an XML parser on untrusted input.&lt;/p&gt;

&lt;p&gt;Second, the parser runs automatically. Many image processing libraries will automatically parse XML-based formats. You call &lt;code&gt;image.open('avatar.svg')&lt;/code&gt; thinking you're just loading an image, but behind the scenes, an XML parser is processing the entire document structure including any DTDs and external entities.&lt;/p&gt;

&lt;p&gt;Third, users expect file uploads to be safe. Unlike an API endpoint that explicitly accepts XML data, file uploads feel like simple data storage. Developers might thoroughly secure their API parsing but forget that uploaded files need the same scrutiny.&lt;/p&gt;

&lt;p&gt;Example attack: An attacker uploads this SVG as their profile picture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE svg [
  &amp;lt;!ENTITY xxe SYSTEM "file:///etc/passwd"&amp;gt;&lt;/span&gt;
]&amp;gt;
&lt;span class="nt"&gt;&amp;lt;svg&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;"http://www.w3.org/2000/svg"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"100"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"100"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;text&lt;/span&gt; &lt;span class="na"&gt;x=&lt;/span&gt;&lt;span class="s"&gt;"10"&lt;/span&gt; &lt;span class="na"&gt;y=&lt;/span&gt;&lt;span class="s"&gt;"20"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="ni"&gt;&amp;amp;xxe;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/text&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/svg&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your application processes this SVG to validate its dimensions before storing it, generate a thumbnail preview, extract and display metadata, or render it server-side for a preview, then your XML parser reads &lt;code&gt;/etc/passwd&lt;/code&gt; and potentially exposes its contents.&lt;/p&gt;

&lt;p&gt;The attacker might see the file contents in an error message like "Invalid text content: root❌0:0:root:/root:/bin/bash...", a thumbnail that renders the text, server logs that your application saves, or admin panels that show "problematic uploads."&lt;/p&gt;

&lt;p&gt;Office documents have the same issue. A .docx file is a ZIP archive containing these XML files: &lt;code&gt;word/document.xml&lt;/code&gt; for the actual document content, &lt;code&gt;word/styles.xml&lt;/code&gt; for formatting information, and &lt;code&gt;[Content_Types].xml&lt;/code&gt; for file type mappings.&lt;/p&gt;

&lt;p&gt;If your application accepts document uploads and uses a library to extract text, count words, or index content, you're parsing XML. An attacker can embed malicious entities in these XML files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- word/document.xml --&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;?xml version="1.0"?&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE document [
  &amp;lt;!ENTITY xxe SYSTEM "file:///var/www/app/.env"&amp;gt;&lt;/span&gt;
]&amp;gt;
&lt;span class="nt"&gt;&amp;lt;w:document&lt;/span&gt; &lt;span class="na"&gt;xmlns:w=&lt;/span&gt;&lt;span class="s"&gt;"http://schemas.openxmlformats.org/wordprocessingml/2006/main"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;w:body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;w:p&amp;gt;&amp;lt;w:t&amp;gt;&lt;/span&gt;&lt;span class="ni"&gt;&amp;amp;xxe;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/w:t&amp;gt;&amp;lt;/w:p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/w:body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/w:document&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When your document processor extracts text to index it for search or display a preview, it exposes your environment variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Fix XXE: Secure Configuration
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjso0r5yu2kuxosptg1jk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjso0r5yu2kuxosptg1jk.png" alt=" " width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The most effective defense is disabling external entity processing entirely. Unless you specifically need DTDs, and you probably don't, turn them off.&lt;/p&gt;

&lt;p&gt;The reason this works: you're removing the dangerous functionality at its source. If the parser can't resolve external entities, all three attack vectors we discussed become impossible. The parser will either skip the entities or throw an error, but it won't make network requests or read files.&lt;/p&gt;

&lt;p&gt;Let's walk through secure configurations for different languages and why each setting matters.&lt;/p&gt;

&lt;h3&gt;
  
  
  Java with DocumentBuilderFactory
&lt;/h3&gt;

&lt;p&gt;Java's XML parsing ecosystem is complex because there are multiple parsers (DOM, SAX, StAX) and multiple ways to configure them. DocumentBuilderFactory is the most common for building DOM parsers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;DocumentBuilderFactory&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DocumentBuilderFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newInstance&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Option 1: Disable DTDs entirely (most secure)&lt;/span&gt;
&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setFeature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://apache.org/xml/features/disallow-doctype-decl"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Option 2: If you must support DTDs, disable external entities&lt;/span&gt;
&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setFeature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://xml.org/sax/features/external-general-entities"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setFeature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://xml.org/sax/features/external-parameter-entities"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Disable entity expansion to prevent billion laughs attacks&lt;/span&gt;
&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setXIncludeAware&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setExpandEntityReferences&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;DocumentBuilder&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newDocumentBuilder&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xmlInput&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Breaking down each setting: Setting &lt;code&gt;disallow-doctype-decl&lt;/code&gt; to true is the nuclear option. It completely disables DTD processing, which means the parser will throw an error if it encounters &lt;code&gt;&amp;lt;!DOCTYPE&amp;gt;&lt;/code&gt;. This is the most secure configuration because it eliminates the entire attack surface. Use this unless you have a specific business requirement for DTD validation.&lt;/p&gt;

&lt;p&gt;Setting &lt;code&gt;external-general-entities&lt;/code&gt; to false means general entities (the ones you reference with &lt;code&gt;&amp;amp;entityName;&lt;/code&gt;) won't fetch external resources. The parser will still process internal entities defined within the document itself, but it won't make network requests or read files.&lt;/p&gt;

&lt;p&gt;Setting &lt;code&gt;external-parameter-entities&lt;/code&gt; to false affects parameter entities (referenced with &lt;code&gt;%entityName;&lt;/code&gt;) which are used within DTDs themselves. They're less commonly exploited but can still be dangerous. Disabling them prevents attackers from using DTDs to pull in external DTD fragments.&lt;/p&gt;

&lt;p&gt;Setting &lt;code&gt;XIncludeAware&lt;/code&gt; to false disables XInclude, which is a separate W3C standard for including external XML documents. It uses &lt;code&gt;&amp;lt;xi:include&amp;gt;&lt;/code&gt; elements instead of entities, but it has the same security implications. Disabling it prevents another vector for including external content.&lt;/p&gt;

&lt;p&gt;Setting &lt;code&gt;ExpandEntityReferences&lt;/code&gt; to false tells the parser not to expand entity references at all. Combined with the other settings, it provides defense in depth.&lt;/p&gt;

&lt;p&gt;For SAX parsers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;SAXParserFactory&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SAXParserFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newInstance&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setFeature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://xml.org/sax/features/external-general-entities"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setFeature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://xml.org/sax/features/external-parameter-entities"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setFeature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://apache.org/xml/features/disallow-doctype-decl"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;SAXParser&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newSAXParser&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SAX parsers are event-driven, meaning they call your code as they encounter elements rather than building a full document tree in memory. They're often used for large documents, but they have the same XXE vulnerabilities.&lt;/p&gt;

&lt;h3&gt;
  
  
  Python with defusedxml
&lt;/h3&gt;

&lt;p&gt;Python's standard library includes multiple XML parsing modules like xml.etree.ElementTree, xml.dom.minidom, and xml.sax, and all of them are vulnerable to XXE by default. The Python documentation even includes warnings about this.&lt;/p&gt;

&lt;p&gt;The defusedxml library is a wrapper around these standard modules that applies secure defaults:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Don't use this - vulnerable to XXE
# from xml.etree import ElementTree
&lt;/span&gt;
&lt;span class="c1"&gt;# Use this instead
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;defusedxml&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ElementTree&lt;/span&gt;

&lt;span class="n"&gt;tree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ElementTree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;data.xml&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getroot&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What defusedxml does under the hood: It disables DTD processing, external entity resolution, XInclude processing, and entity expansion beyond a safe limit.&lt;/p&gt;

&lt;p&gt;The beautiful part: it's a drop-in replacement. You change one import line and your code is secure. The API is identical to the standard library.&lt;/p&gt;

&lt;p&gt;Why this matters: Python makes it extremely easy to accidentally create XXE vulnerabilities because the vulnerable behavior is the default. You have to actively opt into security with the standard library. Defusedxml eliminates this complexity. Install it with &lt;code&gt;pip install defusedxml&lt;/code&gt; and use it everywhere you process XML.&lt;/p&gt;

&lt;p&gt;For lxml, a popular alternative XML library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;lxml&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;etree&lt;/span&gt;

&lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;etree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;XMLParser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;resolve_entities&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# Don't resolve external entities
&lt;/span&gt;    &lt;span class="n"&gt;no_network&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;         &lt;span class="c1"&gt;# Disable all network access
&lt;/span&gt;    &lt;span class="n"&gt;dtd_validation&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="c1"&gt;# Don't validate against DTDs
&lt;/span&gt;    &lt;span class="n"&gt;load_dtd&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;           &lt;span class="c1"&gt;# Don't load external DTDs
&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;tree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;etree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;data.xml&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;lxml is faster than the standard library and has more features, but it requires explicit security configuration. The settings above disable all the dangerous functionality.&lt;/p&gt;

&lt;h3&gt;
  
  
  Node.js with libxmljs
&lt;/h3&gt;

&lt;p&gt;Node.js has several XML parsing libraries. libxmljs is a popular choice that binds to the C library libxml2. It's fast but requires careful configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;libxmljs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;libxmljs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;libxmljs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parseXml&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;xmlString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;noent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// Disable entity substitution&lt;/span&gt;
  &lt;span class="na"&gt;nonet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;    &lt;span class="c1"&gt;// Prevent network access&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding these options: &lt;code&gt;noent&lt;/code&gt; stands for "no entity substitution." Setting it to false means entity substitution is disabled. This is confusing naming because it's a double negative, but it's how libxml2 works. When disabled, the parser won't replace &lt;code&gt;&amp;amp;xxe;&lt;/code&gt; with external content.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;nonet&lt;/code&gt; option explicitly prevents all network access. Even if entity substitution is enabled, the parser won't make HTTP requests. This is defense in depth.&lt;/p&gt;

&lt;p&gt;Alternative: xml2js is a pure JavaScript parser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;xml2js&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;xml2js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;xml2js&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Parser&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;explicitCharkey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;// xml2js doesn't support external entities by default&lt;/span&gt;
  &lt;span class="c1"&gt;// It's safer because it's pure JavaScript (no C bindings)&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;xmlString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Handle parsing error&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// Process result&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;xml2js is a pure JavaScript implementation, which means it doesn't have the same feature set as libxml2-based parsers. Importantly, it doesn't support external entities at all not because of security configuration, but because the feature was never implemented. This makes it inherently safer for untrusted input, though it's slower for large documents.&lt;/p&gt;

&lt;h3&gt;
  
  
  PHP with libxml
&lt;/h3&gt;

&lt;p&gt;PHP uses libxml2 for XML parsing, the same C library that Node.js uses. It has a global setting that affects all XML operations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Disable external entity loading globally&lt;/span&gt;
&lt;span class="nb"&gt;libxml_disable_entity_loader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nv"&gt;$dom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DOMDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// The LIBXML_NONET flag prevents network access&lt;/span&gt;
&lt;span class="nv"&gt;$dom&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;loadXML&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$xmlString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;LIBXML_NONET&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why global configuration matters in PHP: &lt;code&gt;libxml_disable_entity_loader(true)&lt;/code&gt; affects every XML operation in your PHP process. This is both good and bad. Good because you can't accidentally forget to secure one XML parser. Bad because if any library or dependency needs external entities, it will break. In practice, most applications don't need external entities, so setting this globally is the right approach.&lt;/p&gt;

&lt;p&gt;Additional libxml options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Load XML with multiple security flags&lt;/span&gt;
&lt;span class="nv"&gt;$dom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DOMDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$dom&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;loadXML&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$xmlString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="no"&gt;LIBXML_NONET&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;      &lt;span class="c1"&gt;// Disable network access&lt;/span&gt;
    &lt;span class="no"&gt;LIBXML_NOENT&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;      &lt;span class="c1"&gt;// Disable entity substitution&lt;/span&gt;
    &lt;span class="no"&gt;LIBXML_DTDLOAD&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="c1"&gt;// Don't load external DTDs&lt;/span&gt;
    &lt;span class="no"&gt;LIBXML_DTDATTR&lt;/span&gt;      &lt;span class="c1"&gt;// Don't default attributes from DTDs&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These flags work as bitwise options combined with the pipe operator to layer multiple protections.&lt;/p&gt;

&lt;p&gt;The pattern across all languages is consistent: you're telling the parser to ignore DTDs and external references. Once configured, XXE becomes impossible because the parser simply won't execute the dangerous operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defense in Depth: Additional Layers
&lt;/h2&gt;

&lt;p&gt;Disabling external entities is your primary defense, but secure systems use multiple layers. If a developer accidentally uses an insecure parser or if you need to support legacy systems, these additional protections can prevent exploitation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Input Validation with XML Schemas
&lt;/h3&gt;

&lt;p&gt;If your application only accepts XML with a specific structure, which is common in APIs, enforce that structure before parsing anything. XML Schema (XSD) lets you define exactly what your XML should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- schema.xsd --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;xs:schema&lt;/span&gt; &lt;span class="na"&gt;xmlns:xs=&lt;/span&gt;&lt;span class="s"&gt;"http://www.w3.org/2001/XMLSchema"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"order"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;xs:complexType&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;xs:sequence&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"xs:string"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"amount"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"xs:decimal"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;xs:element&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"customer"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"xs:string"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/xs:sequence&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/xs:complexType&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/xs:element&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/xs:schema&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This schema says: "An order must have an id (string), amount (decimal), and customer (string), in that order. Nothing else is allowed."&lt;/p&gt;

&lt;p&gt;Validate before parsing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;lxml&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;etree&lt;/span&gt;

&lt;span class="c1"&gt;# Load your schema
&lt;/span&gt;&lt;span class="n"&gt;schema_doc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;etree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;schema.xsd&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;etree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;XMLSchema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;schema_doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Create a parser that enforces the schema AND disables dangerous features
&lt;/span&gt;&lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;etree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;XMLParser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;schema&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;no_network&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;resolve_entities&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;etree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;input.xml&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# If we get here, the XML is valid and safe
&lt;/span&gt;&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;etree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;XMLSyntaxError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Validation failed - reject the input
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invalid XML: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;etree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DocumentInvalid&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# XML is well-formed but doesn't match schema
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Schema validation failed: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why schema validation helps: It rejects unexpected structure early. If your schema doesn't include DTD declarations, any document with &lt;code&gt;&amp;lt;!DOCTYPE&amp;gt;&lt;/code&gt; fails validation before the parser processes it. It limits what attackers can send. Even if they bypass your entity protections, they can't include arbitrary elements that might trigger other vulnerabilities in your application logic. It catches malformed attacks. Many XXE payloads will violate your schema's structural requirements, failing validation before reaching the vulnerable code.&lt;/p&gt;

&lt;p&gt;Real-world example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- Attacker's XXE payload --&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE order [
  &amp;lt;!ENTITY xxe SYSTEM "file:///etc/passwd"&amp;gt;&lt;/span&gt;
]&amp;gt;
&lt;span class="nt"&gt;&amp;lt;order&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;id&amp;gt;&lt;/span&gt;&lt;span class="ni"&gt;&amp;amp;xxe;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;amount&amp;gt;&lt;/span&gt;100.00&lt;span class="nt"&gt;&amp;lt;/amount&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;customer&amp;gt;&lt;/span&gt;John Doe&lt;span class="nt"&gt;&amp;lt;/customer&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/order&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your schema says id must be a string with max length 20 characters, and &lt;code&gt;/etc/passwd&lt;/code&gt; is 1,500 characters, the validation fails. The content never reaches your application logic.&lt;/p&gt;

&lt;p&gt;Caveat: Schema validation alone doesn't protect you. Older XML validators might resolve entities during validation. Always combine schema validation with secure parser configuration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sanitize File Uploads
&lt;/h3&gt;

&lt;p&gt;For file types that contain XML like SVG and Office documents, apply extra scrutiny. The challenge here is that you often need to process these files you can't just store them as opaque blobs.&lt;/p&gt;

&lt;p&gt;For SVG files, SVGs are particularly dangerous because they're images that contain executable code (JavaScript via &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags) and can reference external resources. Use a library specifically designed for sanitization:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DOMPurify&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;isomorphic-dompurify&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sanitizeSVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;svgContent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// DOMPurify removes dangerous elements while preserving valid SVG&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;DOMPurify&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sanitize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;svgContent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;USE_PROFILES&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;svg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;svgFilters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;ADD_TAGS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;  &lt;span class="c1"&gt;// Allow specific SVG features you need&lt;/span&gt;
    &lt;span class="na"&gt;ADD_ATTR&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;href&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;// Allow specific attributes&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Process only the sanitized version&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;uploadedContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cleanSVG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sanitizeSVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uploadedContent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Now you can safely parse it&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;libxmljs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parseXml&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cleanSVG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;noent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;nonet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What DOMPurify removes: &lt;code&gt;&amp;lt;!DOCTYPE&amp;gt;&lt;/code&gt; declarations and DTDs, external entity references, &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags for JavaScript in SVG, event handlers like &lt;code&gt;onload="malicious()"&lt;/code&gt;, &lt;code&gt;&amp;lt;foreignObject&amp;gt;&lt;/code&gt; elements which can embed HTML with scripts, and references to external stylesheets that could leak data.&lt;/p&gt;

&lt;p&gt;The result is a clean SVG that only contains drawing instructions.&lt;/p&gt;

&lt;p&gt;For Office documents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;zipfile&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;defusedxml&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ElementTree&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;io&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BytesIO&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;extract_text_from_docx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_path&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Safely extract text from a .docx file.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;zipfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ZipFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;docx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# Read the main document XML
&lt;/span&gt;            &lt;span class="n"&gt;xml_content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;docx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;word/document.xml&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="c1"&gt;# Parse with defusedxml (secure by default)
&lt;/span&gt;            &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ElementTree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromstring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xml_content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="c1"&gt;# Extract text (namespace handling omitted for brevity)
&lt;/span&gt;            &lt;span class="n"&gt;paragraphs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;.//{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;paragraphs&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;zipfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BadZipFile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invalid document file&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;ElementTree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ParseError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Malformed XML in document&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why this is safer: We're explicitly reading only &lt;code&gt;word/document.xml&lt;/code&gt;, not processing arbitrary files from the ZIP. We're using defusedxml which won't resolve external entities. We're only extracting text content, not executing any macros or scripts. We catch parsing errors and treat them as invalid uploads.&lt;/p&gt;

&lt;p&gt;Alternative approach: Don't parse at all. If your use case is just storage and download, like in a document management system, the safest approach is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Just store the file&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;s3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;putObject&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;uploads&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`documents/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;fileBuffer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;ContentType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;application/vnd.openxmlformats-officedocument.wordprocessingml.document&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;ContentDisposition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;attachment&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// Force download, not inline display&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Serve it back with security headers&lt;/span&gt;
&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;application/octet-stream&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// Generic binary&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Disposition&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`attachment; filename="&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;X-Content-Type-Options&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nosniff&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// Don't MIME-sniff&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Security-Policy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;default-src 'none'&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;// No script execution&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By not parsing the file, you avoid the entire vulnerability. Let users download files and open them in desktop applications where they control the security environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Network-Level Controls
&lt;/h3&gt;

&lt;p&gt;Even with secure parsers, add network restrictions as a last line of defense. This protects you if a developer adds a new dependency with a vulnerable parser, a zero-day vulnerability is discovered in your XML library, or an attacker finds a way to bypass your parser configuration.&lt;/p&gt;

&lt;p&gt;Firewall rules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Example iptables rule (Linux)&lt;/span&gt;
&lt;span class="c"&gt;# Block outbound requests from application servers to internal networks&lt;/span&gt;

iptables &lt;span class="nt"&gt;-A&lt;/span&gt; OUTPUT &lt;span class="nt"&gt;-d&lt;/span&gt; 169.254.169.254 &lt;span class="nt"&gt;-j&lt;/span&gt; REJECT  &lt;span class="c"&gt;# Block AWS metadata service&lt;/span&gt;
iptables &lt;span class="nt"&gt;-A&lt;/span&gt; OUTPUT &lt;span class="nt"&gt;-d&lt;/span&gt; 10.0.0.0/8 &lt;span class="nt"&gt;-j&lt;/span&gt; REJECT       &lt;span class="c"&gt;# Block private network (10.x.x.x)&lt;/span&gt;
iptables &lt;span class="nt"&gt;-A&lt;/span&gt; OUTPUT &lt;span class="nt"&gt;-d&lt;/span&gt; 172.16.0.0/12 &lt;span class="nt"&gt;-j&lt;/span&gt; REJECT    &lt;span class="c"&gt;# Block private network (172.16-31.x.x)&lt;/span&gt;
iptables &lt;span class="nt"&gt;-A&lt;/span&gt; OUTPUT &lt;span class="nt"&gt;-d&lt;/span&gt; 192.168.0.0/16 &lt;span class="nt"&gt;-j&lt;/span&gt; REJECT   &lt;span class="c"&gt;# Block private network (192.168.x.x)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prevents your application servers from making requests to internal resources, even if XXE somehow succeeds. The parser tries to fetch &lt;code&gt;http://169.254.169.254&lt;/code&gt;, but the request is blocked at the network layer.&lt;/p&gt;

&lt;p&gt;Cloud security groups (AWS example):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Security Group configuration&lt;/span&gt;
&lt;span class="na"&gt;Outbound&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;Protocol&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;TCP&lt;/span&gt;
    &lt;span class="na"&gt;Port&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;443&lt;/span&gt;
    &lt;span class="na"&gt;Destination&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;0.0.0.0/0&lt;/span&gt;
    &lt;span class="na"&gt;Description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HTTPS&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;to&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;internet&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;only"&lt;/span&gt;

  &lt;span class="c1"&gt;# Explicitly deny internal ranges&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;Protocol&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ALL&lt;/span&gt;
    &lt;span class="na"&gt;Port&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ALL&lt;/span&gt;
    &lt;span class="na"&gt;Destination&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;10.0.0.0/8&lt;/span&gt;
    &lt;span class="na"&gt;Action&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;DENY&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cloud platforms let you define security groups that act as virtual firewalls. Configure your application servers to only allow outbound HTTPS to the public internet, not to internal services.&lt;/p&gt;

&lt;p&gt;IMDSv2 (Instance Metadata Service version 2) for AWS. AWS introduced IMDSv2 to make SSRF attacks harder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# IMDSv2 requires a session token&lt;/span&gt;
&lt;span class="c"&gt;# Step 1: Get a token (requires PUT request with headers)&lt;/span&gt;
&lt;span class="nv"&gt;TOKEN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; PUT &lt;span class="s2"&gt;"http://169.254.169.254/latest/api/token"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"X-aws-ec2-metadata-token-ttl-seconds: 21600"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# Step 2: Use the token to access metadata&lt;/span&gt;
curl &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"X-aws-ec2-metadata-token: &lt;/span&gt;&lt;span class="nv"&gt;$TOKEN&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  http://169.254.169.254/latest/meta-data/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;XXE attacks can't do PUT requests or set custom headers, so they can't get the token. Enable IMDSv2 on all EC2 instances:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws ec2 modify-instance-metadata-options &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--instance-id&lt;/span&gt; i-1234567890abcdef0 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--http-tokens&lt;/span&gt; required &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--http-put-response-hop-limit&lt;/span&gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Allowlists for legitimate external resources. If your application legitimately needs to fetch external XML, like RSS feeds, use an allowlist:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;ALLOWED_DOMAINS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;rss.example.com&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;feeds.partner.com&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;is_allowed_url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;urllib.parse&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;urlparse&lt;/span&gt;
    &lt;span class="n"&gt;domain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;urlparse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;netloc&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;domain&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;ALLOWED_DOMAINS&lt;/span&gt;

&lt;span class="c1"&gt;# In your XML entity resolver
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SecureEntityResolver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;etree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Resolver&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;is_allowed_url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="c1"&gt;# Log the attempt
&lt;/span&gt;            &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;warning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Blocked XXE attempt to: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="c1"&gt;# Return empty content
&lt;/span&gt;            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# Allow only specific resources
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;  &lt;span class="c1"&gt;# Use default resolution
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This provides defense in depth. Even if entities are enabled, only pre-approved domains can be accessed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing Your Application
&lt;/h2&gt;

&lt;p&gt;To verify your defenses work, test with actual XXE payloads in a safe environment. Never test on production.&lt;/p&gt;

&lt;p&gt;Create a test server to receive callbacks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Start a simple HTTP server to detect SSRF attempts&lt;/span&gt;
python3 &lt;span class="nt"&gt;-m&lt;/span&gt; http.server 8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Test file disclosure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST http://localhost:3000/api/parse &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/xml"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;!DOCTYPE test [&amp;lt;!ENTITY xxe SYSTEM "file:///etc/hostname"&amp;gt;]&amp;gt;&amp;lt;data&amp;gt;&amp;amp;xxe;&amp;lt;/data&amp;gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected result if properly secured: The application either rejects the document or returns &lt;code&gt;&amp;lt;data&amp;gt;&amp;amp;xxe;&amp;lt;/data&amp;gt;&lt;/code&gt; literally (entity not expanded).&lt;/p&gt;

&lt;p&gt;Vulnerable result: The application returns the contents of &lt;code&gt;/etc/hostname&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Test SSRF:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST http://localhost:3000/api/parse &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/xml"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;!DOCTYPE test [&amp;lt;!ENTITY xxe SYSTEM "http://your-test-server:8000/xxe-test"&amp;gt;]&amp;gt;&amp;lt;data&amp;gt;&amp;amp;xxe;&amp;lt;/data&amp;gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Watch your test server logs. If you see a request to &lt;code&gt;/xxe-test&lt;/code&gt;, your application is vulnerable the XML parser made an outbound HTTP request.&lt;/p&gt;

&lt;p&gt;Test denial of service, but be careful with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST http://localhost:3000/api/parse &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/xml"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;!DOCTYPE lolz [&amp;lt;!ENTITY lol "lol"&amp;gt;&amp;lt;!ENTITY lol2 "&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;&amp;amp;lol;"&amp;gt;&amp;lt;!ENTITY lol3 "&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;&amp;amp;lol2;"&amp;gt;]&amp;gt;&amp;lt;lolz&amp;gt;&amp;amp;lol3;&amp;lt;/lolz&amp;gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected result: The request is rejected or times out gracefully.&lt;/p&gt;

&lt;p&gt;Vulnerable result: The application hangs or crashes with out-of-memory errors.&lt;/p&gt;

&lt;p&gt;Automated testing with OWASP ZAP or Burp Suite. These tools have built-in XXE detection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# OWASP ZAP active scan includes XXE checks&lt;/span&gt;
docker run &lt;span class="nt"&gt;-t&lt;/span&gt; owasp/zap2docker-stable zap-baseline.py &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-t&lt;/span&gt; http://localhost:3000 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-r&lt;/span&gt; report.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Burp Suite Professional has XXE scanning in its active scanner. It will automatically test various payloads and entity expansion techniques.&lt;/p&gt;

&lt;h2&gt;
  
  
  When You Can't Disable XML
&lt;/h2&gt;

&lt;p&gt;Some scenarios require XML processing with external entities. Legacy system integrations where you don't control the format and the vendor requires DTD validation. Standards compliance where some industry specifications require DTD support. Content management systems where users expect to use XInclude for modular documents.&lt;/p&gt;

&lt;p&gt;These are rare, but they exist. In these cases, you need a different approach because completely disabling external entities breaks functionality.&lt;/p&gt;

&lt;p&gt;Use a separate, isolated service. Run XML processing in a dedicated service with no access to sensitive resources.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Bottom Line
&lt;/h2&gt;

&lt;p&gt;XXE exists because XML parsers are powerful by default and most developers don't know how to lock them down. The vulnerability isn't complex, it's a dangerous default configuration that's been around for decades.&lt;/p&gt;

&lt;p&gt;Disable external entities in your XML parser configuration. This is non-negotiable for any parser processing untrusted input. Validate input structure before parsing using XML schemas to enforce exactly what your application expects. Treat any XML from untrusted sources as dangerous, including API inputs, file uploads, data from partners, and anything users can control. For file uploads containing XML like SVG and Office documents, sanitize or avoid parsing entirely. Add network-level controls as defense in depth by blocking access to metadata services and internal networks from application servers.&lt;/p&gt;

&lt;p&gt;XXE is completely preventable. The vulnerability only exists when developers don't configure their parsers securely or don't realize they're processing XML. Test your applications, review your dependencies, and make secure XML parsing the default in your codebase.&lt;/p&gt;

</description>
      <category>xmlexternalentity</category>
      <category>sql</category>
      <category>xxe</category>
      <category>programming</category>
    </item>
    <item>
      <title>Top 10 Free APIs You Need to Use in Your Projects</title>
      <dc:creator>Iroro Chadere</dc:creator>
      <pubDate>Wed, 07 Aug 2024 12:40:53 +0000</pubDate>
      <link>https://dev.to/irorochad/top-10-free-apis-you-need-to-use-in-your-projects-42jk</link>
      <guid>https://dev.to/irorochad/top-10-free-apis-you-need-to-use-in-your-projects-42jk</guid>
      <description>&lt;p&gt;Finding reliable and free APIs can be a game-changer for your development projects. Here’s a list of the top 10 free APIs that you can integrate into your applications to enhance functionality and save time:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;OpenWeatherMap&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://openweathermap.org/api" rel="noopener noreferrer"&gt;OpenWeatherMap&lt;/a&gt; provides current weather data, forecasts, and historical data to millions of developers for free. It's perfect for any weather-related applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;NewsAPI&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://newsapi.org/" rel="noopener noreferrer"&gt;NewsAPI&lt;/a&gt; allows you to fetch the latest headlines from various news sources worldwide. It’s great for building news aggregators or keeping your users updated with current events.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Unsplash API&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://unsplash.com/developers" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt; offers high-quality, royalty-free images. Their API allows you to access a vast library of stunning photos, perfect for enhancing your website or app's visual appeal.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;OpenAI GPT-3&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://platform.openai.com/docs/overview" rel="noopener noreferrer"&gt;OpenAI's&lt;/a&gt; GPT-3 API provides access to advanced natural language processing capabilities. While it has a free tier, it’s highly versatile for building chatbots, content generation tools, and more.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;CoinGecko&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.coingecko.com/en/api" rel="noopener noreferrer"&gt;CoinGecko&lt;/a&gt; API provides comprehensive data on cryptocurrencies, including current prices, market data, and historical information. It’s ideal for any crypto-related projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. &lt;strong&gt;NASA API&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://api.nasa.gov/" rel="noopener noreferrer"&gt;NASA's&lt;/a&gt; API offers access to a treasure trove of data, including images, videos, and information about space missions. It’s a fantastic resource for educational and space-themed projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. &lt;strong&gt;The Cat API&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://thecatapi.com/" rel="noopener noreferrer"&gt;The Cat&lt;/a&gt; API provides random pictures of cats, perfect for adding a bit of fun to your application. It’s widely used in various projects for entertainment and light-hearted content.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. &lt;strong&gt;REST Countries&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://restcountries.com/" rel="noopener noreferrer"&gt;REST Countries&lt;/a&gt; API provides information about countries, including population, languages, and more. It’s useful for applications that need to display or utilize geographical data.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. &lt;strong&gt;JokeAPI&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://sv443.net/jokeapi/v2/" rel="noopener noreferrer"&gt;JokeAPI&lt;/a&gt; delivers a wide range of jokes in a simple, easy-to-use format. It’s perfect for adding humor to your projects or creating entertainment apps.&lt;/p&gt;

&lt;h3&gt;
  
  
  10. &lt;strong&gt;Open Library&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://openlibrary.org/developers/api" rel="noopener noreferrer"&gt;Open Library&lt;/a&gt; API allows access to a vast collection of book data. It’s excellent for building book-related applications, such as reading lists or book discovery tools.&lt;/p&gt;

&lt;p&gt;These free APIs can significantly enhance your projects by providing valuable data and functionality without additional costs. Explore them and see how they can add value to your next development endeavor!&lt;/p&gt;




&lt;p&gt;If you're building APIs, you'd likely want to test your APIs,document and maybe even want to Mock your APIs. With &lt;a href="https://www.apidog.com" rel="noopener noreferrer"&gt;Apidog&lt;/a&gt;, you can do all of those at ease. With its easy to use dashboard, and fast functionality, Apidog is a good choice to consider if you're building APIs.&lt;/p&gt;

</description>
      <category>api</category>
      <category>chatgpt</category>
      <category>opensource</category>
      <category>unsplash</category>
    </item>
    <item>
      <title>API Security Scanning Tools: Ensuring the Safety of Your APIs</title>
      <dc:creator>Iroro Chadere</dc:creator>
      <pubDate>Thu, 25 Jul 2024 21:05:38 +0000</pubDate>
      <link>https://dev.to/irorochad/api-security-scanning-tools-ensuring-the-safety-of-your-apis-3e8n</link>
      <guid>https://dev.to/irorochad/api-security-scanning-tools-ensuring-the-safety-of-your-apis-3e8n</guid>
      <description>&lt;p&gt;APIs (Application Programming Interfaces) are the invisible connectors powering modern web and mobile applications. They enable different systems to communicate and share data seamlessly, making them indispensable in today's software ecosystem. However, their pivotal role also makes them prime targets for cyberattacks. This article delves into the critical world of API security scanning tools, exploring their importance, key features, popular options, and best practices for implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why API Security is Crucial&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;APIs have become the backbone of modern software architectures, facilitating seamless integration and data exchange across various platforms. However, this widespread adoption has made APIs attractive targets for cybercriminals. Understanding the significance of API security involves recognizing the common threats and real-world implications of security breaches.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Injection Attacks&lt;/strong&gt;: Malicious actors exploit vulnerabilities to inject harmful code or commands into APIs. This can lead to unauthorized access, data corruption, or system compromise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Broken Authentication&lt;/strong&gt;: Weak or improperly implemented authentication mechanisms can be bypassed, allowing attackers to gain unauthorized access to sensitive data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Exposure&lt;/strong&gt;: Inadequate access controls and lack of data encryption can result in sensitive information being exposed to unauthorized parties.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rate Limiting Bypasses&lt;/strong&gt;: Attackers may overwhelm an API with excessive requests, causing denial of service (DoS) attacks that disrupt normal operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Real-world incidents, such as the &lt;a href="https://www.twingate.com/blog/tips/Facebook-data-breach" rel="noopener noreferrer"&gt;Facebook API data breach&lt;/a&gt; and the &lt;a href="https://www.traceable.ai/blog-post/t-mobile-api-data-breach-the-api-security-reckoning-is-here" rel="noopener noreferrer"&gt;T-Mobile API vulnerability&lt;/a&gt;, underscore the devastating impact of security lapses. These breaches compromised millions of user records, leading to significant financial and reputational damage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features of API Security Scanning Tools&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;API security scanning tools are designed to identify and mitigate vulnerabilities, ensuring the safety and integrity of your APIs. Key features of these tools include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Automated Vulnerability Detection&lt;/strong&gt;: Continuous scanning for known vulnerabilities, misconfigurations, and security flaws helps identify and address issues promptly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CI/CD Integration&lt;/strong&gt;: Seamless integration with continuous integration/continuous deployment pipelines ensures that security checks are an integral part of the development process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comprehensive Reporting and Analytics&lt;/strong&gt;: Detailed reports and analytics provide insights into vulnerabilities, helping developers understand and prioritize remediation efforts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Support for Various API Types&lt;/strong&gt;: These tools can scan REST, SOAP, GraphQL, and other API types, ensuring comprehensive security coverage.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Popular API Security Scanning Tools&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Several tools have established themselves as leaders in API security scanning. Here are some of the most popular options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://www.zaproxy.org/" rel="noopener noreferrer"&gt;OWASP ZAP&lt;/a&gt;&lt;/strong&gt;: An open-source tool known for its robust vulnerability scanning capabilities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://portswigger.net/burp" rel="noopener noreferrer"&gt;Burp Suite&lt;/a&gt;&lt;/strong&gt;: A comprehensive suite of tools for web application security testing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://apidog.com/" rel="noopener noreferrer"&gt;Apidog&lt;/a&gt;&lt;/strong&gt;: A versatile tool that offers API development, testing, and security scanning functionalities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://www.postman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt;&lt;/strong&gt;:Beyond its API development features, Postman offers security testing functionalities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://www.acunetix.com/" rel="noopener noreferrer"&gt;Acunetix&lt;/a&gt;&lt;/strong&gt;: A powerful web vulnerability scanner with extensive API scanning features.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://www.tenable.com/products/nessus" rel="noopener noreferrer"&gt;Nessus&lt;/a&gt;&lt;/strong&gt;: Known for its network vulnerability scanning, Nessus also supports API security testing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://www.apisec.ai/" rel="noopener noreferrer"&gt;APIsec&lt;/a&gt;&lt;/strong&gt;: A specialized tool focused on API security with advanced scanning and reporting features.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In-Depth Tool Reviews&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  OWASP ZAP
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;: The OWASP Zed Attack Proxy (ZAP) is a popular open-source security tool maintained by the Open Web Application Security Project (OWASP). It is designed to help developers and security professionals identify vulnerabilities in web applications and APIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automated scanners for finding common vulnerabilities.&lt;/li&gt;
&lt;li&gt;Passive and active scanning capabilities.&lt;/li&gt;
&lt;li&gt;Integration with CI/CD pipelines.&lt;/li&gt;
&lt;li&gt;Extensive community support and documentation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;: Ideal for developers and security teams looking for a robust, open-source solution to integrate into their development workflows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Free and open-source.&lt;/li&gt;
&lt;li&gt;Regular updates and a large community.&lt;/li&gt;
&lt;li&gt;Comprehensive scanning capabilities.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can be resource-intensive.&lt;/li&gt;
&lt;li&gt;Steeper learning curve for beginners.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Burp Suite
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;: Burp Suite is a comprehensive platform for performing security testing of web applications. Its advanced tools and features make it a favorite among security professionals.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Advanced scanning for various vulnerabilities.&lt;/li&gt;
&lt;li&gt;Intruder tool for automated customized attacks.&lt;/li&gt;
&lt;li&gt;Extensible through plugins and integrations.&lt;/li&gt;
&lt;li&gt;Detailed reporting and analysis.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;: Best suited for security professionals and penetration testers who need a powerful and flexible tool for in-depth security assessments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Highly customizable and extensible.&lt;/li&gt;
&lt;li&gt;Powerful suite of tools for various testing needs.&lt;/li&gt;
&lt;li&gt;Detailed and actionable reports.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High cost for the professional version.&lt;/li&gt;
&lt;li&gt;Requires expertise to fully utilize its capabilities.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Apidog
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.apidog.com%2Fstatic%2Fwww%2Fassets%2Fimages%2Fv2%2Fmain-interface-dark.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.apidog.com%2Fstatic%2Fwww%2Fassets%2Fimages%2Fv2%2Fmain-interface-dark.webp" title="Apidog Image" alt="Apidog's Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;: Apidog is a versatile tool that stands out for its comprehensive approach to API development, testing, and security. Designed with both developers and security professionals in mind, Apidog simplifies the process of creating and securing APIs, making it an invaluable asset in any API lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;API Design and Documentation&lt;/strong&gt;: Apidog offers intuitive tools for designing and documenting APIs, ensuring that your API specifications are clear and comprehensive. The platform supports OpenAPI and Swagger, allowing for seamless integration with other tools and services.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automated Security Scanning&lt;/strong&gt;: Apidog includes robust security scanning capabilities that automatically detect vulnerabilities in your APIs. It scans for common security issues such as SQL injection, cross-site scripting (XSS), and broken authentication, providing detailed reports and remediation guidance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mock Server&lt;/strong&gt;: The mock server functionality allows you to simulate API responses without needing a fully developed backend. This is particularly useful for testing and development purposes, enabling you to validate API behavior early in the development cycle.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testing and Debugging&lt;/strong&gt;: Apidog provides comprehensive testing tools, including automated test generation, test scripts, and debugging features. These tools help ensure that your APIs function correctly and securely before deployment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CI/CD Integration&lt;/strong&gt;: Apidog seamlessly integrates with popular CI/CD pipelines, enabling continuous security checks and automated testing as part of your development workflow. This integration ensures that security is a continuous process rather than an afterthought.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User-Friendly Interface&lt;/strong&gt;: With its clean and intuitive interface, Apidog makes it easy for both beginners and experienced professionals to navigate and utilize its features effectively. Detailed documentation and tutorials further enhance the user experience.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;API Development Teams&lt;/strong&gt;: Apidog is ideal for development teams that need a unified platform for designing, testing, and securing APIs. Its comprehensive feature set supports the entire API lifecycle, from conception to deployment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security Professionals&lt;/strong&gt;: For security teams, Apidog provides advanced scanning and reporting tools that help identify and mitigate vulnerabilities, ensuring that APIs are secure before they go live.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DevOps Engineers&lt;/strong&gt;: The CI/CD integration makes Apidog a valuable tool for DevOps engineers looking to incorporate security checks into their automated pipelines, ensuring continuous security monitoring and compliance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;All-in-One Solution&lt;/strong&gt;: Combines API design, testing, and security in a single platform, reducing the need for multiple tools.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ease of Use&lt;/strong&gt;: User-friendly interface and detailed documentation make it accessible to users of all skill levels.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comprehensive Security Features&lt;/strong&gt;: Robust security scanning and detailed reporting help ensure that APIs are secure and compliant.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Suitable for projects of all sizes, from small startups to large enterprises.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Newer Tool&lt;/strong&gt;: As a relatively new tool in the market, Apidog may still be evolving, and users might encounter occasional bugs or missing features compared to more established tools.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Advanced Features&lt;/strong&gt;: While it covers a broad range of functionalities, some highly specialized or advanced security features might not be as developed as those in dedicated security tools like Burp Suite or Nessus.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By integrating &lt;a href="https://apidog.com/" rel="noopener noreferrer"&gt;Apidog&lt;/a&gt; into your API development workflow, you can streamline the process of designing, testing, and securing your APIs. Its comprehensive feature set and user-friendly design make it an excellent choice for teams looking to enhance their API security without sacrificing efficiency or ease of use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Postman
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.postman.com%2F_wp-assets%2Fhome%2Fhomepage-hero-light_1260w.21bd14bd629f14c1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.postman.com%2F_wp-assets%2Fhome%2Fhomepage-hero-light_1260w.21bd14bd629f14c1.png" title="Postman Image" alt="Postman"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;: Postman is widely known for its API development and testing capabilities. It also offers security testing features that help identify and mitigate potential vulnerabilities in APIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automated testing and monitoring.&lt;/li&gt;
&lt;li&gt;Security scanning and vulnerability detection.&lt;/li&gt;
&lt;li&gt;Collaboration features for teams.&lt;/li&gt;
&lt;li&gt;Detailed reporting and analytics.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;: Ideal for development teams looking to integrate API security testing into their existing Postman workflows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Well-established and widely used tool.&lt;/li&gt;
&lt;li&gt;Comprehensive API testing and security features.&lt;/li&gt;
&lt;li&gt;Strong community and support.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Security features are secondary to its primary development functions.&lt;/li&gt;
&lt;li&gt;Can become complex with large-scale API projects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Acunetix
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.acunetix.com%2Fwp-content%2Fuploads%2F2023%2F11%2F21213807%2Fresolve-ab-2023.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.acunetix.com%2Fwp-content%2Fuploads%2F2023%2F11%2F21213807%2Fresolve-ab-2023.png" title="Acunetix Image" alt="Acunetix"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;: Acunetix is a powerful web vulnerability scanner with extensive features for API security testing. It offers automated scanning and detailed reporting to help secure APIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automated vulnerability scanning for web applications and APIs.&lt;/li&gt;
&lt;li&gt;Detailed reports with remediation advice.&lt;/li&gt;
&lt;li&gt;Integration with popular CI/CD tools.&lt;/li&gt;
&lt;li&gt;Support for various API types.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;: Suitable for organizations needing a robust, automated solution for continuous API security monitoring.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extensive scanning capabilities.&lt;/li&gt;
&lt;li&gt;Detailed and actionable reports.&lt;/li&gt;
&lt;li&gt;Strong support and regular updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High cost for enterprise use.&lt;/li&gt;
&lt;li&gt;Can be resource-intensive.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Nessus
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;: Nessus is a well-known vulnerability scanner with capabilities to test network infrastructures and APIs. It helps identify security issues across a wide range of environments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Comprehensive vulnerability scanning.&lt;/li&gt;
&lt;li&gt;Integration with CI/CD pipelines.&lt;/li&gt;
&lt;li&gt;Detailed reporting and remediation guidance.&lt;/li&gt;
&lt;li&gt;Support for various environments and API types.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;: Best suited for organizations needing a comprehensive vulnerability management tool that includes API security testing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extensive vulnerability database.&lt;/li&gt;
&lt;li&gt;Detailed and actionable reporting.&lt;/li&gt;
&lt;li&gt;Strong support and regular updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High cost for the professional version.&lt;/li&gt;
&lt;li&gt;Requires expertise to fully utilize its capabilities.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  APIsec
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;: APIsec is a specialized tool focused on API security. It provides advanced scanning and reporting features to help identify and mitigate vulnerabilities in APIs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automated security testing for APIs.&lt;/li&gt;
&lt;li&gt;Detailed reporting and analytics.&lt;/li&gt;
&lt;li&gt;Integration with CI/CD pipelines.&lt;/li&gt;
&lt;li&gt;Support for various API types.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use Cases&lt;/strong&gt;: Ideal for organizations looking for a specialized tool dedicated to API security.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Specialized focus on API security.&lt;/li&gt;
&lt;li&gt;Advanced scanning and reporting capabilities.&lt;/li&gt;
&lt;li&gt;Easy integration with existing workflows.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Higher cost compared to general vulnerability scanners.&lt;/li&gt;
&lt;li&gt;May require expertise to fully leverage its features.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Implementing API Security Scanning in Your Workflow&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Integrating API security scanning into your development workflow is crucial for maintaining the security and integrity of your APIs. Here are some best practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CI/CD Integration&lt;/strong&gt;: Ensure that security scans are part of your CI/CD pipelines. This allows for continuous security checks and immediate detection of vulnerabilities during the development process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Regular Scans&lt;/strong&gt;: Schedule regular security scans to keep your APIs protected against new vulnerabilities and emerging threats.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Analyze and Act on Scan Results&lt;/strong&gt;: Review scan reports thoroughly and prioritize remediation efforts based on the severity of identified vulnerabilities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Collaboration and Documentation&lt;/strong&gt;: Encourage collaboration between development and security teams. Maintain comprehensive documentation of your security processes and scan results.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best Practices for API Security&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Beyond using security scanning tools, adopting best practices for API security is essential. Here are some recommendations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Secure Authentication and Authorization&lt;/strong&gt;: Implement strong authentication mechanisms (e.g., OAuth, JWT) and enforce proper authorization checks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Input Validation and Sanitization&lt;/strong&gt;: Validate and sanitize all inputs to prevent injection attacks and other vulnerabilities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rate Limiting and Throttling&lt;/strong&gt;: Implement rate limiting to protect your APIs from abuse and DoS attacks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Regular Security Audits and Updates&lt;/strong&gt;: Conduct regular security audits and keep your software and dependencies up to date to address known vulnerabilities.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;API security scanning tools are indispensable in the modern software development landscape. They help identify and mitigate vulnerabilities, ensuring the safety and integrity of your APIs. By integrating these tools into your development workflows and adhering to best practices, you can protect your applications and data from potential threats. For further reading and resources, consider exploring documentation and community forums&lt;/p&gt;

</description>
      <category>scanningscanning</category>
      <category>apisecurity</category>
      <category>apiscanningtools</category>
      <category>apidog</category>
    </item>
    <item>
      <title>My new Blog design</title>
      <dc:creator>Iroro Chadere</dc:creator>
      <pubDate>Fri, 27 Oct 2023 14:10:22 +0000</pubDate>
      <link>https://dev.to/irorochad/my-new-blog-design-35k0</link>
      <guid>https://dev.to/irorochad/my-new-blog-design-35k0</guid>
      <description>&lt;p&gt;Hey guys! I'm so happy to finally announce that my side blog project that I've been working on is finally out - and I'm soo loving it!&lt;/p&gt;

&lt;p&gt;BrightsideCodes took a lot of time to make due to many reasons and a lack of motivation. But today, I'm happy to finally release it. &lt;br&gt;
You can visit &lt;a href="https://www.brightsidecodes.com/blog/welcome-to-brightsidecodes-bytes-of-wisdom-one-click"&gt;brightsidecodes.com&lt;/a&gt; and learn more about it.&lt;/p&gt;

&lt;p&gt;if you're wondering if I'd still be here, the simple answer is yes. I'll still be here posting content.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>blog</category>
    </item>
    <item>
      <title>Server-side Rendering (SSR) vs. Client-side Rendering (CSR) in React</title>
      <dc:creator>Iroro Chadere</dc:creator>
      <pubDate>Wed, 30 Aug 2023 10:07:01 +0000</pubDate>
      <link>https://dev.to/irorochad/server-side-rendering-ssr-vs-client-side-rendering-csr-in-react-3d24</link>
      <guid>https://dev.to/irorochad/server-side-rendering-ssr-vs-client-side-rendering-csr-in-react-3d24</guid>
      <description>&lt;p&gt;In the world of modern web development, choosing the right rendering approach for your React applications is crucial. Server-side Rendering (SSR) and Client-side Rendering (CSR) are two prominent methods for delivering content to users. Each approach has its own set of advantages, disadvantages, and use cases. In this article, we'll dive deep into the differences between SSR and CSR in React, exploring their benefits, drawbacks, and best practices, including how to fetch data in each approach.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Before delving into the specifics, let's understand the fundamental concepts of SSR and CSR. Both techniques relate to how the content of a web page is generated and delivered to the user's browser.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Server-side Rendering (SSR): With SSR, the initial HTML is generated on the server and sent to the client's browser. This means that the user receives a fully-rendered page right from the start, which can improve perceived loading speed and search engine optimization (SEO).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Client-side Rendering (CSR): In CSR, the initial HTML is minimal, often containing only a loading script. The majority of the content is generated on the client side using JavaScript. This approach allows for dynamic content updates without full-page reloads, resulting in a smoother user experience.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Differences
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Initial Page Load
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SSR&lt;/strong&gt;: When a user requests a page, the server generates the complete HTML for that page, including data fetched from APIs or databases. This pre-rendered HTML is sent to the client's browser, providing a fast initial loading experience.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CSR&lt;/strong&gt;: The initial HTML sent to the browser is minimal and includes JavaScript bundles. The page's content is fetched and rendered on the client side after JavaScript execution. This may lead to a slower initial load, especially on slower devices or connections.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  SEO and Social Sharing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SSR&lt;/strong&gt;: Search engines can easily crawl and index the content, as it's available in the initial HTML response. This can lead to better SEO and improved social sharing previews.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CSR&lt;/strong&gt;: Search engines may have difficulties indexing dynamic content that is generated on the client side. Special techniques like server-side rendering for specific routes are often required to ensure proper indexing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Performance
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SSR&lt;/strong&gt;: The user gets a fully-rendered page on the first load, which can result in a faster perceived performance. However, subsequent interactions might involve more round trips to the server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CSR&lt;/strong&gt;: While the initial load might be slower, subsequent interactions within the app can be faster, as only the necessary components are updated without full-page reloads.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Fetching Data in SSR
&lt;/h2&gt;

&lt;p&gt;In SSR, fetching data is often done on the server side, as part of the initial rendering process. This data can then be included in the pre-rendered HTML that is sent to the client. Here's a simplified example of how you might fetch data in an SSR setup using React and Next.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// pages/index.js (Next.js page)
import React from 'react';

function HomePage({ data }) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;{data.title}&amp;lt;/h1&amp;gt;
      &amp;lt;p&amp;gt;{data.description}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export async function getServerSideProps() {
  const response = await fetch('https://api.example.com/data');
  const data = await response.json();

  return {
    props: {
      data,
    },
  };
}

export default HomePage;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the getServerSideProps function is called on the server side before rendering the page. It fetches data from an API and includes it in the props that are passed to the HomePage component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fetching Data in CSR
&lt;/h2&gt;

&lt;p&gt;In CSR, data fetching typically happens on the client side, often triggered by user interactions or component lifecycle events. This allows for dynamic content updates without requiring a full page reload. Here's a basic example of data fetching in a CSR scenario using React's useEffect hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// components/PostList.js
import React, { useState, useEffect } from 'react';

function PostList() {
  const [posts, setPosts] = useState([]);

  useEffect(() =&amp;gt; {
    fetch('https://api.example.com/posts')
      .then(response =&amp;gt; response.json())
      .then(data =&amp;gt; setPosts(data));
  }, []);

  return (
    &amp;lt;ul&amp;gt;
      {posts.map(post =&amp;gt; (
        &amp;lt;li key={post.id}&amp;gt;{post.title}&amp;lt;/li&amp;gt;
      ))}
    &amp;lt;/ul&amp;gt;
  );
}

export default PostList;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the useEffect hook is used to fetch a list of posts from an API after the component mounts. The fetched data is then used to update the component's state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding how to fetch data in both Server-side Rendering (SSR) and Client-side Rendering (CSR) scenarios is crucial for building robust and performant React applications. SSR involves fetching data on the server side during the initial rendering, while CSR involves fetching data on the client side as part of user interactions or component lifecycle events. By choosing the appropriate approach based on your application's needs, you can ensure a smooth and efficient user experience.&lt;/p&gt;

</description>
      <category>react</category>
      <category>ssr</category>
      <category>csr</category>
    </item>
    <item>
      <title>How to Fix: Generating static pages (0/8)TypeError: Cannot read properties of undefined (reading 'data')</title>
      <dc:creator>Iroro Chadere</dc:creator>
      <pubDate>Sun, 09 Jul 2023 11:03:28 +0000</pubDate>
      <link>https://dev.to/irorochad/how-to-fix-generating-static-pages-08typeerror-cannot-read-properties-of-undefined-reading-data-f9p</link>
      <guid>https://dev.to/irorochad/how-to-fix-generating-static-pages-08typeerror-cannot-read-properties-of-undefined-reading-data-f9p</guid>
      <description>&lt;p&gt;When generating static pages in web development, developers often encounter errors that can hinder progress. One common error is the "TypeError: Cannot read properties of undefined (reading 'data')." This article aims to provide a comprehensive understanding of this error and offer effective solutions to resolve it. Through a real-life scenario and step-by-step guidance, we will explore the causes of this error and demonstrate how to fix it.&lt;/p&gt;

&lt;p&gt;A few days ago, after I finished developing a side project I was working on, I pushed over to GitHub which is connected to Vercel. However, when Vercel started to run &lt;code&gt;yarn build&lt;/code&gt;, I got an error:  &lt;code&gt;Generating static pages (0/8)TypeError: Cannot read properties of undefined (reading 'data')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What could be the problem? I asked myself. Do you know what happened next? I started to google.&lt;/p&gt;

&lt;p&gt;Sadly, I didn't see enough material that addressed the issue, I was tired, I really wanted to build the application so I can see it live!&lt;/p&gt;

&lt;p&gt;But hold on, why is the app working on my local machine, but not working when I run &lt;code&gt;yarn build&lt;/code&gt;?&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Error:
&lt;/h2&gt;

&lt;p&gt;The "TypeError: Cannot read properties of undefined (reading 'data')" error message typically occurs when a variable or object is accessed without being properly defined or initialized. In the context of generating static pages, this error commonly arises when there is an issue with the data being used or accessed during the page generation process.&lt;/p&gt;

&lt;p&gt;Sound confusing? Don't be. Let's break it down.&lt;/p&gt;

&lt;p&gt;In my case, I was trying to build a blog, and I have &lt;code&gt;blog/[slug]&lt;/code&gt; page.&lt;/p&gt;

&lt;p&gt;The error is always shown in the &lt;code&gt;slug&lt;/code&gt; page, and that's because I was using &lt;code&gt;getStaticPaths&lt;/code&gt; and I don't want all the blog posts to be rendered when the page is first rendered, after all, that'd slow down the page rendering and increase the loading time. I don't want that!&lt;/p&gt;

&lt;p&gt;To make sure all the page static blog posts don't render when the page first loads, I did the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export async function getStaticPaths() {
  const client = createClient();

  const allPosts = await client.getAllByType('post');

  return {
    paths: allPosts.map((post) =&amp;gt; post.url),
    fallback: true,
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where the error is, I asked the &lt;code&gt;getStaticPaths&lt;/code&gt; to have a &lt;code&gt;fallback:true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Well, this is a good practice, I don't want to fetch all the blog posts from the server before the page loads, that will slow down the page!&lt;/p&gt;

&lt;p&gt;But since fallback is true, I need to tell nextJs that &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;hey buddy, some of my blog posts are not fetched yet, so if the visitor should navigate to &lt;code&gt;/blog/not-yet-found-post&lt;/code&gt;, don't return an error page immediately. Instead, try to run &lt;code&gt;yarn build&lt;/code&gt; again to see if the post is on the server, if yes, render the post content, else return a 404.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Do You get the point now? The main issue in my case, I didn't ask Nextjs to first run &lt;code&gt;yarn build&lt;/code&gt; if a visitor should navigate to &lt;code&gt;/blog/not-yet-found-post&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://nextjs.org/docs/pages/api-reference/functions/get-static-paths#fallback-pages"&gt;next.js&lt;/a&gt; to resolve the issue is simply to &lt;code&gt;import { useRouter } from 'next/router';&lt;/code&gt; and use it to check if the page is falling back like so;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useRouter } from 'next/router'

function Post({ post }) {
  const router = useRouter()

  // If the page is not yet generated, this will be displayed
  // initially until getStaticProps() finishes running
  if (router.isFallback) {
    return &amp;lt;div&amp;gt;Loading...&amp;lt;/div&amp;gt;
  }

  // Render post...
}

// This function gets called at build time
export async function getStaticPaths() {
  return {
    // Only `/posts/1` and `/posts/2` are generated at build time
    paths: [{ params: { id: '1' } }, { params: { id: '2' } }],
    // Enable statically generating additional pages
    // For example: `/posts/3`
    fallback: true,
  }
}

// This also gets called at build time
export async function getStaticProps({ params }) {
  // params contains the post `id`.
  // If the route is like /posts/1, then params.id is 1
  const res = await fetch(`https://.../posts/${params.id}`)
  const post = await res.json()

  // Pass post data to the page via props
  return {
    props: { post },
    // Re-generate the post at most once per second
    // if a request comes in
    revalidate: 1,
  }
}

export default Post
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so by adding router.isFallback, I was able to fix the issue by first asking nextjs to in the background build the page, fetch the post the user is trying to access, and show it to them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;When generating static pages, encountering errors like "TypeError: Cannot read properties of undefined (reading 'data')" is not uncommon. However, with a systematic approach to understanding the error's origin and implementing the appropriate solutions, developers can overcome this issue effectively.&lt;br&gt;
I hope that this blog post has helped you to understand and fix the error you're having Generating static pages (0/8)TypeError: Cannot read properties of undefined (reading 'data').&lt;/p&gt;

&lt;p&gt;Let me know in the comments if it did help you, or you have any questions.&lt;/p&gt;

&lt;p&gt;All the best!&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>typescript</category>
      <category>react</category>
      <category>typeerro</category>
    </item>
  </channel>
</rss>
