diff --git a/packages/server/annotate.ts b/packages/server/annotate.ts index 3cfb3f1..6803c70 100644 --- a/packages/server/annotate.ts +++ b/packages/server/annotate.ts @@ -15,6 +15,7 @@ import { mkdirSync } from "fs"; import { isRemoteSession, getServerPort } from "./remote"; import { openBrowser } from "./browser"; import { getRepoInfo } from "./repo"; +import { validateImagePath, validateUploadExtension, UPLOAD_DIR } from "./image"; // Re-export utilities export { isRemoteSession, getServerPort } from "./remote"; @@ -129,8 +130,12 @@ export async function startAnnotateServer( if (!imagePath) { return new Response("Missing path parameter", { status: 400 }); } + const validation = validateImagePath(imagePath); + if (!validation.valid) { + return new Response(validation.error!, { status: 403 }); + } try { - const file = Bun.file(imagePath); + const file = Bun.file(validation.resolved); if (!(await file.exists())) { return new Response("File not found", { status: 404 }); } @@ -149,10 +154,12 @@ export async function startAnnotateServer( return new Response("No file provided", { status: 400 }); } - const ext = file.name.split(".").pop() || "png"; - const tempDir = "/tmp/plannotator"; - mkdirSync(tempDir, { recursive: true }); - const tempPath = `${tempDir}/${crypto.randomUUID()}.${ext}`; + const extResult = validateUploadExtension(file.name); + if (!extResult.valid) { + return Response.json({ error: extResult.error }, { status: 400 }); + } + mkdirSync(UPLOAD_DIR, { recursive: true }); + const tempPath = `${UPLOAD_DIR}/${crypto.randomUUID()}.${extResult.ext}`; await Bun.write(tempPath, file); return Response.json({ path: tempPath, originalName: file.name }); diff --git a/packages/server/image.ts b/packages/server/image.ts new file mode 100644 index 0000000..bfeafa1 --- /dev/null +++ b/packages/server/image.ts @@ -0,0 +1,65 @@ +import { resolve } from "path"; + +const ALLOWED_IMAGE_EXTENSIONS = new Set([ + "png", + "jpg", + "jpeg", + "gif", + "webp", + "svg", + "bmp", + "ico", + "tiff", + "tif", + "avif", +]); + +const UPLOAD_DIR = "/tmp/plannotator"; + +function getExtension(filePath: string): string { + const lastDot = filePath.lastIndexOf("."); + if (lastDot === -1) return ""; + return filePath.slice(lastDot + 1).toLowerCase(); +} + +function hasImageExtension(filePath: string): boolean { + return ALLOWED_IMAGE_EXTENSIONS.has(getExtension(filePath)); +} + +export function validateImagePath(rawPath: string): { + valid: boolean; + resolved: string; + error?: string; +} { + const resolved = resolve(rawPath); + + if (!hasImageExtension(resolved)) { + return { + valid: false, + resolved, + error: "Path does not point to a supported image file", + }; + } + + return { valid: true, resolved }; +} + +export function validateUploadExtension(fileName: string): { + valid: boolean; + ext: string; + error?: string; +} { + const ext = getExtension(fileName) || "png"; + + if (!ALLOWED_IMAGE_EXTENSIONS.has(ext)) { + return { + valid: false, + ext, + error: `File extension ".${ext}" is not a supported image type`, + }; + } + + return { valid: true, ext }; +} + +export { UPLOAD_DIR }; diff --git a/packages/server/index.ts b/packages/server/index.ts index 3642e1e..b0c1457 100644 --- a/packages/server/index.ts +++ b/packages/server/index.ts @@ -12,6 +12,7 @@ import { mkdirSync } from "fs"; import { isRemoteSession, getServerPort } from "./remote"; import { openBrowser } from "./browser"; +import { validateImagePath, validateUploadExtension, UPLOAD_DIR } from "./image"; import { detectObsidianVaults, saveToObsidian, @@ -146,8 +147,12 @@ export async function startPlannotatorServer( if (!imagePath) { return new Response("Missing path parameter", { status: 400 }); } + const validation = validateImagePath(imagePath); + if (!validation.valid) { + return new Response(validation.error!, { status: 403 }); + } try { - const file = Bun.file(imagePath); + const file = Bun.file(validation.resolved); if (!(await file.exists())) { return new Response("File not found", { status: 404 }); } @@ -166,10 +171,12 @@ export async function startPlannotatorServer( return new Response("No file provided", { status: 400 }); } - const ext = file.name.split(".").pop() || "png"; - const tempDir = "/tmp/plannotator"; - mkdirSync(tempDir, { recursive: true }); - const tempPath = `${tempDir}/${crypto.randomUUID()}.${ext}`; + const extResult = validateUploadExtension(file.name); + if (!extResult.valid) { + return Response.json({ error: extResult.error }, { status: 400 }); + } + mkdirSync(UPLOAD_DIR, { recursive: true }); + const tempPath = `${UPLOAD_DIR}/${crypto.randomUUID()}.${extResult.ext}`; await Bun.write(tempPath, file); return Response.json({ path: tempPath, originalName: file.name }); diff --git a/packages/server/review.ts b/packages/server/review.ts index 4744863..696617d 100644 --- a/packages/server/review.ts +++ b/packages/server/review.ts @@ -14,6 +14,7 @@ import { isRemoteSession, getServerPort } from "./remote"; import { openBrowser } from "./browser"; import { type DiffType, type GitContext, runGitDiff } from "./git"; import { getRepoInfo } from "./repo"; +import { validateImagePath, validateUploadExtension, UPLOAD_DIR } from "./image"; // Re-export utilities export { isRemoteSession, getServerPort } from "./remote"; @@ -174,8 +175,12 @@ export async function startReviewServer( if (!imagePath) { return new Response("Missing path parameter", { status: 400 }); } + const validation = validateImagePath(imagePath); + if (!validation.valid) { + return new Response(validation.error!, { status: 403 }); + } try { - const file = Bun.file(imagePath); + const file = Bun.file(validation.resolved); if (!(await file.exists())) { return new Response("File not found", { status: 404 }); } @@ -194,10 +199,12 @@ export async function startReviewServer( return new Response("No file provided", { status: 400 }); } - const ext = file.name.split(".").pop() || "png"; - const tempDir = "/tmp/plannotator"; - mkdirSync(tempDir, { recursive: true }); - const tempPath = `${tempDir}/${crypto.randomUUID()}.${ext}`; + const extResult = validateUploadExtension(file.name); + if (!extResult.valid) { + return Response.json({ error: extResult.error }, { status: 400 }); + } + mkdirSync(UPLOAD_DIR, { recursive: true }); + const tempPath = `${UPLOAD_DIR}/${crypto.randomUUID()}.${extResult.ext}`; await Bun.write(tempPath, file); return Response.json({ path: tempPath, originalName: file.name });