Skip to content

Commit 6272639

Browse files
committed
#889 修复一些潜在的XXE漏洞代码
1 parent 9b68931 commit 6272639

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public class WxCryptUtil {
3737
@Override
3838
protected DocumentBuilder initialValue() {
3939
try {
40-
return DocumentBuilderFactory.newInstance().newDocumentBuilder();
40+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
41+
factory.setExpandEntityReferences(false);
42+
return factory.newDocumentBuilder();
4143
} catch (ParserConfigurationException exc) {
4244
throw new IllegalArgumentException(exc);
4345
}

weixin-java-common/src/test/java/me/chanjar/weixin/common/util/crypto/WxCryptUtilTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package me.chanjar.weixin.common.util.crypto;
22

3+
import java.io.IOException;
4+
import java.io.StringReader;
5+
import javax.xml.parsers.DocumentBuilder;
6+
import javax.xml.parsers.DocumentBuilderFactory;
7+
import javax.xml.parsers.ParserConfigurationException;
8+
39
import org.testng.annotations.*;
410
import org.w3c.dom.Document;
511
import org.w3c.dom.Element;
612
import org.w3c.dom.NodeList;
713
import org.xml.sax.InputSource;
814
import org.xml.sax.SAXException;
915

10-
import javax.xml.parsers.DocumentBuilder;
11-
import javax.xml.parsers.DocumentBuilderFactory;
12-
import javax.xml.parsers.ParserConfigurationException;
13-
import java.io.IOException;
14-
import java.io.StringReader;
15-
1616
import static org.testng.Assert.*;
1717

1818
@Test
@@ -39,6 +39,7 @@ public void testNormal() throws ParserConfigurationException, SAXException, IOEx
3939
System.out.println(encryptedXml);
4040

4141
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
42+
documentBuilderFactory.setExpandEntityReferences(false);
4243
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
4344
Document document = documentBuilder.parse(new InputSource(new StringReader(encryptedXml)));
4445

@@ -81,6 +82,7 @@ public void testValidateSignatureError() throws ParserConfigurationException, SA
8182
WxCryptUtil pc = new WxCryptUtil(this.token, this.encodingAesKey, this.appId);
8283
String afterEncrpt = pc.encrypt(this.replyMsg);
8384
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
85+
dbf.setExpandEntityReferences(false);
8486
DocumentBuilder db = dbf.newDocumentBuilder();
8587
StringReader sr = new StringReader(afterEncrpt);
8688
InputSource is = new InputSource(sr);

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/BaseWxPayResult.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.google.common.base.Joiner;
2828
import com.google.common.collect.Lists;
2929
import com.google.common.collect.Maps;
30-
import com.google.gson.GsonBuilder;
3130
import com.thoughtworks.xstream.XStream;
3231
import com.thoughtworks.xstream.annotations.XStreamAlias;
3332
import lombok.Data;
@@ -191,9 +190,9 @@ private Document getXmlDoc() {
191190
}
192191

193192
try {
194-
this.xmlDoc = DocumentBuilderFactory
195-
.newInstance()
196-
.newDocumentBuilder()
193+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
194+
factory.setExpandEntityReferences(false);
195+
this.xmlDoc = factory.newDocumentBuilder()
197196
.parse(new ByteArrayInputStream(this.xmlString.getBytes(StandardCharsets.UTF_8)));
198197
return xmlDoc;
199198
} catch (SAXException | IOException | ParserConfigurationException e) {

0 commit comments

Comments
 (0)