Skip to content

Commit 8dbb0a5

Browse files
committed
Rewrote tests to work
1 parent 01798f6 commit 8dbb0a5

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

java/ql/test/experimental/query-tests/security/CWE-078/RuntimeExecTest.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,37 @@
1111
import java.util.Arrays;
1212

1313
public class RuntimeExecTest {
14-
public static void test(String[] args) {
14+
public static void test() {
1515
System.out.println("Command injection test");
1616

17-
try {
18-
// 1. array literal
19-
String[] commandArray1 = new String[]{"/bin/sh", args[2], args[3], args[4]};
20-
Runtime.getRuntime().exec(commandArray1);
21-
22-
// 2. array assignment after it is created
23-
String[] commandArray2 = new String[4];
24-
commandArray2[0] = "/bin/sh";
25-
commandArray2[1] = args[2];
26-
commandArray2[2] = args[3];
27-
commandArray2[3] = args[4];
28-
Runtime.getRuntime().exec(commandArray2);
29-
30-
// 3. Stream concatenation
31-
Runtime.getRuntime().exec(
32-
Stream.concat(
33-
Arrays.stream(new String[]{"/bin/sh"}),
34-
Arrays.stream(new String[]{args[2], args[3], args[4]})
35-
).toArray(String[]::new)
36-
);
37-
38-
} catch (Exception e) {
39-
System.err.println("ERROR: " + e.getMessage());
17+
String script = System.getenv("SCRIPTNAME");
18+
19+
if (script != null) {
20+
try {
21+
// 1. array literal in the args
22+
Runtime.getRuntime().exec(new String[]{"/bin/sh", script});
23+
24+
// 2. array literal with dataflow
25+
String[] commandArray1 = new String[]{"/bin/sh", script};
26+
Runtime.getRuntime().exec(commandArray1);
27+
28+
// 3. array assignment after it is created
29+
String[] commandArray2 = new String[4];
30+
commandArray2[0] = "/bin/sh";
31+
commandArray2[1] = script;
32+
Runtime.getRuntime().exec(commandArray2);
33+
34+
// 4. Stream concatenation
35+
Runtime.getRuntime().exec(
36+
Stream.concat(
37+
Arrays.stream(new String[]{"/bin/sh"}),
38+
Arrays.stream(new String[]{script})
39+
).toArray(String[]::new)
40+
);
41+
42+
} catch (Exception e) {
43+
System.err.println("ERROR: " + e.getMessage());
44+
}
4045
}
4146
}
4247
}

0 commit comments

Comments
 (0)