Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions apps/sim/lib/api/contracts/tools/onepassword.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 2 additions & 0 deletions apps/sim/lib/catalog/projection/block-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
}
Expand Down
29 changes: 29 additions & 0 deletions apps/sim/lib/internal/onepassword/json-patch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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)
})
})
28 changes: 19 additions & 9 deletions apps/sim/lib/internal/onepassword/json-patch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isSafeKey } from '@/tools/safe-assign'

export interface JsonPatchOperation {
op: 'add' | 'remove' | 'replace'
path: string
Expand All @@ -10,27 +12,35 @@ function recordValue(value: unknown): Record<string, unknown> | 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<unknown>(['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))
)
})
)
Expand Down Expand Up @@ -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') {
Expand Down
4 changes: 3 additions & 1 deletion apps/sim/lib/internal/onepassword/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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') {
Expand Down
2 changes: 1 addition & 1 deletion apps/sim/providers/anthropic/structured-output-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down