只能說Code寫一天,修補要3天…
有問題的寫法:(讀XML檔案放入自定義的EmployeeUpdate Object)
- File XMLfile = new File("employee.xml");
- JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeUpdate.class);
- Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
- employee = (EmployeeUpdate) jaxbUnmarshaller.unmarshal(XMLfile);
Fortify建議寫法:
修正後:
- File XMLfile = new File("employee.xml");
- JAXBContext jc = JAXBContext.newInstance(EmployeeUpdate.class);
- Unmarshaller u = jc.createUnmarshaller();
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
- dbf.setNamespaceAware(true);
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document document = db.parse(XMLfile);
- employee = (EmployeeUpdate) u.unmarshal(document);
輕描淡寫的紀錄,事實上試了很久才發現重點是DocumentBuilderFactory的setFeature,因為一開始並不是用JAXBContext來處理XML的。
成功解決一高風險! (誤