Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

handle invalid project slugs

Steven Teye01570a1be0f
diff --git a/apps/web/app/api/projects/[slug]/route.ts b/apps/web/app/api/projects/[slug]/route.ts
index 0279087414..27907fb3a7 100644
--- a/apps/web/app/api/projects/[slug]/route.ts
+++ b/apps/web/app/api/projects/[slug]/route.ts
@@ -2,6 +2,8 @@ import { withAuth } from "@/lib/auth";
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { deleteProject } from "@/lib/api/project";
+import { DEFAULT_REDIRECTS, validSlugRegex } from "@dub/utils";
+import { isReservedKey } from "@/lib/edge-config";
// GET /api/projects/[slug] – get a specific project
export const GET = withAuth(async ({ project, headers }) => {
@@ -12,6 +14,22 @@ export const GET = withAuth(async ({ project, headers }) => {
export const PUT = withAuth(
async ({ req, project }) => {
const { name, slug } = await req.json();
+
+ // check if slug is too long
+ if (slug.length > 48) {
+ return new Response("Slug must be less than 48 characters", {
+ status: 400,
+ });
+
+ // check if slug is valid
+ } else if (!validSlugRegex.test(slug)) {
+ return new Response("Invalid slug", { status: 400 });
+
+ // check if slug is reserved
+ } else if ((await isReservedKey(slug)) || DEFAULT_REDIRECTS[slug]) {
+ return new Response("Cannot use reserved slugs", { status: 422 });
+ }
+
try {
const response = await prisma.project.update({
where: {
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/page-client.tsx
index 685cd6c7c3..f7c47000ed 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/page-client.tsx
@@ -79,10 +79,9 @@ export default function ProjectSettingsClient() {
await mutate("/api/projects");
router.push(`/${newSlug}/settings`);
toast.success("Successfully updated project slug!");
- } else if (res.status === 422) {
- toast.error("Project slug already exists");
} else {
- toast.error("Something went wrong");
+ const error = await res.text();
+ toast.error(error || "Something went wrong");
}
})
}