diff --git a/apps/api/src/ingest/service.ts b/apps/api/src/ingest/service.ts index ff189bd..f3680b3 100644 --- a/apps/api/src/ingest/service.ts +++ b/apps/api/src/ingest/service.ts @@ -5,6 +5,7 @@ import sharp from "sharp"; import type { Db } from "../db.js"; import { Prisma } from "../generated/prisma/client.js"; import { photoInclude, toSummary } from "../library/serialize.js"; +import { stripNulBytes } from "../lib/sanitize.js"; import type { Broadcaster } from "../realtime.js"; import { LocalStorage, sha256File, UrlSigner, VARIANT_SIZE } from "../storage/storage.js"; import { readEmbeddedIptc } from "./embedded.js"; @@ -177,7 +178,7 @@ export class IngestService { let photo; try { photo = await db.photo.create({ - data: { + data: stripNulBytes({ organizationId: orgId, eventId, uploaderId: origin.userId, @@ -198,7 +199,7 @@ export class IngestService { iso: camera.iso ?? null, ...(camera.extra && Object.keys(camera.extra).length && { exif: camera.extra }), ...(Object.keys(embedded).length && { embeddedIptc: embedded }), - }, + }), }); } catch (err) { // Two copies of the same file finishing at once: the other one won. diff --git a/apps/api/src/lib/sanitize.test.ts b/apps/api/src/lib/sanitize.test.ts new file mode 100644 index 0000000..0b6f352 --- /dev/null +++ b/apps/api/src/lib/sanitize.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from "vitest"; +import { stripNulBytes } from "./sanitize.js"; + +const NUL = String.fromCharCode(0); + +describe("stripNulBytes", () => { + it("removes NUL bytes anywhere in a string, not only at the end", () => { + expect(stripNulBytes(`Canon${NUL}EOS R5`)).toBe("CanonEOS R5"); + expect(stripNulBytes(`padded${NUL}${NUL}${NUL}`)).toBe("padded"); + expect(stripNulBytes("clean")).toBe("clean"); + }); + + it("recurses into plain objects and arrays, the shape of camera.extra / embeddedIptc", () => { + const input = { + cameraMake: `Canon${NUL}`, + keywords: [`sport${NUL}`, "final"], + exif: { owner: `Jo${NUL}o`, focalLength35: 85 }, + capturedAt: new Date("2026-10-15T10:00:00Z"), + eventId: null, + }; + expect(stripNulBytes(input)).toEqual({ + cameraMake: "Canon", + keywords: ["sport", "final"], + exif: { owner: "Joo", focalLength35: 85 }, + capturedAt: new Date("2026-10-15T10:00:00Z"), + eventId: null, + }); + }); + + it("leaves Date instances untouched instead of flattening them to {}", () => { + const date = new Date("2026-10-15T10:00:00Z"); + expect(stripNulBytes(date)).toBe(date); + }); +}); diff --git a/apps/api/src/lib/sanitize.ts b/apps/api/src/lib/sanitize.ts new file mode 100644 index 0000000..428a2c7 --- /dev/null +++ b/apps/api/src/lib/sanitize.ts @@ -0,0 +1,16 @@ +/** + * Postgres (`invalid byte sequence for encoding "UTF8": 0x00`, code 22021) refuses NUL bytes in `text`/`jsonb` + * columns. Cameras pad fixed-length EXIF/IPTC string fields with NUL bytes, and the readers under `ingest/` + * only trim the ones at the very end (or none at all for IPTC), so a stray byte in the middle of a value can + * still reach the insert. Run anything headed for a Prisma `data:` object through this first. + */ +const NUL = String.fromCharCode(0); + +export function stripNulBytes(value: T): T { + if (typeof value === "string") return (value.includes(NUL) ? value.split(NUL).join("") : value) as unknown as T; + if (Array.isArray(value)) return value.map(stripNulBytes) as unknown as T; + if (value !== null && typeof value === "object" && value.constructor === Object) { + return Object.fromEntries(Object.entries(value as Record).map(([k, v]) => [k, stripNulBytes(v)])) as T; + } + return value; +}