refactor(misc): Harden request handling, tighten defaults, and pin CI deps (#2878)
* refactor(misc): Harden request handling, tighten defaults, and pin CI dependencies * feat(auth): normalize usernames for login rate limiting * refactor(epub): fix XML document builder creation * feat(http): enhance image download with redirect handling and DNS rebinding protection * chore: upgrade Node.js version to 24 and add frontend dependency audit step * feat(auth): refactor username normalization for login rate limiting and enhance image download security * fix(metadata): simplify JAXB validation event handling by using direct import * chore(pipeline): remove unnecessary --force flag from npm ci and format audit step * chore(pipeline): streamline frontend dependency audits and add validation step * fix(image): improve error handling for image processing and download, enforce pixel limits * test(image): update tests * fix(icon): update DOMPurify import and replace deprecated theme package * fix(imports): update theme package imports and format code for consistency * fix(cors): update allowed origins configuration to support wildcard and trim whitespace
This commit is contained in:
@@ -18,6 +18,7 @@ import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
@@ -61,7 +62,9 @@ class FileServiceTest {
|
||||
.build();
|
||||
lenient().when(appSettingService.getAppSettings()).thenReturn(appSettings);
|
||||
|
||||
fileService = new FileService(appProperties, mock(RestTemplate.class), appSettingService);
|
||||
RestTemplate mockRestTemplate = mock(RestTemplate.class);
|
||||
RestTemplate mockNoRedirectRestTemplate = mock(RestTemplate.class);
|
||||
fileService = new FileService(appProperties, mockRestTemplate, appSettingService, mockNoRedirectRestTemplate);
|
||||
}
|
||||
|
||||
@Nested
|
||||
@@ -374,9 +377,9 @@ class FileServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidData_returnsNull() throws IOException {
|
||||
void invalidData_throwsException() {
|
||||
byte[] invalidData = "not an image".getBytes();
|
||||
assertNull(FileService.readImage(invalidData));
|
||||
assertThrows(IOException.class, () -> FileService.readImage(invalidData));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -485,11 +488,11 @@ class FileServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidImageData_skipsSave() {
|
||||
void invalidImageData_throwsException() {
|
||||
byte[] invalidData = "not an image".getBytes();
|
||||
Path outputPath = tempDir.resolve("invalid.jpg");
|
||||
|
||||
assertDoesNotThrow(() ->
|
||||
assertThrows(IOException.class, () ->
|
||||
FileService.saveImage(invalidData, outputPath.toString()));
|
||||
assertFalse(Files.exists(outputPath));
|
||||
}
|
||||
@@ -913,12 +916,11 @@ class FileServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidImageBytes_skipsThumbnail() {
|
||||
void invalidImageBytes_throwsException() {
|
||||
byte[] invalidData = "not an image".getBytes();
|
||||
|
||||
assertDoesNotThrow(() ->
|
||||
assertThrows(RuntimeException.class, () ->
|
||||
fileService.createThumbnailFromBytes(16L, invalidData));
|
||||
assertFalse(Files.exists(Path.of(fileService.getCoverFile(16L))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1031,15 +1033,14 @@ class FileServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void corruptImageData_skipsThumbnail() {
|
||||
void corruptImageData_throwsException() {
|
||||
// Valid MIME type but corrupt image data
|
||||
byte[] corruptData = ("not an image but has jpeg mime type").getBytes();
|
||||
MockMultipartFile corruptFile = new MockMultipartFile(
|
||||
"file", "corrupt.jpg", "image/jpeg", corruptData);
|
||||
|
||||
assertDoesNotThrow(() ->
|
||||
assertThrows(RuntimeException.class, () ->
|
||||
fileService.createThumbnailFromFile(12L, corruptFile));
|
||||
assertFalse(Files.exists(Path.of(fileService.getCoverFile(12L))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1176,7 +1177,7 @@ class FileServiceTest {
|
||||
.build();
|
||||
lenient().when(appSettingServiceForNetwork.getAppSettings()).thenReturn(appSettings);
|
||||
|
||||
fileService = new FileService(appProperties, restTemplate, appSettingServiceForNetwork);
|
||||
fileService = new FileService(appProperties, restTemplate, appSettingServiceForNetwork, restTemplate);
|
||||
}
|
||||
|
||||
@Nested
|
||||
@@ -1187,17 +1188,17 @@ class FileServiceTest {
|
||||
@DisplayName("downloads and returns valid image")
|
||||
@Timeout(5)
|
||||
void downloadImageFromUrl_validImage_returnsBufferedImage() throws IOException {
|
||||
String imageUrl = "http://example.com/image.jpg";
|
||||
String imageUrl = "http://1.1.1.1/image.jpg";
|
||||
BufferedImage testImage = createTestImage(100, 100);
|
||||
byte[] imageBytes = imageToBytes(testImage);
|
||||
|
||||
RestTemplate mockRestTemplate = mock(RestTemplate.class);
|
||||
AppSettingService mockAppSettingService = mock(AppSettingService.class);
|
||||
FileService testFileService = new FileService(appProperties, mockRestTemplate, mockAppSettingService);
|
||||
FileService testFileService = new FileService(appProperties, mockRestTemplate, mockAppSettingService, mockRestTemplate);
|
||||
|
||||
ResponseEntity<byte[]> responseEntity = ResponseEntity.ok(imageBytes);
|
||||
when(mockRestTemplate.exchange(
|
||||
eq(imageUrl),
|
||||
anyString(),
|
||||
eq(HttpMethod.GET),
|
||||
any(HttpEntity.class),
|
||||
eq(byte[].class)
|
||||
@@ -1214,10 +1215,10 @@ class FileServiceTest {
|
||||
@DisplayName("throws exception when response body is null")
|
||||
@Timeout(5)
|
||||
void downloadImageFromUrl_nullBody_throwsException() {
|
||||
String imageUrl = "http://example.com/image.jpg";
|
||||
String imageUrl = "http://1.1.1.1/image.jpg";
|
||||
ResponseEntity<byte[]> responseEntity = ResponseEntity.ok(null);
|
||||
when(restTemplate.exchange(
|
||||
eq(imageUrl),
|
||||
anyString(),
|
||||
eq(HttpMethod.GET),
|
||||
any(HttpEntity.class),
|
||||
eq(byte[].class)
|
||||
@@ -1228,31 +1229,38 @@ class FileServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("returns null when ImageIO cannot read bytes")
|
||||
@DisplayName("throws IOException when ImageIO cannot read bytes")
|
||||
@Timeout(5)
|
||||
void downloadImageFromUrl_invalidImageData_returnsNull() throws IOException {
|
||||
String imageUrl = "http://example.com/image.jpg";
|
||||
void downloadImageFromUrl_invalidImageData_throwsException() throws IOException {
|
||||
String imageUrl = "http://1.1.1.1/image.jpg";
|
||||
byte[] invalidBytes = "not an image".getBytes();
|
||||
ResponseEntity<byte[]> responseEntity = ResponseEntity.ok(invalidBytes);
|
||||
when(restTemplate.exchange(
|
||||
eq(imageUrl),
|
||||
// Note: using ReflectionTestUtils to get the private mock if needed,
|
||||
// but wait, setup() already created fileService with mocks.
|
||||
// We just need to know which mock to use.
|
||||
// The setup() creates and injects mockNoRedirectRestTemplate.
|
||||
|
||||
// Let's use ReflectionTestUtils to mock the correct one since the field in test class is 'restTemplate'
|
||||
RestTemplate noRedirectMock = (RestTemplate) ReflectionTestUtils.getField(fileService, "noRedirectRestTemplate");
|
||||
|
||||
when(noRedirectMock.exchange(
|
||||
anyString(),
|
||||
eq(HttpMethod.GET),
|
||||
any(HttpEntity.class),
|
||||
eq(byte[].class)
|
||||
)).thenReturn(responseEntity);
|
||||
|
||||
BufferedImage result = fileService.downloadImageFromUrl(imageUrl);
|
||||
assertNull(result);
|
||||
assertThrows(IOException.class, () -> fileService.downloadImageFromUrl(imageUrl));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("throws exception on HTTP error status")
|
||||
@Timeout(5)
|
||||
void downloadImageFromUrl_httpError_throwsException() {
|
||||
String imageUrl = "http://example.com/image.jpg";
|
||||
String imageUrl = "http://1.1.1.1/image.jpg";
|
||||
ResponseEntity<byte[]> responseEntity = ResponseEntity.notFound().build();
|
||||
when(restTemplate.exchange(
|
||||
eq(imageUrl),
|
||||
anyString(),
|
||||
eq(HttpMethod.GET),
|
||||
any(HttpEntity.class),
|
||||
eq(byte[].class)
|
||||
@@ -1271,14 +1279,14 @@ class FileServiceTest {
|
||||
@DisplayName("downloads and saves cover images successfully")
|
||||
@Timeout(5)
|
||||
void createThumbnailFromUrl_validImage_createsCoverAndThumbnail() throws IOException {
|
||||
String imageUrl = "http://example.com/cover.jpg";
|
||||
String imageUrl = "http://1.1.1.1/cover.jpg";
|
||||
long bookId = 42L;
|
||||
BufferedImage testImage = createTestImage(800, 1200); // Portrait image
|
||||
byte[] imageBytes = imageToBytes(testImage);
|
||||
|
||||
ResponseEntity<byte[]> responseEntity = ResponseEntity.ok(imageBytes);
|
||||
when(restTemplate.exchange(
|
||||
eq(imageUrl),
|
||||
anyString(),
|
||||
eq(HttpMethod.GET),
|
||||
any(HttpEntity.class),
|
||||
eq(byte[].class)
|
||||
@@ -1304,7 +1312,7 @@ class FileServiceTest {
|
||||
long bookId = 42L;
|
||||
|
||||
when(restTemplate.exchange(
|
||||
eq(imageUrl),
|
||||
anyString(),
|
||||
eq(HttpMethod.GET),
|
||||
any(HttpEntity.class),
|
||||
eq(byte[].class)
|
||||
|
||||
Reference in New Issue
Block a user