Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion apps/sim/lib/internal/onepassword/json-patch.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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<string, unknown> = { fields: [] }

applyOnePasswordPatch(item, { op: 'add', path, value: 'yes' })
Comment thread
waleedlatif1 marked this conversation as resolved.
Resolved

expect(({} as Record<string, unknown>).polluted).toBeUndefined()
expect(Object.hasOwn(Object.prototype, 'polluted')).toBe(false)
}
)

it('never walks into a property the item only inherits', () => {
const shared: Record<string, unknown> = {}
const item = Object.create({ section: shared }) as Record<string, unknown>

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)
}
)
})
35 changes: 33 additions & 2 deletions apps/sim/lib/internal/onepassword/json-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@ function recordValue(value: unknown): Record<string, unknown> | 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<unknown>(['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' &&
Comment thread
waleedlatif1 marked this conversation as resolved.
Resolved
path.split('/').every((segment) => !PROTOTYPE_SEGMENTS.has(segment))
)
})
)
}

/** A record's own property, never one inherited from its prototype. */
function ownValue(record: Record<string, unknown> | 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
Expand All @@ -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') {
Expand All @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions apps/sim/lib/internal/onepassword/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -276,7 +276,12 @@ export async function executeOnePasswordUpdateItem(
context: OnePasswordOperationContext
): Promise<unknown> {
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, () =>
Expand Down
Loading