Skip to content

Commit 1356521

Browse files
committed
feat: add path_traversal
1 parent 4ede83a commit 1356521

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

src/main/java/org/joychou/controller/PathTraversal.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import org.springframework.web.bind.annotation.GetMapping;
88
import org.springframework.web.bind.annotation.RestController;
99

10+
import java.io.BufferedReader;
1011
import java.io.File;
12+
import java.io.FileReader;
1113
import java.io.IOException;
1214
import java.nio.charset.StandardCharsets;
1315
import java.nio.file.Files;
@@ -23,7 +25,7 @@ public class PathTraversal {
2325
*/
2426
@GetMapping("/path_traversal/vul")
2527
public String getImage(String filepath) throws IOException {
26-
return getImgBase64(filepath);
28+
return newFile(filepath);
2729
}
2830

2931
@GetMapping("/path_traversal/sec")
@@ -34,7 +36,14 @@ public String getImageSec(String filepath) throws IOException {
3436
}
3537
return getImgBase64(filepath);
3638
}
37-
39+
public static String newFile(String text) {
40+
try {
41+
File f = new File(text);
42+
return readFile(f);
43+
} catch (Exception e) {
44+
return e.getMessage();
45+
}
46+
}
3847
private String getImgBase64(String imgFile) throws IOException {
3948

4049
logger.info("Working directory: " + System.getProperty("user.dir"));
@@ -53,4 +62,20 @@ public static void main(String[] argv) throws IOException {
5362
String aa = new String(Files.readAllBytes(Paths.get("pom.xml")), StandardCharsets.UTF_8);
5463
System.out.println(aa);
5564
}
65+
private static String readFile(File file) {
66+
try {
67+
FileReader fileStream = new FileReader(file);
68+
BufferedReader bufferedReader = new BufferedReader(fileStream);
69+
70+
String line = null;
71+
StringBuilder sb = new StringBuilder();
72+
73+
while ((line = bufferedReader.readLine()) != null) {
74+
sb.append(line).append("\n");
75+
}
76+
return sb.toString();
77+
} catch (IOException e) {
78+
return "file read failed: " + e;
79+
}
80+
}
5681
}

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
spring.datasource.url=jdbc:mysql://localhost:3306/java_sec_code?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC
2+
spring.datasource.url=jdbc:mysql://j_mysql:3306/java_sec_code?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC
33
spring.datasource.username=root
44
spring.datasource.password=woshishujukumima
55
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

0 commit comments

Comments
 (0)