fix: update corrupted image test expectations from 422 to 400

validateImageBuffer catches corrupt image data before processing
reaches the tool handler, so the correct status code is 400 (bad
request) rather than 422 (processing failure). Also fix SVGZ
watermark validation by returning early for compressed SVG (Sharp
cannot read gzip-compressed SVGZ directly) and passing the actual
watermark filename to validateImageBuffer for correct format
detection.
This commit is contained in:
SnapOtter
2026-05-14 00:09:31 +08:00
parent 2f41629a14
commit cd24bb92b6
4 changed files with 18 additions and 12 deletions
+4 -2
View File
@@ -229,10 +229,12 @@ export async function validateImageBuffer(
detectedFormat = "tga";
}
// SVGZ: gzip-compressed SVG, detected by extension + gzip magic
// SVGZ: gzip-compressed SVG, detected by extension + gzip magic.
// Return early because Sharp cannot read compressed SVGZ directly;
// decompression happens later in the route pipeline.
if (!detectedFormat && ext === "svgz") {
if (buffer.length >= 2 && buffer[0] === 0x1f && buffer[1] === 0x8b) {
detectedFormat = "svg";
return { valid: true, format: "svg", width: 0, height: 0 };
}
}
+3 -1
View File
@@ -23,6 +23,7 @@ export function registerWatermarkImage(app: FastifyInstance) {
let mainBuffer: Buffer | null = null;
let watermarkBuffer: Buffer | null = null;
let filename = "image";
let watermarkFilename = "watermark";
let settingsRaw: string | null = null;
try {
@@ -36,6 +37,7 @@ export function registerWatermarkImage(app: FastifyInstance) {
const buf = Buffer.concat(chunks);
if (part.fieldname === "watermark") {
watermarkBuffer = buf;
watermarkFilename = sanitizeFilename(part.filename ?? "watermark");
} else {
mainBuffer = buf;
filename = sanitizeFilename(part.filename ?? "image");
@@ -117,7 +119,7 @@ export function registerWatermarkImage(app: FastifyInstance) {
}
mainBuffer = await autoOrient(mainBuffer);
const valWm = await validateImageBuffer(watermarkBuffer, "watermark");
const valWm = await validateImageBuffer(watermarkBuffer, watermarkFilename);
if (!valWm.valid) {
return reply.status(400).send({ error: `Invalid watermark image: ${valWm.reason}` });
}