-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
v1ll4n
committed
Aug 7, 2024
1 parent
c91590a
commit 213f551
Showing
4 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
desc( | ||
title: 'checking [Directly relative Files.write from MultipartFile]', | ||
type: audit, | ||
level: warning, | ||
) | ||
|
||
<include('spring-param')>?{<typeName>?{have: MultipartFile}} as $upload; | ||
Files.write(*?{!opcode: param} as $sink); | ||
|
||
$sink #{ | ||
until: `*?{<typeName>?{have: MultipartFile}} & $upload as $inter` | ||
}-> | ||
|
||
check $inter then "Upload and Write Files directly" else "No Upload and Files.write" | ||
alert $inter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.example.blog.controller; | ||
|
||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
@RestController | ||
@RequestMapping("/api/blog") | ||
public class ImageUploadController { | ||
|
||
private static final String UPLOAD_DIR = "/opt/blog/uploads/"; | ||
|
||
@PostMapping("/upload-image") | ||
public String handleImageUpload(@RequestParam("image") MultipartFile file) { | ||
if (!file.isEmpty()) { | ||
try { | ||
byte[] bytes = file.getBytes(); | ||
String fileName = file.getOriginalFilename(); | ||
String extension = fileName.substring(fileName.lastIndexOf(".") + 1); | ||
if (!extension.equalsIgnoreCase("png") && !extension.equalsIgnoreCase("jpg") && !extension.equalsIgnoreCase("jpeg")) { | ||
return "Only PNG, JPG and JPEG images are allowed."; | ||
} | ||
Path path = Paths.get(UPLOAD_DIR + fileName); | ||
Files.write(path, bytes); | ||
return "Image uploaded successfully: " + fileName; | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return "Failed to upload image: " + file.getOriginalFilename(); | ||
} | ||
} else { | ||
return "Failed to upload image because the file was empty."; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
./target/ | ||
target | ||
target/ | ||
target/** | ||
logs/** |