Restore the community photo after reload and reject empty, oversized, SVG, and spoofed uploads.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
adilallo
2026-09-10 15:50:32 -06:00
co-authored by Cursor
parent 6ccc1e8c8e
commit 234f3998ad
18 changed files with 1072 additions and 112 deletions
+57
View File
@@ -0,0 +1,57 @@
import { mkdtemp, readdir } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const getUploadRootFromEnvMock = vi.fn();
let uploadRoot: string | null = null;
vi.mock("../../lib/server/uploads/uploadRoot", async () => {
const actual = await vi.importActual<
typeof import("../../lib/server/uploads/uploadRoot")
>("../../lib/server/uploads/uploadRoot");
return {
...actual,
getUploadRootFromEnv: () => getUploadRootFromEnvMock(),
};
});
import { saveCreateFlowUpload } from "../../lib/server/uploads/saveCreateFlowUpload";
const PNG_1X1 = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
"base64",
);
describe("saveCreateFlowUpload", () => {
beforeEach(async () => {
uploadRoot = await mkdtemp(path.join(tmpdir(), "cr-upload-save-"));
getUploadRootFromEnvMock.mockReset();
getUploadRootFromEnvMock.mockImplementation(() => uploadRoot);
});
afterEach(() => {
uploadRoot = null;
});
it("writes a sniffed PNG even when the client declared the wrong MIME", async () => {
const saved = await saveCreateFlowUpload({
purpose: "communityAvatar",
buffer: PNG_1X1,
});
expect("error" in saved).toBe(false);
if ("error" in saved) return;
expect(saved.mimeType).toBe("image/png");
const files = await readdir(uploadRoot!);
expect(files).toEqual([`${saved.id}.png`]);
});
it("rejects spoofed bytes without writing", async () => {
const saved = await saveCreateFlowUpload({
purpose: "communityAvatar",
buffer: Buffer.from("not an image"),
});
expect(saved).toEqual({ error: "validation", reason: "invalidType" });
expect(await readdir(uploadRoot!)).toEqual([]);
});
});