Skip to content
Skills Library
Security · Review & audit

xml-serialization-security

/xml-serialization-security

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.

Licence
Apache-2.0
Last repository change
15.07.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
6
Deprecation

No signals detected. Repository changed on 15.07.2026, without references to retired models or APIs.

Licence

Apache-2.0, otwarta: można używać i zmieniać, z zachowaniem licencji i informacji o zmianach.

What it does and when to use it

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.

Original author description

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 it works

  1. XML Parser Hardening
  2. Secure XSLT / Transformer Usage
  3. Deserialization Safety
  4. Implementation Checklist
  5. Test Plan
Original SKILL.md headings · 103 lines, 457 words
SKILL.md preview
---
name: xml-serialization-security
description: >
  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.
license: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
origin: Adapted from CoSAI Project CodeGuard (https://github.com/cosai-oasis/project-codeguard)
category: "secure_development"
subcategory: "web-security"
---

# 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

- Disable DTDs and external entities by default; reject DOCTYPE declarations.
- Validate strictly against local, trusted XSDs; set explicit limits (size, depth, element counts).
- Sandbox or block resolver access; no network fetches during parsing; monitor for unexpected DNS activity.

### Java

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

```java
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:

```java
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

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

### Python

```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

- Set `ACCESS_EXTERNAL_DTD` and `ACCESS_EXTERNAL_STYLESHEET` to empty strings.
- Avoid loading remote resources during transformation.

## 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

- Sign and verify serialized payloads where applicable.
- Log and alert on deserialization failures and anomalies.
- Keep all parsing and deserialization libraries up to date.

## Implementation Checklist

- [ ] DTDs disabled; external entities off; strict schema validation; parser limits set
- [ ] No network access during XML parsing; resolvers restricted
- [ ] XSLT transformers block external DTD and stylesheet access
- [ ] No unsafe native deserialization (`pickle`, `unserialize`, `BinaryFormatter`, `Marshal.load`)
- [ ] JSON preferred over native serialization for untrusted data
- [ ] Allow-lists enforced for any remaining native deserialization
- [ ] Regular testing with XXE and deserialization payloads

## Test Plan

- Automated checks for dangerous parser configurations and deserialization calls.
- Test with XXE payloads (external entity, parameter entity, Billion Laughs).
- Verify XSLT resolvers block external resources.
- Fuzz deserialization endpoints with malformed and oversized payloads.

Versions

The database has no other skills named xml-serialization-security.

Database snapshot: 13.11.2026 · package version c2f314233cf8 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100xml-external-entity-xxe-parser-hardening
    Use this skill when disable external DTDs and entity resolution in XML parsers to prevent XXE vulnerabilities.
  • 90java-deserialization-and-parser-security
    Use this skill when statically reviewing the JVM's untrusted-deserialization and data-parsing surface for remote-code-execution and injection risk — Java native ObjectInputStream gadget chains (and the ObjectInputFilter/JEP 290 control), SnakeYAML bare Constructor, Jackson…
  • 100xxe-prevention
    OWASP A04 - XML External Entity (XXE) Prevention. Use this skill when parsing XML, processing SOAP requests, handling SVG uploads, or working with XML-based formats. Activate when: XML parsing, SOAP, SVG upload, XML input, DOCTYPE, DTD, external entity, XML bomb, billion laughs, XSLT.
  • 100security-hardening
    Use when adding security controls for untrusted input, auth/authz, data storage, or external integrations. Not for audit: use security-review. Not for finding: use security-finding-verification.
  • 100skill-security-framing
    URL validation and content sanitization for untrusted sources — use when handling external input safely
  • 100security-and-trust-boundaries
    Use when writing or reviewing code that parses user input, builds SQL/shell commands, handles secrets/credentials, hashes passwords, changes auth checks, deserializes untrusted data, or constructs paths/URLs from input

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.