Skip to content

Commit c937db3

Browse files
fix: verify document ownership before appending version (vercel#929)
1 parent 291c6ab commit c937db3

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { auth } from '@/app/(auth)/auth';
2-
import { ArtifactKind } from '@/components/artifact';
2+
import type { ArtifactKind } from '@/components/artifact';
33
import {
44
deleteDocumentsByIdAfterTimestamp,
55
getDocumentsById,
@@ -45,7 +45,7 @@ export async function POST(request: Request) {
4545

4646
const session = await auth();
4747

48-
if (!session) {
48+
if (!session?.user?.id) {
4949
return new Response('Unauthorized', { status: 401 });
5050
}
5151

@@ -56,19 +56,25 @@ export async function POST(request: Request) {
5656
}: { content: string; title: string; kind: ArtifactKind } =
5757
await request.json();
5858

59-
if (session.user?.id) {
60-
const document = await saveDocument({
61-
id,
62-
content,
63-
title,
64-
kind,
65-
userId: session.user.id,
66-
});
59+
const documents = await getDocumentsById({ id: id });
6760

68-
return Response.json(document, { status: 200 });
61+
if (documents.length > 0) {
62+
const [document] = documents;
63+
64+
if (document.userId !== session.user.id) {
65+
return new Response('Forbidden', { status: 403 });
66+
}
6967
}
7068

71-
return new Response('Unauthorized', { status: 401 });
69+
const document = await saveDocument({
70+
id,
71+
content,
72+
title,
73+
kind,
74+
userId: session.user.id,
75+
});
76+
77+
return Response.json(document, { status: 200 });
7278
}
7379

7480
export async function PATCH(request: Request) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ai-chatbot",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"private": true,
55
"scripts": {
66
"dev": "next dev --turbo",

0 commit comments

Comments
 (0)