1
- import org .w3c .dom .Document ;
2
- import org .xml .sax .InputSource ;
3
- import org .xml .sax .SAXException ;
1
+ import java .io .ByteArrayInputStream ;
2
+ import java .io .StringReader ;
4
3
4
+ import javax .servlet .http .HttpServletRequest ;
5
5
import javax .xml .parsers .DocumentBuilder ;
6
6
import javax .xml .parsers .DocumentBuilderFactory ;
7
- import javax .xml .parsers .ParserConfigurationException ;
8
7
import javax .xml .xpath .XPath ;
9
8
import javax .xml .xpath .XPathConstants ;
10
9
import javax .xml .xpath .XPathExpression ;
11
- import javax .xml .xpath .XPathExpressionException ;
12
10
import javax .xml .xpath .XPathFactory ;
13
11
14
- import java .io .BufferedInputStream ;
15
- import java .io .ByteArrayInputStream ;
16
- import java .io .InputStream ;
17
- import java .io .StringReader ;
18
-
19
- import javax .servlet .http .HttpServletRequest ;
12
+ import org .w3c .dom .Document ;
13
+ import org .xml .sax .InputSource ;
20
14
21
15
public class A {
22
16
public void handle (HttpServletRequest request ) throws Exception {
@@ -34,17 +28,13 @@ public void handle(HttpServletRequest request) throws Exception {
34
28
String user = request .getParameter ("user" );
35
29
String pass = request .getParameter ("pass" );
36
30
if (user != null && pass != null ) {
37
- boolean isExist = false ;
38
-
39
31
// Bad expression
40
32
String expression1 = "/users/user[@name='" + user + "' and @pass='" + pass + "']" ;
41
- isExist = (boolean ) xpath .evaluate (expression1 , doc , XPathConstants .BOOLEAN ); // $hasXPathInjection
42
- System .out .println (isExist );
33
+ xpath .evaluate (expression1 , doc , XPathConstants .BOOLEAN ); // $hasXPathInjection
43
34
44
35
// Bad expression
45
36
XPathExpression expression2 = xpath .compile ("/users/user[@name='" + user + "' and @pass='" + pass + "']" ); // $hasXPathInjection
46
- isExist = (boolean ) expression2 .evaluate (doc , XPathConstants .BOOLEAN );
47
- System .out .println (isExist );
37
+ expression2 .evaluate (doc , XPathConstants .BOOLEAN );
48
38
49
39
// Bad expression
50
40
StringBuffer sb = new StringBuffer ("/users/user[@name=" );
@@ -54,8 +44,7 @@ public void handle(HttpServletRequest request) throws Exception {
54
44
sb .append ("']" );
55
45
String query = sb .toString ();
56
46
XPathExpression expression3 = xpath .compile (query ); // $hasXPathInjection
57
- isExist = (boolean ) expression3 .evaluate (doc , XPathConstants .BOOLEAN );
58
- System .out .println (isExist );
47
+ expression3 .evaluate (doc , XPathConstants .BOOLEAN );
59
48
60
49
// Good expression
61
50
String expression4 = "/users/user[@name=$user and @pass=$pass]" ;
@@ -69,13 +58,12 @@ public void handle(HttpServletRequest request) throws Exception {
69
58
throw new IllegalArgumentException ();
70
59
}
71
60
});
72
- isExist = (boolean ) xpath .evaluate (expression4 , doc , XPathConstants .BOOLEAN );
73
- System .out .println (isExist );
61
+ xpath .evaluate (expression4 , doc , XPathConstants .BOOLEAN ); // Safe
74
62
75
63
// Bad Dom4j
76
64
org .dom4j .io .SAXReader reader = new org .dom4j .io .SAXReader ();
77
65
org .dom4j .Document document = reader .read (new ByteArrayInputStream (xmlStr .getBytes ()));
78
- isExist = document .selectSingleNode ("/users/user[@name='" + user + "' and @pass='" + pass + "']" ) // $hasXPathInjection
66
+ document .selectSingleNode ("/users/user[@name='" + user + "' and @pass='" + pass + "']" ) // $hasXPathInjection
79
67
.hasContent ();
80
68
document .selectNodes ("/users/user[@name='" + user + "' and @pass='" + pass + "']" ); // $hasXPathInjection
81
69
}
0 commit comments