Skip to content

Commit e60f23d

Browse files
authored
fix(onepassword): apply item patches to the item's own fields only (#10505)
* 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. * test(onepassword): cover the own-property walk with an inherited segment * fix(onepassword): accept only add, remove and replace patch operations
1 parent 1115a56 commit e60f23d

3 files changed

Lines changed: 81 additions & 5 deletions

File tree

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { describe, expect, it } from 'vitest'
2-
import { applyOnePasswordPatch } from '@/lib/internal/onepassword/json-patch'
2+
import {
3+
applyOnePasswordPatch,
4+
isSafeOnePasswordPatch,
5+
} from '@/lib/internal/onepassword/json-patch'
36

47
describe('applyOnePasswordPatch', () => {
58
it('resolves array segments by 1Password ID before numeric index', () => {
@@ -31,3 +34,40 @@ describe('applyOnePasswordPatch', () => {
3134
expect(item.tags).toEqual(['two'])
3235
})
3336
})
37+
38+
describe('prototype paths', () => {
39+
it.each(['/__proto__/polluted', '/constructor/prototype/polluted', '/fields/__proto__/polluted'])(
40+
'never writes through %s onto shared prototypes',
41+
(path) => {
42+
const item: Record<string, unknown> = { fields: [] }
43+
44+
applyOnePasswordPatch(item, { op: 'add', path, value: 'yes' })
45+
46+
expect(({} as Record<string, unknown>).polluted).toBeUndefined()
47+
expect(Object.hasOwn(Object.prototype, 'polluted')).toBe(false)
48+
}
49+
)
50+
51+
it('never walks into a property the item only inherits', () => {
52+
const shared: Record<string, unknown> = {}
53+
const item = Object.create({ section: shared }) as Record<string, unknown>
54+
55+
applyOnePasswordPatch(item, { op: 'add', path: '/section/label', value: 'yes' })
56+
57+
expect(shared).toEqual({})
58+
})
59+
60+
it('refuses a patch whose path reaches a prototype', () => {
61+
expect(isSafeOnePasswordPatch([{ op: 'add', path: '/__proto__/polluted', value: 1 }])).toBe(
62+
false
63+
)
64+
expect(isSafeOnePasswordPatch([{ op: 'replace', path: '/title', value: 'ok' }])).toBe(true)
65+
})
66+
67+
it.each(['test', 'copy', 'move', undefined])(
68+
'refuses a %s operation, which would otherwise be applied as a removal',
69+
(op) => {
70+
expect(isSafeOnePasswordPatch([{ op, path: '/title', value: 'keep' }])).toBe(false)
71+
}
72+
)
73+
})

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,37 @@ function recordValue(value: unknown): Record<string, unknown> | undefined {
1010
: undefined
1111
}
1212

13+
/** Path segments that reach an object's prototype instead of its own data. */
14+
const PROTOTYPE_SEGMENTS = new Set(['__proto__', 'prototype', 'constructor'])
15+
16+
/** The operations 1Password's item patch accepts; anything else would be applied as a removal. */
17+
const PATCH_OPS = new Set<unknown>(['add', 'remove', 'replace'])
18+
19+
/**
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.
23+
*/
24+
export function isSafeOnePasswordPatch(operations: unknown): operations is JsonPatchOperation[] {
25+
return (
26+
Array.isArray(operations) &&
27+
operations.every((operation) => {
28+
const record = recordValue(operation)
29+
const path = record?.path
30+
return (
31+
PATCH_OPS.has(record?.op) &&
32+
typeof path === 'string' &&
33+
path.split('/').every((segment) => !PROTOTYPE_SEGMENTS.has(segment))
34+
)
35+
})
36+
)
37+
}
38+
39+
/** A record's own property, never one inherited from its prototype. */
40+
function ownValue(record: Record<string, unknown> | undefined, key: string): unknown {
41+
return record && Object.hasOwn(record, key) ? record[key] : undefined
42+
}
43+
1344
function arrayIndexForSegment(target: unknown[], segment: string): number {
1445
const byId = target.findIndex((element) => recordValue(element)?.id === segment)
1546
if (byId !== -1) return byId
@@ -29,7 +60,7 @@ export function applyOnePasswordPatch(
2960
): void {
3061
const segments = operation.path.split('/').filter(Boolean)
3162
const rootKey = segments[0]
32-
if (!rootKey) return
63+
if (!rootKey || segments.some((segment) => PROTOTYPE_SEGMENTS.has(segment))) return
3364

3465
if (segments.length === 1) {
3566
if (operation.op === 'replace' || operation.op === 'add') {
@@ -47,7 +78,7 @@ export function applyOnePasswordPatch(
4778
if (Array.isArray(target)) {
4879
target = arrayElementForSegment(target, segment)
4980
} else {
50-
target = recordValue(target)?.[segment]
81+
target = ownValue(recordValue(target), segment)
5182
}
5283
if (target === undefined || target === null) return
5384
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
import { OnePasswordOperationError } from '@/lib/internal/onepassword/errors'
3333
import {
3434
applyOnePasswordPatch,
35-
type JsonPatchOperation,
35+
isSafeOnePasswordPatch,
3636
} from '@/lib/internal/onepassword/json-patch'
3737
import {
3838
createInternalToolFileResult,
@@ -276,7 +276,12 @@ export async function executeOnePasswordUpdateItem(
276276
context: OnePasswordOperationContext
277277
): Promise<unknown> {
278278
const credentials = resolveCredentials(input)
279-
const operations = JSON.parse(input.operations) as JsonPatchOperation[]
279+
const operations: unknown = JSON.parse(input.operations)
280+
if (!isSafeOnePasswordPatch(operations)) {
281+
throw new OnePasswordOperationError(400, {
282+
error: "Each patch operation needs a path to the item's own fields.",
283+
})
284+
}
280285
if (credentials.mode === 'service_account') {
281286
const client = await createOnePasswordClient(credentials.serviceAccountToken, context.signal)
282287
const existing = await runSdk(context.signal, () =>

0 commit comments

Comments
 (0)