Skip to content

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

fix: possible infinite loop

Marc Seitza8bfecf52ab6
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts
index 80358927..79d29e5b 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts
@@ -246,12 +246,13 @@ export default async function handle(
// Duplicate name handling
let folderName = name;
let counter = 1;
+ const MAX_RETRIES = 50;
let childFolderPath = path
? "/" + path + "/" + slugify(folderName)
: "/" + slugify(folderName);
- while (true) {
+ while (counter <= MAX_RETRIES) {
const existingFolder = await prisma.dataroomFolder.findUnique({
where: {
dataroomId_path: {
@@ -268,6 +269,13 @@ export default async function handle(
counter++;
}
+ if (counter > MAX_RETRIES) {
+ return res.status(400).json({
+ error: "Failed to create folder",
+ message: "Too many folders with similar names",
+ });
+ }
+
const folder = await prisma.dataroomFolder.create({
data: {
name: folderName,
diff --git a/pages/api/teams/[teamId]/folders/index.ts b/pages/api/teams/[teamId]/folders/index.ts
index 87bd91d1..1d820fa7 100644
--- a/pages/api/teams/[teamId]/folders/index.ts
+++ b/pages/api/teams/[teamId]/folders/index.ts
@@ -144,12 +144,13 @@ export default async function handle(
let folderName = name;
let counter = 1;
+ const MAX_RETRIES = 50;
let childFolderPath = path
? "/" + path + "/" + slugify(folderName)
: "/" + slugify(folderName);
- while (true) {
+ while (counter <= MAX_RETRIES) {
const existingFolder = await prisma.folder.findUnique({
where: {
teamId_path: {
@@ -168,6 +169,13 @@ export default async function handle(
counter++;
}
+ if (counter > MAX_RETRIES) {
+ return res.status(400).json({
+ error: "Failed to create folder",
+ message: "Too many folders with similar names",
+ });
+ }
+
const folder = await prisma.folder.create({
data: {
name: folderName,