From 7863b96c31cd6788e5e1e6467673c420bf6f96bf Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Mon, 30 Nov 2026 13:57:54 -0800 Subject: [PATCH 1/3] fix(onepassword): refuse item patches whose path reaches nothing In service-account mode an update_item patch is applied in-process before the SDK saves the item. An operation whose path named no field was skipped, so the unchanged item was saved and reported as a success, where the Connect server rejects it per RFC 6902. applyOnePasswordPatch now reports whether each operation reached its target (replace/remove need an existing target, add an existing parent, '-' only appends), and update_item refuses with a 400 before saving. --- .../internal/onepassword/json-patch.test.ts | 23 ++++++ .../lib/internal/onepassword/json-patch.ts | 76 ++++++++----------- .../internal/onepassword/operations.test.ts | 25 ++++++ .../lib/internal/onepassword/operations.ts | 7 +- 4 files changed, 85 insertions(+), 46 deletions(-) diff --git a/apps/sim/lib/internal/onepassword/json-patch.test.ts b/apps/sim/lib/internal/onepassword/json-patch.test.ts index 814c10b24e1..e70b865a377 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.test.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.test.ts @@ -35,6 +35,29 @@ describe('applyOnePasswordPatch', () => { expect(item.tags).toEqual(['two']) }) + + it.each([ + { name: 'a missing field ID', op: 'replace', path: '/fields/missing-id/value' }, + { name: 'a missing array element', op: 'remove', path: '/fields/missing-id' }, + { name: 'an absent key', op: 'replace', path: '/title' }, + { name: 'removal of an absent key', op: 'remove', path: '/fields/0/label' }, + { name: 'a parent that is not a container', op: 'add', path: '/fields/0/value/x' }, + { name: 'an append that is not an add', op: 'replace', path: '/fields/-' }, + ] as const)('reports $name as missed and leaves the item unchanged', ({ op, path }) => { + const item: Record = { fields: [{ id: 'password', value: 'old' }] } + + expect(applyOnePasswordPatch(item, { op, path, value: 'new' })).toBe(false) + expect(item).toEqual({ fields: [{ id: 'password', value: 'old' }] }) + }) + + it('adds a new key under an existing parent', () => { + const item: Record = { fields: [{ id: 'password', value: 'old' }] } + + expect( + applyOnePasswordPatch(item, { op: 'add', path: '/fields/password/label', value: 'pw' }) + ).toBe(true) + expect(item.fields).toEqual([{ id: 'password', value: 'old', label: 'pw' }]) + }) }) describe('prototype paths', () => { diff --git a/apps/sim/lib/internal/onepassword/json-patch.ts b/apps/sim/lib/internal/onepassword/json-patch.ts index b1837567245..38d35e9967d 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.ts @@ -22,10 +22,10 @@ export const MAX_PATCH_PATH_DEPTH = 16 const PATCH_OPS = new Set(['add', 'remove', 'replace']) /** - * 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. + * Whether every operation is one 1Password accepts and its path is a non-empty path of safe keys, + * 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. Whether a path reaches a field of the item is + * known only once applied ({@link applyOnePasswordPatch}). */ export function isSafeOnePasswordPatch(operations: unknown): operations is JsonPatchOperation[] { return ( @@ -63,57 +63,43 @@ function arrayElementForSegment(target: unknown[], segment: string): unknown { return index === -1 ? undefined : target[index] } -/** Applies the Connect API's ID-aware RFC6902 path semantics. */ +/** + * Applies one operation with the Connect API's ID-aware RFC 6902 path semantics, and returns + * whether it reached its target. As in RFC 6902, `replace` and `remove` need an existing target and + * `add` an existing parent; a patch naming nothing is refused, never reported as a change. + */ export function applyOnePasswordPatch( item: Record, operation: JsonPatchOperation -): void { +): boolean { const segments = operation.path.split('/').filter(Boolean) - const rootKey = segments[0] - if (!rootKey || !segments.every((segment) => isSafeKey(segment))) return - - if (segments.length === 1) { - if (operation.op === 'replace' || operation.op === 'add') { - item[rootKey] = operation.value - } else { - delete item[rootKey] - } - return - } + const lastSegment = segments.at(-1) + if (!lastSegment || !segments.every((segment) => isSafeKey(segment))) return false let target: unknown = item - for (let index = 0; index < segments.length - 1; index++) { - const segment = segments[index] - if (!segment) return - if (Array.isArray(target)) { - target = arrayElementForSegment(target, segment) - } else { - target = ownValue(recordValue(target), segment) - } - if (target === undefined || target === null) return + for (const segment of segments.slice(0, -1)) { + target = Array.isArray(target) + ? arrayElementForSegment(target, segment) + : ownValue(recordValue(target), segment) + if (target === undefined) return false } - const lastSegment = segments.at(-1) - if (!lastSegment) return - - if (operation.op === 'replace' || operation.op === 'add') { - if (Array.isArray(target) && lastSegment === '-') { + if (Array.isArray(target)) { + if (operation.op === 'add' && lastSegment === '-') { target.push(operation.value) - } else if (Array.isArray(target)) { - const index = arrayIndexForSegment(target, lastSegment) - if (index !== -1) target[index] = operation.value - } else { - const record = recordValue(target) - if (record) record[lastSegment] = operation.value + return true } - return - } - - if (Array.isArray(target)) { const index = arrayIndexForSegment(target, lastSegment) - if (index !== -1) target.splice(index, 1) - } else { - const record = recordValue(target) - if (record) delete record[lastSegment] + if (index === -1) return false + if (operation.op === 'remove') target.splice(index, 1) + else target[index] = operation.value + return true } + + const record = recordValue(target) + if (!record) return false + if (operation.op !== 'add' && !Object.hasOwn(record, lastSegment)) return false + if (operation.op === 'remove') delete record[lastSegment] + else record[lastSegment] = operation.value + return true } diff --git a/apps/sim/lib/internal/onepassword/operations.test.ts b/apps/sim/lib/internal/onepassword/operations.test.ts index 62ddb203fcc..bee094227ef 100644 --- a/apps/sim/lib/internal/onepassword/operations.test.ts +++ b/apps/sim/lib/internal/onepassword/operations.test.ts @@ -87,6 +87,31 @@ describe('1Password operations', () => { expect(put).toHaveBeenCalledOnce() }) + it('refuses a patch naming a field the item does not have, without saving', async () => { + const put = vi.fn() + clientMocks.createOnePasswordClient.mockResolvedValue({ + items: { + get: vi + .fn() + .mockResolvedValue({ id: 'item-1', fields: [{ id: 'password', value: 'old' }] }), + put, + }, + }) + + await expect( + executeOnePasswordUpdateItem( + { + ...SERVICE_CREDENTIALS, + vaultId: 'vault-1', + itemId: 'item-1', + operations: '[{"op":"replace","path":"/fields/missing-id/value","value":"new"}]', + }, + {} + ) + ).rejects.toMatchObject>({ status: 400 }) + expect(put).not.toHaveBeenCalled() + }) + it('bounds SDK file reads before and after materialization', async () => { const read = vi.fn().mockResolvedValue(new Uint8Array(6)) clientMocks.createOnePasswordClient.mockResolvedValue({ diff --git a/apps/sim/lib/internal/onepassword/operations.ts b/apps/sim/lib/internal/onepassword/operations.ts index cea578fdc89..e7c05cea3a8 100644 --- a/apps/sim/lib/internal/onepassword/operations.ts +++ b/apps/sim/lib/internal/onepassword/operations.ts @@ -290,7 +290,12 @@ export async function executeOnePasswordUpdateItem( client.items.get(input.vaultId, input.itemId) ) const connectItem: Record = { ...normalizeSdkItem(existing) } - for (const operation of operations) applyOnePasswordPatch(connectItem, operation) + const missed = operations.find((operation) => !applyOnePasswordPatch(connectItem, operation)) + if (missed) { + throw new OnePasswordOperationError(400, { + error: `Patch operation "${missed.op} ${missed.path}" names nothing on this item, so the item was not changed.`, + }) + } const result = await runSdk(context.signal, () => client.items.put(connectItemToSdkItem(connectItem, existing)) ) From d9ad1bca8482d2ee203f7753647f189087edd4a7 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Mon, 30 Nov 2026 14:12:11 -0800 Subject: [PATCH 2/3] fix(onepassword): keep notes patchable, insert array adds --- .../internal/onepassword/json-patch.test.ts | 9 ++++++ .../lib/internal/onepassword/json-patch.ts | 14 +++++++--- .../internal/onepassword/operations.test.ts | 28 +++++++++++++++++++ .../lib/internal/onepassword/operations.ts | 6 +++- 4 files changed, 52 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 e70b865a377..d7b5ae2e024 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.test.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.test.ts @@ -50,6 +50,15 @@ describe('applyOnePasswordPatch', () => { expect(item).toEqual({ fields: [{ id: 'password', value: 'old' }] }) }) + it('inserts an array add at its index rather than overwriting the element there', () => { + const item: Record = { tags: ['a', 'b'] } + + expect(applyOnePasswordPatch(item, { op: 'add', path: '/tags/0', value: 'first' })).toBe(true) + expect(applyOnePasswordPatch(item, { op: 'add', path: '/tags/3', value: 'last' })).toBe(true) + expect(applyOnePasswordPatch(item, { op: 'add', path: '/tags/9', value: 'x' })).toBe(false) + expect(item.tags).toEqual(['first', 'a', 'b', 'last']) + }) + it('adds a new key under an existing parent', () => { const item: Record = { fields: [{ id: 'password', value: 'old' }] } diff --git a/apps/sim/lib/internal/onepassword/json-patch.ts b/apps/sim/lib/internal/onepassword/json-patch.ts index 38d35e9967d..0379768778f 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.ts @@ -65,8 +65,9 @@ function arrayElementForSegment(target: unknown[], segment: string): unknown { /** * Applies one operation with the Connect API's ID-aware RFC 6902 path semantics, and returns - * whether it reached its target. As in RFC 6902, `replace` and `remove` need an existing target and - * `add` an existing parent; a patch naming nothing is refused, never reported as a change. + * whether it reached its target. As in RFC 6902, `replace` and `remove` need an existing target, + * `add` an existing parent, and `add` into an array inserts; a patch naming nothing is refused, + * never reported as a change. */ export function applyOnePasswordPatch( item: Record, @@ -85,8 +86,13 @@ export function applyOnePasswordPatch( } if (Array.isArray(target)) { - if (operation.op === 'add' && lastSegment === '-') { - target.push(operation.value) + if (operation.op === 'add') { + const index = + lastSegment === '-' || lastSegment === String(target.length) + ? target.length + : arrayIndexForSegment(target, lastSegment) + if (index === -1) return false + target.splice(index, 0, operation.value) return true } const index = arrayIndexForSegment(target, lastSegment) diff --git a/apps/sim/lib/internal/onepassword/operations.test.ts b/apps/sim/lib/internal/onepassword/operations.test.ts index bee094227ef..9ce670e81a9 100644 --- a/apps/sim/lib/internal/onepassword/operations.test.ts +++ b/apps/sim/lib/internal/onepassword/operations.test.ts @@ -87,6 +87,34 @@ describe('1Password operations', () => { expect(put).toHaveBeenCalledOnce() }) + it('updates the notes the SDK item carries', async () => { + const existing = { id: 'item-1', notes: 'old notes', fields: [] } + // The real normalizer leaves notes out of the Connect shape. + clientMocks.normalizeSdkItem.mockImplementationOnce( + ({ notes, ...connectShape }) => connectShape + ) + const put = vi.fn().mockImplementation(async (item) => item) + clientMocks.createOnePasswordClient.mockResolvedValue({ + items: { get: vi.fn().mockResolvedValue(existing), put }, + }) + + await executeOnePasswordUpdateItem( + { + ...SERVICE_CREDENTIALS, + vaultId: 'vault-1', + itemId: 'item-1', + operations: '[{"op":"replace","path":"/notes","value":"new notes"}]', + }, + {} + ) + + expect(clientMocks.connectItemToSdkItem).toHaveBeenCalledWith( + expect.objectContaining({ notes: 'new notes' }), + existing + ) + expect(put).toHaveBeenCalledOnce() + }) + it('refuses a patch naming a field the item does not have, without saving', async () => { const put = vi.fn() clientMocks.createOnePasswordClient.mockResolvedValue({ diff --git a/apps/sim/lib/internal/onepassword/operations.ts b/apps/sim/lib/internal/onepassword/operations.ts index e7c05cea3a8..8347880ab04 100644 --- a/apps/sim/lib/internal/onepassword/operations.ts +++ b/apps/sim/lib/internal/onepassword/operations.ts @@ -289,7 +289,11 @@ export async function executeOnePasswordUpdateItem( const existing = await runSdk(context.signal, () => client.items.get(input.vaultId, input.itemId) ) - const connectItem: Record = { ...normalizeSdkItem(existing) } + // The Connect shape has no notes, but the SDK item has them and saves a patch to them. + const connectItem: Record = { + ...normalizeSdkItem(existing), + notes: existing.notes, + } const missed = operations.find((operation) => !applyOnePasswordPatch(connectItem, operation)) if (missed) { throw new OnePasswordOperationError(400, { From d02acd7b064ed5e5fca60c9a70cee08a338fd25b Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Mon, 30 Nov 2026 14:25:04 -0800 Subject: [PATCH 3/3] fix(onepassword): require patch values, match connect array adds --- .../internal/onepassword/json-patch.test.ts | 26 +++++++++++++++++++ .../lib/internal/onepassword/json-patch.ts | 19 ++++++++++---- 2 files changed, 40 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 d7b5ae2e024..d29d4c16ae2 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.test.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.test.ts @@ -59,6 +59,23 @@ describe('applyOnePasswordPatch', () => { expect(item.tags).toEqual(['first', 'a', 'b', 'last']) }) + it('resolves an add segment by ID before treating it as the array length', () => { + const item: Record = { fields: [{ id: '1', value: 'by-id' }] } + + expect( + applyOnePasswordPatch(item, { op: 'add', path: '/fields/1', value: { id: 'new' } }) + ).toBe(true) + expect(item.fields).toEqual([{ id: 'new' }, { id: '1', value: 'by-id' }]) + }) + + it('appends a single field added at the fields array, as Connect does', () => { + const item: Record = { fields: [{ id: 'password', value: 'old' }] } + const field = { label: 'API key', type: 'CONCEALED', value: 'secret' } + + expect(applyOnePasswordPatch(item, { op: 'add', path: '/fields', value: field })).toBe(true) + expect(item.fields).toEqual([{ id: 'password', value: 'old' }, field]) + }) + it('adds a new key under an existing parent', () => { const item: Record = { fields: [{ id: 'password', value: 'old' }] } @@ -115,6 +132,15 @@ describe('patch bounds', () => { expect(isSafeOnePasswordPatch(operations)).toBe(false) }) + it.each(['add', 'replace'])( + 'refuses a %s without a value, which would blank its target', + (op) => { + expect(isSafeOnePasswordPatch([{ op, path: '/fields/password/value' }])).toBe(false) + expect(isSafeOnePasswordPatch([{ op, path: '/fields/password/value', value: '' }])).toBe(true) + expect(isSafeOnePasswordPatch([{ op: 'remove', path: '/fields/password' }])).toBe(true) + } + ) + it('refuses a patch with more operations than the cap', () => { const operations = Array.from({ length: MAX_PATCH_OPERATIONS + 1 }, () => ({ op: 'replace', diff --git a/apps/sim/lib/internal/onepassword/json-patch.ts b/apps/sim/lib/internal/onepassword/json-patch.ts index 0379768778f..da1795702ac 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.ts @@ -35,7 +35,9 @@ export function isSafeOnePasswordPatch(operations: unknown): operations is JsonP operations.every((operation) => { const record = recordValue(operation) const path = record?.path - if (!PATCH_OPS.has(record?.op) || typeof path !== 'string') return false + if (!record || !PATCH_OPS.has(record.op) || typeof path !== 'string') return false + // RFC 6902: add and replace carry a value; without one the target would be blanked. + if (record.op !== 'remove' && !Object.hasOwn(record, 'value')) return false const segments = path.split('/').filter(Boolean) return ( segments.length > 0 && @@ -66,8 +68,8 @@ function arrayElementForSegment(target: unknown[], segment: string): unknown { /** * Applies one operation with the Connect API's ID-aware RFC 6902 path semantics, and returns * whether it reached its target. As in RFC 6902, `replace` and `remove` need an existing target, - * `add` an existing parent, and `add` into an array inserts; a patch naming nothing is refused, - * never reported as a change. + * `add` an existing parent, and `add` into an array inserts. As in the Connect API, a single element + * added at an array appends to it. A patch naming nothing is refused, never reported as a change. */ export function applyOnePasswordPatch( item: Record, @@ -87,10 +89,11 @@ export function applyOnePasswordPatch( if (Array.isArray(target)) { if (operation.op === 'add') { + const found = lastSegment === '-' ? -1 : arrayIndexForSegment(target, lastSegment) const index = - lastSegment === '-' || lastSegment === String(target.length) + found === -1 && (lastSegment === '-' || lastSegment === String(target.length)) ? target.length - : arrayIndexForSegment(target, lastSegment) + : found if (index === -1) return false target.splice(index, 0, operation.value) return true @@ -104,6 +107,12 @@ export function applyOnePasswordPatch( const record = recordValue(target) if (!record) return false + const existing = ownValue(record, lastSegment) + // Connect appends a single element added to an array, e.g. a field added at `/fields`. + if (operation.op === 'add' && Array.isArray(existing) && !Array.isArray(operation.value)) { + existing.push(operation.value) + return true + } if (operation.op !== 'add' && !Object.hasOwn(record, lastSegment)) return false if (operation.op === 'remove') delete record[lastSegment] else record[lastSegment] = operation.value