Skip to content

Commit 810854d

Browse files
joefarebrotheratorralba
authored andcommitted
Add tests
1 parent 0e04f2b commit 810854d

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

java/ql/lib/semmle/code/java/frameworks/android/ExternalStorage.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private predicate externalStorageFlow(DataFlow::Node node1, DataFlow::Node node2
3434

3535
/**
3636
* Holds if `n` is a node that reads the contents of an external file in Android.
37-
* This may be controlable by third-party applications, so is treated as a remote flow source.
37+
* This is controlable by third-party applications, so is treated as a remote flow source.
3838
*/
3939
predicate androidExternalStorageSource(DataFlow::Node n) {
4040
exists(ConstructorCall fInp, DataFlow::Node externalDir |
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.io.File;
2+
import java.io.InputStream;
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import android.content.Context;
6+
import android.os.Environment;
7+
8+
class Test {
9+
void sink(Object o) {}
10+
11+
void test1(Context ctx) throws IOException {
12+
File f = new File(ctx.getExternalFilesDir(null), "file.txt");
13+
InputStream is = new FileInputStream(f);
14+
byte[] data = new byte[is.available()];
15+
is.read(data);
16+
sink(data); // $hasTaintFlow
17+
is.close();
18+
}
19+
20+
void test2(Context ctx) throws IOException {
21+
File f = new File(new File(new File(ctx.getExternalFilesDirs(null)[0], "things"), "stuff"), "file.txt");
22+
sink(new FileInputStream(f)); // $hasTaintFlow
23+
}
24+
25+
void test3(Context ctx) throws IOException {
26+
File f = new File(ctx.getExternalCacheDir(), "file.txt");
27+
sink(new FileInputStream(f)); // $hasTaintFlow
28+
}
29+
30+
void test4(Context ctx) throws IOException {
31+
File f = new File(ctx.getExternalCacheDirs()[0], "file.txt");
32+
sink(new FileInputStream(f)); // $hasTaintFlow
33+
}
34+
35+
void test5(Context ctx) throws IOException {
36+
File f = new File(Environment.getExternalStorageDirectory(), "file.txt");
37+
sink(new FileInputStream(f)); // $hasTaintFlow
38+
}
39+
40+
void test6(Context ctx) throws IOException {
41+
File f = new File(Environment.getExternalStoragePublicDirectory(null), "file.txt");
42+
sink(new FileInputStream(f)); // $hasTaintFlow
43+
}
44+
45+
static final File dir = Environment.getExternalStorageDirectory();
46+
47+
void test7(Context ctx) throws IOException {
48+
File f = new File(dir, "file.txt");
49+
sink(new FileInputStream(f)); // $hasTaintFlow
50+
}
51+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --javac-args -cp ${testdir}/../../../../stubs/google-android-9.0.0

java/ql/test/library-tests/frameworks/android/external-storage/test.expected

Whitespace-only changes.
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.dataflow.DataFlow
3+
import semmle.code.java.dataflow.FlowSources
4+
import TestUtilities.InlineFlowTest
5+
6+
class Conf extends TaintTracking::Configuration {
7+
Conf() { this = "test:AndroidExternalFlowConf" }
8+
9+
override predicate isSource(DataFlow::Node src) { src instanceof RemoteFlowSource }
10+
11+
override predicate isSink(DataFlow::Node sink) {
12+
sink.asExpr().(Argument).getCall().getCallee().hasName("sink")
13+
}
14+
}
15+
16+
class ExternalStorageTest extends InlineFlowTest {
17+
override DataFlow::Configuration getValueFlowConfig() { none() }
18+
19+
override DataFlow::Configuration getTaintFlowConfig() { result instanceof Conf }
20+
}

java/ql/test/stubs/google-android-9.0.0/android/os/Environment.java

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)