Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: Sanitize and validate team names on update

Cursor Agente59bd4e67bd8
diff --git a/pages/api/teams/[teamId]/update-name.ts b/pages/api/teams/[teamId]/update-name.ts
index 966148f4..97ebf4ed 100644
--- a/pages/api/teams/[teamId]/update-name.ts
+++ b/pages/api/teams/[teamId]/update-name.ts
@@ -4,6 +4,7 @@ import { getServerSession } from "next-auth";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
+import { validateContent } from "@/lib/utils/sanitize-html";
import { authOptions } from "../../auth/[...nextauth]";
@@ -46,13 +47,34 @@ export default async function handle(
.json("You are not permitted to perform this action");
}
+ // Validate and sanitize the team name
+ let sanitizedName: string;
+ try {
+ sanitizedName = validateContent(req.body.name);
+ } catch (error) {
+ return res.status(400).json({
+ error: {
+ message: (error as Error).message || "Invalid team name",
+ },
+ });
+ }
+
+ // Check if name exceeds the maximum length (32 characters as per frontend)
+ if (sanitizedName.length > 32) {
+ return res.status(400).json({
+ error: {
+ message: "Team name cannot exceed 32 characters",
+ },
+ });
+ }
+
// update name
await prisma.team.update({
where: {
id: teamId,
},
data: {
- name: req.body.name,
+ name: sanitizedName,
},
});
diff --git a/pages/settings/general.tsx b/pages/settings/general.tsx
index f0aa1487..494dfcd0 100644
--- a/pages/settings/general.tsx
+++ b/pages/settings/general.tsx
@@ -7,6 +7,7 @@ import { mutate } from "swr";
import { useAnalytics } from "@/lib/analytics";
import { usePlan } from "@/lib/swr/use-billing";
+import { validateContent } from "@/lib/utils/sanitize-html";
import { UpgradePlanModal } from "@/components/billing/upgrade-plan-modal";
import AppLayout from "@/components/layouts/app";
@@ -76,33 +77,41 @@ export default function General() {
};
const handleTeamNameChange = async (updateData: any) => {
- analytics.capture("Update Team Name", {
- teamId,
- name: updateData.name,
- });
-
- const promise = fetch(`/api/teams/${teamId}/update-name`, {
- method: "PATCH",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(updateData),
- }).then(async (res) => {
- if (!res.ok) {
- const { error } = await res.json();
- throw new Error(error.message);
- }
- await Promise.all([mutate(`/api/teams/${teamId}`), mutate(`/api/teams`)]);
- return res.json();
- });
-
- toast.promise(promise, {
- loading: "Updating team name...",
- success: "Successfully updated team name!",
- error: (err) => err.message || "Failed to update team name",
- });
-
- return promise;
+ try {
+ // Sanitize and validate team name before sending
+ const sanitizedName = validateContent(updateData.name);
+
+ analytics.capture("Update Team Name", {
+ teamId,
+ name: sanitizedName,
+ });
+
+ const promise = fetch(`/api/teams/${teamId}/update-name`, {
+ method: "PATCH",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({ name: sanitizedName }),
+ }).then(async (res) => {
+ if (!res.ok) {
+ const { error } = await res.json();
+ throw new Error(error.message);
+ }
+ await Promise.all([mutate(`/api/teams/${teamId}`), mutate(`/api/teams`)]);
+ return res.json();
+ });
+
+ toast.promise(promise, {
+ loading: "Updating team name...",
+ success: "Successfully updated team name!",
+ error: (err) => err.message || "Failed to update team name",
+ });
+
+ return promise;
+ } catch (error) {
+ toast.error((error as Error).message || "Failed to validate team name");
+ throw error;
+ }
};
return (