diff --git a/apps/sim/lib/api/contracts/tools/onepassword.ts b/apps/sim/lib/api/contracts/tools/onepassword.ts index 349b13296f9..3c5ed0d737c 100644 --- a/apps/sim/lib/api/contracts/tools/onepassword.ts +++ b/apps/sim/lib/api/contracts/tools/onepassword.ts @@ -1,6 +1,9 @@ import { z } from 'zod' import { defineRouteContract } from '@/lib/api/contracts/types' +/** The longest item JSON a 1Password operation accepts; a real item is a few kilobytes. */ +const MAX_ITEM_JSON_LENGTH = 256_000 + export const onePasswordCredentialsBodySchema = z.object({ connectionMode: z.enum(['service_account', 'connect']).nullish(), serviceAccountToken: z.string().nullish(), @@ -28,15 +31,15 @@ export const onePasswordCreateItemBodySchema = onePasswordGetVaultBodySchema.ext category: z.string().min(1, 'Category is required'), title: z.string().nullish(), tags: z.string().nullish(), - fields: z.string().nullish(), + fields: z.string().max(MAX_ITEM_JSON_LENGTH).nullish(), }) export const onePasswordUpdateItemBodySchema = onePasswordGetItemBodySchema.extend({ - operations: z.string().min(1, 'Patch operations are required'), + operations: z.string().min(1, 'Patch operations are required').max(MAX_ITEM_JSON_LENGTH), }) export const onePasswordReplaceItemBodySchema = onePasswordGetItemBodySchema.extend({ - item: z.string().min(1, 'Item JSON is required'), + item: z.string().min(1, 'Item JSON is required').max(MAX_ITEM_JSON_LENGTH), }) export const onePasswordResolveSecretBodySchema = onePasswordCredentialsBodySchema.extend({ diff --git a/apps/sim/lib/catalog/projection/block-detail.ts b/apps/sim/lib/catalog/projection/block-detail.ts index be919a7262a..3c07768ec1e 100644 --- a/apps/sim/lib/catalog/projection/block-detail.ts +++ b/apps/sim/lib/catalog/projection/block-detail.ts @@ -29,6 +29,7 @@ import { type OutputFieldDefinition, type SubBlockConfig, } from '@/blocks/types' +import { isSafeKey } from '@/tools/safe-assign' import { getTrigger, isTriggerValid } from '@/triggers' const logger = createLogger('CatalogBlockProjection') @@ -300,6 +301,7 @@ export function computeOperationLevelInputs( const definition = declaredInputFor(subBlock, inputs) if (!definition) continue for (const operationId of gate.values) { + if (!isSafeKey(operationId) || !isSafeKey(subBlock.id)) continue operationInputs[operationId] ??= {} operationInputs[operationId][subBlock.id] = definition } diff --git a/apps/sim/lib/internal/onepassword/json-patch.test.ts b/apps/sim/lib/internal/onepassword/json-patch.test.ts index 7f72d20190c..814c10b24e1 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.test.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.test.ts @@ -2,6 +2,8 @@ import { describe, expect, it } from 'vitest' import { applyOnePasswordPatch, isSafeOnePasswordPatch, + MAX_PATCH_OPERATIONS, + MAX_PATCH_PATH_DEPTH, } from '@/lib/internal/onepassword/json-patch' describe('applyOnePasswordPatch', () => { @@ -71,3 +73,30 @@ describe('prototype paths', () => { } ) }) + +describe('patch bounds', () => { + it.each([ + { name: 'no operations', operations: [] }, + { name: 'an empty path', operations: [{ op: 'replace', path: '', value: 1 }] }, + { name: 'the root path', operations: [{ op: 'replace', path: '/', value: 1 }] }, + ])('refuses a patch with $name', ({ operations }) => { + expect(isSafeOnePasswordPatch(operations)).toBe(false) + }) + + it('refuses a patch with more operations than the cap', () => { + const operations = Array.from({ length: MAX_PATCH_OPERATIONS + 1 }, () => ({ + op: 'replace', + path: '/title', + value: 'x', + })) + expect(isSafeOnePasswordPatch(operations)).toBe(false) + expect(isSafeOnePasswordPatch(operations.slice(0, MAX_PATCH_OPERATIONS))).toBe(true) + }) + + it('refuses a path deeper than the cap', () => { + const deep = `/${Array.from({ length: MAX_PATCH_PATH_DEPTH + 1 }, () => 'a').join('/')}` + const atCap = `/${Array.from({ length: MAX_PATCH_PATH_DEPTH }, () => 'a').join('/')}` + expect(isSafeOnePasswordPatch([{ op: 'add', path: deep, value: 1 }])).toBe(false) + expect(isSafeOnePasswordPatch([{ op: 'add', path: atCap, value: 1 }])).toBe(true) + }) +}) diff --git a/apps/sim/lib/internal/onepassword/json-patch.ts b/apps/sim/lib/internal/onepassword/json-patch.ts index 4f6f34f5793..b1837567245 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.ts @@ -1,3 +1,5 @@ +import { isSafeKey } from '@/tools/safe-assign' + export interface JsonPatchOperation { op: 'add' | 'remove' | 'replace' path: string @@ -10,27 +12,35 @@ function recordValue(value: unknown): Record | undefined { : undefined } -/** Path segments that reach an object's prototype instead of its own data. */ -const PROTOTYPE_SEGMENTS = new Set(['__proto__', 'prototype', 'constructor']) +/** The most operations one item patch may carry. */ +export const MAX_PATCH_OPERATIONS = 500 + +/** The most segments one patch path may have; 1Password item paths are a few levels deep. */ +export const MAX_PATCH_PATH_DEPTH = 16 /** The operations 1Password's item patch accepts; anything else would be applied as a removal. */ const PATCH_OPS = new Set(['add', 'remove', 'replace']) /** - * Whether every operation is one 1Password accepts and its path names only an item's own data. - * Patches are applied to a plain object in this process, so a path through `__proto__` would - * write onto every object here. + * Whether every operation is one 1Password accepts and its path names a field of the item's own + * data, within the size bounds. Patches are applied to a plain object in this process, so a path + * through `__proto__` would write onto every object here, and a path naming nothing would report + * success for a patch that changed nothing. */ export function isSafeOnePasswordPatch(operations: unknown): operations is JsonPatchOperation[] { return ( Array.isArray(operations) && + operations.length > 0 && + operations.length <= MAX_PATCH_OPERATIONS && operations.every((operation) => { const record = recordValue(operation) const path = record?.path + if (!PATCH_OPS.has(record?.op) || typeof path !== 'string') return false + const segments = path.split('/').filter(Boolean) return ( - PATCH_OPS.has(record?.op) && - typeof path === 'string' && - path.split('/').every((segment) => !PROTOTYPE_SEGMENTS.has(segment)) + segments.length > 0 && + segments.length <= MAX_PATCH_PATH_DEPTH && + path.split('/').every((segment) => isSafeKey(segment)) ) }) ) @@ -60,7 +70,7 @@ export function applyOnePasswordPatch( ): void { const segments = operation.path.split('/').filter(Boolean) const rootKey = segments[0] - if (!rootKey || segments.some((segment) => PROTOTYPE_SEGMENTS.has(segment))) return + if (!rootKey || !segments.every((segment) => isSafeKey(segment))) return if (segments.length === 1) { if (operation.op === 'replace' || operation.op === 'add') { diff --git a/apps/sim/lib/internal/onepassword/operations.ts b/apps/sim/lib/internal/onepassword/operations.ts index d56fe502305..cea578fdc89 100644 --- a/apps/sim/lib/internal/onepassword/operations.ts +++ b/apps/sim/lib/internal/onepassword/operations.ts @@ -33,6 +33,8 @@ import { OnePasswordOperationError } from '@/lib/internal/onepassword/errors' import { applyOnePasswordPatch, isSafeOnePasswordPatch, + MAX_PATCH_OPERATIONS, + MAX_PATCH_PATH_DEPTH, } from '@/lib/internal/onepassword/json-patch' import { createInternalToolFileResult, @@ -279,7 +281,7 @@ export async function executeOnePasswordUpdateItem( const operations: unknown = JSON.parse(input.operations) if (!isSafeOnePasswordPatch(operations)) { throw new OnePasswordOperationError(400, { - error: "Each patch operation needs a path to the item's own fields.", + error: `Patch operations must be 1 to ${MAX_PATCH_OPERATIONS} add, remove or replace operations, each with a path of 1 to ${MAX_PATCH_PATH_DEPTH} segments naming the item's own fields.`, }) } if (credentials.mode === 'service_account') { diff --git a/apps/sim/providers/anthropic/structured-output-schema.ts b/apps/sim/providers/anthropic/structured-output-schema.ts index 3b743615249..717a9aed86d 100644 --- a/apps/sim/providers/anthropic/structured-output-schema.ts +++ b/apps/sim/providers/anthropic/structured-output-schema.ts @@ -149,7 +149,7 @@ function resolvePath(root: JsonSchemaNode, path: (string | number)[]): JsonSchem current = current[segment] continue } - if (!isSchemaNode(current)) return null + if (!isSchemaNode(current) || !Object.hasOwn(current, segment)) return null current = current[segment] } return isSchemaNode(current) ? current : null