From 898e11b424c1cd9a3a559dbfe98d4b3de9e070aa Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 13 Feb 2025 18:14:55 -0500 Subject: [PATCH] fix: Improve validation of document and collection IDs, closes #8401 --- server/routes/api/collections/schema.ts | 4 ++-- server/routes/api/comments/schema.ts | 2 +- server/routes/api/documents/documents.test.ts | 24 +++++++++++-------- server/routes/api/documents/schema.ts | 18 +++++--------- server/routes/api/notifications/schema.ts | 2 +- server/utils/zod.ts | 6 +++++ 6 files changed, 30 insertions(+), 26 deletions(-) diff --git a/server/routes/api/collections/schema.ts b/server/routes/api/collections/schema.ts index 0d807dc1ca..216b42b2de 100644 --- a/server/routes/api/collections/schema.ts +++ b/server/routes/api/collections/schema.ts @@ -6,13 +6,13 @@ import { FileOperationFormat, } from "@shared/types"; import { Collection } from "@server/models"; -import { zodIconType } from "@server/utils/zod"; +import { zodIconType, zodIdType } from "@server/utils/zod"; import { ValidateColor, ValidateIndex } from "@server/validation"; import { BaseSchema, ProsemirrorSchema } from "../schema"; const BaseIdSchema = z.object({ /** Id of the collection to be updated */ - id: z.string(), + id: zodIdType(), }); export const CollectionsCreateSchema = BaseSchema.extend({ diff --git a/server/routes/api/comments/schema.ts b/server/routes/api/comments/schema.ts index 774f07292b..e8a05054cb 100644 --- a/server/routes/api/comments/schema.ts +++ b/server/routes/api/comments/schema.ts @@ -28,7 +28,7 @@ export const CommentsCreateSchema = BaseSchema.extend({ id: z.string().uuid().optional(), /** Create comment for this document */ - documentId: z.string(), + documentId: z.string().uuid(), /** Create comment under this parent */ parentCommentId: z.string().uuid().optional(), diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index d43f786372..9ecdc39621 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -1997,14 +1997,18 @@ describe("#documents.templatize", () => { }); const body = await res.json(); expect(res.status).toBe(400); - expect(body.message).toBe("id: Required"); + expect(body.message).toBe("id: Must be a valid UUID or slug"); }); it("should require publish", async () => { const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); const res = await server.post("/api/documents.templatize", { body: { token: user.getJwtToken(), - id: "random-id", + id: document.id, }, }); const body = await res.json(); @@ -2581,7 +2585,7 @@ describe("#documents.move", () => { }); const body = await res.json(); expect(res.status).toEqual(400); - expect(body.message).toEqual("id: Required"); + expect(body.message).toEqual("id: Must be a valid UUID or slug"); }); it("should fail for invalid index", async () => { @@ -2887,7 +2891,7 @@ describe("#documents.restore", () => { }); const body = await res.json(); expect(res.status).toEqual(400); - expect(body.message).toEqual("id: Required"); + expect(body.message).toEqual("id: Must be a valid UUID or slug"); }); it("should fail for invalid collectionId", async () => { @@ -4331,7 +4335,7 @@ describe("#documents.update", () => { }); const body = await res.json(); expect(res.status).toBe(400); - expect(body.message).toBe("id: Required"); + expect(body.message).toBe("id: Must be a valid UUID or slug"); }); }); @@ -4345,7 +4349,7 @@ describe("#documents.archive", () => { }); const body = await res.json(); expect(res.status).toEqual(400); - expect(body.message).toEqual("id: Required"); + expect(body.message).toEqual("id: Must be a valid UUID or slug"); }); it("should allow archiving document", async () => { @@ -4388,7 +4392,7 @@ describe("#documents.delete", () => { }); const body = await res.json(); expect(res.status).toEqual(400); - expect(body.message).toEqual("id: Required"); + expect(body.message).toEqual("id: Must be a valid UUID or slug"); }); it("should allow deleting document", async () => { @@ -4537,7 +4541,7 @@ describe("#documents.unpublish", () => { }); const body = await res.json(); expect(res.status).toEqual(400); - expect(body.message).toEqual("id: Required"); + expect(body.message).toEqual("id: Must be a valid UUID or slug"); }); it("should unpublish a document", async () => { @@ -5083,7 +5087,7 @@ describe("#documents.add_user", () => { }); const body = await res.json(); expect(res.status).toEqual(400); - expect(body.message).toEqual("id: Required"); + expect(body.message).toEqual("id: Must be a valid UUID or slug"); }); it("should require authentication", async () => { @@ -5169,7 +5173,7 @@ describe("#documents.remove_user", () => { }); const body = await res.json(); expect(res.status).toEqual(400); - expect(body.message).toEqual("id: Required"); + expect(body.message).toEqual("id: Must be a valid UUID or slug"); }); it("should require authentication", async () => { diff --git a/server/routes/api/documents/schema.ts b/server/routes/api/documents/schema.ts index f8f97421cf..e089544a0f 100644 --- a/server/routes/api/documents/schema.ts +++ b/server/routes/api/documents/schema.ts @@ -5,7 +5,7 @@ import { z } from "zod"; import { DocumentPermission, StatusFilter } from "@shared/types"; import { UrlHelper } from "@shared/utils/UrlHelper"; import { BaseSchema } from "@server/routes/api/schema"; -import { zodIconType } from "@server/utils/zod"; +import { zodIconType, zodIdType } from "@server/utils/zod"; import { ValidateColor } from "@server/validation"; const DocumentsSortParamsSchema = z.object({ @@ -64,7 +64,7 @@ const BaseSearchSchema = DateFilterSchema.extend({ const BaseIdSchema = z.object({ /** Id of the document to be updated */ - id: z.string(), + id: zodIdType(), }); export const DocumentsListSchema = BaseSchema.extend({ @@ -137,9 +137,7 @@ export type DocumentsDraftsReq = z.infer; export const DocumentsInfoSchema = BaseSchema.extend({ body: z.object({ - /** Id of the document to be retrieved */ - id: z.string().optional(), - + id: zodIdType().optional(), /** Share Id, if available */ shareId: z .string() @@ -328,7 +326,7 @@ export type DocumentsImportReq = z.infer; export const DocumentsCreateSchema = BaseSchema.extend({ body: z.object({ /** Id of the document to be created */ - id: z.string().uuid().optional(), + id: zodIdType().optional(), /** Document title */ title: z.string().optional(), @@ -393,9 +391,7 @@ export const DocumentsUsersSchema = BaseSchema.extend({ export type DocumentsUsersReq = z.infer; export const DocumentsAddUserSchema = BaseSchema.extend({ - body: z.object({ - /** Id of the document to which the user is supposed to be added */ - id: z.string().uuid(), + body: BaseIdSchema.extend({ /** Id of the user who is to be added */ userId: z.string().uuid(), /** Permission to be granted to the added user */ @@ -406,9 +402,7 @@ export const DocumentsAddUserSchema = BaseSchema.extend({ export type DocumentsAddUserReq = z.infer; export const DocumentsRemoveUserSchema = BaseSchema.extend({ - body: z.object({ - /** Id of the document from which to remove the user */ - id: z.string().uuid(), + body: BaseIdSchema.extend({ /** Id of the user who is to be removed */ userId: z.string().uuid(), }), diff --git a/server/routes/api/notifications/schema.ts b/server/routes/api/notifications/schema.ts index e0a5074aab..2e4c66f48c 100644 --- a/server/routes/api/notifications/schema.ts +++ b/server/routes/api/notifications/schema.ts @@ -74,7 +74,7 @@ export type NotificationsUpdateAllReq = z.infer< export const NotificationsPixelSchema = BaseSchema.extend({ query: z.object({ - id: z.string(), + id: z.string().uuid(), token: z.string(), }), }); diff --git a/server/utils/zod.ts b/server/utils/zod.ts index 5181c88f43..caedca0b51 100644 --- a/server/utils/zod.ts +++ b/server/utils/zod.ts @@ -1,6 +1,7 @@ import emojiRegex from "emoji-regex"; import { z } from "zod"; import { IconLibrary } from "@shared/utils/IconLibrary"; +import { UrlHelper } from "@shared/utils/UrlHelper"; export function zodEnumFromObjectKeys< TI extends Record, @@ -10,6 +11,11 @@ export function zodEnumFromObjectKeys< return z.enum([firstKey, ...otherKeys]); } +export const zodIdType = () => + z.union([z.string().regex(UrlHelper.SLUG_URL_REGEX), z.string().uuid()], { + message: "Must be a valid UUID or slug", + }); + export const zodIconType = () => z.union([ z.string().regex(emojiRegex()),