【问题标题】:JAXB XXE attack with Default Parser使用默认解析器的 JAXB XXE 攻击
【发布时间】:2014-10-02 09:56:27
【问题描述】:

这是关于在使用 JAXB API 时避免 XXE 攻击。我知道在使用 JAXB 时,可以覆盖默认解析机制,并且可以使用备用 SAX 解析器并设置实体功能以避免 XXE 攻击。但想了解默认解析器到底是什么,并在其上设置安全功能。有什么帮助吗?

【问题讨论】:

  • 在阅读了 Unmarshaller (class) 和 Object unmarshal(Source source) 的 javadoc 之后还有什么需要回答的。我可以在答案中引用这一切,但这似乎很荒谬。您可以轻松阅读 Unmarshaller 实现的源代码。

标签: java xml security jaxb xxe


【解决方案1】:

您可以通过使用 JAXB 和禁用外部实体支持的 StAX 解析器来执行以下操作:

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        XMLInputFactory xif = XMLInputFactory.newFactory();
        xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("input.xml"));
        Customer customer = (Customer) unmarshaller.unmarshal(xsr);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }

}

【讨论】:

    猜你喜欢
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2017-03-31
    • 2015-04-03
    • 2015-10-10
    • 2020-11-27
    • 2012-12-23
    相关资源
    最近更新 更多