我是JAXB的新手,在我们的代码审计中,有关于用JAXB防止XXE攻击的建议。我找到了相关的答案:Prevent XXE Attack with JAXB
我现有的代码如下所示:
if (properties.getProperty(MANIFEST) != null && !properties.getProperty(MANIFEST).isEmpty()) {
String manifestString = properties.getProperty(MANIFEST);
ByteArrayInputStream is = new ByteArrayInputStream(manifestString.getBytes());
try {
this.manifest = (Manifest) getJaxbContext().createUnmarshaller().unmarshal(is);
}
catch (JAXBException e) {
LOG.warn("There was an error trying to convert xml String to Manifest - {}", e.getMessage(), e);
}
}根据答案,我应该使用带有一些属性false的XMLStreamReader,而不是使用ByteArrayInputStream。
在建议的答案中,它说:
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("src/xxe/input.xml"));
我不明白'src/xxe/input.xml‘是什么,我的解决方案需要什么。有谁能解释一下吗?
发布于 2020-12-15 05:50:56
另一个问题中答案的src/xxe/input.xml是该问题正在处理的XML的源位置-即作为URL资源访问的文件名。
在您的示例中,您的XML是在String manifestString中提供的-因此,您的StreamSource需要将此字符串指定为其源,而不是文件位置。
这可以使用StringReader来完成
import java.io.StringReader
...
StringReader manifestReader = new StringReader(manifestString);
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(manifestReader));我将代码分成两行以使其更清晰-但如果您愿意,也可以将它们折叠回一行:
XMLStreamReader xsr = xif.createXMLStreamReader(
new StreamSource(new StringReader(manifestString)));上面的代码假设您已经创建了上下文和xif输入工厂:
JAXBContext jc = JAXBContext.newInstance(Manifest.class);
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);然后,您可以用通常的方式进行解组:
Unmarshaller unmarshaller = jc.createUnmarshaller();
Manifest manifest = (Manifest) unmarshaller.unmarshal(xsr);https://stackoverflow.com/questions/65295943
复制相似问题