2018年4月26日 星期四

【Fortify】XML External Entity Injection



只能說Code寫一天,修補要3


有問題的寫法:(XML檔案放入自定義的EmployeeUpdate Object)
  1. File XMLfile = new File("employee.xml");
  2. JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeUpdate.class);
  3. Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  4. employee = (EmployeeUpdate) jaxbUnmarshaller.unmarshal(XMLfile);


Fortify建議寫法:




修正後:
  1. File XMLfile = new File("employee.xml");
  2. JAXBContext jc = JAXBContext.newInstance(EmployeeUpdate.class);
  3. Unmarshaller u = jc.createUnmarshaller();
  4. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  5. dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl"true);
  6. dbf.setNamespaceAware(true);
  7. DocumentBuilder db = dbf.newDocumentBuilder();
  8. Document document = db.parse(XMLfile);
  9. employee = (EmployeeUpdate) u.unmarshal(document);




輕描淡寫的紀錄,事實上試了很久才發現重點是DocumentBuilderFactorysetFeature,因為一開始並不是用JAXBContext來處理XML的。

成功解決一高風險! (


沒有留言:

張貼留言