Skip to content

Commit fc2be16

Browse files
committed
chore: add more tests for the path traversal sanitizer
1 parent d465ff7 commit fc2be16

3 files changed

Lines changed: 200 additions & 0 deletions

File tree

sanitizers/src/test/java/com/example/BUILD.bazel

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,58 @@ java_fuzz_target_test(
8888
],
8989
)
9090

91+
java_fuzz_target_test(
92+
name = "FilePathTraversalPass",
93+
srcs = [
94+
"FilePathTraversalPass.java",
95+
],
96+
allowed_findings = [],
97+
fuzzer_args = [
98+
# Test does not depend on fuzzing input; just run it once
99+
"-runs=1",
100+
],
101+
target_class = "com.example.FilePathTraversalPass",
102+
runtime_deps = [
103+
"@maven//:org_junit_jupiter_junit_jupiter_engine",
104+
],
105+
deps = [
106+
"//deploy:jazzer-junit",
107+
"@maven//:org_junit_jupiter_junit_jupiter_api",
108+
],
109+
)
110+
111+
[java_fuzz_target_test(
112+
name = "FilePathTraversalCrash_" + method,
113+
srcs = [
114+
"FilePathTraversalCrash.java",
115+
],
116+
allowed_findings = [
117+
"com.code_intelligence.jazzer.api.FuzzerSecurityIssueCritical",
118+
],
119+
env = {
120+
"JAZZER_FUZZ": "1",
121+
},
122+
expect_number_of_findings = 1,
123+
fuzzer_args = [
124+
"-runs=0",
125+
],
126+
target_class = "com.example.FilePathTraversalCrash",
127+
target_method = method,
128+
verify_crash_reproducer = False,
129+
runtime_deps = [
130+
"@maven//:org_junit_jupiter_junit_jupiter_engine",
131+
],
132+
deps = [
133+
"//deploy:jazzer-junit",
134+
"@maven//:org_junit_jupiter_junit_jupiter_api",
135+
],
136+
) for method in [
137+
"beforeEachWorks",
138+
"overwritingBeforeEachWorks",
139+
"crashWhenAllowIsFalse",
140+
"crashWhenDefaultTarget",
141+
]]
142+
91143
java_fuzz_target_test(
92144
name = "OsCommandInjectionProcessBuilder",
93145
srcs = [
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2025 Code Intelligence GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example;
18+
19+
import com.code_intelligence.jazzer.api.BugDetectors;
20+
import com.code_intelligence.jazzer.api.SilentCloseable;
21+
import com.code_intelligence.jazzer.junit.FuzzTest;
22+
import java.io.FileInputStream;
23+
import java.io.FileNotFoundException;
24+
import java.io.IOException;
25+
import java.nio.file.Path;
26+
import java.nio.file.Paths;
27+
import org.junit.jupiter.api.BeforeEach;
28+
29+
public class FilePathTraversalCrash {
30+
@BeforeEach
31+
public void setUp() {
32+
BugDetectors.setFilePathTraversalTarget(() -> Paths.get("../../hello"));
33+
}
34+
35+
@FuzzTest
36+
void beforeEachWorks(boolean ignore) throws Exception {
37+
try (FileInputStream fis = new FileInputStream("../../hello")) {
38+
fis.read();
39+
} catch (FileNotFoundException ignored) {
40+
}
41+
}
42+
43+
@FuzzTest
44+
void overwritingBeforeEachWorks(boolean ignore) {
45+
try (SilentCloseable unused =
46+
BugDetectors.setFilePathTraversalTarget(() -> Paths.get("../../jazzer-hey"))) {
47+
try (FileInputStream fis = new FileInputStream("../../jazzer-hey")) {
48+
fis.read();
49+
} catch (NullPointerException | IOException ignored) {
50+
}
51+
}
52+
}
53+
54+
@FuzzTest
55+
void crashWhenAllowIsFalse(boolean ignore) {
56+
try (SilentCloseable unused = BugDetectors.setFilePathTraversalAllowPath((Path p) -> false)) {
57+
try (FileInputStream fis = new FileInputStream("whatever")) {
58+
fis.read();
59+
} catch (NullPointerException | IOException ignored) {
60+
}
61+
}
62+
}
63+
64+
@FuzzTest
65+
void crashWhenDefaultTarget(boolean ignore) {
66+
try (SilentCloseable unused = BugDetectors.setFilePathTraversalAllowPath((Path p) -> true)) {
67+
try (FileInputStream fis = new FileInputStream("../../hello")) {
68+
fis.read();
69+
} catch (NullPointerException | IOException ignored) {
70+
}
71+
}
72+
}
73+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2025 Code Intelligence GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example;
18+
19+
import com.code_intelligence.jazzer.api.BugDetectors;
20+
import com.code_intelligence.jazzer.api.SilentCloseable;
21+
import com.code_intelligence.jazzer.junit.FuzzTest;
22+
import java.io.FileInputStream;
23+
import java.io.FileNotFoundException;
24+
import java.io.IOException;
25+
import java.nio.file.Path;
26+
import java.nio.file.Paths;
27+
import org.junit.jupiter.api.BeforeEach;
28+
29+
public class FilePathTraversalPass {
30+
@BeforeEach
31+
public void setUp() {
32+
BugDetectors.setFilePathTraversalTarget(() -> Paths.get("../../hello"));
33+
}
34+
35+
@FuzzTest
36+
void beforeEachWorks(boolean ignore) throws Exception {
37+
try (FileInputStream fis = new FileInputStream("test")) {
38+
fis.read();
39+
} catch (FileNotFoundException ignored) {
40+
}
41+
}
42+
43+
@FuzzTest
44+
void overwritingBeforeEachWorks(boolean ignore) {
45+
try (SilentCloseable unused =
46+
BugDetectors.setFilePathTraversalTarget(() -> Paths.get("../../jazzer-hey"))) {
47+
try (FileInputStream fis = new FileInputStream("../../hello")) {
48+
fis.read();
49+
} catch (NullPointerException | IOException ignored) {
50+
}
51+
}
52+
}
53+
54+
@FuzzTest
55+
void allow(boolean ignore) {
56+
try (SilentCloseable unused =
57+
BugDetectors.setFilePathTraversalAllowPath((Path p) -> p.toString().contains("secret"))) {
58+
try (FileInputStream fis = new FileInputStream("my-secret-file")) {
59+
fis.read();
60+
} catch (NullPointerException | IOException ignored) {
61+
}
62+
}
63+
}
64+
65+
@FuzzTest
66+
void targetMissed(boolean ignore) {
67+
try (SilentCloseable ignored =
68+
BugDetectors.setFilePathTraversalAllowPath((Path ignoredAgain) -> true)) {
69+
try (FileInputStream fis = new FileInputStream("../../hello-world")) {
70+
fis.read();
71+
} catch (NullPointerException | IOException ignoredEvenMore) {
72+
}
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)