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.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -24,6 +25,8 @@ public class FileScanService {
private static final Logger LOGGER = LoggerFactory.getLogger(FileScanService.class);
@Value("${file.upload.directory}")
private String uploadDirectory;
@Value("${password.protected.result.name}:Password-protected objects")
private String passwordProtectedResultName;
public ScanResult scanFile(MultipartFile file) {
File tempFile = null;
@ -62,29 +65,47 @@ public class FileScanService {
);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
int exitCode = process.waitFor();
if (exitCode == 0) {
return new ScanResult(
null,
null,
100,
null,
"completed",
new String[] {ScanResult.Scan.VERDICT_CLEAN}
);
}
else if (exitCode == 72) {
return new ScanResult(
null,
null,
100,
null,
"completed",
new String[] {ScanResult.Scan.VERDICT_INFECTED}
);
}
else {
throw new AvException("KESL error, exit code: " + exitCode);
try (InputStream inputStream = process.getInputStream()) {
String result = new String(inputStream.readAllBytes());
checkScanResult(result);
int exitCode = process.waitFor();
if (exitCode == 0) {
return new ScanResult(
null,
null,
100,
null,
"completed",
new String[] {ScanResult.Scan.VERDICT_CLEAN}
);
}
else if (exitCode == 72) {
return new ScanResult(
null,
null,
100,
null,
"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");
}
}
}
}
}