mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
fix: file library upload accepts all file types, not just images
The upload endpoint was rejecting non-image files via validateImageBuffer. Now non-image files skip validation and use the MIME type from the multipart upload. Width/height are null for non-image files.
This commit is contained in:
@@ -250,16 +250,12 @@ export async function userFileRoutes(app: FastifyInstance): Promise<void> {
|
|||||||
|
|
||||||
if (buffer.length === 0) continue;
|
if (buffer.length === 0) continue;
|
||||||
|
|
||||||
// Validate image
|
// Try image validation; non-image files skip validation and use MIME from extension
|
||||||
const validation = await validateImageBuffer(buffer, part.filename);
|
const validation = await validateImageBuffer(buffer, part.filename).catch(() => null);
|
||||||
if (!validation.valid) {
|
const isValidImage = validation?.valid === true;
|
||||||
return reply.status(400).send({
|
|
||||||
error: `Invalid file "${part.filename}": ${validation.reason}`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sanitize SVG uploads to prevent XXE, SSRF, and script injection
|
// Sanitize SVG uploads to prevent XXE, SSRF, and script injection
|
||||||
const safeBuffer = isSvgBuffer(buffer) ? sanitizeSvg(buffer) : buffer;
|
const safeBuffer = isValidImage && isSvgBuffer(buffer) ? sanitizeSvg(buffer) : buffer;
|
||||||
|
|
||||||
// Re-check quota with actual file size before persisting
|
// Re-check quota with actual file size before persisting
|
||||||
try {
|
try {
|
||||||
@@ -270,7 +266,9 @@ export async function userFileRoutes(app: FastifyInstance): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const safeName = sanitizeFilename(part.filename ?? "upload");
|
const safeName = sanitizeFilename(part.filename ?? "upload");
|
||||||
const mimeType = formatToMime(validation.format);
|
const mimeType = isValidImage
|
||||||
|
? formatToMime(validation.format)
|
||||||
|
: part.mimetype || "application/octet-stream";
|
||||||
|
|
||||||
// Persist to disk
|
// Persist to disk
|
||||||
const storedName = await saveFile(safeBuffer, safeName);
|
const storedName = await saveFile(safeBuffer, safeName);
|
||||||
@@ -286,8 +284,8 @@ export async function userFileRoutes(app: FastifyInstance): Promise<void> {
|
|||||||
storedName,
|
storedName,
|
||||||
mimeType,
|
mimeType,
|
||||||
size: fileSize,
|
size: fileSize,
|
||||||
width: validation.width,
|
width: isValidImage ? validation.width : null,
|
||||||
height: validation.height,
|
height: isValidImage ? validation.height : null,
|
||||||
version: 1,
|
version: 1,
|
||||||
parentId: null,
|
parentId: null,
|
||||||
toolChain: null,
|
toolChain: null,
|
||||||
|
|||||||
Reference in New Issue
Block a user