最佳安全实践

更新时间:2025.04.22

XML解析存在的安全问题修复指引

商户接收回调通知URL对应的程序代码,如果不遵循安全规范,将会有XML外部实体注入漏洞(XML External Entity Injection,简称 XXE),该安全问题是由XML组件默认没有禁用外部实体引用导致,非微信支付系统存在漏洞。
XXE的攻击原理:外界攻击者通过模拟回调通知,在回调通知中引入不安全的XML,即可在商户服务器上执行系统命令。

XXE漏洞可能带来的危害:外界攻击者可读取商户服务器上的任意文件;执行系统命令;探测内网端口;攻击内网网站。商户可能出现资金损失的情况,所以请贵司重视,必须修复漏洞。
如果你有使用以下任意场景的回调业务,且回调URL对应的程序代码有使用XML组件解析内容的情况,请务必检查其是否对XXE漏洞进行了防范。

场景1:支付成功通知:notify_url参数值对应的URL;
场景2:退款成功通知:notify_url参数值对应的URL;
场景3:委托代扣签约、解约、扣款通知;
场景4:车主解约通知;
场景5:扫码支付模式一回调:在商户平台-->产品中心-->开发配置-->支付配置-->扫码支付-->支付回调链接;
注:APP支付的用户端SDK不受影响,但需检查支付回调通知URL对应的程序代码。

举例:需检查统一下单接口中传的notify_url参数,排查该URL对应的程序代码是否有XXE漏洞。

1<xml>
2   <appid>wx2421b1c4370ec43b</appid>
3   <attach>支付测试</attach>
4   <body>JSAPI支付测试</body>
5   <mch_id>10000100</mch_id>
6   <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>
7   <notify_url>http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url>
8   <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>
9   <out_trade_no>1415659990</out_trade_no>
10   <spbill_create_ip>14.23.150.211</spbill_create_ip>
11   <total_fee>1</total_fee>
12   <trade_type>JSAPI</trade_type>
13   <sign>0CB01533B8C1EF103065174F50BCA001</sign>
14</xml>

检查方法

使用贵司商户号登录商户平台,进入【产品中心】-->【安全医生】,开通安全医生后即可自助进行安全检测(免费开通)。

修复建议

1.如果您的后台系统使用了官方SDK,请更新SDK到最新版本(SDK下载链接:DEMO下载
2.如果您是有系统提供商,请联系提供商进行核查和升级修复;
3.如果您是自研系统,请联系技术部门按以下指引核查和修复;
4.如果您的收款系统已不再使用,请将回调URL的服务删除;
5.采用主动查单,不再使用回调。

温馨提醒:

因XXE属于高危漏洞,如贵司一直未修复,将被停止部分功能权限或停交易权限。
如果贵司(或贵司的技术服务商)无法修复漏洞,可以更换无漏洞的支付系统(比如使用微信收款商业版收款或其他服务商收款系统)。
如果以下方法不能解决您的问题,可进入微信支付在线技术支持页面,微信扫码登录后输入“人工”,选择问题分类为“其他问题”-“商户安全”与我们联系,我们将协助贵司修复。

Q&A

修复方法

XXE漏洞的修复方法,是在解析XML之前加入禁用实体解析的代码。不同开发语言和XML组件对应的修复方法不同,您需要根据自身程序代码的情况,选择对应的修复方法。下面提供了几种常见开发语言和XML组件的修复指引,更多语言和XML组件的修复指引可参考:https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#C.2FC.2B.2B
【PHP】 解析XML代码前加入:

1
2libxml_disable_entity_loader(true);	//关键代码
3$xml = simplexml_load_string($xmlContent);
4......

【JAVA】

1
2import javax.xml.parsers.DocumentBuilderFactory;
3import javax.xml.parsers.ParserConfigurationException; // catching unsupported features
4DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5String FEATURE = null;
6try {
7	// This is the PRIMARY defense. If DTDs (doctypes) are disallowed, almost all XML entity attacks are prevented
8	// Xerces 2 only - http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl
9	FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
10	dbf.setFeature(FEATURE, true);
11	
12	// If you can't completely disable DTDs, then at least do the following:
13	// Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities
14	// Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities
15	// JDK7+ - http://xml.org/sax/features/external-general-entities 
16	FEATURE = "http://xml.org/sax/features/external-general-entities";
17	dbf.setFeature(FEATURE, false);
18	
19	// Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-parameter-entities
20	// Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities
21	// JDK7+ - http://xml.org/sax/features/external-parameter-entities 
22	FEATURE = "http://xml.org/sax/features/external-parameter-entities";
23	dbf.setFeature(FEATURE, false);
24	
25	// Disable external DTDs as well
26	FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
27	dbf.setFeature(FEATURE, false);
28	
29	// and these as well, per Timothy Morgan's 2014 paper: "XML Schema, DTD, and Entity Attacks"
30	dbf.setXIncludeAware(false);
31	dbf.setExpandEntityReferences(false);
32	
33	// And, per Timothy Morgan: "If for some reason support for inline DOCTYPEs are a requirement, then 
34	// ensure the entity settings are disabled (as shown above) and beware that SSRF attacks
35	// (http://cwe.mitre.org/data/definitions/918.html) and denial 
36	// of service attacks (such as billion laughs or decompression bombs via "jar:") are a risk."
37	
38	// remaining parser logic
39} catch (ParserConfigurationException e) {
40	// This should catch a failed setFeature feature
41	logger.info("ParserConfigurationException was thrown. The feature '" +
42	FEATURE + "' is probably not supported by your XML processor.");
43}
44catch (SAXException e) {
45	// On Apache, this should be thrown when disallowing DOCTYPE
46	logger.warning("A DOCTYPE was passed into the XML document");
47}
48catch (IOException e) {
49	// XXE that points to a file that doesn't exist
50	logger.error("IOException occurred, XXE may still possible: " + e.getMessage());
51}
52DocumentBuilder safebuilder = dbf.newDocumentBuilder();
53

【.Net】

1
2XmlDocument doc= new XmlDocument();
3doc.XmlResolver = null;//关键代码
4......
5

【ASP】

1
2Set xmldom = Server.CreateObject("MSXML2.DOMDocument")
3xmldom.resolveExternals = false  '关键代码
4......
5

【Python】

1
2from lxml import etree
3xmlData = etree.parse(xmlSource,etree.XMLParser(resolve_entities=False))
4......
5

【c/c++(常用库为libxml2 libxerces-c)】 【libxml2】:   确保关闭配置选项:XML_PARSE_NOENT 和 XML_PARSE_DTDLOAD
2.9版本以上已修复XXE

【Coldfusion/lucee和node.js】
请更新到库的最新版本

【libxerces-c】:
如果用的是XercesDOMParser:

1
2XercesDOMParser *parser = new XercesDOMParser;
3parser->setCreateEntityReferenceNodes(false);

如果是用SAXParser:

1
2SAXParser* parser = new SAXParser;
3parser->setDisableDefaultEntityResolution(true);

如果是用SAX2XMLReader:

1
2SAX2XMLReader* reader = XMLReaderFactory::createXMLReader();
3parser->setFeature(XMLUni::fgXercesDisableDefaultEntityResolution, true);

附录:更多开源库/语言版本的修复建议可参考:
https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#C.2FC.2B.2B

反馈
咨询
目录
置顶