在 java 中使用 XSD 进行 XML 验证
我有以下课程:
package com.somedir.someotherdir;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
public class SchemaValidator
{
private static Logger _logger = Logger.getLogger(SchemaValidator.class.getName());
/**
* @param file - the relative path to and the name of the XML file to be validated
* @return true if validation succeeded, false otherwise
*/
public final static boolean validateXML(String file)
{
try
{
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema();
Validator validator = schema.newValidator();
validator.validate(new StreamSource(file));
return true;
}
catch (Exception e)
{
_logger.log(Level.WARNING, "SchemaValidator: failed validating " + file + ". Reason: " + e.getMessage(), e);
return false;
}
}
}
我想知道我是否应该使用 schema.newValidator("dir/to/schema.xsd") 还是当前版本可以?我读到有一些 DoS 漏洞,也许有人可以提供更多信息?另外,路径必须是绝对路径还是相对路径?
大多数要验证的 XML 都有自己的 XSD,因此我想读取 XML 本身中提到的架构 (xs:noNamespaceSchemaLocation="schemaname.xsd"
)。
仅在启动或手动重新加载(服务器软件)期间进行验证。
I have the following class:
package com.somedir.someotherdir;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
public class SchemaValidator
{
private static Logger _logger = Logger.getLogger(SchemaValidator.class.getName());
/**
* @param file - the relative path to and the name of the XML file to be validated
* @return true if validation succeeded, false otherwise
*/
public final static boolean validateXML(String file)
{
try
{
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema();
Validator validator = schema.newValidator();
validator.validate(new StreamSource(file));
return true;
}
catch (Exception e)
{
_logger.log(Level.WARNING, "SchemaValidator: failed validating " + file + ". Reason: " + e.getMessage(), e);
return false;
}
}
}
I would like to know if I should use schema.newValidator("dir/to/schema.xsd")
after all or is the current version alright? I read that there's some DoS vulnerability, maybe someone could provide more info on that? Also, does the path have to be absolute or relative?
Most of the XMLs to be validated each have their own XSD, so I'd like to read the schema that is mentioned in the XML itself (xs:noNamespaceSchemaLocation="schemaname.xsd"
).
The validation is done only during startup or manual reload (server software).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您真的是指 XML DTD DOS 攻击吗?如果是这样,网上有一些不错的文章:
XML 拒绝服务攻击和防御 http://msdn.microsoft.com/en-us/magazine/ee335713.aspx
来自 IBMdeveloperWorks。 “提示:配置 SAX 解析器以进行安全处理”:
我想我不确定它是否可以直接应用于你的程序,它可以为进一步调查提供一些线索
Are you really meaning XML DTD DOS attack? If so, there are some good articles on the net:
XML Denial of Service Attacks and Defenses http://msdn.microsoft.com/en-us/magazine/ee335713.aspx
From IBM developerWorks. "Tip: Configure SAX parsers for secure processing":
Thought I am not sure that it can be directly applied to your program, it can give some clues for further investigation
据我解释,
Schema
对象将尝试在运行时获取它们。据我所知,默认的 Schema 实现不会缓存这些架构。 W3C 已报告 不良编码实践导致对其网站事实上的 DDoS(每天高达 1.3 亿个 dtd 请求!)。Schema
。对于更多邪恶的攻击媒介,请查看 sign 之前的答案
为了避免这个陷阱,您可以在本地存储所有外部资源并使用 SchemaFactory.setResourceResolver 方法指示
Schema
如何获取它们。As I interpret it, the javax.xml.validation.Schema object returned by
SchemaFactory.newSchema()
will try to fetch other schemas referred in the xml/xsd files to validate as indicated in the correspondingxsi:schemaLocation
attributes. This implies that:Schema
object will try to fetch them during runtime. As long as I'm aware, the defaultSchema
implementation does not cache those schemas. The W3C already reported on bad coding practices resulting in de-facto DDoS to their website (up to 130M dtd requests per day!).Schema
trying to fetch other schemas from "possibly bad intended" xml sources.For more evil attack vectors, take a look into sign's previous answer
To avoid this pitfall, you can store all external resources locally and use the SchemaFactory.setResourceResolver method to instruct the
Schema
how to fetch them.