Skip to content

Fix for invalid XML characters causing etree errors - #86

Merged
jrhoads merged 6 commits into
masterfrom
invalid-xml-fix
Nov 24, 2026
Merged

jrhoads merged 6 commits into
masterfrom
invalid-xml-fix

Conversation

@codycooperross

@codycooperross codycooperross commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

closes: datacite/datacite#2570

Some DOIs store DataCite XML that contains characters XML 1.0 doesn't allow, such as a literal U+0002 in 10.7805/hsmc-h261. lxml refuses to parse that XML. GetRecord and ListRecords then return HTTP 500 for the datacite and oai_datacite metadata prefixes. oai_dc was already unaffected, because it strips these characters field by field.

Approach

  • Shared parsing: a new parse_datacite_xml helper in viringo/metadata.py, used by both datacite_writer and oai_datacite_writer.
    • Strict parse first: the stored bytes are parsed unchanged, so lxml still uses the encoding declared in the XML. Valid ISO-8859-1, UTF-16 and other non-UTF-8 records parse exactly as before.
    • Fallback for UTF-8 only: if strict parsing fails and the document is UTF-8, the helper removes characters XML doesn't allow and parses again. A document counts as UTF-8 when it declares UTF-8 (or no encoding) and its bytes decode as UTF-8.
    • No guessing: if the document isn't UTF-8, or removing characters doesn't change it, the original XMLSyntaxError is raised. Records in other encodings are never re-decoded or altered.
  • One regex: the invalid-character pattern is now a single compiled regex shared with oai_dc_writer.
  • Fixtures: test fixtures now store xml as bytes, matching what the DataCite API service returns after base64 decoding.

Open Questions and Pre-Merge TODOs

  • Don't break records in other encodings. A first version decoded every record as UTF-8 before parsing. That turned valid ISO-8859-1 and UTF-16 records, which master serves fine, into 500s. It's fixed by parsing strictly first.
  • Don't silently change non-UTF-8 text. Some encodings (e.g. 7-bit ISO-2022-JP) also decode as valid UTF-8. Removing characters there would have garbled the text instead of failing. It's fixed by checking the declared encoding before the fallback.
  • XML that is broken for other reasons (bad markup, bad entities) still makes ListRecords fail for the whole page. Skip records with unparseable XML instead of failing ListRecords #90 handles that: it skips the bad record instead of failing the page.
  • Lupo should strip or reject these characters when metadata is written, and existing affected DOIs should be repaired. That will be tracked separately.

Learning

  • XML 1.0 allowed characters: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] (spec).
  • Passing bytes to lxml.etree.fromstring lets lxml detect the encoding from the BOM or the XML declaration. Decoding to text first loses that information.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

  • New feature (non-breaking change which adds functionality)

  • Breaking change (fix or feature that would cause existing functionality to change)

Tests

  • Integration: GetRecord tests for datacite and oai_datacite use a record that contains U+0002. The fixtures are oai_getrecord_datacite_invalid.xml and oai_getrecord_oaidatacite_invalid.xml.
  • Unit: tests/unit/test_metadata.py covers:
    • valid UTF-8, ISO-8859-1 and UTF-16 records
    • removing invalid characters with a BOM, a lowercase declaration, or no declaration
    • raising the original error for broken markup, for ISO-8859-1 with control characters, and for declared ISO-8859-1/ISO-2022-JP that also decodes as UTF-8
  • Manual check: with the real metadata of 10.7805/hsmc-h261, GetRecord and ListRecords both return 200 locally in both formats.

Reviewer, please remember our guidelines:

  • Be humble in the language and feedback you give, ask don't tell.
  • Consider using positive language as opposed to neutral when offering feedback. This is to avoid the negative bias that can occur with neutral language appearing negative.
  • Offer suggestions on how to improve code e.g. simplification or expanding clarity.
  • Ensure you give reasons for the changes you are proposing.

@jrhoads

jrhoads commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

It could be good to have an example of a currently failing XML string that we could test against.

@codycooperross

Copy link
Copy Markdown
Contributor Author

It could be good to have an example of a currently failing XML string that we could test against.

See now-linked issue for an example DOI and a request that currently fails. Added tests for this as well.

@codycooperross
codycooperross requested a review from a team November 23, 2026 18:07
Decoding every record as UTF-8 before parsing broke valid ISO-8859-1 and
UTF-16 metadata that lxml decodes from the XML declaration. Parse the
original bytes first and only fall back to removing characters that XML
1.0 disallows when strict parsing fails on a UTF-8 document, meaning one
that declares UTF-8 (or no encoding) and decodes as UTF-8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jrhoads
jrhoads merged commit e9eee0e into master Nov 24, 2026
2 checks passed
@codycooperross

Copy link
Copy Markdown
Contributor Author

As far as I can tell, lupo doesn't serve ISO-8859-1 and UTF-16 records. The commit is still effective for the linked problem, though—tested locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOIs with XML with invalid XML characters cause 500 errors in OAI-PMH

3 participants