Skip to content

Commit 9ddfc06

Browse files
committed
Merge branch 'develop'
2 parents 44e43a8 + 6c9b0a3 commit 9ddfc06

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

backend/src/main/java/net/modtale/service/security/validation/FileValidationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void validateGalleryImage(MultipartFile file) {
8181
if (file == null || file.isEmpty()) {
8282
return;
8383
}
84-
projectImageValidationService.validateImage(file, 16.0 / 9.0, "Gallery", "16:9");
84+
projectImageValidationService.validateImage(file, "Gallery");
8585
}
8686

8787
public static class ManifestInspection {

backend/src/main/java/net/modtale/service/security/validation/ProjectImageValidationService.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ public class ProjectImageValidationService {
2525
private static final Pattern SVG_WIDTH_PATTERN = Pattern.compile("width\\s*=\\s*['\"]([^'\"]+)['\"]", Pattern.CASE_INSENSITIVE);
2626
private static final Pattern SVG_HEIGHT_PATTERN = Pattern.compile("height\\s*=\\s*['\"]([^'\"]+)['\"]", Pattern.CASE_INSENSITIVE);
2727

28+
public void validateImage(MultipartFile file, String type) {
29+
validateImage(file, null, type, null);
30+
}
31+
2832
public void validateImage(MultipartFile file, double targetRatio, String type, String ratioLabel) {
33+
validateImage(file, Double.valueOf(targetRatio), type, ratioLabel);
34+
}
35+
36+
private void validateImage(MultipartFile file, Double targetRatio, String type, String ratioLabel) {
2937
if (file.getSize() > MAX_IMAGE_FILE_SIZE) {
3038
throw new InvalidProjectRequestException(type + " image size must not exceed 10MB.");
3139
}
@@ -61,7 +69,7 @@ public void validateImage(MultipartFile file, double targetRatio, String type, S
6169
}
6270

6371
double actualRatio = (double) image.getWidth() / image.getHeight();
64-
if (Math.abs(actualRatio - targetRatio) > 0.05) {
72+
if (targetRatio != null && Math.abs(actualRatio - targetRatio) > 0.05) {
6573
throw new InvalidProjectRequestException(
6674
String.format("%s image must have an aspect ratio of %s (Uploaded: %.2f).", type, ratioLabel, actualRatio)
6775
);
@@ -88,7 +96,7 @@ private boolean isLikelySvg(MultipartFile file, byte[] bytes) {
8896
return sample.startsWith("<?xml") || sample.contains("<svg");
8997
}
9098

91-
private void validateSvgAspectRatio(byte[] bytes, String type, String ratioLabel, double targetRatio) {
99+
private void validateSvgAspectRatio(byte[] bytes, String type, String ratioLabel, Double targetRatio) {
92100
String svgText = new String(bytes, StandardCharsets.UTF_8);
93101
Matcher svgTagMatcher = SVG_TAG_PATTERN.matcher(svgText);
94102
if (!svgTagMatcher.find()) {
@@ -122,7 +130,7 @@ private void validateSvgAspectRatio(byte[] bytes, String type, String ratioLabel
122130
}
123131

124132
double actualRatio = width / height;
125-
if (Math.abs(actualRatio - targetRatio) > 0.05) {
133+
if (targetRatio != null && Math.abs(actualRatio - targetRatio) > 0.05) {
126134
throw new InvalidProjectRequestException(
127135
String.format("%s image must have an aspect ratio of %s (Uploaded: %.2f).", type, ratioLabel, actualRatio)
128136
);

backend/src/test/java/net/modtale/service/security/validation/FileValidationServiceTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.modtale.service.security.validation;
22

3+
import java.awt.image.BufferedImage;
34
import java.io.ByteArrayOutputStream;
45
import java.io.IOException;
56
import java.nio.charset.StandardCharsets;
@@ -8,6 +9,7 @@
89
import java.util.Map;
910
import java.util.zip.ZipEntry;
1011
import java.util.zip.ZipOutputStream;
12+
import javax.imageio.ImageIO;
1113
import org.junit.jupiter.api.Test;
1214
import org.springframework.mock.web.MockMultipartFile;
1315
import tools.jackson.databind.ObjectMapper;
@@ -149,6 +151,16 @@ void validateProjectFileRejectsNestedArchivesForArtProjects() throws IOException
149151
assertEquals("Security Violation: Nested archives (.zip) are not allowed in ART", error.getMessage());
150152
}
151153

154+
@Test
155+
void validateGalleryImageAllowsNonSixteenByNineImages() throws IOException {
156+
BufferedImage image = new BufferedImage(800, 500, BufferedImage.TYPE_INT_RGB);
157+
ByteArrayOutputStream out = new ByteArrayOutputStream();
158+
ImageIO.write(image, "png", out);
159+
MockMultipartFile file = new MockMultipartFile("file", "gallery.png", "image/png", out.toByteArray());
160+
161+
fileValidationService.validateGalleryImage(file);
162+
}
163+
152164
private static String validManifest() {
153165
return """
154166
{

0 commit comments

Comments
 (0)