feat: validate book image correctly
Node.js CI / CI (pull_request) Failing after 1m10s
Security Scan and Upload / Security & DefectDojo Upload (pull_request) Successful in 1m13s

This commit is contained in:
2026-02-20 16:40:46 -08:00
parent aa6252d79f
commit b3eac4f584
+17 -2
View File
@@ -10,6 +10,7 @@ import {
validateUrl, validateUrl,
validateRating, validateRating,
validateStringLength, validateStringLength,
validateDataUrl,
MAX_LENGTHS, MAX_LENGTHS,
} from "../utils/validation"; } from "../utils/validation";
@@ -44,10 +45,24 @@ export class BookService {
throw new Error("Rating must be an integer between 0 and 10."); throw new Error("Rating must be an integer between 0 and 10.");
} }
// Validate cover image URL if (data.coverImage) {
if (data.coverImage && !validateUrl(data.coverImage)) { if (data.coverImage.startsWith("data:")) {
const sizeInBytes = data.coverImage.length * 0.75;
if (sizeInBytes > MAX_LENGTHS.IMAGE_DATA) {
throw new Error("Cover image must be under 5MB.");
}
if (!validateDataUrl(data.coverImage)) {
throw new Error("Invalid image data URL.");
}
} else {
if (!validateStringLength(data.coverImage, MAX_LENGTHS.URL)) {
throw new Error(`Cover image URL must be ${MAX_LENGTHS.URL} characters or less.`);
}
if (!validateUrl(data.coverImage)) {
throw new Error("Invalid cover image URL. Only http and https URLs are allowed."); throw new Error("Invalid cover image URL. Only http and https URLs are allowed.");
} }
}
}
// Validate tags // Validate tags
if (data.tags) { if (data.tags) {