2
2
// http://cwe.mitre.org/data/definitions/807.html
3
3
package test .cwe807 .semmle .tests ;
4
4
5
-
6
-
7
-
8
5
import java .net .InetAddress ;
9
6
import java .net .Inet4Address ;
10
7
import java .net .UnknownHostException ;
11
8
12
9
import javax .servlet .http .Cookie ;
10
+ import javax .servlet .http .HttpServletRequest ;
13
11
import org .apache .shiro .SecurityUtils ;
14
12
import org .apache .shiro .subject .Subject ;
15
13
16
- class Test {
17
- public static void main (String [] args ) throws UnknownHostException {
18
- String user = args [ 0 ] ;
19
- String password = args [ 1 ] ;
20
-
21
- String isAdmin = args [ 3 ] ;
22
-
14
+ class ConditionalBypassTest {
15
+ public static void main (HttpServletRequest request ) throws Exception {
16
+ String user = request . getParameter ( "user" ) ;
17
+ String password = request . getParameter ( "password" ) ;
18
+
19
+ String isAdmin = request . getParameter ( "isAdmin" ) ;
20
+
23
21
// BAD: login is only executed if isAdmin is false, but isAdmin
24
22
// is controlled by the user
25
- if (isAdmin == "false" )
23
+ if (isAdmin == "false" ) // $ hasConditionalBypassTest
26
24
login (user , password );
27
-
25
+
28
26
Cookie adminCookie = getCookies ()[0 ];
29
27
// BAD: login is only executed if the cookie value is false, but the cookie
30
28
// is controlled by the user
31
- if (adminCookie .getValue ().equals ("false" ))
29
+ if (adminCookie .getValue ().equals ("false" )) // $ hasConditionalBypassTest
32
30
login (user , password );
33
-
31
+
34
32
// FALSE POSITIVES: both methods are conditionally executed, but they probably
35
33
// both perform the security-critical action
36
- if (adminCookie .getValue ()== "false" ) {
34
+ if (adminCookie .getValue () == "false" ) { // $ SPURIOUS: $ hasConditionalBypassTest
37
35
login (user , password );
38
36
} else {
39
37
reCheckAuth (user , password );
40
38
}
41
-
39
+
42
40
// FALSE NEGATIVE: we have no way of telling that the skipped method is sensitive
43
- if (adminCookie .getValue ()== "false" )
41
+ if (adminCookie .getValue () == "false" ) // $ MISSING: $ hasConditionalBypassTest
44
42
doReallyImportantSecurityWork ();
45
-
46
- // Apache Shiro permissions system
47
- String whatDoTheyWantToDo = args [4 ];
48
- Subject subject = SecurityUtils .getSubject ();
49
- // BAD: permissions decision made using tainted data
50
- if (subject .isPermitted ("domain:sublevel:" + whatDoTheyWantToDo ))
51
- doIt ();
52
-
53
- // GOOD: use fixed checks
54
- if (subject .isPermitted ("domain:sublevel:whatTheMethodDoes" ))
55
- doIt ();
56
-
43
+
57
44
InetAddress local = InetAddress .getLocalHost ();
58
45
// GOOD: reverse DNS on localhost is fine
59
46
if (local .getCanonicalHostName ().equals ("localhost" )) {
@@ -63,68 +50,68 @@ public static void main(String[] args) throws UnknownHostException {
63
50
login (user , password );
64
51
}
65
52
}
66
-
53
+
67
54
public static void test (String user , String password ) {
68
55
Cookie adminCookie = getCookies ()[0 ];
69
56
// GOOD: login always happens
70
- if (adminCookie .getValue ()== "false" )
57
+ if (adminCookie .getValue () == "false" )
71
58
login (user , password );
72
59
else {
73
60
// do something else
74
61
login (user , password );
75
62
}
76
63
}
77
-
64
+
78
65
public static void test2 (String user , String password ) {
79
66
Cookie adminCookie = getCookies ()[0 ];
80
67
// BAD: login may happen once or twice
81
- if (adminCookie .getValue ()== "false" )
68
+ if (adminCookie .getValue () == "false" ) // $ hasConditionalBypassTest
82
69
login (user , password );
83
70
else {
84
71
// do something else
85
72
}
86
73
login (user , password );
87
74
}
88
-
75
+
89
76
public static void test3 (String user , String password ) {
90
77
Cookie adminCookie = getCookies ()[0 ];
91
- if (adminCookie .getValue ()== "false" )
78
+ if (adminCookie .getValue () == "false" ) // $ hasConditionalBypassTest
92
79
login (user , password );
93
80
else {
94
81
// do something else
95
82
// BAD: login may not happen
96
83
return ;
97
84
}
98
85
}
99
-
86
+
100
87
public static void test4 (String user , String password ) {
101
88
Cookie adminCookie = getCookies ()[0 ];
102
89
// GOOD: login always happens
103
- if (adminCookie .getValue ()== "false" ) {
90
+ if (adminCookie .getValue () == "false" ) {
104
91
login (user , password );
105
92
return ;
106
93
}
107
-
94
+
108
95
// do other things
109
96
login (user , password );
110
97
return ;
111
98
}
112
-
99
+
113
100
public static void login (String user , String password ) {
114
101
// login
115
102
}
116
-
103
+
117
104
public static void reCheckAuth (String user , String password ) {
118
105
// login
119
106
}
120
-
107
+
121
108
public static Cookie [] getCookies () {
122
109
// get cookies from a servlet
123
110
return new Cookie [0 ];
124
111
}
125
-
112
+
126
113
public static void doIt () {}
127
-
114
+
128
115
public static void doReallyImportantSecurityWork () {
129
116
// login, authenticate, everything
130
117
}
0 commit comments