-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(security): chat attachment XSS, MCP OAuth SSRF guards, Teams clientState verification #4877
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
983370e
fix(chat): prevent XSS in attachment preview via filename/data URL es…
waleedlatif1 94e6f88
fix(mcp): guard OAuth discovery and token revocation against SSRF
waleedlatif1 80e460d
fix(webhooks): verify Graph clientState on Teams chat-subscription no…
waleedlatif1 07fd134
fix(webhooks): fail closed when Teams chat-subscription webhook id is…
waleedlatif1 6636456
docs(mcp): note AbortSignal does not bound SSRF-guard DNS lookup
waleedlatif1 617073c
improvement(chat): hoist HTML escape map to module-level constant
waleedlatif1 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
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
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
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
120 changes: 120 additions & 0 deletions
120
apps/sim/lib/webhooks/providers/microsoft-teams.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,120 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { authOAuthUtilsMock, inputValidationMock } from '@sim/testing' | ||
| import { NextRequest } from 'next/server' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock) | ||
| vi.mock('@/app/api/auth/oauth/utils', () => authOAuthUtilsMock) | ||
|
|
||
| import { microsoftTeamsHandler } from '@/lib/webhooks/providers/microsoft-teams' | ||
|
|
||
| const WEBHOOK_ID = 'webhook-uuid-1234' | ||
|
|
||
| function makeRequest(body: string): NextRequest { | ||
| return new NextRequest('https://app.example.com/api/webhooks/trigger/abc', { | ||
| method: 'POST', | ||
| body, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }) | ||
| } | ||
|
|
||
| function makeNotificationBody(clientState?: unknown): string { | ||
| return JSON.stringify({ | ||
| value: [ | ||
| { | ||
| subscriptionId: 'sub-1', | ||
| changeType: 'created', | ||
| resource: 'chats/19:abc@thread.v2/messages/1700000000000', | ||
| resourceData: { id: '1700000000000' }, | ||
| ...(clientState !== undefined ? { clientState } : {}), | ||
| }, | ||
| ], | ||
| }) | ||
| } | ||
|
|
||
| async function runVerifyAuth(rawBody: string, providerConfig: Record<string, unknown>) { | ||
| return microsoftTeamsHandler.verifyAuth!({ | ||
| webhook: { id: WEBHOOK_ID }, | ||
| workflow: {}, | ||
| request: makeRequest(rawBody), | ||
| rawBody, | ||
| requestId: 'test-req', | ||
| providerConfig, | ||
| }) | ||
| } | ||
|
|
||
| describe('microsoftTeamsHandler verifyAuth (chat subscription clientState)', () => { | ||
| const chatSubscriptionConfig = { triggerId: 'microsoftteams_chat_subscription' } | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('accepts notifications whose clientState matches the webhook id', async () => { | ||
| const res = await runVerifyAuth(makeNotificationBody(WEBHOOK_ID), chatSubscriptionConfig) | ||
| expect(res).toBeNull() | ||
| }) | ||
|
|
||
| it('rejects notifications with a forged clientState', async () => { | ||
| const res = await runVerifyAuth(makeNotificationBody('forged'), chatSubscriptionConfig) | ||
| expect(res?.status).toBe(401) | ||
| }) | ||
|
|
||
| it('rejects notifications missing clientState', async () => { | ||
| const res = await runVerifyAuth(makeNotificationBody(), chatSubscriptionConfig) | ||
| expect(res?.status).toBe(401) | ||
| }) | ||
|
|
||
| it('rejects non-string clientState values', async () => { | ||
| const res = await runVerifyAuth( | ||
| makeNotificationBody({ nested: WEBHOOK_ID }), | ||
| chatSubscriptionConfig | ||
| ) | ||
| expect(res?.status).toBe(401) | ||
| }) | ||
|
|
||
| it('rejects payloads without a value array', async () => { | ||
| const res = await runVerifyAuth(JSON.stringify({ hello: 'world' }), chatSubscriptionConfig) | ||
| expect(res?.status).toBe(401) | ||
| }) | ||
|
|
||
| it('rejects payloads with an empty value array', async () => { | ||
| const res = await runVerifyAuth(JSON.stringify({ value: [] }), chatSubscriptionConfig) | ||
| expect(res?.status).toBe(401) | ||
| }) | ||
|
|
||
| it('rejects unparseable bodies', async () => { | ||
| const res = await runVerifyAuth('not-json', chatSubscriptionConfig) | ||
| expect(res?.status).toBe(401) | ||
| }) | ||
|
|
||
| it('rejects batches where any notification has a mismatched clientState', async () => { | ||
| const rawBody = JSON.stringify({ | ||
| value: [ | ||
| { subscriptionId: 'sub-1', resourceData: { id: '1' }, clientState: WEBHOOK_ID }, | ||
| { subscriptionId: 'sub-2', resourceData: { id: '2' }, clientState: 'forged' }, | ||
| ], | ||
| }) | ||
| const res = await runVerifyAuth(rawBody, chatSubscriptionConfig) | ||
| expect(res?.status).toBe(401) | ||
| }) | ||
|
|
||
| it('fails closed when the webhook record has no id', async () => { | ||
| const res = await microsoftTeamsHandler.verifyAuth!({ | ||
| webhook: {}, | ||
| workflow: {}, | ||
| request: makeRequest(makeNotificationBody('')), | ||
| rawBody: makeNotificationBody(''), | ||
| requestId: 'test-req', | ||
| providerConfig: chatSubscriptionConfig, | ||
| }) | ||
| expect(res?.status).toBe(401) | ||
| }) | ||
|
|
||
| it('does not require clientState for non-subscription trigger types', async () => { | ||
| const res = await runVerifyAuth(JSON.stringify({ type: 'message', text: 'hi' }), {}) | ||
| expect(res).toBeNull() | ||
| }) | ||
| }) |
Oops, something went wrong.
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.