
Prevent XXE by disabling external entity resolution in your XML parser libraries. Enforce strict input validation and use JSON instead of XML where possible. Never trust user-supplied XML data, and ensure your application code explicitly forbids the inclusion of external resources during parsing operations.
Understanding the Attack Surface
XML external entity attacks exploit the way parsers process external references within XML documents. An attacker injects a malicious entity definition that references local files or remote servers. The parser then retrieves and includes this content, potentially exposing sensitive data or causing server-side request forgery.
You must understand that the vulnerability lies in the parser configuration, not necessarily in the application logic. The parser decides whether to follow external references. If it follows them, the application becomes vulnerable regardless of other security measures. This mechanism allows attackers to read arbitrary files from the server filesystem. It also enables blind attacks where the attacker cannot see the response directly.

Parser Configuration Hardening
This section covers the technical settings required to neutralise the threat at the source. Most modern languages provide libraries for parsing XML data. These libraries often include features for resolving external entities by default. You must change these defaults before your application goes into production. Relying on application-level validation is insufficient because the parser processes the document before your code can inspect it.
- Disable external entity resolution: This prevents the parser from fetching any external resources defined in the XML document.
- Disable DTD processing entirely: Document type definitions allow attackers to redefine entities, so turning this off removes the attack vector.
- Use secure parser libraries: Newer versions of parsing libraries often have stricter defaults than older versions you might be using.
- Validate library versions regularly: Older parser versions may contain known vulnerabilities that allow bypassing these configuration settings.
Input Validation and Sanitisation
Even with secure parser settings, you should validate input to maintain defence in depth. Input validation ensures that the data entering your system matches expected formats and structures. This reduces the risk of other injection attacks alongside XXE. You must treat all XML input as untrusted data. Do not assume that internal users or trusted partners will send clean data. Compromised accounts can send malicious payloads just as easily as external attackers.
- Enforce strict schema validation: Define exactly what elements and attributes are allowed in your XML input.
- Reject documents with DOCTYPE declarations: Most legitimate XML inputs do not require document type definitions, so blocking them is safe.
- Limit input size: Large XML documents can cause denial of service, so setting size limits protects your server resources.
- Validate against a whitelist: Only allow known good values for specific fields rather than trying to block known bad patterns.
Architectural Mitigations
Changing how your application handles data can eliminate the need for XML parsing altogether. Many modern applications use JSON for data exchange. JSON does not support external entities or document type definitions. This makes it inherently immune to XXE attacks. If your application does not require the specific features of XML, such as complex schema validation or namespace support, switching to JSON is the most effective long-term solution.
- Migrate APIs to JSON format: JSON parsers do not process external entities, removing the XXE attack surface completely.
- Use internal data formats: Avoid exposing raw XML parsing capabilities to external users through web interfaces.
- Implement strict access controls: Limit which services can initiate XML parsing requests to reduce the potential impact of a breach.
- Monitor for unexpected outbound connections: XXE attacks often attempt to exfiltrate data to external servers, so network monitoring can detect this activity.
Testing and Verification
You must verify that your defences work in practice. Static analysis tools can identify insecure parser configurations in your codebase. Dynamic testing can attempt to exploit these configurations to confirm they are properly locked down. Regular testing ensures that new dependencies or code changes have not reintroduced the vulnerability.
- Run dynamic application security testing (DAST): Automated scanners can attempt XXE payloads against your running application to check for exposure.
- Perform manual penetration testing: Human testers can craft complex XXE payloads that automated tools might miss or fail to exploit correctly.
- Review dependency updates: New versions of parsing libraries may change default security settings, requiring you to re-verify your configuration.
- Audit legacy code paths: Older parts of your application may still use insecure parsing methods that were not updated during recent refactoring.
See also: Prevent NoSQL Injection: Validation, Operators and Query Builders · Command Injection Explained: How Code Runs Outside Its Box
Operational Maintenance
Security is an ongoing process, not a one-time configuration. You must maintain your defences as your application evolves. New features may introduce new XML parsing requirements. Third-party libraries may receive updates that affect security defaults. Regular reviews ensure that your XXE protections remain effective over time.
- Establish a review cycle: Schedule periodic reviews of XML handling code and parser configurations to catch drift.
- Train developers on secure parsing: Ensure your team understands why external entities are dangerous and how to disable them.
- Monitor security advisories: Stay informed about new XXE variants or bypass techniques that may emerge in the security community.
- Document secure defaults: Create internal documentation that specifies the required parser settings for all new projects.
Key takeaways
- Disabling external entities at the parser level provides the strongest defence against data exfiltration.
- Input validation fails if attackers can manipulate the structure of the XML document itself.
- Replacing XML with JSON eliminates XXE risks entirely for new application development projects.
Disabling external entity processing at the parser level is the only reliable defence against XXE attacks. Review your current parser configurations immediately to ensure they block all external references.
Frequently asked questions
Can input validation alone prevent XXE attacks?
No, input validation often fails because the parser processes external entities before your application code can validate the content. You must disable entity resolution at the parser level.
Is JSON completely safe from injection attacks?
JSON is immune to XXE specifically, but it remains vulnerable to other injection types if input is not properly validated and sanitised before use.
Do I need to worry about XXE if I only use internal XML?
Yes, if an attacker can influence the XML content through any input field, they can inject malicious entities regardless of the data source.
How do I know if my parser library is vulnerable?
Check the documentation for your specific library version to see if it supports disabling DTD and external entity processing. Older versions often lack these options.
How this guide was produced: written by the Cipher Insider editorial team with AI assistance, checked against the public references listed below, and reviewed when the facts change. See our editorial policy or report an error.



