2

Elementのアンマーシャリング中に、コード スキャン監査 (Veracode) から XML 外部エンティティ参照 (XXE) の脆弱性を取得しています。

    public static <T> T unMarshal(org.w3c.dom.Element content, Class<T> clazz) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    return (T) unmarshaller.unmarshal(content, clazz).getValue();
}

上記のコードの XML 外部エンティティ参照 ('XXE') の不適切な制限を修正するにはどうすればよいですか?

4

1 に答える 1

3

あなたの例によれば、このコードを試すことができます:

public static <T> T unMarshal(org.w3c.dom.Element content, Class<T> clazz) throws JAXBException, XMLStreamException {
  JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

  XMLInputFactory xmlif = XMLInputFactory.newFactory();
  xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
  xmlif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
  XMLStreamReader xsr = xmlif.createXMLStreamReader(content);

  return (T) unmarshaller.unmarshal(xsr, clazz).getValue();
}

上記の解決策は、(CWE 611) XML 外部エンティティ参照に関連する問題を解決できると思います

于 2019-10-24T14:31:24.547 に答える