JAXB 解组:意外元素

JAXB unmarshal: unexpected element

背景:

我正在使用 JAXB 将 XML 解组为 Java 对象。最初,我只使用 JAXB 来执行解组。然后对代码进行了静态分析,并提出了 XML 外部实体注入的高危问题。经过一些研究,我发现了一个建议 (https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#JAXB_Unmarshaller) 使用配置为防止外部实体被解析的解析器。提供了一个操作示例:

//Disable XXE
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

//Do unmarshall operation
Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(new StringReader(xml)));
JAXBContext jc = JAXBContext.newInstance(Object.class);
Unmarshaller um = jc.createUnmarshaller();
um.unmarshal(xmlSource);

我没有完全按照显示的那样做,但我相信我做了同样的效果:

XMLReader reader = getXMLReader();

if (reader == null) {
  logger.warn("Unable to create XML reader");
  return;
}

JAXBContext context = JAXBContext.newInstance(messageClass);
Unmarshaller unmarshaller = context.createUnmarshaller();

for (File file : files) {
  try {
    InputSource source = new InputSource(new FileReader(file));
    Source xmlSource = new SAXSource(reader, source);
    JAXBElement<? extends BaseType> object =
        (JAXBElement<? extends BaseType>) unmarshaller.unmarshal(xmlSource);
    messages.add(object.getValue());
  } catch (FileNotFoundException e) {
    logger.error("Exception", e);
  }
}

...

private XMLReader getXMLReader() {
    SAXParserFactory factory = SAXParserFactory.newInstance();

    try {
      factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
      factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
      factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    } catch (SAXNotRecognizedException | SAXNotSupportedException
        | ParserConfigurationException e) {
      logger.error("Exception", e);
    }

    XMLReader reader = null;

    try {
      reader = factory.newSAXParser().getXMLReader();
    } catch (SAXException | ParserConfigurationException e) {
      logger.error("Exception", e);
    }

    return reader;
}

问题:

实施更正后,当程序尝试读入 XML:

时,我现在遇到解组异常
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"ns1:TypeXYZ"). Expected elements are <{protected namespace URI}TypeABC>,...<{protected namespace URI}TypeXYZ>,...

在我只是使用 JAXB 解组的上述修复之前,它能够正确解析提供的 XML 没有问题。

我假设 SAX 解析器期望 XML 提供缺少的额外信息,或者它需要配置为忽略它抱怨的任何内容。我尝试了其他一些 "features" (http://xml.org/sax/features/namespace-prefixes=true and http://xml.org/sax/features/validation=false),但没有解决问题。

我无法控制定义 XML 类型的 XML 模式,也无法控制相应的 Java 类 的生成方式。

如果有任何信息可以帮助我了解正在发生的事情并帮助我解决这个问题,我们将不胜感激。

经过一些试验后,我通过设置以下功能解决了错误:

factory.setFeature("http://xml.org/sax/features/validation", false);
factory.setFeature("http://xml.org/sax/features/namespaces", true);
factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);