Skip to content

Commit c17048c

Browse files
committed
fix(mailchimp): parse tags as JSON only when they look like JSON and reject malformed JSON
1 parent 84e5fc9 commit c17048c

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

‎apps/sim/tools/mailchimp/mailchimp.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ describe('Mailchimp request bodies', () => {
9494
expect(bodyOf(mailchimpAddMemberTagsTool, { ...params, tags: '2024' })).toEqual({
9595
tags: [{ name: '2024', status: 'active' }],
9696
})
97+
expect(bodyOf(mailchimpAddMemberTagsTool, { ...params, tags: '1e3' })).toEqual({
98+
tags: [{ name: '1e3', status: 'active' }],
99+
})
100+
expect(() =>
101+
bodyOf(mailchimpAddMemberTagsTool, { ...params, tags: '[{"name":"VIP"}' })
102+
).toThrow()
97103
expect(() => bodyOf(mailchimpAddMemberTagsTool, { ...params, tags: '[]' })).toThrow()
98104
})
99105

‎apps/sim/tools/mailchimp/types.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,19 +389,24 @@ export function mailchimpMemberSegment(value: string): string {
389389

390390
/**
391391
* Normalizes the tags input for the member tags endpoint, which requires `{ name, status }`
392-
* objects. Accepts a JSON array of tag objects or names, or a comma-separated list of names;
393-
* a tag without a status gets `defaultStatus`.
392+
* objects. Accepts a JSON array (or object) of tag objects or names, or a comma-separated list of
393+
* names kept as text; a tag without a status gets `defaultStatus`.
394394
*/
395395
export function buildMemberTags(
396396
raw: unknown,
397397
defaultStatus: 'active' | 'inactive'
398398
): Array<{ name: string; status: string }> {
399399
let parsed: unknown = raw
400400
if (typeof raw === 'string') {
401-
try {
402-
parsed = JSON.parse(raw)
403-
} catch {
404-
parsed = raw.split(',')
401+
const trimmed = raw.trim()
402+
if (trimmed.startsWith('[') || trimmed.startsWith('{')) {
403+
try {
404+
parsed = JSON.parse(trimmed)
405+
} catch {
406+
throw new Error('Tags look like JSON but could not be parsed')
407+
}
408+
} else {
409+
parsed = trimmed.split(',')
405410
}
406411
}
407412
const items = Array.isArray(parsed) ? parsed : [parsed]

0 commit comments

Comments
 (0)