Xml serialization security

From RedHatProductSecurity/prodsec-skills · View interactive version

Xml serialization security is an AI agent skill from the GitHub repository RedHatProductSecurity/prodsec-skills. Apply when reviewing or writing code that parses XML, processes DTDs, transforms XSLT, or deserializes data from untrusted sources. Covers XXE prevention, entity expansion, parser hardening, and safe deserialization per language.

How to use this skill

Skill file

XML and Serialization Hardening

Secure parsing and processing of XML and serialized data. Prevent XXE, entity expansion, SSRF, DoS, and unsafe deserialization across platforms.

XML Parser Hardening

Java

Java parsers have XXE enabled by default. Primary defense -- disallow DTDs completely:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setXIncludeAware(false);

If DTDs cannot be completely disabled, disable external entities individually:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
String[] featuresToDisable = {
    "http://xml.org/sax/features/external-general-entities",
    "http://xml.org/sax/features/external-parameter-entities",
    "http://apache.org/xml/features/nonvalidating/load-external-dtd"
};
for (String feature : featuresToDisable) {
    dbf.setFeature(feature, false);
}
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

.NET

var settings = new XmlReaderSettings {
    DtdProcessing = DtdProcessing.Prohibit,
    XmlResolver = null
};
var reader = XmlReader.Create(stream, settings);

Python

# Option 1: defusedxml (preferred)
from defusedxml import ElementTree as ET
ET.parse('file.xml')

# Option 2: lxml with safe settings
from lxml import etree
parser = etree.XMLParser(resolve_entities=False, no_network=True)
tree = etree.parse('filename.xml', parser)

Secure XSLT / Transformer Usage

Deserialization Safety

General rule: never deserialize untrusted native objects. Prefer JSON with schema validation. Enforce size/structure limits before parsing. Reject polymorphic types unless strictly allow-listed.

Per-Language Guidance

| Language | Dangerous | Safe Alternative |

|----------|-----------|-----------------|

| PHP | unserialize() | json_decode() |

| Python | pickle, yaml.load() | json.loads(), yaml.safe_load() |

| Java | Default ObjectInputStream, Jackson default typing, XStream without allow-lists | Override resolveClass with allow-list; disable default typing; use XStream allow-lists |

| .NET | BinaryFormatter | DataContractSerializer, System.Text.Json with TypeNameHandling=None |

| Ruby | Marshal.load from untrusted input | JSON.parse() |

Additional Controls

Implementation Checklist

Test Plan

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.