diff --git a/apps/sim/lib/internal/onepassword/json-patch.test.ts b/apps/sim/lib/internal/onepassword/json-patch.test.ts index 2f1d222058c..7f72d20190c 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,40 @@ 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('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 + ) + 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 dbf8ac02d12..4f6f34f5793 100644 --- a/apps/sim/lib/internal/onepassword/json-patch.ts +++ b/apps/sim/lib/internal/onepassword/json-patch.ts @@ -10,6 +10,37 @@ 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 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. + */ +export function isSafeOnePasswordPatch(operations: unknown): operations is JsonPatchOperation[] { + return ( + Array.isArray(operations) && + operations.every((operation) => { + 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)) + ) + }) + ) +} + +/** 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 +60,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 +78,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, () =>