Skip to content

Commit eb12333

Browse files
authored
fix(chat-otp): re-check authType before minting deployment auth cookie (#5600)
PUT verify no longer trusts a stale authType at cookie-mint time — it now re-checks the chat is still email-auth before issuing the cookie, matching the existing POST guard and the public-file OTP route.
1 parent b731162 commit eb12333

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

apps/sim/app/api/chat/[identifier]/otp/route.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,43 @@ describe('Chat OTP API Route', () => {
529529
})
530530
})
531531

532+
describe('PUT - Verify OTP (authType re-check)', () => {
533+
beforeEach(() => {
534+
mockGetStorageMethod.mockReturnValue('redis')
535+
mockRedisGet.mockResolvedValue(`${mockOTP}:0`)
536+
})
537+
538+
it('rejects verification when the chat has switched away from email auth', async () => {
539+
mockDbSelect.mockImplementationOnce(() => ({
540+
from: vi.fn().mockReturnValue({
541+
where: vi.fn().mockReturnValue({
542+
limit: vi.fn().mockResolvedValue([
543+
{
544+
id: mockChatId,
545+
authType: 'password',
546+
password: 'encrypted-password',
547+
},
548+
]),
549+
}),
550+
}),
551+
}))
552+
553+
const request = new NextRequest('http://localhost:3000/api/chat/test/otp', {
554+
method: 'PUT',
555+
body: JSON.stringify({ email: mockEmail, otp: mockOTP }),
556+
})
557+
558+
await PUT(request, { params: Promise.resolve({ identifier: mockIdentifier }) })
559+
560+
expect(mockCreateErrorResponse).toHaveBeenCalledWith(
561+
'This chat does not use email authentication',
562+
400
563+
)
564+
expect(mockRedisGet).not.toHaveBeenCalled()
565+
expect(mockSetChatAuthCookie).not.toHaveBeenCalled()
566+
})
567+
})
568+
532569
describe('PUT - Verify OTP (Database path)', () => {
533570
beforeEach(() => {
534571
mockGetStorageMethod.mockReturnValue('database')

apps/sim/app/api/chat/[identifier]/otp/route.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ export const PUT = withRouteHandler(
174174

175175
const deployment = deploymentResult[0]
176176

177+
if (deployment.authType !== 'email') {
178+
return createErrorResponse('This chat does not use email authentication', 400)
179+
}
180+
177181
const storedValue = await getOTP('chat', deployment.id, email)
178182
if (!storedValue) {
179183
return createErrorResponse('No verification code found, request a new one', 400)

0 commit comments

Comments
 (0)