Skip to content

Commit

Permalink
Merge pull request #493 from hxdy-1/Adds-validation-on-API-routes
Browse files Browse the repository at this point in the history
Added request input validation on API routes
  • Loading branch information
hkirat authored May 12, 2024
2 parents 9db413a + d55703c commit 875caca
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 10 deletions.
23 changes: 22 additions & 1 deletion src/app/api/admin/course/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import { NextRequest, NextResponse } from 'next/server';
import db from '@/db';
import { z } from 'zod';

const requestBodySchema = z.object({
adminSecret: z.string(),
title: z.string(),
description: z.string(),
imageUrl: z.string().url().or(z.string()),
id: z.string(),
slug: z.string(),
appxCourseId: z.string(),
discordRoleId: z.string(),
});

export async function POST(req: NextRequest) {
const parseResult = requestBodySchema.safeParse(await req.json());

if (!parseResult.success) {
return NextResponse.json(
{ error: parseResult.error.message },
{ status: 400 },
);
}

const {
adminSecret,
title,
Expand All @@ -11,7 +32,7 @@ export async function POST(req: NextRequest) {
slug,
appxCourseId,
discordRoleId,
} = await req.json();
} = parseResult.data;

if (adminSecret !== process.env.ADMIN_SECRET) {
return NextResponse.json({}, { status: 401 });
Expand Down
18 changes: 16 additions & 2 deletions src/app/api/admin/discord/refresh/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import db from '@/db';
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';

const requestBodySchema = z.object({
adminSecret: z.string(),
email: z.string().email(),
});

export async function POST(req: NextRequest) {
const { adminSecret, email } = await req.json();
const parseResult = requestBodySchema.safeParse(await req.json());

if (!parseResult.success) {
return NextResponse.json(
{ error: parseResult.error.message },
{ status: 400 },
);
}
const { adminSecret, email } = parseResult.data;

if (adminSecret !== process.env.ADMIN_SECRET) {
return NextResponse.json({}, { status: 401 });
Expand All @@ -15,7 +29,7 @@ export async function POST(req: NextRequest) {
});

if (!user) {
return NextResponse.json({ msg: 'User not found' }, { status: 440 });
return NextResponse.json({ msg: 'User not found' }, { status: 404 });
}

await db.discordConnect.delete({
Expand Down
18 changes: 16 additions & 2 deletions src/app/api/admin/discord/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import db from '@/db';
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';

const requestBodySchema = z.object({
adminSecret: z.string(),
email: z.string().email(),
});

export async function POST(req: NextRequest) {
const { adminSecret, email } = await req.json();
const parseResult = requestBodySchema.safeParse(await req.json());

if (!parseResult.success) {
return NextResponse.json(
{ error: parseResult.error.message },
{ status: 400 },
);
}
const { adminSecret, email } = parseResult.data;

if (adminSecret !== process.env.ADMIN_SECRET) {
return NextResponse.json({}, { status: 401 });
Expand All @@ -15,7 +29,7 @@ export async function POST(req: NextRequest) {
});

if (!user) {
return NextResponse.json({ msg: 'User not found' }, { status: 440 });
return NextResponse.json({ msg: 'User not found' }, { status: 404 });
}

const response = await db.discordConnect.findFirst({
Expand Down
19 changes: 17 additions & 2 deletions src/app/api/admin/drm/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import db from '@/db';
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';

const requestBodySchema = z.object({
adminSecret: z.string(),
email: z.string().email(),
disableDrm: z.boolean(),
});

export async function POST(req: NextRequest) {
const { adminSecret, email, disableDrm } = await req.json();
const parseResult = requestBodySchema.safeParse(await req.json());

if (!parseResult.success) {
return NextResponse.json(
{ error: parseResult.error.message },
{ status: 400 },
);
}
const { adminSecret, email, disableDrm } = parseResult.data;

if (adminSecret !== process.env.ADMIN_SECRET) {
return NextResponse.json({}, { status: 401 });
Expand All @@ -15,7 +30,7 @@ export async function POST(req: NextRequest) {
});

if (!user) {
return NextResponse.json({ msg: 'User not found' }, { status: 440 });
return NextResponse.json({ msg: 'User not found' }, { status: 404 });
}

const response = await db.user.update({
Expand Down
16 changes: 15 additions & 1 deletion src/app/api/admin/segments/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import db from '@/db';
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';

const requestBodySchema = z.object({
adminSecret: z.string(),
contentId: z.number(),
segmentsJson: z.unknown(),
});

export async function POST(req: NextRequest) {
const { adminSecret, contentId, segmentsJson } = await req.json();
const parseResult = requestBodySchema.safeParse(await req.json());
if (!parseResult.success) {
return NextResponse.json(
{ error: parseResult.error.message },
{ status: 400 },
);
}
const { adminSecret, contentId, segmentsJson } = parseResult.data;

if (adminSecret !== process.env.ADMIN_SECRET) {
return NextResponse.json({}, { status: 401 });
Expand Down
15 changes: 14 additions & 1 deletion src/app/api/course/videoProgress/markAsCompleted/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@ import { NextRequest, NextResponse } from 'next/server';
import db from '@/db';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { z } from 'zod';

const requestBodySchema = z.object({
contentId: z.number(),
markAsCompleted: z.boolean(),
});

export async function POST(req: NextRequest) {
const { contentId, markAsCompleted } = await req.json();
const parseResult = requestBodySchema.safeParse(req.json());
if (!parseResult.success) {
return NextResponse.json(
{ error: parseResult.error.message },
{ status: 400 },
);
}
const { contentId, markAsCompleted } = parseResult.data;
const session = await getServerSession(authOptions);
if (!session || !session?.user) {
return NextResponse.json({}, { status: 401 });
Expand Down
16 changes: 15 additions & 1 deletion src/app/api/course/videoProgress/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import db from '@/db';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { revalidatePath } from 'next/cache';
import { z } from 'zod';

export async function GET(req: NextRequest) {
const url = new URL(req.url);
Expand All @@ -26,8 +27,21 @@ export async function GET(req: NextRequest) {
});
}

const requestBodySchema = z.object({
contentId: z.number(),
currentTimestamp: z.number(),
});

export async function POST(req: NextRequest) {
const { contentId, currentTimestamp } = await req.json();
const parseResult = requestBodySchema.safeParse(req.json());
if (!parseResult.success) {
return NextResponse.json(
{ error: parseResult.error.message },
{ status: 400 },
);
}
const { contentId, currentTimestamp } = parseResult.data;

const session = await getServerSession(authOptions);
if (!session || !session?.user) {
return NextResponse.json({}, { status: 401 });
Expand Down

0 comments on commit 875caca

Please sign in to comment.