The minimalistic document archiving platform.
feat(webhooks): add size limits to API parameters (#1020)
Corentin Thomasset015bb5349825
diff --git a/.changeset/new-dolls-wink.md b/.changeset/new-dolls-wink.mdnew file mode 100644index 00000000..7cc36a5a--- /dev/null+++ b/.changeset/new-dolls-wink.md@@ -0,0 +1,9 @@+---+"@papra/app": patch+---++Added some size limits on the webhooks creation and update API endpoints parameters.++- Names are limited to 128 characters.+- Secret keys are limited to 256 characters.+- URLs are limited to 2048 characters.diff --git a/apps/papra-server/src/modules/webhooks/webhook.routes.ts b/apps/papra-server/src/modules/webhooks/webhook.routes.tsindex 26ec7f72..1de3a874 100644--- a/apps/papra-server/src/modules/webhooks/webhook.routes.ts+++ b/apps/papra-server/src/modules/webhooks/webhook.routes.ts@@ -10,7 +10,7 @@ import { validateJsonBody, validateParams } from '../shared/validation/validatioimport { createWebhookNotFoundError } from './webhook.errors';import { createWebhookRepository } from './webhook.repository';import { createWebhook, updateWebhook } from './webhook.usecases';-import { webhookEventListSchema } from './webhooks.schemas';+import { webhookEventListSchema, webhookIdSchema, webhookNameSchema, webhookSecretSchema, webhookUrlSchema } from './webhooks.schemas';export function registerWebhooksRoutes(context: RouteDefinitionContext) {setupCreateWebhookRoute(context);@@ -28,11 +28,11 @@ function setupCreateWebhookRoute({ app, db, config }: RouteDefinitionContext) {organizationId: organizationIdSchema,})),validateJsonBody(z.object({- name: z.string().min(1).max(128),- url: z.string().url(),- secret: z.string().min(1).optional(),+ name: webhookNameSchema,+ url: webhookUrlSchema,+ secret: webhookSecretSchema.optional(),enabled: z.boolean().optional().default(true),- events: webhookEventListSchema.min(1),+ events: webhookEventListSchema,})),async (context) => {const { userId } = getUser({ context });@@ -94,7 +94,7 @@ function setupGetWebhookRoute({ app, db }: RouteDefinitionContext) {requireAuthentication(),validateParams(z.object({organizationId: organizationIdSchema,- webhookId: z.string().min(1),+ webhookId: webhookIdSchema,})),async (context) => {const { userId } = getUser({ context });@@ -124,14 +124,14 @@ function setupUpdateWebhookRoute({ app, db, config }: RouteDefinitionContext) {requireAuthentication(),validateParams(z.object({organizationId: organizationIdSchema,- webhookId: z.string().min(1),+ webhookId: webhookIdSchema,})),validateJsonBody(z.object({- name: z.string().min(1).max(128).optional(),- url: z.string().url().optional(),- secret: z.string().min(1).optional(),+ name: webhookNameSchema.optional(),+ url: webhookUrlSchema.optional(),+ secret: webhookSecretSchema.optional(),enabled: z.boolean().optional(),- events: webhookEventListSchema.min(1).optional(),+ events: webhookEventListSchema.optional(),})),async (context) => {const { userId } = getUser({ context });@@ -167,7 +167,7 @@ function setupDeleteWebhookRoute({ app, db }: RouteDefinitionContext) {requireAuthentication(),validateParams(z.object({organizationId: organizationIdSchema,- webhookId: z.string().min(1),+ webhookId: webhookIdSchema,})),async (context) => {const { userId } = getUser({ context });diff --git a/apps/papra-server/src/modules/webhooks/webhooks.constants.ts b/apps/papra-server/src/modules/webhooks/webhooks.constants.tsindex 2c2547d9..2427482c 100644--- a/apps/papra-server/src/modules/webhooks/webhooks.constants.ts+++ b/apps/papra-server/src/modules/webhooks/webhooks.constants.ts@@ -1 +1,6 @@+import { createPrefixedIdRegex } from '../shared/random/ids';++export const WEBHOOK_ID_PREFIX = 'wbh';+export const WEBHOOK_ID_REGEX = createPrefixedIdRegex({ prefix: WEBHOOK_ID_PREFIX });+export const WEBHOOK_URL_ALLOWED_HOSTNAMES_ENV_VAR = 'WEBHOOK_URL_ALLOWED_HOSTNAMES';diff --git a/apps/papra-server/src/modules/webhooks/webhooks.schemas.ts b/apps/papra-server/src/modules/webhooks/webhooks.schemas.tsindex 8534abc3..6f727a66 100644--- a/apps/papra-server/src/modules/webhooks/webhooks.schemas.ts+++ b/apps/papra-server/src/modules/webhooks/webhooks.schemas.ts@@ -1,5 +1,10 @@import { EVENT_NAMES } from '@papra/webhooks';import { z } from 'zod';+import { WEBHOOK_ID_REGEX } from './webhooks.constants';+export const webhookIdSchema = z.string().regex(WEBHOOK_ID_REGEX);export const webhookEventSchema = z.enum(EVENT_NAMES);-export const webhookEventListSchema = z.array(webhookEventSchema);+export const webhookEventListSchema = z.array(webhookEventSchema).min(1);+export const webhookSecretSchema = z.string().min(1).max(256);+export const webhookNameSchema = z.string().min(1).max(128);+export const webhookUrlSchema = z.string().url().max(2048);diff --git a/apps/papra-server/src/modules/webhooks/webhooks.tables.ts b/apps/papra-server/src/modules/webhooks/webhooks.tables.tsindex 78027a4c..1443f2d6 100644--- a/apps/papra-server/src/modules/webhooks/webhooks.tables.ts+++ b/apps/papra-server/src/modules/webhooks/webhooks.tables.ts@@ -3,9 +3,10 @@ import { integer, sqliteTable, text, unique } from 'drizzle-orm/sqlite-core';import { organizationsTable } from '../organizations/organizations.table';import { createPrimaryKeyField, createTimestampColumns } from '../shared/db/columns.helpers';import { usersTable } from '../users/users.table';+import { WEBHOOK_ID_PREFIX } from './webhooks.constants';export const webhooksTable = sqliteTable('webhooks', {- ...createPrimaryKeyField({ prefix: 'wbh' }),+ ...createPrimaryKeyField({ prefix: WEBHOOK_ID_PREFIX }),...createTimestampColumns(),name: text('name').notNull(),