Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

feat: add backup import transforms — id map, FK rewrite, path sanitization and emptiness policy

Khuram Niaz3bf01665666a
diff --git a/__tests__/backupIdMap.spec.ts b/__tests__/backupIdMap.spec.ts
new file mode 100644
index 0000000..8f79db6
--- /dev/null
+++ b/__tests__/backupIdMap.spec.ts
@@ -0,0 +1,133 @@
+import { IdMap, buildCreateData } from "@/lib/backup/idmap";
+
+describe("IdMap", () => {
+ it("mints a fresh uuid and remembers it", () => {
+ const map = new IdMap();
+ const minted = map.mint("old-1");
+ expect(minted).not.toBe("old-1");
+ expect(minted).toMatch(/^[0-9a-f-]{36}$/);
+ expect(map.get("old-1")).toBe(minted);
+ });
+
+ it("mints distinct ids for distinct rows", () => {
+ const map = new IdMap();
+ expect(map.mint("a")).not.toBe(map.mint("b"));
+ });
+
+ it("lets a lookup resolution overwrite with an existing row id", () => {
+ const map = new IdMap();
+ map.set("old-company", "existing-company");
+ expect(map.get("old-company")).toBe("existing-company");
+ });
+});
+
+describe("buildCreateData", () => {
+ function mapWith(pairs: Record<string, string>): IdMap {
+ const map = new IdMap();
+ for (const [oldId, newId] of Object.entries(pairs)) map.set(oldId, newId);
+ return map;
+ }
+
+ it("forces the ownership column to the session user, ignoring the file", () => {
+ const map = mapWith({ "old-note": "new-note", "old-job": "new-job" });
+ const data = buildCreateData(
+ "Note",
+ { id: "old-note", jobId: "old-job", content: "hi", userId: "ATTACKER" },
+ map,
+ "session-user",
+ );
+ expect(data.userId).toBe("session-user");
+ });
+
+ it("uses createdBy for models owned by createdBy", () => {
+ const map = new IdMap();
+ map.set("old-q", "new-q");
+ const data = buildCreateData(
+ "Question",
+ { id: "old-q", question: "why", createdBy: "ATTACKER" },
+ map,
+ "session-user",
+ );
+ expect(data.createdBy).toBe("session-user");
+ expect(data).not.toHaveProperty("userId");
+ });
+
+ it("rewrites the row id and every declared foreign key through the map", () => {
+ const map = mapWith({
+ "old-skill": "new-skill",
+ "old-tag": "new-tag",
+ "old-section": "new-section",
+ });
+ const data = buildCreateData(
+ "Skill",
+ {
+ id: "old-skill",
+ tagId: "old-tag",
+ resumeSectionId: "old-section",
+ order: 2,
+ category: "Languages",
+ },
+ map,
+ "u",
+ );
+ expect(data).toMatchObject({
+ id: "new-skill",
+ tagId: "new-tag",
+ resumeSectionId: "new-section",
+ order: 2,
+ category: "Languages",
+ });
+ });
+
+ it("leaves a null foreign key null", () => {
+ const map = mapWith({ "old-act": "new-act", "old-type": "new-type" });
+ const data = buildCreateData(
+ "Activity",
+ { id: "old-act", activityTypeId: "old-type", taskId: null, activityName: "x" },
+ map,
+ "u",
+ );
+ expect(data.taskId).toBeNull();
+ });
+
+ it("nulls an optional foreign key whose target is not in the backup", () => {
+ const map = mapWith({ "old-job": "new-job", "old-title": "new-title", "old-co": "new-co" });
+ const data = buildCreateData(
+ "Job",
+ {
+ id: "old-job",
+ jobTitleId: "old-title",
+ companyId: "old-co",
+ // A cover letter that is not in the file at all.
+ coverLetterId: "ghost",
+ description: "d",
+ },
+ map,
+ "u",
+ );
+ expect(data.coverLetterId).toBeNull();
+ });
+
+ it("throws when a required foreign key is dangling", () => {
+ const map = mapWith({ "old-skill": "new-skill", "old-section": "new-section" });
+ expect(() =>
+ buildCreateData(
+ "Skill",
+ { id: "old-skill", tagId: "ghost", resumeSectionId: "old-section" },
+ map,
+ "u",
+ ),
+ ).toThrow(/Skill\.tagId/);
+ });
+
+ it("never leaks the backup's own ids into the created row", () => {
+ const map = mapWith({ "old-resume": "new-resume", "old-profile": "new-profile" });
+ const data = buildCreateData(
+ "Resume",
+ { id: "old-resume", profileId: "old-profile", FileId: null, title: "CV" },
+ map,
+ "u",
+ );
+ expect(JSON.stringify(data)).not.toContain("old-");
+ });
+});
diff --git a/__tests__/backupNormalize.spec.ts b/__tests__/backupNormalize.spec.ts
new file mode 100644
index 0000000..394330a
--- /dev/null
+++ b/__tests__/backupNormalize.spec.ts
@@ -0,0 +1,156 @@
+import path from "path";
+import {
+ safeEntryName,
+ importedFilePath,
+ checkImportedFile,
+ normalizeAutomationRun,
+ normalizeAutomation,
+} from "@/lib/backup/normalize";
+import { APP_CONSTANTS } from "@/lib/constants";
+
+describe("safeEntryName", () => {
+ it("reduces a traversal path to its basename", () => {
+ expect(safeEntryName("../../etc/passwd")).toBe("passwd");
+ });
+
+ it("reduces an absolute path to its basename", () => {
+ expect(safeEntryName("/data/dev.db")).toBe("dev.db");
+ });
+
+ it("reduces a windows-style path to its basename", () => {
+ expect(safeEntryName("C:\\Users\\me\\resume.pdf")).toBe("resume.pdf");
+ });
+
+ it("falls back to a placeholder for an empty or dot-only name", () => {
+ expect(safeEntryName("")).toBe("file");
+ expect(safeEntryName("..")).toBe("file");
+ expect(safeEntryName("/")).toBe("file");
+ });
+
+ it("leaves an ordinary filename alone, dashes included", () => {
+ expect(safeEntryName("files/abc/my-resume-v2.pdf")).toBe("my-resume-v2.pdf");
+ });
+});
+
+describe("importedFilePath", () => {
+ const uploads = APP_CONSTANTS.UPLOADS_DIR;
+
+ it("writes inside the resumes subdirectory, never UPLOADS_DIR itself", () => {
+ const p = importedFilePath("new-id", "resume.pdf", "pdf");
+ expect(p).toBe(path.join(uploads, "files", "resumes", "new-id-resume.pdf"));
+ });
+
+ it("never persists a path carried in the backup", () => {
+ const p = importedFilePath("new-id", "/data/dev.db", "pdf");
+ expect(p).toBe(path.join(uploads, "files", "resumes", "new-id-dev.pdf"));
+ expect(p.startsWith(path.join(uploads, "files", "resumes"))).toBe(true);
+ });
+
+ // The extension follows the sniffed bytes, not the name in the backup, so a
+ // payload cannot choose what the file on disk is called.
+ it("overrides the carried extension with the sniffed one", () => {
+ expect(importedFilePath("new-id", "resume.pdf.exe", "docx")).toBe(
+ path.join(uploads, "files", "resumes", "new-id-resume.pdf.docx"),
+ );
+ expect(importedFilePath("new-id", "payload.sh", "pdf")).toBe(
+ path.join(uploads, "files", "resumes", "new-id-payload.pdf"),
+ );
+ });
+
+ it("cannot collide for two files sharing an original name", () => {
+ expect(importedFilePath("id-a", "resume.pdf", "pdf")).not.toBe(
+ importedFilePath("id-b", "resume.pdf", "pdf"),
+ );
+ });
+});
+
+describe("checkImportedFile", () => {
+ const pdf = Buffer.concat([Buffer.from("%PDF"), Buffer.alloc(64)]);
+ const docx = Buffer.concat([Buffer.from([0x50, 0x4b, 0x03, 0x04]), Buffer.alloc(64)]);
+
+ it("accepts bytes that really are a PDF", () => {
+ expect(checkImportedFile(pdf)).toEqual({ kind: "pdf", mimeType: "application/pdf" });
+ });
+
+ it("accepts bytes that really are a DOCX", () => {
+ expect(checkImportedFile(docx)?.kind).toBe("docx");
+ });
+
+ // The whole point: an executable renamed resume.pdf inside the zip.
+ it("rejects bytes that are neither, whatever they are called", () => {
+ expect(checkImportedFile(Buffer.from("MZ\x90\x00 not a resume"))).toBeNull();
+ expect(checkImportedFile(Buffer.from("<script>alert(1)</script>"))).toBeNull();
+ expect(checkImportedFile(Buffer.alloc(0))).toBeNull();
+ });
+
+ it("rejects a file over the resume size cap", () => {
+ const huge = Buffer.concat([
+ Buffer.from("%PDF"),
+ Buffer.alloc(APP_CONSTANTS.MAX_RESUME_FILE_SIZE_BYTES),
+ ]);
+ expect(checkImportedFile(huge)).toBeNull();
+ });
+});
+
+describe("normalizeAutomationRun", () => {
+ const base = {
+ status: "running",
+ errorMessage: null as string | null,
+ completedAt: null as Date | null,
+ };
+
+ it("rewrites a running run to the shape reapStaleRuns writes", () => {
+ const row = normalizeAutomationRun({ ...base });
+ expect(row.status).toBe("failed");
+ expect(row.errorMessage).toBe("interrupted");
+ expect(row.completedAt).toBeInstanceOf(Date);
+ });
+
+ it("rewrites a cancelling run too, since reapStaleRuns never would", () => {
+ expect(normalizeAutomationRun({ ...base, status: "cancelling" }).status).toBe(
+ "failed",
+ );
+ });
+
+ it("leaves terminal statuses untouched", () => {
+ const completed = normalizeAutomationRun({
+ status: "completed",
+ errorMessage: null,
+ completedAt: new Date("2026-01-01"),
+ });
+ expect(completed.status).toBe("completed");
+ expect(completed.errorMessage).toBeNull();
+ expect(completed.completedAt).toEqual(new Date("2026-01-01"));
+ });
+});
+
+describe("normalizeAutomation", () => {
+ it("moves a past nextRunAt forward without deactivating the automation", () => {
+ const row = normalizeAutomation({
+ status: "active",
+ scheduleHour: 9,
+ nextRunAt: new Date("2020-01-01T09:00:00"),
+ });
+ expect(row.status).toBe("active");
+ expect(row.nextRunAt!.getTime()).toBeGreaterThan(Date.now());
+ expect(row.nextRunAt!.getHours()).toBe(9);
+ });
+
+ it("recomputes even when nextRunAt is null", () => {
+ const row = normalizeAutomation({
+ status: "active",
+ scheduleHour: 14,
+ nextRunAt: null as Date | null,
+ });
+ expect(row.nextRunAt!.getHours()).toBe(14);
+ });
+
+ it("leaves a paused automation without a next run", () => {
+ const row = normalizeAutomation({
+ status: "paused",
+ scheduleHour: 9,
+ nextRunAt: new Date("2020-01-01T09:00:00"),
+ });
+ expect(row.nextRunAt).toBeNull();
+ });
+});
diff --git a/__tests__/backupOrdering.spec.ts b/__tests__/backupOrdering.spec.ts
index 58d342c..953da4d 100644
--- a/__tests__/backupOrdering.spec.ts
+++ b/__tests__/backupOrdering.spec.ts
@@ -3,6 +3,7 @@ import {
INSERT_ORDER,
DELETE_ORDER,
LOOKUP_MODELS,
+ EMPTINESS_MODELS,
type BackupModel,
} from "@/lib/backup/ordering";
@@ -84,3 +85,29 @@ describe("backup ordering", () => {
});
});
});
+
+describe("emptiness models", () => {
+ it("counts content models only, so seeded JobSource rows do not demand a wipe", () => {
+ expect([...EMPTINESS_MODELS].sort()).toEqual([
+ "Activity",
+ "Automation",
+ "CoverLetter",
+ "Job",
+ "Note",
+ "Profile",
+ "Question",
+ "Resume",
+ "Task",
+ ]);
+ });
+
+ it("includes no lookup model", () => {
+ for (const model of LOOKUP_MODELS) {
+ expect(EMPTINESS_MODELS).not.toContain(model);
+ }
+ });
+
+ it("includes Profile, so an import cannot add a second one", () => {
+ expect(EMPTINESS_MODELS).toContain("Profile");
+ });
+});
diff --git a/src/lib/backup/idmap.ts b/src/lib/backup/idmap.ts
new file mode 100644
index 0000000..f59db8f
--- /dev/null
+++ b/src/lib/backup/idmap.ts
@@ -0,0 +1,92 @@
+import { randomUUID } from "crypto";
+import { MODEL_SPECS, type BackupModel } from "./ordering";
+
+// Foreign keys that are nullable in the schema. A dangling value in one of
+// these is degraded to null (the spec's defaultResumeId rule generalised);
+// a dangling required key aborts the import instead of silently reshaping it.
+const OPTIONAL_FKS = new Set<string>([
+ "Resume.FileId",
+ "ResumeSection.summaryId",
+ "WorkExperience.resumeSectionId",
+ "Education.resumeSectionId",
+ "LicenseOrCertification.resumeSectionId",
+ "OtherSection.resumeSectionId",
+ "Job.jobSourceId",
+ "Job.locationId",
+ "Job.resumeId",
+ "Job.coverLetterId",
+ "Job.automationId",
+ "Contact.interviewId",
+ "Task.activityTypeId",
+ "Activity.taskId",
+]);
+
+export class IdMap {
+ private map = new Map<string, string>();
+
+ set(oldId: string, newId: string): void {
+ this.map.set(oldId, newId);
+ }
+
+ get(oldId: string): string | undefined {
+ return this.map.get(oldId);
+ }
+
+ // Fresh uuids remove a whole class of collision, and an id map has to exist
+ // anyway because lookups resolve to pre-existing rows.
+ mint(oldId: string): string {
+ const newId = randomUUID();
+ this.map.set(oldId, newId);
+ return newId;
+ }
+}
+
+export function buildCreateData(
+ model: BackupModel,
+ row: Record<string, unknown>,
+ idMap: IdMap,
+ userId: string,
+): Record<string, unknown> {
+ const spec = MODEL_SPECS[model];
+ const data: Record<string, unknown> = { ...row };
+
+ const newId = idMap.get(row.id as string);
+ if (!newId) {
+ throw new Error(`${model} row ${String(row.id)} has no minted id`);
+ }
+ data.id = newId;
+
+ for (const field of Object.keys(spec.fks)) {
+ const value = data[field];
+ if (value === null || value === undefined) {
+ data[field] = null;
+ continue;
+ }
+ const mapped = idMap.get(value as string);
+ if (mapped) {
+ data[field] = mapped;
+ continue;
+ }
+ if (OPTIONAL_FKS.has(`${model}.${field}`)) {
+ data[field] = null;
+ continue;
+ }
+ throw new Error(
+ `${model}.${field} points at ${String(value)}, which is not in the backup`,
+ );
+ }
+
+ // The client supplies the payload, the server supplies the owner — and no
+ // model keeps an ownership column it does not declare. Both keys go
+ // unconditionally: 13 of the 28 models own through a relation chain and have
+ // neither column, so a stray userId riding in on a hand-edited data.json
+ // would reach Prisma as an unknown argument and abort the whole import.
+ delete data.userId;
+ delete data.createdBy;
+
+ if (spec.owner) {
+ data[spec.owner] = userId;
+ }
+
+ return data;
+}
diff --git a/src/lib/backup/normalize.ts b/src/lib/backup/normalize.ts
new file mode 100644
index 0000000..d3ee194
--- /dev/null
+++ b/src/lib/backup/normalize.ts
@@ -0,0 +1,77 @@
+import path from "path";
+import { APP_CONSTANTS } from "@/lib/constants";
+import { sniffFileType } from "@/lib/ai/import/extract-text";
+import { calculateNextRunAt } from "@/lib/scraper/schedule";
+
+const NON_TERMINAL_RUN_STATUSES = new Set(["running", "cancelling"]);
+
+const IMPORT_MIME: Record<"pdf" | "docx", string> = {
+ pdf: "application/pdf",
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
+};
+
+// Basename of the entry only, with both separators handled — a zip written on
+// Windows carries backslashes that posix path.basename would keep.
+export function safeEntryName(entryName: string): string {
+ const flattened = entryName.replace(/\\/g, "/");
+ const base = path.posix.basename(flattened);
+ if (!base || base === "." || base === "..") return "file";
+ return base;
+}
+
+// The column is always rebuilt server-side. The path in the backup is read only
+// to locate the zip entry: UPLOADS_DIR differs between dev and prod, and the
+// resume download route reads whatever path it is handed. The extension comes
+// from the sniffed bytes rather than the carried name, so a payload cannot pick
+// what lands in the uploads directory — resume.pdf.exe becomes <id>-resume.pdf.
+export function importedFilePath(
+ newFileId: string,
+ entryName: string,
+ kind: "pdf" | "docx",
+): string {
+ const base = safeEntryName(entryName).replace(/\.[^.]+$/, "");
+ return path.join(
+ APP_CONSTANTS.UPLOADS_DIR,
+ "files",
+ "resumes",
+ `${newFileId}-${base}.${kind}`,
+ );
+}
+
+// The same gate the upload route applies, on the same helper: size, then magic
+// bytes. Returns null for anything that is not really a resume, so the caller
+// can drop the bytes and keep going instead of failing the whole import. The
+// mimeType is what File.fileType is set to — never the value in the payload.
+export function checkImportedFile(
+ bytes: Buffer,
+): { kind: "pdf" | "docx"; mimeType: string } | null {
+ if (bytes.length === 0 || bytes.length > APP_CONSTANTS.MAX_RESUME_FILE_SIZE_BYTES) {
+ return null;
+ }
+ const kind = sniffFileType(bytes);
+ if (!kind) return null;
+ return { kind, mimeType: IMPORT_MIME[kind] };
+}
+
+// A run restored as running or cancelling holds the single-active slot forever:
+// runDueAutomations skips the automation and reapStaleRuns only reaps running.
+export function normalizeAutomationRun<
+ T extends { status: string; errorMessage: string | null; completedAt: Date | null },
+>(row: T): T {
+ if (!NON_TERMINAL_RUN_STATUSES.has(row.status)) return row;
+ return {
+ ...row,
+ status: "failed",
+ errorMessage: "interrupted",
+ completedAt: new Date(),
+ };
+}
+
+// Imported automations arrive with a nextRunAt in the past, so every one of
+// them would fire on the next hourly tick. Recompute forward instead.
+export function normalizeAutomation<
+ T extends { status: string; scheduleHour: number; nextRunAt: Date | null },
+>(row: T): T {
+ if (row.status !== "active") return { ...row, nextRunAt: null };
+ return { ...row, nextRunAt: calculateNextRunAt(row.scheduleHour) };
+}
diff --git a/src/lib/backup/ordering.ts b/src/lib/backup/ordering.ts
index 3aa077e..dcb5f9e 100644
--- a/src/lib/backup/ordering.ts
+++ b/src/lib/backup/ordering.ts
@@ -201,3 +201,19 @@ export const DELETE_ORDER: BackupModel[] = [...INSERT_ORDER].reverse();
export const LOOKUP_MODELS: BackupModel[] = INSERT_ORDER.filter(
(m) => MODEL_SPECS[m].lookup,
);
+
+// signup() seeds a JobSource per JOB_SOURCES entry, so no account is ever
+// literally empty. Counting content models only keeps the destructive
+// confirmation meaningful on a fresh instance. Profile is in the list because
+// reads use findFirst — a second profile is a silent coin flip, not an error.
+export const EMPTINESS_MODELS: BackupModel[] = [
+ "Profile",
+ "Resume",
+ "CoverLetter",
+ "Job",
+ "Note",
+ "Task",
+ "Activity",
+ "Question",
+ "Automation",
+];