Skip to content

Commit b1a1438

Browse files
authored
fix(onepassword): bound item patches and refuse ones that change nothing (#10506)
- an item patch carries 1 to 500 operations, each with a path of 1 to 16 segments; a patch with no operations or a path naming no field is refused instead of reporting success - item, field and patch JSON bodies are capped at 256 KB - path segments are checked with the shared isSafeKey - the catalog's operation-input map and the Anthropic schema path walk skip inherited keys
1 parent e60f23d commit b1a1438

6 files changed

Lines changed: 60 additions & 14 deletions

File tree

‎apps/sim/lib/api/contracts/tools/onepassword.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { z } from 'zod'
22
import { defineRouteContract } from '@/lib/api/contracts/types'
33

4+
/** The longest item JSON a 1Password operation accepts; a real item is a few kilobytes. */
5+
const MAX_ITEM_JSON_LENGTH = 256_000
6+
47
export const onePasswordCredentialsBodySchema = z.object({
58
connectionMode: z.enum(['service_account', 'connect']).nullish(),
69
serviceAccountToken: z.string().nullish(),
@@ -28,15 +31,15 @@ export const onePasswordCreateItemBodySchema = onePasswordGetVaultBodySchema.ext
2831
category: z.string().min(1, 'Category is required'),
2932
title: z.string().nullish(),
3033
tags: z.string().nullish(),
31-
fields: z.string().nullish(),
34+
fields: z.string().max(MAX_ITEM_JSON_LENGTH).nullish(),
3235
})
3336

3437
export const onePasswordUpdateItemBodySchema = onePasswordGetItemBodySchema.extend({
35-
operations: z.string().min(1, 'Patch operations are required'),
38+
operations: z.string().min(1, 'Patch operations are required').max(MAX_ITEM_JSON_LENGTH),
3639
})
3740

3841
export const onePasswordReplaceItemBodySchema = onePasswordGetItemBodySchema.extend({
39-
item: z.string().min(1, 'Item JSON is required'),
42+
item: z.string().min(1, 'Item JSON is required').max(MAX_ITEM_JSON_LENGTH),
4043
})
4144

4245
export const onePasswordResolveSecretBodySchema = onePasswordCredentialsBodySchema.extend({

‎apps/sim/lib/catalog/projection/block-detail.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
type OutputFieldDefinition,
3030
type SubBlockConfig,
3131
} from '@/blocks/types'
32+
import { isSafeKey } from '@/tools/safe-assign'
3233
import { getTrigger, isTriggerValid } from '@/triggers'
3334

3435
const logger = createLogger('CatalogBlockProjection')
@@ -300,6 +301,7 @@ export function computeOperationLevelInputs(
300301
const definition = declaredInputFor(subBlock, inputs)
301302
if (!definition) continue
302303
for (const operationId of gate.values) {
304+
if (!isSafeKey(operationId) || !isSafeKey(subBlock.id)) continue
303305
operationInputs[operationId] ??= {}
304306
operationInputs[operationId][subBlock.id] = definition
305307
}

‎apps/sim/lib/internal/onepassword/json-patch.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { describe, expect, it } from 'vitest'
22
import {
33
applyOnePasswordPatch,
44
isSafeOnePasswordPatch,
5+
MAX_PATCH_OPERATIONS,
6+
MAX_PATCH_PATH_DEPTH,
57
} from '@/lib/internal/onepassword/json-patch'
68

79
describe('applyOnePasswordPatch', () => {
@@ -71,3 +73,30 @@ describe('prototype paths', () => {
7173
}
7274
)
7375
})
76+
77+
describe('patch bounds', () => {
78+
it.each([
79+
{ name: 'no operations', operations: [] },
80+
{ name: 'an empty path', operations: [{ op: 'replace', path: '', value: 1 }] },
81+
{ name: 'the root path', operations: [{ op: 'replace', path: '/', value: 1 }] },
82+
])('refuses a patch with $name', ({ operations }) => {
83+
expect(isSafeOnePasswordPatch(operations)).toBe(false)
84+
})
85+
86+
it('refuses a patch with more operations than the cap', () => {
87+
const operations = Array.from({ length: MAX_PATCH_OPERATIONS + 1 }, () => ({
88+
op: 'replace',
89+
path: '/title',
90+
value: 'x',
91+
}))
92+
expect(isSafeOnePasswordPatch(operations)).toBe(false)
93+
expect(isSafeOnePasswordPatch(operations.slice(0, MAX_PATCH_OPERATIONS))).toBe(true)
94+
})
95+
96+
it('refuses a path deeper than the cap', () => {
97+
const deep = `/${Array.from({ length: MAX_PATCH_PATH_DEPTH + 1 }, () => 'a').join('/')}`
98+
const atCap = `/${Array.from({ length: MAX_PATCH_PATH_DEPTH }, () => 'a').join('/')}`
99+
expect(isSafeOnePasswordPatch([{ op: 'add', path: deep, value: 1 }])).toBe(false)
100+
expect(isSafeOnePasswordPatch([{ op: 'add', path: atCap, value: 1 }])).toBe(true)
101+
})
102+
})

‎apps/sim/lib/internal/onepassword/json-patch.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isSafeKey } from '@/tools/safe-assign'
2+
13
export interface JsonPatchOperation {
24
op: 'add' | 'remove' | 'replace'
35
path: string
@@ -10,27 +12,35 @@ function recordValue(value: unknown): Record<string, unknown> | undefined {
1012
: undefined
1113
}
1214

13-
/** Path segments that reach an object's prototype instead of its own data. */
14-
const PROTOTYPE_SEGMENTS = new Set(['__proto__', 'prototype', 'constructor'])
15+
/** The most operations one item patch may carry. */
16+
export const MAX_PATCH_OPERATIONS = 500
17+
18+
/** The most segments one patch path may have; 1Password item paths are a few levels deep. */
19+
export const MAX_PATCH_PATH_DEPTH = 16
1520

1621
/** The operations 1Password's item patch accepts; anything else would be applied as a removal. */
1722
const PATCH_OPS = new Set<unknown>(['add', 'remove', 'replace'])
1823

1924
/**
20-
* Whether every operation is one 1Password accepts and its path names only an item's own data.
21-
* Patches are applied to a plain object in this process, so a path through `__proto__` would
22-
* write onto every object here.
25+
* Whether every operation is one 1Password accepts and its path names a field of the item's own
26+
* data, within the size bounds. Patches are applied to a plain object in this process, so a path
27+
* through `__proto__` would write onto every object here, and a path naming nothing would report
28+
* success for a patch that changed nothing.
2329
*/
2430
export function isSafeOnePasswordPatch(operations: unknown): operations is JsonPatchOperation[] {
2531
return (
2632
Array.isArray(operations) &&
33+
operations.length > 0 &&
34+
operations.length <= MAX_PATCH_OPERATIONS &&
2735
operations.every((operation) => {
2836
const record = recordValue(operation)
2937
const path = record?.path
38+
if (!PATCH_OPS.has(record?.op) || typeof path !== 'string') return false
39+
const segments = path.split('/').filter(Boolean)
3040
return (
31-
PATCH_OPS.has(record?.op) &&
32-
typeof path === 'string' &&
33-
path.split('/').every((segment) => !PROTOTYPE_SEGMENTS.has(segment))
41+
segments.length > 0 &&
42+
segments.length <= MAX_PATCH_PATH_DEPTH &&
43+
path.split('/').every((segment) => isSafeKey(segment))
3444
)
3545
})
3646
)
@@ -60,7 +70,7 @@ export function applyOnePasswordPatch(
6070
): void {
6171
const segments = operation.path.split('/').filter(Boolean)
6272
const rootKey = segments[0]
63-
if (!rootKey || segments.some((segment) => PROTOTYPE_SEGMENTS.has(segment))) return
73+
if (!rootKey || !segments.every((segment) => isSafeKey(segment))) return
6474

6575
if (segments.length === 1) {
6676
if (operation.op === 'replace' || operation.op === 'add') {

‎apps/sim/lib/internal/onepassword/operations.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import { OnePasswordOperationError } from '@/lib/internal/onepassword/errors'
3333
import {
3434
applyOnePasswordPatch,
3535
isSafeOnePasswordPatch,
36+
MAX_PATCH_OPERATIONS,
37+
MAX_PATCH_PATH_DEPTH,
3638
} from '@/lib/internal/onepassword/json-patch'
3739
import {
3840
createInternalToolFileResult,
@@ -279,7 +281,7 @@ export async function executeOnePasswordUpdateItem(
279281
const operations: unknown = JSON.parse(input.operations)
280282
if (!isSafeOnePasswordPatch(operations)) {
281283
throw new OnePasswordOperationError(400, {
282-
error: "Each patch operation needs a path to the item's own fields.",
284+
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.`,
283285
})
284286
}
285287
if (credentials.mode === 'service_account') {

‎apps/sim/providers/anthropic/structured-output-schema.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function resolvePath(root: JsonSchemaNode, path: (string | number)[]): JsonSchem
149149
current = current[segment]
150150
continue
151151
}
152-
if (!isSchemaNode(current)) return null
152+
if (!isSchemaNode(current) || !Object.hasOwn(current, segment)) return null
153153
current = current[segment]
154154
}
155155
return isSchemaNode(current) ? current : null

0 commit comments

Comments
 (0)