Skip to content

Commit c6282d7

Browse files
committed
fix(integrations): return 400 for bad 1password path ids and reject unknown ketch consent values
1 parent 8a95a38 commit c6282d7

3 files changed

Lines changed: 45 additions & 12 deletions

File tree

‎apps/sim/blocks/blocks/ketch.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ describe('KetchBlock', () => {
5353
})
5454
})
5555

56+
it('rejects an allowed value Ketch would silently record as denied', () => {
57+
const buildBody = setConsentTool.request.body
58+
if (!buildBody) throw new Error('set consent has no body builder')
59+
60+
expect(() =>
61+
buildBody({
62+
organizationCode: 'org',
63+
propertyCode: 'web',
64+
environmentCode: 'production',
65+
identities: { email: 'a@example.com' },
66+
purposes: { analytics: { allowed: 'yes' } },
67+
})
68+
).toThrow('invalid "allowed" value')
69+
})
70+
5671
it('surfaces the message of a Ketch error envelope', async () => {
5772
const transform = invokeRightTool.transformResponse
5873
if (!transform) throw new Error('invoke right has no response transform')

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ItemCreateParams } from '@1password/sdk'
2+
import { getErrorMessage } from '@sim/utils/errors'
23
import { generateId } from '@sim/utils/id'
34
import { toRecord } from '@sim/utils/object'
45
import type { ContractBody } from '@/lib/api/contracts'
@@ -67,6 +68,17 @@ async function runSdk<T>(signal: AbortSignal | undefined, operation: () => Promi
6768
return result
6869
}
6970

71+
/** Encodes a Connect path ID, rejecting an unusable one as a 400 rather than an internal error. */
72+
function connectPathSegment(value: string, paramName: string): string {
73+
try {
74+
return safeUrlPathSegment(value, paramName)
75+
} catch (error) {
76+
throw new OnePasswordOperationError(400, {
77+
error: getErrorMessage(error, `Invalid ${paramName}`),
78+
})
79+
}
80+
}
81+
7082
export async function executeOnePasswordListVaults(
7183
input: ListVaultsInput,
7284
context: OnePasswordOperationContext
@@ -117,7 +129,7 @@ export async function executeOnePasswordGetVault(
117129
const response = await connectRequest({
118130
serverUrl: credentials.serverUrl,
119131
apiKey: credentials.apiKey,
120-
path: `/v1/vaults/${safeUrlPathSegment(input.vaultId, 'vaultId')}`,
132+
path: `/v1/vaults/${connectPathSegment(input.vaultId, 'vaultId')}`,
121133
method: 'GET',
122134
signal: context.signal,
123135
})
@@ -148,7 +160,7 @@ export async function executeOnePasswordListItems(
148160
const response = await connectRequest({
149161
serverUrl: credentials.serverUrl,
150162
apiKey: credentials.apiKey,
151-
path: `/v1/vaults/${safeUrlPathSegment(input.vaultId, 'vaultId')}/items`,
163+
path: `/v1/vaults/${connectPathSegment(input.vaultId, 'vaultId')}/items`,
152164
method: 'GET',
153165
query: input.filter ? `filter=${encodeURIComponent(input.filter)}` : undefined,
154166
signal: context.signal,
@@ -177,7 +189,7 @@ export async function executeOnePasswordGetItem(
177189
const response = await connectRequest({
178190
serverUrl: credentials.serverUrl,
179191
apiKey: credentials.apiKey,
180-
path: `/v1/vaults/${safeUrlPathSegment(input.vaultId, 'vaultId')}/items/${safeUrlPathSegment(input.itemId, 'itemId')}`,
192+
path: `/v1/vaults/${connectPathSegment(input.vaultId, 'vaultId')}/items/${connectPathSegment(input.itemId, 'itemId')}`,
181193
method: 'GET',
182194
signal: context.signal,
183195
})
@@ -244,7 +256,7 @@ export async function executeOnePasswordCreateItem(
244256
const response = await connectRequest({
245257
serverUrl: credentials.serverUrl,
246258
apiKey: credentials.apiKey,
247-
path: `/v1/vaults/${safeUrlPathSegment(input.vaultId, 'vaultId')}/items`,
259+
path: `/v1/vaults/${connectPathSegment(input.vaultId, 'vaultId')}/items`,
248260
method: 'POST',
249261
body,
250262
signal: context.signal,
@@ -281,7 +293,7 @@ export async function executeOnePasswordUpdateItem(
281293
const response = await connectRequest({
282294
serverUrl: credentials.serverUrl,
283295
apiKey: credentials.apiKey,
284-
path: `/v1/vaults/${safeUrlPathSegment(input.vaultId, 'vaultId')}/items/${safeUrlPathSegment(input.itemId, 'itemId')}`,
296+
path: `/v1/vaults/${connectPathSegment(input.vaultId, 'vaultId')}/items/${connectPathSegment(input.itemId, 'itemId')}`,
285297
method: 'PATCH',
286298
body: operations,
287299
signal: context.signal,
@@ -316,7 +328,7 @@ export async function executeOnePasswordReplaceItem(
316328
const response = await connectRequest({
317329
serverUrl: credentials.serverUrl,
318330
apiKey: credentials.apiKey,
319-
path: `/v1/vaults/${safeUrlPathSegment(input.vaultId, 'vaultId')}/items/${safeUrlPathSegment(input.itemId, 'itemId')}`,
331+
path: `/v1/vaults/${connectPathSegment(input.vaultId, 'vaultId')}/items/${connectPathSegment(input.itemId, 'itemId')}`,
320332
method: 'PUT',
321333
body: itemData,
322334
signal: context.signal,
@@ -345,7 +357,7 @@ export async function executeOnePasswordDeleteItem(
345357
const response = await connectRequest({
346358
serverUrl: credentials.serverUrl,
347359
apiKey: credentials.apiKey,
348-
path: `/v1/vaults/${safeUrlPathSegment(input.vaultId, 'vaultId')}/items/${safeUrlPathSegment(input.itemId, 'itemId')}`,
360+
path: `/v1/vaults/${connectPathSegment(input.vaultId, 'vaultId')}/items/${connectPathSegment(input.itemId, 'itemId')}`,
349361
method: 'DELETE',
350362
signal: context.signal,
351363
})
@@ -402,7 +414,7 @@ export async function executeOnePasswordGetItemFile(
402414
const metadataResponse = await connectRequest({
403415
serverUrl: credentials.serverUrl,
404416
apiKey: credentials.apiKey,
405-
path: `/v1/vaults/${safeUrlPathSegment(input.vaultId, 'vaultId')}/items/${safeUrlPathSegment(input.itemId, 'itemId')}/files/${safeUrlPathSegment(input.fileId, 'fileId')}`,
417+
path: `/v1/vaults/${connectPathSegment(input.vaultId, 'vaultId')}/items/${connectPathSegment(input.itemId, 'itemId')}/files/${connectPathSegment(input.fileId, 'fileId')}`,
406418
method: 'GET',
407419
signal: context.signal,
408420
})
@@ -419,7 +431,7 @@ export async function executeOnePasswordGetItemFile(
419431
const contentResponse = await connectRequest({
420432
serverUrl: credentials.serverUrl,
421433
apiKey: credentials.apiKey,
422-
path: `/v1/vaults/${safeUrlPathSegment(input.vaultId, 'vaultId')}/items/${safeUrlPathSegment(input.itemId, 'itemId')}/files/${safeUrlPathSegment(input.fileId, 'fileId')}/content`,
434+
path: `/v1/vaults/${connectPathSegment(input.vaultId, 'vaultId')}/items/${connectPathSegment(input.itemId, 'itemId')}/files/${connectPathSegment(input.fileId, 'fileId')}/content`,
423435
method: 'GET',
424436
maxResponseBytes: MAX_FILE_SIZE,
425437
signal: context.signal,

‎apps/sim/tools/ketch/set_consent.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ const ALLOWED_ALIASES = new Map([
1212

1313
/**
1414
* Ketch stores `allowed` as the string `"true"` or `"false"` and records any other value
15-
* (including `"granted"`) as denied, so booleans and granted/denied are converted first.
15+
* (including `"granted"`) as denied, so booleans and granted/denied are converted first and
16+
* anything else is rejected rather than silently saved as denied.
1617
*/
1718
function normalizePurposes(purposes: KetchSetConsentParams['purposes']): Record<string, unknown> {
1819
const normalized: Record<string, unknown> = {}
1920
for (const [code, setting] of Object.entries(purposes ?? {})) {
20-
if (!setting || typeof setting !== 'object') {
21+
if (!setting || typeof setting !== 'object' || setting.allowed === undefined) {
2122
normalized[code] = setting
2223
continue
2324
}
@@ -28,7 +29,12 @@ function normalizePurposes(purposes: KetchSetConsentParams['purposes']): Record<
2829
: typeof allowed === 'string'
2930
? ALLOWED_ALIASES.get(allowed.trim().toLowerCase())
3031
: undefined
31-
normalized[code] = alias ? { ...setting, allowed: alias } : setting
32+
if (!alias) {
33+
throw new Error(
34+
`Purpose "${code}" has an invalid "allowed" value: use "true" or "false" ("granted"/"denied" are also accepted)`
35+
)
36+
}
37+
normalized[code] = { ...setting, allowed: alias }
3238
}
3339
return normalized
3440
}

0 commit comments

Comments
 (0)