Rejecting XML with DTDs during Android imports with XmlPullParser
When importing XML on Android, define a concrete acceptance rule: an ordinary document creates a record with the expected fields, while a document containing a document type declaration—a DTD—is rejected. With XmlPullParser, FEATURE_PROCESS_DOCDECL=false alone is insufficient: the application must detect DOCDECL through nextToken() and stop the import. We will walk through this approach using an entirely fictional example and small documents we own, with no external resources.
The trust boundary comes before record acceptance
Imagine a fictional application that imports a record with a single required field, name. Reading its value does not mean the record can be accepted. Until parsing finishes, the application holds a temporary candidate; it becomes an accepted business record only after END_DOCUMENT and validation of the expected fields. This makes the acceptance rule cover the entire document, including errors that occur after the name has already been read.
This import path has a narrow purpose: reject documents containing DTDs. Android recommends accounting for the specific engine because XXE prevention methods differ between XML parser implementations. Settings and event handling therefore need to be considered alongside the documented contract of the parser in use.
Configure features before the first event
Before the first next() or nextToken() call, explicitly set FEATURE_VALIDATION=false, followed by FEATURE_PROCESS_DOCDECL=false. Enabling FEATURE_VALIDATION implies FEATURE_PROCESS_DOCDECL=true and processing of the internal and external document type declaration. Although FEATURE_PROCESS_DOCDECL is disabled by default, explicitly setting it makes this import requirement verifiable. It cannot be changed during parsing.
setFeature() can throw XmlPullParserException if a feature is unsupported or its value cannot be set. Under the proposed rule, this is grounds to stop the import without accepting a record. After configuration, check the values through getFeature(), but account for its limitation: an unknown feature also returns false. That response alone does not confirm support for the setting; consult the documentation for the specific implementation as well.
Traverse the document with nextToken()
With FEATURE_PROCESS_DOCDECL=false, nextToken() reports DOCDECL, whereas next() ignores that event. Disabling the feature therefore does not automatically enforce the business rule. Use nextToken() throughout this import path and include an explicit branch: on DOCDECL, immediately stop the import and discard the temporary candidate.
nextToken() documents IOException and XmlPullParserException. If either exception occurs, the candidate must also remain unaccepted. Only reaching END_DOCUMENT marks the end of the input document; after that, validate the candidate's fields and accept the record. Do not commit the record immediately after the closing name tag.
Preserve correct field value assembly
Switching to nextToken() requires care with element content. The parser may deliver text through several consecutive TEXT events, so assemble the name value from those fragments instead of replacing it with the latest fragment.
Predefined entity references and character references are also reported as ENTITY_REF rather than TEXT. In the proposed handler, incorporate the available replacement for a predefined reference when assembling the field. For an unknown reference with no available replacement, reject the document without attempting external resolution. This is the application's chosen policy: discard the candidate and create no accepted record.
Run positive checks with your own data
The following checks are original proposed acceptance tests for the fictional application, not an official Android verification procedure. Run them through the same import path that you own. For each run, observe both the final status and the contents of accepted records: a success message alone does not confirm the correct result.
- Submit <record><name>Тест</name></record>. Expect one accepted record with name=Тест after END_DOCUMENT and field validation. Until the document ends, the record remains a temporary candidate.
- Submit <record><name>А&Б</name></record>. Expect name=А&Б. This control checks that ENTITY_REF handling preserves the expected field value.
Verify rejection and the absence of an accepted record
Negative checks must confirm the application's state after rejection. Use only small documents you own, without external resources, and a predefined initial state. If the name has already been read, that must not prevent the candidate from being discarded after a subsequent error.
- Submit <!DOCTYPE record [<!ELEMENT record ANY>]><record><name>Тест</name></record>. The import stops on DOCDECL. If a parse error occurs earlier, the document is also rejected. No new accepted record exists.
- Submit the incomplete document <record><name>Тест</name>. On a parse error, the candidate is discarded even if name has already been populated; no accepted record exists.
- Use your own test double to trigger a failure when setting a feature. Verify that the import stops before traversal and creates no accepted record.
Fix priorities and limits of the result
First, prevent premature record acceptance, then ensure configuration occurs before traversal and implement handling for DOCDECL and exceptions. Next, verify text and reference assembly with positive controls. The acceptance outcome is specific: ordinary XML produces the expected fields, while a DTD, parse error, or configuration failure does not lead to an accepted record.
DOCDECL means the document type declaration has already been read. Observing this token does not prove that the parser made no earlier external requests; the described tests do not establish a universal prohibition on external reads or provide a statistical guarantee of security. To review data acceptance rules in a web product, you can start with a free audit of your website or the web portion of your product.