Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
fix: strip trailing spaces from link passcodes
Marc Seitza100d70e7a6c
diff --git a/app/api/views-dataroom/route.ts b/app/api/views-dataroom/route.tsindex dcfb6f04..f861d9e9 100644--- a/app/api/views-dataroom/route.ts+++ b/app/api/views-dataroom/route.ts@@ -40,7 +40,12 @@ import {} from "@/lib/signing/agreements";import { recordLinkView } from "@/lib/tracking/record-link-view";import { CustomUser, WatermarkConfigSchema } from "@/lib/types";-import { checkPassword, decryptEncrpytedPassword, log } from "@/lib/utils";+import {+ checkPassword,+ decryptEncrpytedPassword,+ log,+ stripTrailingPasswordWhitespace,+} from "@/lib/utils";import {extractEmailDomain,normalizeGroupDomain,@@ -383,12 +388,21 @@ export async function POST(request: NextRequest) {}let isPasswordValid: boolean = false;+ const submittedPassword = stripTrailingPasswordWhitespace(password);const textParts: string[] = link.password.split(":");if (!textParts || textParts.length !== 2) {- isPasswordValid = await checkPassword(password, link.password);+ isPasswordValid = await checkPassword(+ submittedPassword,+ link.password,+ );+ // A legacy bcrypt hash may cover trailing spaces, so the raw input+ // still has to match or those links lock their owners out.+ if (!isPasswordValid && password !== submittedPassword) {+ isPasswordValid = await checkPassword(password, link.password);+ }} else {const decryptedPassword = decryptEncrpytedPassword(link.password);- isPasswordValid = decryptedPassword === password;+ isPasswordValid = decryptedPassword === submittedPassword;}if (!isPasswordValid) {diff --git a/app/api/views/route.ts b/app/api/views/route.tsindex 9bef3363..0c0aa947 100644--- a/app/api/views/route.ts+++ b/app/api/views/route.ts@@ -38,7 +38,12 @@ import {} from "@/lib/signing/agreements";import { recordLinkView } from "@/lib/tracking/record-link-view";import { CustomUser, WatermarkConfigSchema } from "@/lib/types";-import { checkPassword, decryptEncrpytedPassword, log } from "@/lib/utils";+import {+ checkPassword,+ decryptEncrpytedPassword,+ log,+ stripTrailingPasswordWhitespace,+} from "@/lib/utils";import { generateOTP } from "@/lib/utils/generate-otp";import { LOCALHOST_IP } from "@/lib/utils/geo";import { checkViewerEmailAccess } from "@/lib/utils/global-block-list";@@ -333,12 +338,21 @@ export async function POST(request: NextRequest) {}let isPasswordValid: boolean = false;+ const submittedPassword = stripTrailingPasswordWhitespace(password);const textParts: string[] = link.password.split(":");if (!textParts || textParts.length !== 2) {- isPasswordValid = await checkPassword(password, link.password);+ isPasswordValid = await checkPassword(+ submittedPassword,+ link.password,+ );+ // A legacy bcrypt hash may cover trailing spaces, so the raw input+ // still has to match or those links lock their owners out.+ if (!isPasswordValid && password !== submittedPassword) {+ isPasswordValid = await checkPassword(password, link.password);+ }} else {const decryptedPassword = decryptEncrpytedPassword(link.password);- isPasswordValid = decryptedPassword === password;+ isPasswordValid = decryptedPassword === submittedPassword;}if (!isPasswordValid) {diff --git a/components/links/link-sheet/password-section.tsx b/components/links/link-sheet/password-section.tsxindex 3caba288..46904e54 100644--- a/components/links/link-sheet/password-section.tsx+++ b/components/links/link-sheet/password-section.tsx@@ -2,12 +2,13 @@ import { useEffect, useState } from "react";import { motion } from "motion/react";+import { FADE_IN_ANIMATION_SETTINGS } from "@/lib/constants";+import { stripTrailingPasswordWhitespace } from "@/lib/utils";+import Eye from "@/components/shared/icons/eye";import EyeOff from "@/components/shared/icons/eye-off";import { Input } from "@/components/ui/input";-import { FADE_IN_ANIMATION_SETTINGS } from "@/lib/constants";-import { DEFAULT_LINK_TYPE } from ".";import LinkItem from "./link-item";@@ -62,6 +63,14 @@ export default function PasswordSection({onChange={(e) => {setData({ ...data, password: e.target.value });}}+ onBlur={() => {+ setData((prev) => ({+ ...prev,+ password: prev.password+ ? stripTrailingPasswordWhitespace(prev.password)+ : prev.password,+ }));+ }}aria-invalid="true"/><buttondiff --git a/components/view/access-form/index.tsx b/components/view/access-form/index.tsxindex fd26878f..3e8c3541 100644--- a/components/view/access-form/index.tsx+++ b/components/view/access-form/index.tsx@@ -5,6 +5,8 @@ import { Brand, CustomField, DataroomBrand, LinkType } from "@prisma/client";import { ArrowUpRightIcon } from "lucide-react";import { useTranslation } from "react-i18next";+import { stripTrailingPasswordWhitespace } from "@/lib/utils";+import { Button } from "@/components/ui/button";import {@@ -135,7 +137,12 @@ export default function AccessForm({if (requireEmail) {if (!data.email || !isEmailValid) return false;}- if (requirePassword && !data.password) return false;+ if (+ requirePassword &&+ !stripTrailingPasswordWhitespace(data.password ?? "")+ ) {+ return false;+ }if (requireAgreement && !data.hasConfirmedAgreement) return false;if (requireAgreement && requireName && !data.name) return false;if (customFields?.length) {diff --git a/components/view/access-form/password-section.tsx b/components/view/access-form/password-section.tsxindex b96330d9..cd5eccc2 100644--- a/components/view/access-form/password-section.tsx+++ b/components/view/access-form/password-section.tsx@@ -44,13 +44,17 @@ export default function PasswordSection({autoComplete="off"translate="no"className="notranslate flex w-full cursor-text rounded-md border-0 bg-black py-1.5 text-white shadow-sm ring-1 ring-inset ring-gray-600 placeholder:text-[var(--access-placeholder)] focus:ring-2 focus:ring-inset focus:ring-[var(--access-input-focus)] sm:text-sm sm:leading-6"- style={{- backgroundColor: theme.controlBgColor,- borderColor: theme.controlBorderColor,- "--access-placeholder": theme.controlPlaceholderColor,- "--access-input-focus": theme.controlBorderStrongColor,- color: disableEditPassword ? theme.subtleTextColor : theme.textColor,- } as CSSProperties}+ style={+ {+ backgroundColor: theme.controlBgColor,+ borderColor: theme.controlBorderColor,+ "--access-placeholder": theme.controlPlaceholderColor,+ "--access-input-focus": theme.controlBorderStrongColor,+ color: disableEditPassword+ ? theme.subtleTextColor+ : theme.textColor,+ } as CSSProperties+ }value={password || ""}placeholder={t("fields.password.placeholder", "Enter passcode")}onChange={(e) => {diff --git a/lib/utils.ts b/lib/utils.tsindex 2f943b57..258c87b9 100644--- a/lib/utils.ts+++ b/lib/utils.ts@@ -592,21 +592,27 @@ export const generateGravatarHash = (email: string | null): string => {return hash;};+export function stripTrailingPasswordWhitespace(password: string): string {+ return password.trimEnd();+}+export async function generateEncrpytedPassword(password: string,): Promise<string> {// If the password is empty, return an empty stringif (!password) return "";+ const normalized = stripTrailingPasswordWhitespace(password);+ if (!normalized) return "";// If the password is already encrypted, return it// Check if it's encrypted by validating the format: 32-char hex IV + ":" + hex encrypted text- const textParts: string[] = password.split(":");+ const textParts: string[] = normalized.split(":");if (textParts.length === 2 &&textParts[0].length === 32 &&/^[a-fA-F0-9]+$/.test(textParts[0]) &&/^[a-fA-F0-9]+$/.test(textParts[1])) {- return password;+ return normalized;}// Otherwise, encrypt the passwordconst encryptedKey: string = crypto@@ -616,19 +622,20 @@ export async function generateEncrpytedPassword(.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");+ let encryptedText: string = cipher.update(normalized, "utf8", "hex");encryptedText += cipher.final("hex");return IV.toString("hex") + ":" + encryptedText;}export function decryptEncrpytedPassword(password: string): string {if (!password) return "";+ const normalized = stripTrailingPasswordWhitespace(password);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(":");+ const textParts: string[] = normalized.split(":");// Check if it's in the expected encrypted format: 32-char hex IV + ":" + hex encrypted textif (!textParts ||@@ -637,7 +644,7 @@ export function decryptEncrpytedPassword(password: string): string {!/^[a-fA-F0-9]+$/.test(textParts[0]) ||!/^[a-fA-F0-9]+$/.test(textParts[1])) {- return password; // Return as-is if not in encrypted format+ return normalized; // Return as-is if not in encrypted format}try {const IV: Buffer = Buffer.from(textParts[0], "hex");@@ -645,9 +652,9 @@ export function decryptEncrpytedPassword(password: string): string {const decipher = crypto.createDecipheriv("aes-256-ctr", encryptedKey, IV);let decrypted: string = decipher.update(encryptedText, "hex", "utf8");decrypted += decipher.final("utf8");- return decrypted;+ return stripTrailingPasswordWhitespace(decrypted);} catch (error) {- return password;+ return normalized;}}