22// http://cwe.mitre.org/data/definitions/807.html
33package test .cwe807 .semmle .tests ;
44
5-
6-
7-
85import java .net .InetAddress ;
96import java .net .Inet4Address ;
107import java .net .UnknownHostException ;
118
129import javax .servlet .http .Cookie ;
10+ import javax .servlet .http .HttpServletRequest ;
1311import org .apache .shiro .SecurityUtils ;
1412import org .apache .shiro .subject .Subject ;
1513
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+
2321 // BAD: login is only executed if isAdmin is false, but isAdmin
2422 // is controlled by the user
25- if (isAdmin == "false" )
23+ if (isAdmin == "false" ) // $ hasConditionalBypassTest
2624 login (user , password );
27-
25+
2826 Cookie adminCookie = getCookies ()[0 ];
2927 // BAD: login is only executed if the cookie value is false, but the cookie
3028 // is controlled by the user
31- if (adminCookie .getValue ().equals ("false" ))
29+ if (adminCookie .getValue ().equals ("false" )) // $ hasConditionalBypassTest
3230 login (user , password );
33-
31+
3432 // FALSE POSITIVES: both methods are conditionally executed, but they probably
3533 // both perform the security-critical action
36- if (adminCookie .getValue ()== "false" ) {
34+ if (adminCookie .getValue () == "false" ) { // $ SPURIOUS: $ hasConditionalBypassTest
3735 login (user , password );
3836 } else {
3937 reCheckAuth (user , password );
4038 }
41-
39+
4240 // 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
4442 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+
5744 InetAddress local = InetAddress .getLocalHost ();
5845 // GOOD: reverse DNS on localhost is fine
5946 if (local .getCanonicalHostName ().equals ("localhost" )) {
@@ -63,68 +50,68 @@ public static void main(String[] args) throws UnknownHostException {
6350 login (user , password );
6451 }
6552 }
66-
53+
6754 public static void test (String user , String password ) {
6855 Cookie adminCookie = getCookies ()[0 ];
6956 // GOOD: login always happens
70- if (adminCookie .getValue ()== "false" )
57+ if (adminCookie .getValue () == "false" )
7158 login (user , password );
7259 else {
7360 // do something else
7461 login (user , password );
7562 }
7663 }
77-
64+
7865 public static void test2 (String user , String password ) {
7966 Cookie adminCookie = getCookies ()[0 ];
8067 // BAD: login may happen once or twice
81- if (adminCookie .getValue ()== "false" )
68+ if (adminCookie .getValue () == "false" ) // $ hasConditionalBypassTest
8269 login (user , password );
8370 else {
8471 // do something else
8572 }
8673 login (user , password );
8774 }
88-
75+
8976 public static void test3 (String user , String password ) {
9077 Cookie adminCookie = getCookies ()[0 ];
91- if (adminCookie .getValue ()== "false" )
78+ if (adminCookie .getValue () == "false" ) // $ hasConditionalBypassTest
9279 login (user , password );
9380 else {
9481 // do something else
9582 // BAD: login may not happen
9683 return ;
9784 }
9885 }
99-
86+
10087 public static void test4 (String user , String password ) {
10188 Cookie adminCookie = getCookies ()[0 ];
10289 // GOOD: login always happens
103- if (adminCookie .getValue ()== "false" ) {
90+ if (adminCookie .getValue () == "false" ) {
10491 login (user , password );
10592 return ;
10693 }
107-
94+
10895 // do other things
10996 login (user , password );
11097 return ;
11198 }
112-
99+
113100 public static void login (String user , String password ) {
114101 // login
115102 }
116-
103+
117104 public static void reCheckAuth (String user , String password ) {
118105 // login
119106 }
120-
107+
121108 public static Cookie [] getCookies () {
122109 // get cookies from a servlet
123110 return new Cookie [0 ];
124111 }
125-
112+
126113 public static void doIt () {}
127-
114+
128115 public static void doReallyImportantSecurityWork () {
129116 // login, authenticate, everything
130117 }
0 commit comments