Skip to content

Commit f65dccd

Browse files
committed
fix(integrations): validate agentmail
Encode AgentMail inbox, thread, draft and message IDs as single path segments. Message IDs are RFC 5322 Message-IDs, which may contain /, #, ? or %, so the raw interpolation could address the wrong resource.
1 parent 0be28a0 commit f65dccd

20 files changed

Lines changed: 70 additions & 19 deletions
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { agentmailGetMessageTool } from '@/tools/agentmail/get_message'
3+
import { agentmailReplyMessageTool } from '@/tools/agentmail/reply_message'
4+
5+
type UrlBuilder = (params: Record<string, unknown>) => string
6+
7+
describe('agentmail request paths', () => {
8+
it('encodes RFC 5322 message IDs so their characters stay inside one path segment', () => {
9+
const url = (agentmailGetMessageTool.request.url as UrlBuilder)({
10+
apiKey: 'key',
11+
inboxId: 'agent@agentmail.to',
12+
messageId: '<a/b#c?d%e@mail.example.com>',
13+
})
14+
15+
expect(new URL(url).pathname).toBe(
16+
'/v0/inboxes/agent%40agentmail.to/messages/%3Ca%2Fb%23c%3Fd%25e%40mail.example.com%3E'
17+
)
18+
})
19+
20+
it('rejects a dot-segment message ID instead of addressing the parent path', () => {
21+
const url = agentmailReplyMessageTool.request.url as UrlBuilder
22+
23+
expect(() => url({ apiKey: 'key', inboxId: 'agent@agentmail.to', messageId: '..' })).toThrow(
24+
/messageId/
25+
)
26+
})
27+
})

