mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
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:
@@ -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 };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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}` });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user