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

未关闭 适合新手
#923 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
85/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java
领域
backend

调研方向

查看 Base64NameProcessor 和 ReplaceNameProcessor。修改正则表达式模式 VALID_XML_NAME 和允许的字符集以排除冒号,使 a:b 这类名称被转义或替换。相应地更新 encodeName() 方法。添加往返测试,验证包含冒号的名称可序列化为有效XML,且反序列化时不会抛出 StreamReadException。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Java
星标
636
派生
250
平均合并
7 小时 10 分钟
30 天内合并 PR
8

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

FasterXML/jackson-dataformat-xml 的其他 Issue

查看 FasterXML/jackson-dataformat-xml 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。