Open source personal finance application
fix(convex): add string length limits to all public mutations
Ilya Komichevf24f93dd3f8e
diff --git a/convex/categories.ts b/convex/categories.tsindex 668c77f8..a31f1de4 100644--- a/convex/categories.ts+++ b/convex/categories.ts@@ -5,7 +5,7 @@ import type { MutationCtx } from './_generated/server'import { internal } from './_generated/api'import { action, internalMutation, internalQuery, mutation, query } from './_generated/server'-import { getAuthUser, getOwnEntity, requireAuthUser } from './shared'+import { getAuthUser, getOwnEntity, requireAuthUser, validateStringLength } from './shared'import { removeTrnsFromHash } from './trnsHash'// --- Shared helpers ---@@ -29,42 +29,6 @@ async function validateNameUniqueness(ctx: MutationCtx, name: string, parentId:throw new Error('Category with this name already exists')}-async function syncChildIdsOnParentChange(ctx: MutationCtx, id: Id<'categories'>, oldParentId: Id<'categories'> | 0, newParentId: Id<'categories'> | 0, userId: string) {- if (newParentId === oldParentId)- return-- if (oldParentId !== 0) {- const oldParent = await ctx.db.get(oldParentId)- if (oldParent && oldParent.userId === userId) {- const updatedChildIds = (oldParent.childIds ?? []).filter(cid => cid !== id)- await ctx.db.patch(oldParentId, {- childIds: updatedChildIds.length ? updatedChildIds : undefined,- })- }- }-- if (newParentId !== 0) {- const newParent = await ctx.db.get(newParentId)- if (newParent && newParent.userId === userId) {- await ctx.db.patch(newParentId, {- childIds: [...(newParent.childIds ?? []), id],- })- }- }-}--async function removeFromParentChildIds(ctx: MutationCtx, id: Id<'categories'>, parentId: Id<'categories'> | 0, userId: string) {- if (parentId === 0)- return- const parent = await ctx.db.get(parentId)- if (parent && parent.userId === userId) {- const updatedChildIds = (parent.childIds ?? []).filter(cid => cid !== id)- await ctx.db.patch(parentId, {- childIds: updatedChildIds.length ? updatedChildIds : undefined,- })- }-}-async function updateCategoryCore(ctx: MutationCtx,id: Id<'categories'>,@@ -84,6 +48,9 @@ async function updateCategoryCore(throw new Error('Name is required')if (args.color !== undefined && !args.color.trim())throw new Error('Color is required')+ validateStringLength(args.name, 100, 'Name')+ validateStringLength(args.color, 50, 'Color')+ validateStringLength(args.icon, 50, 'Icon')const category = await getOwnEntity(ctx, id, userId)@@ -91,8 +58,6 @@ async function updateCategoryCore(await validateParentId(ctx, args.parentId, userId, id)if (args.name !== undefined || args.parentId !== undefined)await validateNameUniqueness(ctx, args.name ?? category.name, args.parentId ?? category.parentId, userId, id)- if (args.parentId !== undefined)- await syncChildIdsOnParentChange(ctx, id, category.parentId, args.parentId, userId)const now = Date.now()await ctx.db.patch(id, { ...args, updatedAt: now })@@ -131,6 +96,9 @@ export const create = mutation({throw new Error('Name is required')if (!args.color.trim())throw new Error('Color is required')+ validateStringLength(args.name, 100, 'Name')+ validateStringLength(args.color, 50, 'Color')+ validateStringLength(args.icon, 50, 'Icon')const parentId = args.parentId ?? 0@@ -145,15 +113,6 @@ export const create = mutation({userId: user._id,})- if (parentId !== 0) {- const parent = await ctx.db.get(parentId)- if (parent && parent.userId === user._id) {- await ctx.db.patch(parentId, {- childIds: [...(parent.childIds ?? []), newId],- })- }- }-return newId},})@@ -180,7 +139,12 @@ export const validateCategoryRemoval = internalQuery({const category = await ctx.db.get(id)if (!category || category.userId !== userId)throw new Error('Not found')- if (category.childIds?.length)+ const child = await ctx.db+ .query('categories')+ .withIndex('by_user', q => q.eq('userId', userId))+ .filter(q => q.eq(q.field('parentId'), id))+ .first()+ if (child)throw new Error('Cannot delete category with children')},})@@ -219,7 +183,6 @@ export const removeCategoryFinalize = internalMutation({const category = await ctx.db.get(categoryId)if (!category || category.userId !== userId)return- await removeFromParentChildIds(ctx, categoryId, category.parentId, userId)await ctx.db.delete(categoryId)},})diff --git a/convex/shared.ts b/convex/shared.tsindex ce17484e..b4fd33ff 100644--- a/convex/shared.ts+++ b/convex/shared.ts@@ -5,6 +5,14 @@ import { authComponent } from './auth'export { TrnType, walletTypeValidator } from './validators'+/**+ * Validate string field length. Throws if value exceeds maxLen.+ */+export function validateStringLength(value: string | undefined, maxLen: number, fieldName: string) {+ if (value !== undefined && value.length > maxLen)+ throw new Error(`${fieldName} must be at most ${maxLen} characters`)+}+/*** Get authenticated user or return null (for queries).*/diff --git a/convex/trns.ts b/convex/trns.tsindex d5012c06..9d1d7ca4 100644--- a/convex/trns.ts+++ b/convex/trns.ts@@ -6,10 +6,12 @@ import type { MutationCtx } from './_generated/server'import { internal } from './_generated/api'import { mutation, query } from './_generated/server'-import { getAuthUser, getOwnEntity, requireAuthUser, TrnType } from './shared'+import { getAuthUser, getOwnEntity, requireAuthUser, TrnType, validateStringLength } from './shared'import { addTrnToHash, fnv1aNum, removeTrnFromHash } from './trnsHash'async function validateTrnFields(ctx: MutationCtx, args: Record<string, any>, userId: string) {+ validateStringLength(args.desc, 500, 'Description')+if (args.type === TrnType.Transfer) {if (args.categoryId && args.categoryId !== 'transfer')throw new Error('Transfer categoryId must be "transfer"')diff --git a/convex/userSettings.ts b/convex/userSettings.tsindex c5745d4d..745fd8fe 100644--- a/convex/userSettings.ts+++ b/convex/userSettings.ts@@ -2,7 +2,7 @@ import { v } from 'convex/values'import { internal } from './_generated/api'import { action, internalQuery, mutation, query } from './_generated/server'-import { getAuthUser, requireAuthUser } from './shared'+import { getAuthUser, requireAuthUser, validateStringLength } from './shared'export const get = query({args: {},@@ -27,6 +27,7 @@ export const upsert = mutation({if (args.baseCurrency !== undefined && !args.baseCurrency.trim())throw new Error('Base currency is required')+ validateStringLength(args.baseCurrency, 10, 'Base currency')const existing = await ctx.db.query('userSettings')diff --git a/convex/wallets.ts b/convex/wallets.tsindex 25b912a4..cd45ae15 100644--- a/convex/wallets.ts+++ b/convex/wallets.ts@@ -2,7 +2,7 @@ import { v } from 'convex/values'import { internal } from './_generated/api'import { action, internalMutation, mutation, query } from './_generated/server'-import { getAuthUser, getOwnEntity, requireAuthUser, walletTypeValidator } from './shared'+import { getAuthUser, getOwnEntity, requireAuthUser, validateStringLength, walletTypeValidator } from './shared'import { removeTrnsFromHash } from './trnsHash'export const list = query({@@ -39,6 +39,10 @@ export const create = mutation({throw new Error('Color is required')if (!args.currency.trim())throw new Error('Currency is required')+ validateStringLength(args.name, 100, 'Name')+ validateStringLength(args.color, 50, 'Color')+ validateStringLength(args.currency, 10, 'Currency')+ validateStringLength(args.desc, 500, 'Description')return await ctx.db.insert('wallets', {...args,isArchived: args.isArchived ?? false,@@ -72,6 +76,10 @@ export const update = mutation({throw new Error('Color is required')if (args.currency !== undefined && !args.currency.trim())throw new Error('Currency is required')+ validateStringLength(args.name, 100, 'Name')+ validateStringLength(args.color, 50, 'Color')+ validateStringLength(args.currency, 10, 'Currency')+ validateStringLength(args.desc, 500, 'Description')await getOwnEntity(ctx, id, user._id)await ctx.db.patch(id, { ...args, updatedAt: Date.now() })},