Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

A security scan shows that several services parse XML from partners with default parser settings. Explain the risk with JAXB and how to fix it across the company.

With default settings, an XML parser may process DTDs and resolve external entities, which allows XXE attacks: a partner's document can make our service read local files, call internal URLs (SSRF) or exhaust memory through entity expansion. We fix it by unmarshalling only from a hardened XMLStreamReader or SAXSource with DTDs and external entities disabled, external DTD and schema access set to empty, and secure processing limits on. Company-wide, we ship one shared hardened parsing helper, add a static analysis rule that flags raw unmarshal(InputStream), and test with known XXE payloads in CI.

Why this question is asked

XML is still the format of many partner integrations: payment files, insurance claims, logistics messages. Those documents come from outside our trust boundary, and XML has a feature most developers never use on purpose: the document can define entities, and an entity can point to a file or a URL. A security scan that flags default parser settings is pointing at a real, well-known class of vulnerability, listed among the most common web application risks for years. Interviewers ask this as a scenario because the fix is not only a line of code; it is a change across many services. In this answer, we will see what XXE is and how it works with JAXB, how to harden the parser, how to roll the fix out across the company, and how to prove it in CI.

1A Quick Recap of XML Entities and XXE

An XML document may start with a DTD, a Document Type Definition, which can declare entities. An entity is a named piece of text that the parser substitutes wherever &name; appears. An external entity takes its text from a URI, such as file:///etc/passwd or http://internal-host/admin. XXE, XML External Entity injection, is what happens when an attacker sends a document whose entities make our parser read things the attacker should not see.

XML
<?xml version="1.0"?>
<!DOCTYPE claim [
<!ENTITY secret SYSTEM "file:///etc/passwd">
]>
<claim><note>&secret;</note></claim>
Claim c = (Claim) unmarshaller.unmarshal(request.getInputStream());
System.out.println(c.getNote()); // with a permissive parser: the contents of /etc/passwd

If the service echoes the note back, logs it, or stores it where the partner can see it, the file leaks. The same idea with an http:// URI makes our server call an internal address, which is server-side request forgery, or SSRF. A third variant, the billion laughs attack, defines entities that each expand to ten copies of the previous one; a document of a few hundred bytes expands to gigabytes and takes the service down.

2Why JAXB Is Affected

JAXB does not parse XML itself. When we call unmarshal(InputStream), the implementation creates a parser from the JDK's JAXP factories (SAXParserFactory or XMLInputFactory) with whatever settings it and the platform choose, which differ between implementations and versions. Some combinations are safe by default, some are not, and the code gives no hint either way. That is exactly what the scanner flags: we cannot tell from the call site whether DTDs and external entities are processed.

So the rule we adopt is simple: never hand JAXB a raw stream from outside. Create the parser ourselves, configure it explicitly, and pass JAXB the configured reader. Then the behaviour no longer depends on which runtime is on the classpath.

Step throughAn XXE request against a permissive parser
PartnerOur serviceXML parserFile system
POST claim with DOCTYPE and entity
unmarshal(InputStream)
read file:///etc/passwd
file contents
Claim with note = file contents
response echoes the note

6 messages in order. Play them, or step through them one at a time.

The parser, not our code, opens the file. Hardening the parser removes the third step.

3Hardening the Parser for JAXB

The safest configuration refuses DTDs completely, because partner documents almost never need them. With StAX, the streaming XML API, it looks like this.

Java
public final class SafeXml {
private static final XMLInputFactory XIF = XMLInputFactory.newFactory();
static {
XIF.setProperty(XMLInputFactory.SUPPORT_DTD, false); // no DTD at all
XIF.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); // no external entities
XIF.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); // no protocol allowed
}
public static <T> T unmarshal(JAXBContext ctx, Class<T> type, InputStream in) throws Exception {
XMLStreamReader xsr = XIF.createXMLStreamReader(in);
Unmarshaller u = ctx.createUnmarshaller();
return u.unmarshal(xsr, type).getValue();
}
}
SafeXml.unmarshal(ctx, Claim.class, xxePayload);
// fails with an XMLStreamException about the DOCTYPE; nothing is read from disk

Notice that JAXB now receives an XMLStreamReader, not a stream, so it uses our parser and our rules. Setting the properties once on a shared factory is fine, because creating readers from a configured factory is thread-safe in the JDK implementation; check your parser if you use a different one.

Some code paths use SAX instead, for example when validating against a schema. There we configure the SAXParserFactory with the feature http://apache.org/xml/features/disallow-doctype-decl set to true and external-general-entities and external-parameter-entities set to false, enable XMLConstants.FEATURE_SECURE_PROCESSING, and wrap the result in a SAXSource. When we load XSD schemas with SchemaFactory, we also set ACCESS_EXTERNAL_DTD and ACCESS_EXTERNAL_SCHEMA to empty so a schema cannot pull in remote files.

