Skip to content

Commit 776c33a

Browse files
authored
fix(oauth): reject cross-site Instagram workspace launches (#9788)
1 parent 887bece commit 776c33a

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎apps/sim/app/api/auth/instagram/authorize/route.test.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createMockRequest } from '@sim/testing'
2+
import { createRouteContext } from '@sim/testing/helpers/http'
23
import { authMockFns } from '@sim/testing/mocks/auth.mock'
34
import { oauthUtilsMock, oauthUtilsMockFns } from '@sim/testing/mocks/oauth-utils.mock'
45
import { urlsMockFns } from '@sim/testing/mocks/urls.mock'
@@ -45,7 +46,7 @@ describe('Instagram authorize route', () => {
4546
'https://sim.test/api/auth/instagram/authorize?workspaceId=workspace-1&draftId=draft-exact'
4647
)
4748

48-
const response = await GET(request)
49+
const response = await GET(request, createRouteContext({}))
4950

5051
expect(response.status).toBe(307)
5152
expect(response.headers.get('set-cookie')).toContain(
@@ -54,6 +55,20 @@ describe('Instagram authorize route', () => {
5455
expect(mocks.createCredentialConnection).not.toHaveBeenCalled()
5556
})
5657

58+
it('refuses a cross-site workspace launch before creating a connection draft', async () => {
59+
const request = createMockRequest(
60+
'GET',
61+
undefined,
62+
{ 'sec-fetch-site': 'cross-site' },
63+
'https://sim.test/api/auth/instagram/authorize?workspaceId=workspace-1'
64+
)
65+
66+
const response = await GET(request, createRouteContext({}))
67+
68+
expect(response.status).toBe(403)
69+
expect(mocks.createCredentialConnection).not.toHaveBeenCalled()
70+
})
71+
5772
it('returns a conflict when a different connection intent is already active', async () => {
5873
mocks.createCredentialConnection.mockRejectedValue(
5974
new OrchestrationError(
@@ -68,7 +83,7 @@ describe('Instagram authorize route', () => {
6883
'https://sim.test/api/auth/instagram/authorize?workspaceId=workspace-1'
6984
)
7085

71-
const response = await GET(request)
86+
const response = await GET(request, createRouteContext({}))
7287

7388
expect(response.status).toBe(409)
7489
await expect(response.json()).resolves.toEqual({

‎apps/sim/app/api/auth/instagram/authorize/route.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { parseRequest } from '@/lib/api/server'
66
import { getSession } from '@/lib/auth'
77
import { requireConfiguredOAuthClient } from '@/lib/core/config/env-capabilities.server'
88
import { asOrchestrationError } from '@/lib/core/orchestration/types'
9+
import { isCrossSiteSessionRequest } from '@/lib/core/security/same-origin'
910
import { getBaseUrl } from '@/lib/core/utils/urls'
1011
import { isSameOrigin } from '@/lib/core/utils/validation'
1112
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
@@ -41,6 +42,12 @@ export const GET = withRouteHandler(async (request: NextRequest) => {
4142
let credentialDraftId = draftId
4243

4344
if (workspaceId && !draftId) {
45+
// The query alone picks the workspace, so a cross-site page must not be
46+
// able to start this launch with the viewer's session cookie.
47+
if (isCrossSiteSessionRequest(request)) {
48+
logger.warn('Rejected cross-site Instagram workspace launch', { userId: session.user.id })
49+
return NextResponse.json({ error: 'Cross-site launch refused' }, { status: 403 })
50+
}
4451
try {
4552
const connection = await createCredentialConnection.execute({
4653
principal: { kind: 'session', userId: session.user.id, sessionId },

‎scripts/check-test-types.baseline.json‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
"apps/sim/app/api/auth/forget-password/route.test.ts": {
1515
"TS2554": 8
1616
},
17-
"apps/sim/app/api/auth/instagram/authorize/route.test.ts": {
18-
"TS2554": 2
19-
},
2017
"apps/sim/app/api/auth/oauth/credentials/route.test.ts": {
2118
"TS2322": 1,
2219
"TS2345": 2,

0 commit comments

Comments
 (0)