A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!
TypeScript agent-skillsaidsh-pluginhacktoberfestjavascriptmcp-serverreactresume-builderself-hostedtypescript
fix(api): reject mutations, reads, and metrics queries for resumes in trash (#3664)
Hoàng Anh Tuấnae2783b32d88
diff --git a/packages/api/src/features/resume/service.test.ts b/packages/api/src/features/resume/service.test.tsindex 6a5e53843..6c4117383 100644--- a/packages/api/src/features/resume/service.test.ts+++ b/packages/api/src/features/resume/service.test.ts@@ -106,7 +106,7 @@ const slugLookup = (rows: unknown[]) => ({// A `db.update(...).set(...).where(...).returning(...)` chain that resolves to `rows`.const createUpdateChain = (rows: unknown[]) => {const returning = vi.fn(() => Promise.resolve(rows));- const where = vi.fn(() => ({ returning }));+ const where = vi.fn((_condition?: unknown) => ({ returning }));const set = vi.fn((_input: unknown) => ({ where }));return { chain: { set }, set, where, returning };};@@ -238,6 +238,7 @@ describe("update", () => {} else {await expect(resumeService.update(input)).resolves.toMatchObject({ id: "r1" });expect(update.set).toHaveBeenCalledWith({ name: "Saved", autoName: false });+ expect(update.where).toHaveBeenCalledWith(expect.arrayContaining([["trashed_at"], ["is_locked", false]]));}},);@@ -282,6 +283,19 @@ describe("update", () => {});expect(select.forUpdate).toHaveBeenCalledWith("update");});++ it("does not update a resume in Trash", async () => {+ const selectWhere = vi.fn((_condition: unknown) => ({ for: vi.fn(() => Promise.resolve([])) }));+ const update = createUpdateChain([]);+ const tx = { select: () => ({ from: () => ({ where: selectWhere }) }), update: () => update.chain };+ dbMock.transaction.mockImplementationOnce(async (callback: (tx: unknown) => Promise<unknown>) => callback(tx));++ await expect(resumeService.update({ id: "r1", userId: "u1", name: "Updated" })).rejects.toMatchObject({+ code: "NOT_FOUND",+ });+ expect(selectWhere.mock.calls[0]?.[0]).toContainEqual(["trashed_at"]);+ expect(update.set).not.toHaveBeenCalled();+ });});describe("patch", () => {@@ -344,6 +358,7 @@ describe("patch", () => {metadata: expect.objectContaining({ page: expect.objectContaining({ marginX: 14 }) }),}),});+ expect(update.where).toHaveBeenCalledWith(expect.arrayContaining([["trashed_at"], ["is_locked", false]]));});it.each([@@ -382,6 +397,24 @@ describe("patch", () => {expect((error as { data: unknown }).data).toEqual({ index: 0, operation });expect(update.set).not.toHaveBeenCalled();});++ it("does not patch a resume in Trash", async () => {+ const selectWhere = vi.fn((_condition: unknown) => ({ for: vi.fn(() => Promise.resolve([])) }));+ const tx = {+ select: vi.fn(() => ({ from: () => ({ where: selectWhere }) })),+ update: vi.fn(),+ };++ await expect(+ resumeService.patchInTransaction(tx as never, {+ id: "r1",+ userId: "u1",+ operations: [{ op: "replace", path: "/basics/name", value: "Ada" }],+ }),+ ).rejects.toMatchObject({ code: "NOT_FOUND" });+ expect(selectWhere.mock.calls[0]?.[0]).toContainEqual(["trashed_at"]);+ expect(tx.update).not.toHaveBeenCalled();+ });});describe("verifyPassword", () => {@@ -425,6 +458,24 @@ describe("delete", () => {status: 403,});});++ it("allows permanently deleting a resume that is in Trash", async () => {+ const deleteWhere = vi.fn((_condition?: unknown) => Promise.resolve());+ const selectWhere = vi.fn((_condition?: unknown) => Promise.resolve([{ isLocked: false }]));+ runTransaction({+ select: vi+ .fn()+ .mockReturnValueOnce(createLockedSelectChain([{ id: "u1" }]).chain)+ .mockReturnValueOnce({ from: () => ({ where: selectWhere }) }),+ delete: () => ({ where: deleteWhere }),+ });++ await expect(resumeService.delete({ id: "r1", userId: "u1" })).resolves.toBeUndefined();+ expect(selectWhere).toHaveBeenCalledTimes(1);+ expect(selectWhere.mock.calls[0]?.[0]).not.toContainEqual(["trashed_at"]);+ expect(deleteWhere).toHaveBeenCalledTimes(1);+ expect(deleteWhere.mock.calls[0]?.[0]).not.toContainEqual(["trashed_at"]);+ });});describe.each([@@ -441,7 +492,7 @@ describe.each([await expect(run()).resolves.toBeUndefined();expect(update.set).toHaveBeenCalledWith({ password: written });- expect(update.where).toHaveBeenCalledWith(expect.arrayContaining([["is_locked", false]]));+ expect(update.where).toHaveBeenCalledWith(expect.arrayContaining([["trashed_at"], ["is_locked", false]]));});it.each([@@ -453,6 +504,17 @@ describe.each([await expect(run()).rejects.toMatchObject({ code });});++ it("does not update password for a resume in Trash", async () => {+ const update = createUpdateChain([]);+ dbMock.update.mockReturnValueOnce(update.chain);+ const selectWhere = vi.fn((_cond: unknown) => Promise.resolve([]));+ dbMock.select.mockReturnValueOnce({ from: () => ({ where: selectWhere }) });++ await expect(run()).rejects.toMatchObject({ code: "NOT_FOUND" });+ expect(update.where).toHaveBeenCalledWith(expect.arrayContaining([["trashed_at"]]));+ expect(selectWhere).toHaveBeenCalledWith(expect.arrayContaining([["trashed_at"]]));+ });});describe("getBySlug", () => {@@ -495,6 +557,22 @@ describe("getBySlug", () => {}),).rejects.toMatchObject({ code: "NOT_FOUND" });});++ it("does not look in Trash for the resume", async () => {+ const limit = vi.fn(() => Promise.resolve([]));+ const orderBy = vi.fn((_order: unknown) => ({ limit }));+ const where = vi.fn((_condition: unknown) => ({ orderBy }));+ dbMock.select.mockReturnValueOnce({ from: () => ({ innerJoin: () => ({ where }) }) });++ await expect(+ resumeService.getBySlug({+ username: "owner",+ slug: "resume",+ requestHeaders: new Headers(),+ }),+ ).rejects.toMatchObject({ code: "NOT_FOUND" });+ expect(where.mock.calls[0]?.[0]).toContainEqual(["trashed_at"]);+ });});describe("statistics.recordDownload", () => {@@ -563,3 +641,62 @@ describe("statistics.recordDownload", () => {).rejects.toMatchObject({ code: "NOT_FOUND" });});});++describe("statistics.getById", () => {+ it("does not look in Trash for the resume", async () => {+ const where = vi.fn((_condition: unknown) => Promise.resolve([]));+ dbMock.select.mockReturnValueOnce({ from: () => ({ rightJoin: () => ({ where }) }) });++ await expect(resumeService.statistics.getById({ id: "r1", userId: "u1" })).rejects.toMatchObject({+ code: "NOT_FOUND",+ });+ expect(where.mock.calls[0]?.[0]).toContainEqual(["trashed_at"]);+ });+});++describe("statistics.getDailySeries", () => {+ it("does not look in Trash for the resume", async () => {+ const where = vi.fn((_condition: unknown) => Promise.resolve([]));+ dbMock.select.mockReturnValueOnce({ from: () => ({ where }) });++ await expect(resumeService.statistics.getDailySeries({ id: "r1", userId: "u1" })).rejects.toMatchObject({+ code: "NOT_FOUND",+ });+ expect(where.mock.calls[0]?.[0]).toContainEqual(["trashed_at"]);+ });+});++describe("tags.list", () => {+ it("does not include tags from resumes in Trash", async () => {+ const where = vi.fn((_condition: unknown) => Promise.resolve([{ tags: ["tag1", "tag2"] }]));+ dbMock.select.mockReturnValueOnce({ from: () => ({ where }) });++ const result = await resumeService.tags.list({ userId: "u1" });+ expect(result).toEqual(["tag1", "tag2"]);+ expect(where.mock.calls[0]?.[0]).toContainEqual(["trashed_at"]);+ });+});++describe("getById", () => {+ it("reads a resume in Trash so its thumbnail can render", async () => {+ const where = vi.fn((_condition: unknown) => Promise.resolve([]));+ dbMock.select.mockReturnValueOnce({ from: () => ({ where }) });++ await expect(resumeService.getById({ id: "r1", userId: "u1" })).rejects.toMatchObject({+ code: "NOT_FOUND",+ });+ expect(where.mock.calls[0]?.[0]).not.toContainEqual(["trashed_at"]);+ });+});++describe("setLocked", () => {+ it("does not allow locking or unlocking a resume in Trash", async () => {+ const update = createUpdateChain([]);+ dbMock.update.mockReturnValueOnce(update.chain);++ await expect(resumeService.setLocked({ id: "r1", userId: "u1", isLocked: true })).rejects.toMatchObject({+ code: "NOT_FOUND",+ });+ expect(update.where).toHaveBeenCalledWith(expect.arrayContaining([["trashed_at"]]));+ });+});diff --git a/packages/api/src/features/resume/service.ts b/packages/api/src/features/resume/service.tsindex e63e23bfa..99cfeac7a 100644--- a/packages/api/src/features/resume/service.ts+++ b/packages/api/src/features/resume/service.ts@@ -82,6 +82,19 @@ function assertValidPatchPointers(operation: JsonPatchOperation, index: number)}}+const activeResumeConditions = (id: string, userId: string) => [+ eq(schema.resume.id, id),+ eq(schema.resume.userId, userId),+ isNull(schema.resume.trashedAt),+];++/** Matches an active resume (not in Trash) owned by the user. */+const activeResumeWhere = (id: string, userId: string) => and(...activeResumeConditions(id, userId));++/** Matches an active, editable (unlocked and not in Trash) resume owned by the user. */+const activeUnlockedResumeWhere = (id: string, userId: string) =>+ and(...activeResumeConditions(id, userId), eq(schema.resume.isLocked, false));+async function applyResumePatchTx(client: DbOrTx,input: {@@ -99,7 +112,7 @@ async function applyResumePatchTx(updatedAt: schema.resume.updatedAt,}).from(schema.resume)- .where(and(eq(schema.resume.id, input.id), eq(schema.resume.userId, input.userId)))+ .where(activeResumeWhere(input.id, input.userId)).for("update");if (!existing) throw new ORPCError("NOT_FOUND");@@ -142,9 +155,7 @@ async function applyResumePatchTx(const [resume] = await client.update(schema.resume).set({ data: patchedData })- .where(- and(eq(schema.resume.id, input.id), eq(schema.resume.isLocked, false), eq(schema.resume.userId, input.userId)),- )+ .where(activeUnlockedResumeWhere(input.id, input.userId)).returning({id: schema.resume.id,name: schema.resume.name,@@ -181,7 +192,7 @@ const tags = {const result = await db.select({ tags: schema.resume.tags }).from(schema.resume)- .where(eq(schema.resume.userId, input.userId));+ .where(and(eq(schema.resume.userId, input.userId), isNull(schema.resume.trashedAt)));return [...new Set(result.flatMap((tag) => tag.tags))].sort((a, b) => a.localeCompare(b));},@@ -248,7 +259,7 @@ const statistics = {}).from(schema.resumeStatistics).rightJoin(schema.resume, eq(schema.resumeStatistics.resumeId, schema.resume.id))- .where(and(eq(schema.resume.id, input.id), eq(schema.resume.userId, input.userId)));+ .where(activeResumeWhere(input.id, input.userId));if (!statistics) throw new ORPCError("NOT_FOUND");@@ -308,7 +319,7 @@ const statistics = {const [resume] = await db.select({ id: schema.resume.id }).from(schema.resume)- .where(and(eq(schema.resume.id, input.id), eq(schema.resume.userId, input.userId)));+ .where(activeResumeWhere(input.id, input.userId));if (!resume) throw new ORPCError("NOT_FOUND");@@ -372,12 +383,13 @@ async function notifyResumeUpdated(event: ResumeUpdatedEvent) {}// One conditional UPDATE skips locked rows atomically; only a miss pays for the read that says why.+// Resumes in Trash miss both the update and the fallback read (activeResumeWhere), throwing NOT_FOUND without leaking lock state.async function updateResumePassword(input: { id: string; userId: string; password: string | null }) {- const owned = and(eq(schema.resume.id, input.id), eq(schema.resume.userId, input.userId));+ const owned = activeResumeWhere(input.id, input.userId);const [resume] = await db.update(schema.resume).set({ password: input.password })- .where(and(owned, eq(schema.resume.isLocked, false)))+ .where(activeUnlockedResumeWhere(input.id, input.userId)).returning({ id: schema.resume.id, updatedAt: schema.resume.updatedAt });if (!resume) {@@ -502,6 +514,7 @@ export const resumeService = {applicationId: schema.resume.applicationId,}).from(schema.resume)+ // Read-only and owner-scoped, so resumes in Trash stay readable for their thumbnails; writes reject them..where(and(eq(schema.resume.id, input.id), eq(schema.resume.userId, input.userId)));if (!resume) throw new ORPCError("NOT_FOUND");@@ -667,7 +680,7 @@ export const resumeService = {updatedAt: schema.resume.updatedAt,}).from(schema.resume)- .where(and(eq(schema.resume.id, input.id), eq(schema.resume.userId, input.userId)))+ .where(activeResumeWhere(input.id, input.userId)).for("update");if (!existing) throw new ORPCError("NOT_FOUND");@@ -714,13 +727,7 @@ export const resumeService = {const [updated] = await tx.update(schema.resume).set(updateData)- .where(- and(- eq(schema.resume.id, input.id),- eq(schema.resume.isLocked, false),- eq(schema.resume.userId, input.userId),- ),- )+ .where(activeUnlockedResumeWhere(input.id, input.userId)).returning({id: schema.resume.id,name: schema.resume.name,@@ -790,7 +797,7 @@ export const resumeService = {const [resume] = await db.update(schema.resume).set({ isLocked: input.isLocked })- .where(and(eq(schema.resume.id, input.id), eq(schema.resume.userId, input.userId)))+ .where(activeResumeWhere(input.id, input.userId)).returning({ id: schema.resume.id, updatedAt: schema.resume.updatedAt });if (!resume) throw new ORPCError("NOT_FOUND");