fix: verify document ownership before appending version (#929)
This commit is contained in:
parent
291c6ab2fe
commit
c937db3050
2 changed files with 19 additions and 13 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { auth } from '@/app/(auth)/auth';
|
||||
import { ArtifactKind } from '@/components/artifact';
|
||||
import type { ArtifactKind } from '@/components/artifact';
|
||||
import {
|
||||
deleteDocumentsByIdAfterTimestamp,
|
||||
getDocumentsById,
|
||||
|
|
@ -45,7 +45,7 @@ export async function POST(request: Request) {
|
|||
|
||||
const session = await auth();
|
||||
|
||||
if (!session) {
|
||||
if (!session?.user?.id) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
|
|
@ -56,7 +56,16 @@ export async function POST(request: Request) {
|
|||
}: { content: string; title: string; kind: ArtifactKind } =
|
||||
await request.json();
|
||||
|
||||
if (session.user?.id) {
|
||||
const documents = await getDocumentsById({ id: id });
|
||||
|
||||
if (documents.length > 0) {
|
||||
const [document] = documents;
|
||||
|
||||
if (document.userId !== session.user.id) {
|
||||
return new Response('Forbidden', { status: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
const document = await saveDocument({
|
||||
id,
|
||||
content,
|
||||
|
|
@ -68,9 +77,6 @@ export async function POST(request: Request) {
|
|||
return Response.json(document, { status: 200 });
|
||||
}
|
||||
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
export async function PATCH(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const id = searchParams.get('id');
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "ai-chatbot",
|
||||
"version": "3.0.0",
|
||||
"version": "3.0.1",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev --turbo",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue