Skip to content

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

feat: update password validation on links

Marc Seitza2befb169925
diff --git a/.env.example b/.env.example
index f92f667c..81df80cb 100644
--- a/.env.example
+++ b/.env.example
@@ -60,4 +60,7 @@ NEXT_PRIVATE_UPLOAD_DISTRIBUTION_DOMAIN=""
# Defines the public key ID to sign Cloudfront URLs. Relevant when using Cloudfront.
NEXT_PRIVATE_UPLOAD_DISTRIBUTION_KEY_ID=""
# Defines the private key contents to sign Cloudfront URLs. Relevant when using Cloudfront. Should be a PEM-encoded RSA 2048 private key.
-NEXT_PRIVATE_UPLOAD_DISTRIBUTION_KEY_CONTENTS=
\ No newline at end of file
+NEXT_PRIVATE_UPLOAD_DISTRIBUTION_KEY_CONTENTS=
+
+# Encryption key for document passwords.
+NEXT_PRIVATE_DOCUMENT_PASSWORD_KEY=my-superstrong-document-secret
\ No newline at end of file
diff --git a/lib/team/helper.ts b/lib/team/helper.ts
index bdc8fad9..5a011275 100644
--- a/lib/team/helper.ts
+++ b/lib/team/helper.ts
@@ -1,7 +1,7 @@
import prisma from "@/lib/prisma";
import { DocumentError, TeamError } from "../errorHandler";
import { Document, DocumentVersion, Domain, Link, View } from "@prisma/client";
-import { decryptEncrpytedPassword} from "@/lib/utils";
+import { decryptEncrpytedPassword } from "@/lib/utils";
interface ITeamUserAndDocument {
teamId: string;
userId: string;
@@ -71,15 +71,14 @@ export async function getTeamWithUsersAndDocument({
throw new TeamError("Document doesn't exists in the team");
}
}
- if(document && document?.links){
- document?.links?.forEach((res:Link)=>{
- if(res?.password!=null)
- {
- let decryptedPassword:string=decryptEncrpytedPassword(res?.password)
- res["password"]=decryptedPassword
+ if (document && document?.links) {
+ document?.links?.forEach((res: Link) => {
+ if (res?.password != null) {
+ let decryptedPassword: string = decryptEncrpytedPassword(res?.password);
+ res["password"] = decryptedPassword;
}
- })
-}
+ });
+ }
// Check that the user is owner of the document, otherwise return 401
// if (checkOwner) {
// const isUserOwnerOfDocument = document?.ownerId === userId;
diff --git a/lib/utils.ts b/lib/utils.ts
index 0aee1ffb..1f1d5dbb 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -424,30 +424,42 @@ export const generateGravatarHash = (email: string | null): string => {
return hash;
};
-export async function generateEncrpytedPassword(password: string): Promise<string> {
- if(!password) return "";
- const encryptedKey:string = crypto.createHash('sha256').update(String(process.env.Docs_Password_Key)).digest('base64').substring(0,32);
- const IV:Buffer = crypto.randomBytes(16);
- const cipher = crypto.createCipheriv("aes-256-ctr",encryptedKey, IV);
- let encryptedText:string = cipher.update(password, "utf8", "hex");
+
+export async function generateEncrpytedPassword(
+ password: string,
+): Promise<string> {
+ if (!password) return "";
+ const encryptedKey: string = crypto
+ .createHash("sha256")
+ .update(String(process.env.NEXT_PRIVATE_DOCUMENT_PASSWORD_KEY))
+ .digest("base64")
+ .substring(0, 32);
+ const IV: Buffer = crypto.randomBytes(16);
+ const cipher = crypto.createCipheriv("aes-256-ctr", encryptedKey, IV);
+ let encryptedText: string = cipher.update(password, "utf8", "hex");
encryptedText += cipher.final("hex");
- return IV.toString('hex')+":"+ encryptedText;
+ return IV.toString("hex") + ":" + encryptedText;
}
-export function decryptEncrpytedPassword(password:string): string{
- if(!password) return "";
- const encryptedKey:string = crypto.createHash('sha256').update(String(process.env.Docs_Password_Key)).digest('base64').substring(0,32);
- const textParts:string[] = password.split(":");
- if (!textParts || textParts.length !== 2) {
- return "Please Update Your Password"
- }
- const IV:Buffer = Buffer.from(textParts[0], 'hex');
- const encryptedText:string = textParts[1];
- const decipher = crypto.createDecipheriv("aes-256-ctr",encryptedKey, IV);
- let decrypted:string = decipher.update(encryptedText, "hex", "utf8");
- decrypted += decipher.final("utf8");
- return decrypted;
+export function decryptEncrpytedPassword(password: string): string {
+ if (!password) return "";
+ const encryptedKey: string = crypto
+ .createHash("sha256")
+ .update(String(process.env.NEXT_PRIVATE_DOCUMENT_PASSWORD_KEY))
+ .digest("base64")
+ .substring(0, 32);
+ const textParts: string[] = password.split(":");
+ if (!textParts || textParts.length !== 2) {
+ return password;
+ }
+ const IV: Buffer = Buffer.from(textParts[0], "hex");
+ const encryptedText: string = textParts[1];
+ const decipher = crypto.createDecipheriv("aes-256-ctr", encryptedKey, IV);
+ let decrypted: string = decipher.update(encryptedText, "hex", "utf8");
+ decrypted += decipher.final("utf8");
+ return decrypted;
}
+
export const sanitizeAllowDenyList = (list: string): string[] => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const domainRegex = /^@[^\s@]+\.[^\s@]+$/;
diff --git a/pages/api/views-dataroom.ts b/pages/api/views-dataroom.ts
index 1d6ccf81..81dfe01b 100644
--- a/pages/api/views-dataroom.ts
+++ b/pages/api/views-dataroom.ts
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next";
import prisma from "@/lib/prisma";
-import { checkPassword, log } from "@/lib/utils";
+import { checkPassword, decryptEncrpytedPassword, log } from "@/lib/utils";
import { newId } from "@/lib/id-helper";
import { sendVerificationEmail } from "@/lib/emails/send-email-verification";
import { getFile } from "@/lib/files/get-file";
@@ -95,7 +95,15 @@ export default async function handle(
return;
}
- const isPasswordValid = await checkPassword(password, link.password);
+ let isPasswordValid: boolean = false;
+ const textParts: string[] = link.password.split(":");
+ if (!textParts || textParts.length !== 2) {
+ isPasswordValid = await checkPassword(password, link.password);
+ } else {
+ const decryptedPassword = decryptEncrpytedPassword(link.password);
+ isPasswordValid = decryptedPassword === password;
+ }
+
if (!isPasswordValid) {
res.status(403).json({ message: "Invalid password." });
return;
diff --git a/pages/api/views.ts b/pages/api/views.ts
index 117f37e3..fbfd17e9 100644
--- a/pages/api/views.ts
+++ b/pages/api/views.ts
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next";
import prisma from "@/lib/prisma";
-import { checkPassword, log } from "@/lib/utils";
+import { checkPassword, decryptEncrpytedPassword, log } from "@/lib/utils";
import { newId } from "@/lib/id-helper";
import { sendVerificationEmail } from "@/lib/emails/send-email-verification";
import { getFile } from "@/lib/files/get-file";
@@ -84,7 +84,15 @@ export default async function handle(
return;
}
- const isPasswordValid = await checkPassword(password, link.password);
+ let isPasswordValid: boolean = false;
+ const textParts: string[] = link.password.split(":");
+ if (!textParts || textParts.length !== 2) {
+ isPasswordValid = await checkPassword(password, link.password);
+ } else {
+ const decryptedPassword = decryptEncrpytedPassword(link.password);
+ isPasswordValid = decryptedPassword === password;
+ }
+
if (!isPasswordValid) {
res.status(403).json({ message: "Invalid password." });
return;