Secure processing turns on the JDK's limits, such as jdk.xml.entityExpansionLimit and jdk.xml.totalEntitySizeLimit, which stop expansion attacks even if a DTD is somehow allowed. The exact defaults differ by JDK release, so check your version. In Spring, Jaxb2Marshaller exposes supportDtd and processExternalEntities, both false by default in current versions.

4Rolling the Fix Out Across the Company

A fix in one service does not help the other twenty. So we treat this as a platform change. First, we put SafeXml, or its equivalent, in a small shared library owned by the platform team, so every service gets the same, reviewed configuration and upgrades it in one place. For Spring services the library can also provide a preconfigured Jaxb2Marshaller bean.

Second, we make the unsafe call hard to write. A static analysis rule, for example a Semgrep pattern or an ArchUnit test, flags Unmarshaller.unmarshal(InputStream), unmarshal(Reader) and unmarshal(File), and raw factory creation outside the helper. The build fails, with a message pointing to the helper. Third, we fix existing code in order of exposure: services that parse documents from partners or the internet first, internal-only parsing later. For each service we also check logs for past documents with DOCTYPE declarations, because if XXE was exploitable, data may already have leaked and secrets may need rotating.

5Proving It in CI

Hardening is only real if a test shows it. We keep a set of known payloads, an external file entity, an external http entity pointing at a local test server, a parameter entity, and a billion laughs document, and run them through every endpoint or listener that accepts XML.

Java
@ParameterizedTest
@ValueSource(strings = {"xxe-file.xml", "xxe-http.xml", "xxe-parameter.xml", "billion-laughs.xml"})
void rejectsHostileXml(String payload) {
assertThrows(Exception.class, () -> SafeXml.unmarshal(ctx, Claim.class, open(payload)));
assertEquals(0, testHttpServer.requestCount()); // the http entity was never fetched
}

The second assertion matters: a parser that fails after fetching the URL has already made the SSRF call. Running these tests in the shared library and in each service's integration tests means a later dependency upgrade cannot quietly undo the protection.

6Follow-up questions

Is disabling external entities enough, or must we disable DTDs too?

Disabling DTDs entirely is the stronger choice, because it also blocks internal entity expansion and parameter entities, and partner documents rarely need a DTD. Disabling only external entities is the fallback when a partner really sends DTDs; then we rely on secure processing limits to stop expansion attacks.

Does validating against an XSD protect us?

No. Validation happens on the parsed document, and entity resolution happens during parsing, before or during validation. A schema can even be an attack vector itself if it imports remote schemas, which is why ACCESS_EXTERNAL_SCHEMA should be empty.

What about JSON services, are they safe from this?

JSON has no entity mechanism, so XXE does not apply. But a service that accepts both JSON and XML through content negotiation may have an XML converter on the classpath without anyone noticing, so the scan should cover every endpoint that could accept application/xml.

7Conclusion

In this answer, we saw that default XML parser settings may process DTDs and external entities, which lets a partner document read local files, call internal URLs or exhaust memory. With JAXB we fix it by never unmarshalling a raw stream: we build a hardened XMLStreamReader or SAXSource with DTDs and external entities disabled, external DTD and schema access empty and secure processing on, and pass that to the Unmarshaller. Across the company, a shared helper, a static analysis rule against raw unmarshal calls, and CI tests with known XXE payloads keep the fix in place.

What to say in the interviewAbout 80 seconds aloud

Open with

The risk is XXE. XML documents can declare entities in a DTD, and an external entity points to a URI, so a partner could send a document that makes our parser read a local file, call an internal URL, which is SSRF, or expand a few hundred bytes into gigabytes, the billion laughs attack.

Then, if they want more

JAXB delegates parsing to a JAXP parser, and with unmarshal of a raw InputStream the safety depends on the implementation's defaults, which is what the scan flags. The fix is to never give JAXB a raw stream.

I create an XMLInputFactory with SUPPORT_DTD false, external entities off and ACCESS_EXTERNAL_DTD set to empty, or a SAXParserFactory with doctype declarations disallowed and secure processing enabled, and unmarshal from that reader. Schema loading also gets ACCESS_EXTERNAL_SCHEMA empty.

Across the company I would put that in one shared helper library, or a preconfigured Jaxb2Marshaller for Spring, add a static analysis rule that fails the build on raw unmarshal(InputStream), and fix internet- and partner-facing services first.

Finally, CI runs a set of XXE payloads, file, http, parameter entity and billion laughs, and also asserts that no outbound request was made.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A security scan shows that several services parse XML from partners with default parser...": https://scrivio-interview-library.netlify.app/java/jaxb/e7e4a2166c/