1

I am adding a POST endpoint to a Spring Boot REST service to allow it to accept file uploads from web apps and other sources:

@PostMapping("/fileUpload") public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) { // ... } 

I'd like to run a security check against this file and see if there's anything malicious inside of it. Does Spring have any tools or libs to help with such an effort?

1
  • Thanks @Drew1208 but I believe that has to do with RBAC and verifying which users have permissions to access methods. I'm not talking about authorization here, I'm wondering if Spring Boot or Spring Security have anything in the way of scanning files for malicious content. Commented Feb 1, 2018 at 19:41

2 Answers 2

4

Add the ClamAV Java library (ClamAV4J) as a dependency in your project's build file.

Create a service class that uses the ClamAV4J library to scan files for viruses.

@Service public class VirusScanService { private final ClamAVClient client; public VirusScanService(ClamAVClient client) { this.client = client; } public boolean isFileInfected(MultipartFile file) throws IOException { return client.scan(file.getInputStream()).isInfected(); } } @RestController public class FileUploadController { private final VirusScanService virusScanService; public FileUploadController(VirusScanService virusScanService) { this.virusScanService = virusScanService; } @PostMapping("/upload") public String handleFileUpload(@RequestParam("file") MultipartFile file) throws IOException { if (virusScanService.isFileInfected(file)) { return "The file is infected!"; } else { // save the file and return a success message return "File uploaded successfully!"; } } } 
Sign up to request clarification or add additional context in comments.

1 Comment

there isnt something like ClamAVClient in ClamAV4J dependency
1

Spring Security does not provide any type of scanning for malicious files. You will need to use an anti virus tool that provides a Java API that you can use in your application. Off the top of my head I know Symantec offers a Java API, have a look here.

https://www.symantec.com/connect/articles/how-use-symantec-scan-engine-52-content-scanning-technologies-direct-integration-your-appli

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.