Escaping `XmlNameProcessors` write a colon in XML names as-is, producing output that can not be read back

Aperta Adatta ai principianti
#923 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java
Ambito
backend

Direzione di ricerca

Guarda Base64NameProcessor e ReplaceNameProcessor. Modifica i pattern di regex VALID_XML_NAME e gli insiemi di caratteri consentiti per escludere i due punti, in modo che nomi come a:b vengano escapati o sostituiti. Aggiorna il metodo encodeName() di conseguenza. Aggiungi test di andata e ritorno per verificare che i nomi che contengono due punti si serializzino in XML valido e si deserializzino senza StreamReadException.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Describe the bug

Both escaping processors from XmlNameProcessors treat a colon as a valid XML name character, so a name that contains one is written out verbatim:

  • Base64NameProcessor.VALID_XML_NAME is [a-zA-Z_:]([a-zA-Z0-9_:.-])*, i.e. a name holding a colon counts as already valid and is not escaped
  • ReplaceNameProcessor keeps : in both of its allowed sets (^[^a-zA-Z_:], [^a-zA-Z0-9_:-])

Output is namespace-aware, though, so the colon comes back as a prefix that nothing declares and the document can no longer be read by this module:

<DTO><badMap><a:b>xyz</a:b></badMap></DTO>
tools.jackson.core.exc.StreamReadException:
Undeclared namespace prefix "a"
 at [row,col {unknown-source}]: [1,18]
 at [Source: (String)"<DTO><badMap><a:b>xyz</a:b></badMap></DTO>"; line: 1, column: 19]
Caused by: com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "a"

Keys like a:b:c or :leading are not even well-formed, so they fail in the Stax reader before namespace resolution.

The javadoc of both processors lists the colon as "only exposed if underlying parser is in non-namespace-aware mode", but encoding has no knowledge of which parser will read the result back, and newAlwaysOnBase64Processor() already handles the same keys cleanly. Round-tripping names that are not valid XML names is what these two processors exist for, so the colon looks like an oversight in the allowed sets rather than a deliberate exception.

Version information

3.3.0-SNAPSHOT (3.x). The allowed sets have looked this way since the mechanism was added (#531), so 3.0/3.1/3.2 and 2.x behave the same.

Reproduction
public static class DTO {
    public Map<String, String> badMap = new HashMap<>();
}

private XmlFactory xmlFactory(XmlNameProcessor nameProcessor) {
    return XmlFactory.builder().xmlNameProcessor(nameProcessor).build();
}

@Test
public void colonInNameRoundTrips() throws Exception {
    XmlMapper mapper = XmlMapper.builder(
            xmlFactory(XmlNameProcessors.newBase64Processor())).build();

    DTO dto = new DTO();
    dto.badMap.put("a:b", "xyz");

    String xml = mapper.writeValueAsString(dto);
    // writes <DTO><badMap><a:b>xyz</a:b></badMap></DTO>
    mapper.readValue(xml, DTO.class); // StreamReadException: Undeclared namespace prefix "a"
}

Same for newReplacementProcessor(), which writes <a:b> rather than replacing the colon. newAlwaysOnBase64Processor() is fine, since it escapes every name.

Expected behavior

A name containing a colon should be escaped (base64, still a lossless round trip) or replaced, like any other character that cannot appear in a written XML name, so that what these processors write can be read back.

Only encodeName() would need to change: decodeName() keys off the prefix for base64 and is a no-op for replacement, so names read from a non-namespace-aware parser are still passed through untouched.

Additional context

PR #921 has the fix plus round-trip tests. Targeted at 3.x since it changes what gets written for these names.

Lingua principale
Java
Stelle
636
Fork
250
Merge medio
7h 10m
PR unite (30g)
8

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di FasterXML/jackson-dataformat-xml

Tutte le issue di FasterXML/jackson-dataformat-xml

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.