-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(imap): resolve mailbox selector contexts server-side #7117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BillLeoutsakosvl346
wants to merge
1
commit into
fix/server-resolved-selector-context
Choose a base branch
from
fix/server-resolved-imap-selectors
base: fix/server-resolved-selector-context
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
apps/sim/app/api/tools/imap/mailboxes/server-resolved-selector.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { createMockRequest } from '@sim/testing' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| authenticate: vi.fn(), | ||
| resolveContext: vi.fn(), | ||
| validateDatabaseHost: vi.fn(), | ||
| imapConstruct: vi.fn(), | ||
| imapConnect: vi.fn(), | ||
| imapList: vi.fn(), | ||
| imapLogout: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/selectors/server/resolve-authorized-context', () => ({ | ||
| authenticateSelectorRequest: mocks.authenticate, | ||
| resolveAuthorizedSelectorContext: mocks.resolveContext, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/core/security/input-validation.server', () => ({ | ||
| validateDatabaseHost: mocks.validateDatabaseHost, | ||
| })) | ||
|
|
||
| vi.mock('imapflow', () => ({ | ||
| ImapFlow: class MockImapFlow { | ||
| constructor(config: unknown) { | ||
| mocks.imapConstruct(config) | ||
| } | ||
|
|
||
| connect = mocks.imapConnect | ||
| list = mocks.imapList | ||
| logout = mocks.imapLogout | ||
| }, | ||
| })) | ||
|
|
||
| import { POST as listMailboxes } from '@/app/api/tools/imap/mailboxes/route' | ||
|
|
||
| function request(body: unknown) { | ||
| return createMockRequest('POST', body, {}, 'http://localhost:3000/api/tools/imap/mailboxes') | ||
| } | ||
|
|
||
| const wireBody = { | ||
| workflowId: 'workflow-1', | ||
| host: '{{IMAP_HOST}}', | ||
| port: '{{IMAP_PORT}}', | ||
| secure: '{{IMAP_TLS}}', | ||
| username: '{{IMAP_USERNAME}}', | ||
| password: '{{IMAP_PASSWORD}}', | ||
| } | ||
|
|
||
| describe('server-resolved IMAP mailbox selector', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mocks.authenticate.mockResolvedValue({ | ||
| ok: true, | ||
| principal: { kind: 'session', userId: 'viewer-1', sessionId: 'session-1' }, | ||
| }) | ||
| mocks.resolveContext.mockResolvedValue({ | ||
| ok: true, | ||
| context: { | ||
| host: 'imap.example.com', | ||
| port: '993', | ||
| secure: 'true', | ||
| username: 'mailbox@example.com', | ||
| password: 'resolved-password', | ||
| }, | ||
| requesterUserId: 'viewer-1', | ||
| workspaceId: 'workspace-1', | ||
| }) | ||
| mocks.validateDatabaseHost.mockResolvedValue({ | ||
| isValid: true, | ||
| resolvedIP: '203.0.113.10', | ||
| }) | ||
| mocks.imapConnect.mockResolvedValue(undefined) | ||
| mocks.imapList.mockResolvedValue([ | ||
| { path: 'Archive', name: 'Archive', delimiter: '/' }, | ||
| { path: 'INBOX', name: 'INBOX', delimiter: '/' }, | ||
| ]) | ||
| mocks.imapLogout.mockResolvedValue(undefined) | ||
| }) | ||
|
|
||
| it('authenticates before parsing malformed requests', async () => { | ||
| mocks.authenticate.mockResolvedValue({ ok: false, status: 401, error: 'Unauthorized' }) | ||
|
|
||
| const response = await listMailboxes(request({})) | ||
|
|
||
| expect(response.status).toBe(401) | ||
| expect(mocks.resolveContext).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('passes every raw connection field to the resolver and maps sorted mailboxes', async () => { | ||
| const response = await listMailboxes(request(wireBody)) | ||
|
|
||
| expect(await response.json()).toEqual({ | ||
| success: true, | ||
| mailboxes: [ | ||
| { path: 'INBOX', name: 'INBOX', delimiter: '/' }, | ||
| { path: 'Archive', name: 'Archive', delimiter: '/' }, | ||
| ], | ||
| }) | ||
| expect(mocks.resolveContext).toHaveBeenCalledWith(expect.anything(), { | ||
| workflowId: 'workflow-1', | ||
| context: { | ||
| host: '{{IMAP_HOST}}', | ||
| port: '{{IMAP_PORT}}', | ||
| secure: '{{IMAP_TLS}}', | ||
| username: '{{IMAP_USERNAME}}', | ||
| password: '{{IMAP_PASSWORD}}', | ||
| }, | ||
| }) | ||
| expect(mocks.validateDatabaseHost).toHaveBeenCalledWith('imap.example.com', 'host', { | ||
| logFailureDetails: false, | ||
| }) | ||
| expect(mocks.imapConstruct).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| host: '203.0.113.10', | ||
| servername: 'imap.example.com', | ||
| port: 993, | ||
| secure: true, | ||
| auth: { user: 'mailbox@example.com', pass: 'resolved-password' }, | ||
| }) | ||
| ) | ||
| expect(mocks.imapLogout).toHaveBeenCalledOnce() | ||
| }) | ||
|
|
||
| it('short-circuits inaccessible references before DNS or provider access', async () => { | ||
| mocks.resolveContext.mockResolvedValue({ | ||
| ok: false, | ||
| status: 400, | ||
| error: 'Unable to resolve selector configuration', | ||
| }) | ||
|
|
||
| const response = await listMailboxes(request(wireBody)) | ||
|
|
||
| expect(response.status).toBe(400) | ||
| expect(await response.json()).toEqual({ | ||
| success: false, | ||
| message: 'Unable to resolve selector configuration', | ||
| }) | ||
| expect(mocks.validateDatabaseHost).not.toHaveBeenCalled() | ||
| expect(mocks.imapConstruct).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('rejects invalid resolved port/TLS values before DNS or provider access', async () => { | ||
| mocks.resolveContext.mockResolvedValue({ | ||
| ok: true, | ||
| context: { | ||
| host: 'imap.example.com', | ||
| port: 'invalid', | ||
| secure: 'invalid', | ||
| username: 'mailbox@example.com', | ||
| password: 'resolved-password', | ||
| }, | ||
| requesterUserId: 'viewer-1', | ||
| workspaceId: 'workspace-1', | ||
| }) | ||
|
|
||
| const response = await listMailboxes(request(wireBody)) | ||
|
|
||
| expect(response.status).toBe(400) | ||
| expect(mocks.validateDatabaseHost).not.toHaveBeenCalled() | ||
| expect(mocks.imapConstruct).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('sanitizes provider connection failures and logs out best-effort', async () => { | ||
| mocks.imapConnect.mockRejectedValue( | ||
| new Error('connection failed with resolved-password and imap.example.com') | ||
| ) | ||
|
|
||
| const response = await listMailboxes(request(wireBody)) | ||
| const body = await response.json() | ||
|
|
||
| expect(response.status).toBe(500) | ||
| expect(body).toEqual({ | ||
| success: false, | ||
| message: 'Failed to connect to IMAP server. Please check your connection settings.', | ||
| }) | ||
| expect(mocks.imapLogout).toHaveBeenCalledOnce() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
apps/sim/hooks/selectors/providers/imap/server-resolved-context.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const mocks = vi.hoisted(() => ({ requestJson: vi.fn() })) | ||
|
|
||
| vi.mock('@/lib/api/client/request', () => ({ requestJson: mocks.requestJson })) | ||
|
|
||
| import { imapSelectors } from '@/hooks/selectors/providers/imap/selectors' | ||
|
|
||
| describe('IMAP server-resolved selector context', () => { | ||
| beforeEach(() => vi.clearAllMocks()) | ||
|
|
||
| it('opts every connection field into server resolution', () => { | ||
| expect(imapSelectors['imap.mailboxes']?.serverResolvedContextFields).toEqual([ | ||
| 'host', | ||
| 'port', | ||
| 'secure', | ||
| 'username', | ||
| 'password', | ||
| ]) | ||
| }) | ||
|
|
||
| it('forwards raw references and maps mailbox options', async () => { | ||
| const definition = imapSelectors['imap.mailboxes']! | ||
| const context = { | ||
| workflowId: 'workflow-1', | ||
| host: '{{IMAP_HOST}}', | ||
| port: '{{IMAP_PORT}}', | ||
| secure: '{{IMAP_TLS}}', | ||
| username: '{{IMAP_USERNAME}}', | ||
| password: '{{IMAP_PASSWORD}}', | ||
| } | ||
| mocks.requestJson.mockResolvedValue({ | ||
| success: true, | ||
| mailboxes: [{ path: 'INBOX', name: 'Inbox', delimiter: '/' }], | ||
| }) | ||
|
|
||
| expect(await definition.fetchList!({ key: 'imap.mailboxes', context })).toEqual([ | ||
| { id: 'INBOX', label: 'Inbox' }, | ||
| ]) | ||
| expect(mocks.requestJson.mock.calls[0][1].body).toEqual(context) | ||
| }) | ||
|
|
||
| it('requires a workflow and keeps connection plaintext out of base query keys', () => { | ||
| const definition = imapSelectors['imap.mailboxes']! | ||
| const context = { | ||
| workspaceId: 'workspace-1', | ||
| workflowId: 'workflow-1', | ||
| host: 'private-imap.example.com', | ||
| port: '993', | ||
| secure: 'true', | ||
| username: 'private-user@example.com', | ||
| password: 'imap-literal-secret', | ||
| } | ||
| const key = definition.getQueryKey!({ key: 'imap.mailboxes', context }) | ||
|
|
||
| expect(definition.enabled?.({ key: 'imap.mailboxes', context })).toBe(true) | ||
| expect( | ||
| definition.enabled?.({ | ||
| key: 'imap.mailboxes', | ||
| context: { ...context, workflowId: undefined }, | ||
| }) | ||
| ).toBe(false) | ||
| for (const secret of [ | ||
| 'private-imap.example.com', | ||
| 'private-user@example.com', | ||
| 'imap-literal-secret', | ||
| ]) { | ||
| expect(JSON.stringify(key)).not.toContain(secret) | ||
| } | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.