fix: Improve validation of document and collection IDs, closes #8401

This commit is contained in:
Tom Moor committed 2025-02-13 18:34:15 -05:00
1 parent ac48767132
commit 898e11b424
6 files changed
+30 -26

No files matched your search

+2 -2
View File
@@ -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({
+1 -1
View File
@@ -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(),
+14 -10
View File
@@ -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 () => {
+6 -12
View File
@@ -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<typeof DocumentsDraftsSchema>;
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<typeof DocumentsImportSchema>;
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<typeof DocumentsUsersSchema>;
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<typeof DocumentsAddUserSchema>;
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(),
}),
+1 -1
View File
@@ -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(),
}),
});
+6
View File
@@ -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<string, any>,
@@ -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()),