From 068c89d6b675e5219ec752d81264246b15dcee15 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 28 Nov 2026 16:50:17 -0800 Subject: [PATCH 1/3] fix(onepassword): apply item patches to the item's own fields only A patch path is now walked through an item's own properties, and the update operation refuses a path that names __proto__, prototype or constructor before calling 1Password. --- .../internal/onepassword/json-patch.test.ts | 26 ++++++++++++++++- .../lib/internal/onepassword/json-patch.ts | 29 +++++++++++++++++-- .../lib/internal/onepassword/operations.ts | 9 ++++-- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/apps/sim/lib/internal/onepassword/json-patch.test.ts b/apps/sim/lib/internal/onepassword/json-patch.test.ts index 2f1d222058c..406eb432c19 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.test.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.test.ts @@ -1,5 +1,8 @@ import { describe, expect, it } from 'vitest' -import { applyOnePasswordPatch } from '@/lib/internal/onepassword/json-patch' +import { + applyOnePasswordPatch, + isSafeOnePasswordPatch, +} from '@/lib/internal/onepassword/json-patch' describe('applyOnePasswordPatch', () => { it('resolves array segments by 1Password ID before numeric index', () => { @@ -31,3 +34,24 @@ describe('applyOnePasswordPatch', () => { expect(item.tags).toEqual(['two']) }) }) + +describe('prototype paths', () => { + it.each(['/__proto__/polluted', '/constructor/prototype/polluted', '/fields/__proto__/polluted'])( + 'never writes through %s onto shared prototypes', + (path) => { + const item: Record = { fields: [] } + + applyOnePasswordPatch(item, { op: 'add', path, value: 'yes' }) + + expect(({} as Record).polluted).toBeUndefined() + expect(Object.hasOwn(Object.prototype, 'polluted')).toBe(false) + } + ) + + it('refuses a patch whose path reaches a prototype', () => { + expect(isSafeOnePasswordPatch([{ op: 'add', path: '/__proto__/polluted', value: 1 }])).toBe( + false + ) + expect(isSafeOnePasswordPatch([{ op: 'replace', path: '/title', value: 'ok' }])).toBe(true) + }) +}) diff --git a/apps/sim/lib/internal/onepassword/json-patch.ts b/apps/sim/lib/internal/onepassword/json-patch.ts index dbf8ac02d12..16938334eed 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.ts @@ -10,6 +10,31 @@ 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']) + +/** + * Whether every operation's 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. + */ +export function isSafeOnePasswordPatch(operations: unknown): operations is JsonPatchOperation[] { + return ( + Array.isArray(operations) && + operations.every((operation) => { + const path = recordValue(operation)?.path + return ( + typeof path === 'string' && + path.split('/').every((segment) => !PROTOTYPE_SEGMENTS.has(segment)) + ) + }) + ) +} + +/** A record's own property, never one inherited from its prototype. */ +function ownValue(record: Record | undefined, key: string): unknown { + return record && Object.hasOwn(record, key) ? record[key] : undefined +} + function arrayIndexForSegment(target: unknown[], segment: string): number { const byId = target.findIndex((element) => recordValue(element)?.id === segment) if (byId !== -1) return byId @@ -29,7 +54,7 @@ export function applyOnePasswordPatch( ): void { const segments = operation.path.split('/').filter(Boolean) const rootKey = segments[0] - if (!rootKey) return + if (!rootKey || segments.some((segment) => PROTOTYPE_SEGMENTS.has(segment))) return if (segments.length === 1) { if (operation.op === 'replace' || operation.op === 'add') { @@ -47,7 +72,7 @@ export function applyOnePasswordPatch( if (Array.isArray(target)) { target = arrayElementForSegment(target, segment) } else { - target = recordValue(target)?.[segment] + target = ownValue(recordValue(target), segment) } if (target === undefined || target === null) return } diff --git a/apps/sim/lib/internal/onepassword/operations.ts b/apps/sim/lib/internal/onepassword/operations.ts index e1a7fc91e3f..d56fe502305 100644 --- a/apps/sim/lib/internal/onepassword/operations.ts +++ b/apps/sim/lib/internal/onepassword/operations.ts @@ -32,7 +32,7 @@ import { import { OnePasswordOperationError } from '@/lib/internal/onepassword/errors' import { applyOnePasswordPatch, - type JsonPatchOperation, + isSafeOnePasswordPatch, } from '@/lib/internal/onepassword/json-patch' import { createInternalToolFileResult, @@ -276,7 +276,12 @@ export async function executeOnePasswordUpdateItem( context: OnePasswordOperationContext ): Promise { const credentials = resolveCredentials(input) - const operations = JSON.parse(input.operations) as JsonPatchOperation[] + 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.", + }) + } if (credentials.mode === 'service_account') { const client = await createOnePasswordClient(credentials.serviceAccountToken, context.signal) const existing = await runSdk(context.signal, () => From f3c99c33604826d1a09a3049e852ff28e7cc4b50 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 28 Nov 2026 16:54:36 -0800 Subject: [PATCH 2/3] test(onepassword): cover the own-property walk with an inherited segment --- apps/sim/lib/internal/onepassword/json-patch.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/apps/sim/lib/internal/onepassword/json-patch.test.ts b/apps/sim/lib/internal/onepassword/json-patch.test.ts index 406eb432c19..74c83686f28 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.test.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.test.ts @@ -48,6 +48,15 @@ describe('prototype paths', () => { } ) + it('never walks into a property the item only inherits', () => { + const shared: Record = {} + const item = Object.create({ section: shared }) as Record + + applyOnePasswordPatch(item, { op: 'add', path: '/section/label', value: 'yes' }) + + expect(shared).toEqual({}) + }) + it('refuses a patch whose path reaches a prototype', () => { expect(isSafeOnePasswordPatch([{ op: 'add', path: '/__proto__/polluted', value: 1 }])).toBe( false From e76cde9b2487d2a77e56e911a3d8e1e727f07407 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sat, 28 Nov 2026 17:04:04 -0800 Subject: [PATCH 3/3] fix(onepassword): accept only add, remove and replace patch operations --- apps/sim/lib/internal/onepassword/json-patch.test.ts | 7 +++++++ apps/sim/lib/internal/onepassword/json-patch.ts | 12 +++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/apps/sim/lib/internal/onepassword/json-patch.test.ts b/apps/sim/lib/internal/onepassword/json-patch.test.ts index 74c83686f28..7f72d20190c 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.test.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.test.ts @@ -63,4 +63,11 @@ describe('prototype paths', () => { ) expect(isSafeOnePasswordPatch([{ op: 'replace', path: '/title', value: 'ok' }])).toBe(true) }) + + it.each(['test', 'copy', 'move', undefined])( + 'refuses a %s operation, which would otherwise be applied as a removal', + (op) => { + expect(isSafeOnePasswordPatch([{ op, path: '/title', value: 'keep' }])).toBe(false) + } + ) }) diff --git a/apps/sim/lib/internal/onepassword/json-patch.ts b/apps/sim/lib/internal/onepassword/json-patch.ts index 16938334eed..4f6f34f5793 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.ts @@ -13,16 +13,22 @@ function recordValue(value: unknown): Record | undefined { /** Path segments that reach an object's prototype instead of its own data. */ const PROTOTYPE_SEGMENTS = new Set(['__proto__', 'prototype', 'constructor']) +/** 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's 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 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. */ export function isSafeOnePasswordPatch(operations: unknown): operations is JsonPatchOperation[] { return ( Array.isArray(operations) && operations.every((operation) => { - const path = recordValue(operation)?.path + const record = recordValue(operation) + const path = record?.path return ( + PATCH_OPS.has(record?.op) && typeof path === 'string' && path.split('/').every((segment) => !PROTOTYPE_SEGMENTS.has(segment)) )