Skip to content

Commit a378757

Browse files
fix: check chat/vote ownership during actions (#847)
1 parent c58fd52 commit a378757

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

app/(chat)/api/chat/route.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ export async function POST(request: Request) {
5858
const title = await generateTitleFromUserMessage({
5959
message: userMessage,
6060
});
61+
6162
await saveChat({ id, userId: session.user.id, title });
63+
} else {
64+
if (chat.userId !== session.user.id) {
65+
return new Response('Unauthorized', { status: 401 });
66+
}
6267
}
6368

6469
await saveMessages({

app/(chat)/api/document/route.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export async function POST(request: Request) {
6767

6868
return Response.json(document, { status: 200 });
6969
}
70+
7071
return new Response('Unauthorized', { status: 401 });
7172
}
7273

app/(chat)/api/vote/route.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { auth } from '@/app/(auth)/auth';
2-
import { getVotesByChatId, voteMessage } from '@/lib/db/queries';
2+
import { getChatById, getVotesByChatId, voteMessage } from '@/lib/db/queries';
33

44
export async function GET(request: Request) {
55
const { searchParams } = new URL(request.url);
@@ -15,6 +15,16 @@ export async function GET(request: Request) {
1515
return new Response('Unauthorized', { status: 401 });
1616
}
1717

18+
const chat = await getChatById({ id: chatId });
19+
20+
if (!chat) {
21+
return new Response('Chat not found', { status: 404 });
22+
}
23+
24+
if (chat.userId !== session.user.id) {
25+
return new Response('Unauthorized', { status: 401 });
26+
}
27+
1828
const votes = await getVotesByChatId({ id: chatId });
1929

2030
return Response.json(votes, { status: 200 });
@@ -38,6 +48,16 @@ export async function PATCH(request: Request) {
3848
return new Response('Unauthorized', { status: 401 });
3949
}
4050

51+
const chat = await getChatById({ id: chatId });
52+
53+
if (!chat) {
54+
return new Response('Chat not found', { status: 404 });
55+
}
56+
57+
if (chat.userId !== session.user.id) {
58+
return new Response('Unauthorized', { status: 401 });
59+
}
60+
4161
await voteMessage({
4262
chatId,
4363
messageId,

playwright.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default defineConfig({
2929
/* Fail the build on CI if you accidentally left test.only in the source code. */
3030
forbidOnly: !!process.env.CI,
3131
/* Retry on CI only */
32-
retries: process.env.CI ? 2 : 0,
32+
retries: process.env.CI ? 2 : 1,
3333
/* Opt out of parallel tests on CI. */
3434
workers: process.env.CI ? 1 : undefined,
3535
/* Reporter to use. See https://playwright.dev/docs/test-reporters */

0 commit comments

Comments
 (0)