Skip to content

Commit c9f2a06

Browse files
waleedlatif1claude
andcommitted
fix(security): pin IMAP connections to validated resolved IP
Pass the resolved IP from validateDatabaseHost to ImapFlow as host, with the original hostname as servername for TLS SNI verification. Closes the DNS TOCTOU rebinding window. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d747565 commit c9f2a06

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

apps/sim/app/api/tools/imap/mailboxes/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export async function POST(request: NextRequest) {
3737
}
3838

3939
const client = new ImapFlow({
40-
host,
40+
host: hostValidation.resolvedIP!,
41+
servername: host,
4142
port: port || 993,
4243
secure: secure ?? true,
4344
auth: {

apps/sim/lib/webhooks/imap-polling-service.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ export async function pollImapWebhooks() {
182182
return
183183
}
184184

185-
const fetchResult = await fetchNewEmails(config, requestId)
185+
const fetchResult = await fetchNewEmails(
186+
config,
187+
requestId,
188+
hostValidation.resolvedIP!
189+
)
186190
const { emails, latestUidByMailbox } = fetchResult
187191
const pollTimestamp = new Date().toISOString()
188192

@@ -200,7 +204,8 @@ export async function pollImapWebhooks() {
200204
emails,
201205
webhookData,
202206
config,
203-
requestId
207+
requestId,
208+
hostValidation.resolvedIP!
204209
)
205210

206211
await updateWebhookLastProcessedUids(webhookId, latestUidByMailbox, pollTimestamp)
@@ -267,9 +272,14 @@ export async function pollImapWebhooks() {
267272
}
268273
}
269274

270-
async function fetchNewEmails(config: ImapWebhookConfig, requestId: string) {
275+
async function fetchNewEmails(
276+
config: ImapWebhookConfig,
277+
requestId: string,
278+
resolvedIP: string
279+
) {
271280
const client = new ImapFlow({
272-
host: config.host,
281+
host: resolvedIP,
282+
servername: config.host,
273283
port: config.port || 993,
274284
secure: config.secure ?? true,
275285
auth: {
@@ -563,13 +573,15 @@ async function processEmails(
563573
}>,
564574
webhookData: WebhookRecord,
565575
config: ImapWebhookConfig,
566-
requestId: string
576+
requestId: string,
577+
resolvedIP: string
567578
) {
568579
let processedCount = 0
569580
let failedCount = 0
570581

571582
const client = new ImapFlow({
572-
host: config.host,
583+
host: resolvedIP,
584+
servername: config.host,
573585
port: config.port || 993,
574586
secure: config.secure ?? true,
575587
auth: {

0 commit comments

Comments
 (0)