Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public ResponseEntity<?> getProfilePicture(HttpServletRequest request) {
}
try {
var id = request.getParameter("id");
ensurePathIsRelativeToDest(catPicturesDirectory, (id == null ? RandomUtils.nextInt(1, 11) : id) + ".jpg");
var catPicture =
new File(catPicturesDirectory, (id == null ? RandomUtils.nextInt(1, 11) : id) + ".jpg");

Expand All @@ -113,4 +114,26 @@ public ResponseEntity<?> getProfilePicture(HttpServletRequest request) {

return ResponseEntity.badRequest().build();
}

private static void ensurePathIsRelativeToDest(File dest, String path) {
File file = new File(dest, path);
String destCanonicalPath;
String fileCanonicalPath;

try {
destCanonicalPath = dest.getCanonicalPath();
fileCanonicalPath = file.getCanonicalPath();
} catch (IOException e) {
throw new RuntimeException("Potential directory traversal attempt", e);
}

if (!fileCanonicalPath.startsWith(destCanonicalPath + File.separator)) {
throw new RuntimeException("Potential directory traversal attempt");
}
}


private static void ensurePathIsRelativeToDest(String dest, String path) {
ensurePathIsRelativeToDest(new File(dest), path);
}
}