How to prevent XXE: parser hardening by language
The single most effective XXE fix is to disable DOCTYPE and external entity resolution in your XML parser. These guides give the exact, copy-pasteable configuration for the common parsers in each ecosystem, plus the mistakes that quietly re-introduce the vulnerability.
To disable external entities in .NET and prevent XXE, rely on the safe defaults of modern XmlReader (DtdProcessing.Prohibit) and set XmlResolver to null on XmlDocument; the main risk is legacy code using XmlTextReader or explicitly re-enabling DTD processing.
Go encoding/xml XXE attacks do not work against the standard library: encoding/xml tokenises directives but never resolves external entities or loads DTDs, so file-read and SSRF via XXE are not possible — the caveats are third-party cgo bindings to libxml and basic entity-expansion limits.
To disable external entities in Java and prevent XXE, configure each XML factory before you parse: the single strongest setting is the Xerces feature disallow-doctype-decl, which rejects any document containing a DOCTYPE and so eliminates both file-read and billion-laughs attacks in one line.
Node.js XXE prevention is mostly about parser choice: the popular pure-JavaScript parsers (fast-xml-parser, xml2js) do not process DTDs or external entities at all, so they are safe by default — the real exposure is libxmljs, a binding to libxml2, when it is told to load DTDs or substitute entities.
To prevent XXE in PHP you mostly need to avoid re-enabling a danger that is off by default: since libxml 2.9.0, external entity substitution is disabled, so DOMDocument and SimpleXML are safe unless you explicitly pass LIBXML_NOENT or LIBXML_DTDLOAD when loading untrusted XML.
To prevent XXE in Python, use the defusedxml package in place of the standard-library XML modules — it rejects DTDs and entities outright — and when you need lxml, build an XMLParser with resolve_entities=False, no_network=True and load_dtd=False before parsing untrusted input.
To prevent XXE in Ruby, lean on Nokogiri's safe defaults: it parses without substituting external entities and without network access unless you explicitly add the NOENT, DTDLOAD or NONET-clearing options — so the rule is simply not to enable them on untrusted XML.