Skip to content

Commit f8d1e2a

Browse files
committed
Refactor tests to use InlineExpectationsTest
1 parent 1f7990d commit f8d1e2a

File tree

7 files changed

+79
-74
lines changed

7 files changed

+79
-74
lines changed

java/ql/test/query-tests/security/CWE-807/semmle/tests/ConditionalBypass.expected

Lines changed: 0 additions & 25 deletions
This file was deleted.

java/ql/test/query-tests/security/CWE-807/semmle/tests/ConditionalBypass.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.

java/ql/test/query-tests/security/CWE-807/semmle/tests/ConditionalBypassTest.expected

Whitespace-only changes.

java/ql/test/query-tests/security/CWE-807/semmle/tests/Test.java renamed to java/ql/test/query-tests/security/CWE-807/semmle/tests/ConditionalBypassTest.java

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,45 @@
22
// http://cwe.mitre.org/data/definitions/807.html
33
package test.cwe807.semmle.tests;
44

5-
6-
7-
85
import java.net.InetAddress;
96
import java.net.Inet4Address;
107
import java.net.UnknownHostException;
118

129
import javax.servlet.http.Cookie;
10+
import javax.servlet.http.HttpServletRequest;
1311
import org.apache.shiro.SecurityUtils;
1412
import 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
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java
2+
import semmle.code.java.security.ConditionalBypassQuery
3+
import TestUtilities.InlineExpectationsTest
4+
5+
class ConditionalBypassTest extends InlineExpectationsTest {
6+
ConditionalBypassTest() { this = "ConditionalBypassTest" }
7+
8+
override string getARelevantTag() { result = "hasConditionalBypassTest" }
9+
10+
override predicate hasActualResult(Location location, string element, string tag, string value) {
11+
tag = "hasConditionalBypassTest" and
12+
exists(DataFlow::Node src, DataFlow::Node sink, ConditionalBypassFlowConfig conf |
13+
conf.hasFlow(src, sink)
14+
|
15+
sink.getLocation() = location and
16+
element = sink.toString() and
17+
value = ""
18+
)
19+
}
20+
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
edges
2-
| Test.java:17:26:17:38 | args : String[] | Test.java:50:26:50:64 | ... + ... |
2+
| TaintedPermissionsCheckTest.java:12:19:12:48 | getParameter(...) : String | TaintedPermissionsCheckTest.java:15:27:15:53 | ... + ... |
33
nodes
4-
| Test.java:17:26:17:38 | args : String[] | semmle.label | args : String[] |
5-
| Test.java:50:26:50:64 | ... + ... | semmle.label | ... + ... |
6-
subpaths
4+
| TaintedPermissionsCheckTest.java:12:19:12:48 | getParameter(...) : String | semmle.label | getParameter(...) : String |
5+
| TaintedPermissionsCheckTest.java:15:27:15:53 | ... + ... | semmle.label | ... + ... |
76
#select
8-
| Test.java:50:6:50:65 | isPermitted(...) | Test.java:17:26:17:38 | args : String[] | Test.java:50:26:50:64 | ... + ... | Permissions check uses user-controlled $@. | Test.java:17:26:17:38 | args | data |
7+
| TaintedPermissionsCheckTest.java:15:7:15:54 | isPermitted(...) | TaintedPermissionsCheckTest.java:12:19:12:48 | getParameter(...) : String | TaintedPermissionsCheckTest.java:15:27:15:53 | ... + ... | Permissions check uses user-controlled $@. | TaintedPermissionsCheckTest.java:12:19:12:48 | getParameter(...) | data |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Test case for CWE-807 (Reliance on Untrusted Inputs in a Security Decision)
2+
// http://cwe.mitre.org/data/definitions/807.html
3+
package test.cwe807.semmle.tests;
4+
5+
import javax.servlet.http.HttpServletRequest;
6+
import org.apache.shiro.SecurityUtils;
7+
import org.apache.shiro.subject.Subject;
8+
9+
class TaintedPermissionsCheckTest {
10+
public static void main(HttpServletRequest request) throws Exception {
11+
// Apache Shiro permissions system
12+
String action = request.getParameter("action");
13+
Subject subject = SecurityUtils.getSubject();
14+
// BAD: permissions decision made using tainted data
15+
if (subject.isPermitted("domain:sublevel:" + action))
16+
doIt();
17+
18+
// GOOD: use fixed checks
19+
if (subject.isPermitted("domain:sublevel:whatTheMethodDoes"))
20+
doIt();
21+
}
22+
23+
public static void doIt() {}
24+
25+
}

0 commit comments

Comments
 (0)