SUPPORT-8841: add check if file is password protected

This commit is contained in:
Alexandr Shalaginov 2024-12-30 09:53:13 +03:00
parent 5aa56f344e
commit 78985aab33

View file

@ -2,6 +2,7 @@ package ru.micord.av.service.service;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -24,6 +25,8 @@ public class FileScanService {
private static final Logger LOGGER = LoggerFactory.getLogger(FileScanService.class); private static final Logger LOGGER = LoggerFactory.getLogger(FileScanService.class);
@Value("${file.upload.directory}") @Value("${file.upload.directory}")
private String uploadDirectory; private String uploadDirectory;
@Value("${password.protected.result.name}:Password-protected objects")
private String passwordProtectedResultName;
public ScanResult scanFile(MultipartFile file) { public ScanResult scanFile(MultipartFile file) {
File tempFile = null; File tempFile = null;
@ -62,29 +65,47 @@ public class FileScanService {
); );
processBuilder.redirectErrorStream(true); processBuilder.redirectErrorStream(true);
Process process = processBuilder.start(); Process process = processBuilder.start();
int exitCode = process.waitFor();
if (exitCode == 0) { try (InputStream inputStream = process.getInputStream()) {
return new ScanResult( String result = new String(inputStream.readAllBytes());
null, checkScanResult(result);
null,
100, int exitCode = process.waitFor();
null, if (exitCode == 0) {
"completed", return new ScanResult(
new String[] {ScanResult.Scan.VERDICT_CLEAN} null,
); null,
} 100,
else if (exitCode == 72) { null,
return new ScanResult( "completed",
null, new String[] {ScanResult.Scan.VERDICT_CLEAN}
null, );
100, }
null, else if (exitCode == 72) {
"completed", return new ScanResult(
new String[] {ScanResult.Scan.VERDICT_INFECTED} null,
); null,
} 100,
else { null,
throw new AvException("KESL error, exit code: " + exitCode); "completed",
new String[] {ScanResult.Scan.VERDICT_INFECTED}
);
}
else {
throw new AvException("KESL error, exit code: " + exitCode);
}
} }
} }
private void checkScanResult(String result) {
for (String line : result.split("\n")) {
String[] linePart = line.split(":");
if (linePart.length > 1) {
if (linePart[0].startsWith(passwordProtectedResultName)
&& Integer.parseInt(linePart[1].trim()) > 0) {
throw new AvException("Detected password-protected file");
}
}
}
}
} }