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 { auth } from '@/app/(auth)/auth';
|
||||||
import { ArtifactKind } from '@/components/artifact';
|
import type { ArtifactKind } from '@/components/artifact';
|
||||||
import {
|
import {
|
||||||
deleteDocumentsByIdAfterTimestamp,
|
deleteDocumentsByIdAfterTimestamp,
|
||||||
getDocumentsById,
|
getDocumentsById,
|
||||||
|
|
@ -45,7 +45,7 @@ export async function POST(request: Request) {
|
||||||
|
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
|
|
||||||
if (!session) {
|
if (!session?.user?.id) {
|
||||||
return new Response('Unauthorized', { status: 401 });
|
return new Response('Unauthorized', { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,7 +56,16 @@ export async function POST(request: Request) {
|
||||||
}: { content: string; title: string; kind: ArtifactKind } =
|
}: { content: string; title: string; kind: ArtifactKind } =
|
||||||
await request.json();
|
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({
|
const document = await saveDocument({
|
||||||
id,
|
id,
|
||||||
content,
|
content,
|
||||||
|
|
@ -68,9 +77,6 @@ export async function POST(request: Request) {
|
||||||
return Response.json(document, { status: 200 });
|
return Response.json(document, { status: 200 });
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response('Unauthorized', { status: 401 });
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function PATCH(request: Request) {
|
export async function PATCH(request: Request) {
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
const id = searchParams.get('id');
|
const id = searchParams.get('id');
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "ai-chatbot",
|
"name": "ai-chatbot",
|
||||||
"version": "3.0.0",
|
"version": "3.0.1",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "next dev --turbo",
|
"dev": "next dev --turbo",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue