Skip to content

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

fix: return link password unencrypted when editing

Marc Seitz16530c40a4ff
diff --git a/lib/utils.ts b/lib/utils.ts
index 3cf5447b..60ea428b 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -477,7 +477,15 @@ export const generateGravatarHash = (email: string | null): string => {
export async function generateEncrpytedPassword(
password: string,
): Promise<string> {
+ // If the password is empty, return an empty string
if (!password) return "";
+ // If the password is already encrypted, return it
+ const textParts: string[] = password.split(":");
+ console.log("textparts in encryption", textParts);
+ if (textParts.length === 2) {
+ return password;
+ }
+ // Otherwise, encrypt the password
const encryptedKey: string = crypto
.createHash("sha256")
.update(String(process.env.NEXT_PRIVATE_DOCUMENT_PASSWORD_KEY))
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
index 5c28d41b..b5ced41d 100644
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@ -9,7 +9,10 @@ import {
} from "@/lib/api/links/link-data";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
-import { generateEncrpytedPassword } from "@/lib/utils";
+import {
+ decryptEncrpytedPassword,
+ generateEncrpytedPassword,
+} from "@/lib/utils";
import { authOptions } from "../../auth/[...nextauth]";
@@ -290,6 +293,11 @@ export default async function handle(
`${process.env.NEXTAUTH_URL}/api/revalidate?secret=${process.env.REVALIDATE_TOKEN}&linkId=${id}&hasDomain=${updatedLink.domainId ? "true" : "false"}`,
);
+ // Decrypt the password for the updated link
+ if (updatedLink.password !== null) {
+ updatedLink.password = decryptEncrpytedPassword(updatedLink.password);
+ }
+
return res.status(200).json(updatedLink);
} else if (req.method == "DELETE") {
// DELETE /api/links/:id
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index 0bf76ab0..12c6f4bd 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -5,12 +5,11 @@ import { getServerSession } from "next-auth/next";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import {
- getDocumentWithTeamAndUser,
- getTeamWithUsersAndDocument,
-} from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
-import { generateEncrpytedPassword } from "@/lib/utils";
+import {
+ decryptEncrpytedPassword,
+ generateEncrpytedPassword,
+} from "@/lib/utils";
import { authOptions } from "../auth/[...nextauth]";
@@ -178,6 +177,11 @@ export default async function handler(
return res.status(404).json({ error: "Link not found" });
}
+ // Decrypt the password for the new link
+ if (linkWithView.password !== null) {
+ linkWithView.password = decryptEncrpytedPassword(linkWithView.password);
+ }
+
return res.status(200).json(linkWithView);
} catch (error) {
errorhandler(error, res);
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/links.ts b/pages/api/teams/[teamId]/datarooms/[id]/links.ts
index 8c5e9065..23a0f5bd 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/links.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/links.ts
@@ -6,7 +6,7 @@ import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
-import { log } from "@/lib/utils";
+import { decryptEncrpytedPassword, log } from "@/lib/utils";
import { authOptions } from "../../../../auth/[...nextauth]";
@@ -68,6 +68,13 @@ export default async function handle(
},
});
+ // Decrypt the password for each link
+ links.forEach((link) => {
+ if (link.password !== null) {
+ link.password = decryptEncrpytedPassword(link.password);
+ }
+ });
+
return res.status(200).json(links);
} catch (error) {
log({
diff --git a/pages/api/teams/[teamId]/documents/[id]/links.ts b/pages/api/teams/[teamId]/documents/[id]/links.ts
index a8593a26..65077b05 100644
--- a/pages/api/teams/[teamId]/documents/[id]/links.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/links.ts
@@ -5,7 +5,7 @@ import { getServerSession } from "next-auth/next";
import { errorhandler } from "@/lib/errorHandler";
import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
-import { log } from "@/lib/utils";
+import { decryptEncrpytedPassword, log } from "@/lib/utils";
import { authOptions } from "../../../../auth/[...nextauth]";
@@ -60,6 +60,14 @@ export default async function handle(
});
const links = document!.links;
+
+ // Decrypt the password for each link
+ links?.forEach((link) => {
+ if (link.password !== null) {
+ link.password = decryptEncrpytedPassword(link.password);
+ }
+ });
+
return res.status(200).json(links);
} catch (error) {
log({