Skip to content

Commit 566ae1e

Browse files
committed
Add a test for java
Add end-to-end test for Symbol Reachability pipeline Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent c9414ea commit 566ae1e

7 files changed

Lines changed: 381 additions & 7 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.nio.file.Files;
2+
import java.nio.file.Paths;
3+
import java.util.Map;
4+
5+
public class App {
6+
public static boolean debug = true;
7+
8+
public static class ReportGenerator {
9+
private String baseDir;
10+
11+
public ReportGenerator(String baseDir) {
12+
this.baseDir = baseDir;
13+
}
14+
15+
public String getBaseDir() {
16+
return baseDir;
17+
}
18+
}
19+
20+
public static String serveReport(Map<String, String> requestPayload) {
21+
ReportGenerator generator = new ReportGenerator("/var/reports");
22+
String requestedFile = requestPayload.get("file");
23+
24+
if (requestedFile == null || requestedFile.isEmpty()) {
25+
return "Error: No file specified";
26+
}
27+
28+
// VULNERABLE: Direct concatenation allows Path Traversal
29+
// An attacker passing "../../etc/passwd" could read system files.
30+
String targetPath = buildFilePath(generator, requestedFile);
31+
32+
try {
33+
if (Files.exists(Paths.get(targetPath))) {
34+
return "Serving content of " + targetPath;
35+
}
36+
} catch (Exception e) {
37+
return "Error: Invalid path";
38+
}
39+
40+
return "Error: File not found";
41+
}
42+
43+
private static String buildFilePath(ReportGenerator generator, String filename) {
44+
return Paths.get(generator.getBaseDir(), filename).toString();
45+
}
46+
47+
public static String unrelatedTopLevelFunction() {
48+
return "I am just here to add AST complexity.";
49+
}
50+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.nio.file.Files;
2+
import java.nio.file.Path;
3+
import java.nio.file.Paths;
4+
import java.util.Map;
5+
6+
public class App {
7+
public static boolean debug = false;
8+
9+
public static class ReportGenerator {
10+
public String baseDir;
11+
12+
public ReportGenerator(String baseDir) {
13+
this.baseDir = baseDir;
14+
}
15+
}
16+
17+
public static String serveReport(Map<String, String> requestPayload) {
18+
ReportGenerator generator = new ReportGenerator("/var/reports");
19+
String requestedFile = requestPayload.get("file");
20+
21+
if (requestedFile == null || requestedFile.isEmpty()) {
22+
return "Error: No file specified";
23+
}
24+
25+
String targetPath;
26+
try {
27+
targetPath = buildFilePath(generator, requestedFile);
28+
} catch (Exception e) {
29+
return "Error: Invalid path";
30+
}
31+
32+
try {
33+
if (Files.exists(Paths.get(targetPath))) {
34+
return "Serving content of " + targetPath;
35+
}
36+
} catch (Exception e) {
37+
return "Error: Invalid path";
38+
}
39+
40+
return "Error: File not found";
41+
}
42+
43+
/**
44+
* FIXED: Validate that the resolved path stays within the base_dir
45+
*/
46+
private static String buildFilePath(ReportGenerator generator, String filename) throws Exception {
47+
Path base = Paths.get(generator.baseDir).toAbsolutePath().normalize();
48+
Path target = Paths.get(generator.baseDir, filename).toAbsolutePath().normalize();
49+
50+
if (!target.startsWith(base)) {
51+
throw new Exception("Path Traversal Detected");
52+
}
53+
return target.toString();
54+
}
55+
56+
57+
public static String unrelatedTopLevelFunction() {
58+
return "I am just here to add AST complexity.";
59+
}
60+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.nio.file.Files;
2+
import java.nio.file.Paths;
3+
import java.util.Map;
4+
5+
public class App {
6+
public static boolean debug = true;
7+
8+
public static class ReportGenerator {
9+
public String baseDir;
10+
11+
public ReportGenerator(String baseDir) {
12+
this.baseDir = baseDir;
13+
}
14+
}
15+
16+
public static String serveReport(Map<String, String> requestPayload) {
17+
ReportGenerator generator = new ReportGenerator("/var/reports");
18+
String requestedFile = requestPayload.get("file");
19+
20+
if (requestedFile == null || requestedFile.isEmpty()) {
21+
return "Error: No file specified";
22+
}
23+
24+
// VULNERABLE: Direct concatenation allows Path Traversal
25+
// An attacker passing "../../etc/passwd" could read system files.
26+
String targetPath = buildFilePath(generator, requestedFile);
27+
28+
try {
29+
if (Files.exists(Paths.get(targetPath))) {
30+
return "Serving content of " + targetPath;
31+
}
32+
} catch (Exception e) {
33+
return "Error: Invalid path";
34+
}
35+
36+
return "Error: File not found";
37+
}
38+
39+
private static String buildFilePath(ReportGenerator generator, String filename) {
40+
return Paths.get(generator.baseDir, filename).toString();
41+
}
42+
43+
public static String unrelatedTopLevelFunction() {
44+
return "I am just here to add AST complexity.";
45+
}
46+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)