Skip to content

Commit f05f161

Browse files
committed
Testing for Environment variable injection
1 parent 8ed3f3c commit f05f161

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
| TaintedEnvironment.java:28:35:28:55 | new String[] | Command with a relative path 'ls' is executed. |
12
| Test.java:50:46:50:49 | "ls" | Command with a relative path 'ls' is executed. |

java/ql/test/query-tests/security/CWE-078/ExecTaintedEnvironment.expected

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import java
2+
import semmle.code.java.dataflow.FlowSources
3+
import semmle.code.java.security.TaintedEnvironmentVariableQuery
4+
import TestUtilities.InlineFlowTest
5+
6+
private class TestSource extends RemoteFlowSource {
7+
TestSource() { this.asExpr().(MethodCall).getMethod().hasName("source") }
8+
9+
override string getSourceType() { result = "test source" }
10+
}
11+
12+
import TaintFlowTest<ExecTaintedEnvironmentConfig>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.lang.ProcessBuilder;
2+
import java.lang.Runtime;
3+
import java.util.Map;
4+
5+
public class TaintedEnvironment {
6+
public Object source() {
7+
return null;
8+
}
9+
10+
public void buildProcess() throws java.io.IOException {
11+
String s = (String) source();
12+
ProcessBuilder pb = new ProcessBuilder();
13+
14+
pb.environment().put("foo", s); // $hasTaintFlow
15+
16+
pb.environment().put(s, "foo"); // $hasTaintFlow
17+
18+
Map<String, String> env = pb.environment();
19+
20+
env.put("foo", s); // $hasTaintFlow
21+
22+
pb.start();
23+
}
24+
25+
public void exec() throws java.io.IOException {
26+
String kv = (String) source();
27+
28+
Runtime.getRuntime().exec(new String[] { "ls" }, new String[] { kv }); // $hasTaintFlow
29+
}
30+
}

0 commit comments

Comments
 (0)