Repository navigation
Expand file tree
/
Copy pathProfileController.java
More file actions
81 lines (65 loc) · 3.28 KB
/
Copy pathProfileController.java
File metadata and controls
81 lines (65 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class ProfileController {
private final String uploadDir = "/var/app/resources/images";
private String storedUsername = "alice";
@RequestMapping("/profile")
public String processProfile(
@RequestParam(value = "username", required = true) String username, // $ Source
@RequestParam(value = "file", required = false) MultipartFile file)
throws IOException {
String extension = ".png";
// String concatenation into `new File(String)` then `MultipartFile.transferTo(File)`
String path = uploadDir + File.separator + username + extension;
file.transferTo(new File(path)); // $ Alert
File dest = new File(uploadDir + "/" + username + ".png");
file.transferTo(dest); // $ Alert
// `new File(File, String)` child argument
file.transferTo(new File(new File(uploadDir), username + ".png")); // $ Alert
// `MultipartFile.transferTo(Path)`
file.transferTo(Paths.get(uploadDir, username + ".png")); // $ Alert
file.transferTo(Paths.get(uploadDir).resolve(username + ".png")); // $ Alert
// Already detected without custom models: `File` constructor summary into a built-in sink
new FileOutputStream(new File(uploadDir + "/" + username + ".png")).close(); // $ Alert
return "profile";
}
@RequestMapping("/profile/update")
public String updateProfile(
@RequestParam(value = "username", required = true) String username, // $ Source
@RequestParam(value = "file", required = false) MultipartFile file) // $ Source
throws IOException {
// An equality check on one branch does not sanitize the merged path below
if (!username.equals(storedUsername)) {
storedUsername = username.toLowerCase();
}
if (file != null && !file.isEmpty()) {
String extension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); // $ Source
String path = uploadDir + File.separator + username + extension;
file.transferTo(new File(path)); // $ Alert
}
return "profile";
}
@RequestMapping("/profile/safe")
public String processProfileSafe(
@RequestParam(value = "username", required = true) String username,
@RequestParam(value = "file", required = false) MultipartFile file)
throws IOException {
// Constant destination: not tainted
file.transferTo(new File(uploadDir + "/avatar.png"));
// Value that does not come from the request: not a source
file.transferTo(new File(uploadDir + "/" + storedUsername + ".png"));
// Path traversal check sanitizes the request value
if (username.contains("..") || username.contains("/") || username.contains("\\")) {
return "error";
}
file.transferTo(new File(uploadDir + "/" + username + ".png"));
return "profile";
}
}