fix: verify document ownership before appending version (#929)

This commit is contained in:
Jeremy 2025-04-13 23:36:41 -07:00 committed by GitHub
parent 291c6ab2fe
commit c937db3050
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 13 deletions

View file

@ -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');

View file

@ -1,6 +1,6 @@
{
"name": "ai-chatbot",
"version": "3.0.0",
"version": "3.0.1",
"private": true,
"scripts": {
"dev": "next dev --turbo",