‎apps/sim/tools/agentmail/create_draft.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { CreateDraftParams, CreateDraftResult } from '@/tools/agentmail/types'
22
import { AGENTMAIL_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
33
import type { ToolConfig } from '@/tools/types'
4+
import { safeUrlPathSegment } from '@/tools/url-path'
45

56
export const agentmailCreateDraftTool: ToolConfig<CreateDraftParams, CreateDraftResult> = {
67
id: 'agentmail_create_draft',
@@ -73,7 +74,8 @@ export const agentmailCreateDraftTool: ToolConfig<CreateDraftParams, CreateDraft
7374
},
7475

7576
request: {
76-
url: (params) => `https://api.agentmail.to/v0/inboxes/${params.inboxId.trim()}/drafts`,
77+
url: (params) =>
78+
`https://api.agentmail.to/v0/inboxes/${safeUrlPathSegment(params.inboxId, 'inboxId')}/drafts`,
7779
method: 'POST',
7880
headers: (params) => ({
7981
Authorization: `Bearer ${params.apiKey}`,

‎apps/sim/tools/agentmail/delete_draft.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { DeleteDraftParams, DeleteDraftResult } from '@/tools/agentmail/types'
22
import { AGENTMAIL_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
33
import type { ToolConfig } from '@/tools/types'
4+
import { safeUrlPathSegment } from '@/tools/url-path'
45

56
export const agentmailDeleteDraftTool: ToolConfig<DeleteDraftParams, DeleteDraftResult> = {
67
id: 'agentmail_delete_draft',
@@ -32,7 +33,7 @@ export const agentmailDeleteDraftTool: ToolConfig<DeleteDraftParams, DeleteDraft
3233

3334
request: {
3435
url: (params) =>
35-
`https://api.agentmail.to/v0/inboxes/${params.inboxId.trim()}/drafts/${params.draftId.trim()}`,
36+
`https://api.agentmail.to/v0/inboxes/${safeUrlPathSegment(params.inboxId, 'inboxId')}/drafts/${safeUrlPathSegment(params.draftId, 'draftId')}`,
3637
method: 'DELETE',
3738
headers: (params) => ({
3839
Authorization: `Bearer ${params.apiKey}`,

‎apps/sim/tools/agentmail/delete_inbox.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { DeleteInboxParams, DeleteInboxResult } from '@/tools/agentmail/types'
22
import { AGENTMAIL_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
33
import type { ToolConfig } from '@/tools/types'
4+
import { safeUrlPathSegment } from '@/tools/url-path'
45

56
export const agentmailDeleteInboxTool: ToolConfig<DeleteInboxParams, DeleteInboxResult> = {
67
id: 'agentmail_delete_inbox',
@@ -25,7 +26,8 @@ export const agentmailDeleteInboxTool: ToolConfig<DeleteInboxParams, DeleteInbox
2526
},
2627

2728
request: {
28-
url: (params) => `https://api.agentmail.to/v0/inboxes/${params.inboxId.trim()}`,
29+
url: (params) =>
30+
`https://api.agentmail.to/v0/inboxes/${safeUrlPathSegment(params.inboxId, 'inboxId')}`,
2931
method: 'DELETE',
3032
headers: (params) => ({
3133
Authorization: `Bearer ${params.apiKey}`,

‎apps/sim/tools/agentmail/delete_thread.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { DeleteThreadParams, DeleteThreadResult } from '@/tools/agentmail/types'
22
import { AGENTMAIL_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
33
import type { ToolConfig } from '@/tools/types'
4+
import { safeUrlPathSegment } from '@/tools/url-path'
45

56
export const agentmailDeleteThreadTool: ToolConfig<DeleteThreadParams, DeleteThreadResult> = {
67
id: 'agentmail_delete_thread',
@@ -42,7 +43,7 @@ export const agentmailDeleteThreadTool: ToolConfig<DeleteThreadParams, DeleteThr
4243
const query = new URLSearchParams()
4344
if (params.permanent) query.set('permanent', 'true')
4445
const qs = query.toString()
45-
return `https://api.agentmail.to/v0/inboxes/${params.inboxId.trim()}/threads/${params.threadId.trim()}${qs ? `?${qs}` : ''}`
46+
return `https://api.agentmail.to/v0/inboxes/${safeUrlPathSegment(params.inboxId, 'inboxId')}/threads/${safeUrlPathSegment(params.threadId, 'threadId')}${qs ? `?${qs}` : ''}`
4647
},
4748
method: 'DELETE',
4849
headers: (params) => ({

‎apps/sim/tools/agentmail/forward_message.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ForwardMessageParams, ForwardMessageResult } from '@/tools/agentmail/types'
22
import { AGENTMAIL_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
33
import type { ToolConfig } from '@/tools/types'
4+
import { safeUrlPathSegment } from '@/tools/url-path'
45

56
export const agentmailForwardMessageTool: ToolConfig<ForwardMessageParams, ForwardMessageResult> = {
67
id: 'agentmail_forward_message',
@@ -68,7 +69,7 @@ export const agentmailForwardMessageTool: ToolConfig<ForwardMessageParams, Forwa
6869

6970
request: {
7071
url: (params) =>
71-
`https://api.agentmail.to/v0/inboxes/${params.inboxId.trim()}/messages/${params.messageId.trim()}/forward`,
72+
`https://api.agentmail.to/v0/inboxes/${safeUrlPathSegment(params.inboxId, 'inboxId')}/messages/${safeUrlPathSegment(params.messageId, 'messageId', { allowSeparators: true })}/forward`,
7273
method: 'POST',
7374
headers: (params) => ({
7475
Authorization: `Bearer ${params.apiKey}`,

‎apps/sim/tools/agentmail/get_draft.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { GetDraftParams, GetDraftResult } from '@/tools/agentmail/types'
22
import { AGENTMAIL_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
33
import type { ToolConfig } from '@/tools/types'
4+
import { safeUrlPathSegment } from '@/tools/url-path'
45

56
export const agentmailGetDraftTool: ToolConfig<GetDraftParams, GetDraftResult> = {
67
id: 'agentmail_get_draft',
@@ -32,7 +33,7 @@ export const agentmailGetDraftTool: ToolConfig<GetDraftParams, GetDraftResult> =
3233

3334
request: {
3435
url: (params) =>
35-
`https://api.agentmail.to/v0/inboxes/${params.inboxId.trim()}/drafts/${params.draftId.trim()}`,
36+
`https://api.agentmail.to/v0/inboxes/${safeUrlPathSegment(params.inboxId, 'inboxId')}/drafts/${safeUrlPathSegment(params.draftId, 'draftId')}`,
3637
method: 'GET',
3738
headers: (params) => ({
3839
Authorization: `Bearer ${params.apiKey}`,

‎apps/sim/tools/agentmail/get_inbox.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { GetInboxParams, GetInboxResult } from '@/tools/agentmail/types'
22
import { AGENTMAIL_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
33
import type { ToolConfig } from '@/tools/types'
4+
import { safeUrlPathSegment } from '@/tools/url-path'
45

56
export const agentmailGetInboxTool: ToolConfig<GetInboxParams, GetInboxResult> = {
67
id: 'agentmail_get_inbox',
@@ -25,7 +26,8 @@ export const agentmailGetInboxTool: ToolConfig<GetInboxParams, GetInboxResult> =
2526
},
2627

2728
request: {
28-
url: (params) => `https://api.agentmail.to/v0/inboxes/${params.inboxId.trim()}`,
29+
url: (params) =>
30+
`https://api.agentmail.to/v0/inboxes/${safeUrlPathSegment(params.inboxId, 'inboxId')}`,
2931
method: 'GET',
3032
headers: (params) => ({
3133
Authorization: `Bearer ${params.apiKey}`,

‎apps/sim/tools/agentmail/get_message.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { GetMessageParams, GetMessageResult } from '@/tools/agentmail/types'
22
import { AGENTMAIL_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
33
import type { ToolConfig } from '@/tools/types'
4+
import { safeUrlPathSegment } from '@/tools/url-path'
45

56
export const agentmailGetMessageTool: ToolConfig<GetMessageParams, GetMessageResult> = {
67
id: 'agentmail_get_message',
@@ -32,7 +33,7 @@ export const agentmailGetMessageTool: ToolConfig<GetMessageParams, GetMessageRes
3233

3334
request: {
3435
url: (params) =>
35-
`https://api.agentmail.to/v0/inboxes/${params.inboxId.trim()}/messages/${params.messageId.trim()}`,
36+
`https://api.agentmail.to/v0/inboxes/${safeUrlPathSegment(params.inboxId, 'inboxId')}/messages/${safeUrlPathSegment(params.messageId, 'messageId', { allowSeparators: true })}`,
3637
method: 'GET',
3738
headers: (params) => ({
3839
Authorization: `Bearer ${params.apiKey}`,

‎apps/sim/tools/agentmail/get_thread.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { GetThreadParams, GetThreadResult } from '@/tools/agentmail/types'
22
import { AGENTMAIL_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
33
import type { ToolConfig } from '@/tools/types'
4+
import { safeUrlPathSegment } from '@/tools/url-path'
45

56
export const agentmailGetThreadTool: ToolConfig<GetThreadParams, GetThreadResult> = {
67
id: 'agentmail_get_thread',
@@ -32,7 +33,7 @@ export const agentmailGetThreadTool: ToolConfig<GetThreadParams, GetThreadResult
3233

3334
request: {
3435
url: (params) =>
35-
`https://api.agentmail.to/v0/inboxes/${params.inboxId.trim()}/threads/${params.threadId.trim()}`,
36+
`https://api.agentmail.to/v0/inboxes/${safeUrlPathSegment(params.inboxId, 'inboxId')}/threads/${safeUrlPathSegment(params.threadId, 'threadId')}`,
3637
method: 'GET',
3738
headers: (params) => ({
3839
Authorization: `Bearer ${params.apiKey}`,

0 commit comments

Comments
 (0)