Skip to content

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

improvement

Ashish Viradiya612a67440f61
diff --git a/app/api/views-dataroom/route.ts b/app/api/views-dataroom/route.ts
index 9304be48..8564a715 100644
--- a/app/api/views-dataroom/route.ts
+++ b/app/api/views-dataroom/route.ts
@@ -25,6 +25,8 @@ import { checkPassword, decryptEncrpytedPassword, log } from "@/lib/utils";
import { generateOTP } from "@/lib/utils/generate-otp";
import { LOCALHOST_IP } from "@/lib/utils/geo";
import { validateEmail } from "@/lib/utils/validate-email";
+import { checkGlobalBlockList } from "@/lib/utils/global-block-list";
+import { extractEmailDomain, isEmailBlocked } from "@/lib/utils/email-domain";
export async function POST(request: NextRequest) {
try {
@@ -272,18 +274,30 @@ export async function POST(request: NextRequest) {
);
}
+ // Check global block list first - this overrides all other access controls
+ const globalBlockCheck = checkGlobalBlockList(email, link.team?.globalBlockList);
+ if (globalBlockCheck.error) {
+ return NextResponse.json(
+ { message: globalBlockCheck.error },
+ { status: 400 },
+ );
+ }
+ if (globalBlockCheck.isBlocked) {
+ return NextResponse.json(
+ { message: "Access denied" },
+ { status: 403 },
+ );
+ }
+
// Check if email is allowed to visit the link
if (link.allowList && link.allowList.length > 0) {
- // Extract the domain from the email address
- const emailDomain = email.substring(email.lastIndexOf("@"));
-
// Determine if the email or its domain is allowed
- const isAllowed = link.allowList.some((allowed) => {
- return (
+ const isAllowed = link.allowList.some((allowed) =>
+ isEmailBlocked(email, allowed) === false && (
allowed === email ||
- (allowed.startsWith("@") && emailDomain === allowed)
- );
- });
+ (allowed.startsWith("@") && extractEmailDomain(email) === allowed)
+ )
+ );
// Deny access if the email is not allowed
if (!isAllowed) {
@@ -296,16 +310,10 @@ export async function POST(request: NextRequest) {
// Check if email is denied to visit the link
if (link.denyList && link.denyList.length > 0) {
- // Extract the domain from the email address
- const emailDomain = email.substring(email.lastIndexOf("@"));
-
// Determine if the email or its domain is denied
- const isDenied = link.denyList.some((denied) => {
- return (
- denied === email ||
- (denied.startsWith("@") && emailDomain === denied)
- );
- });
+ const isDenied = link.denyList.some((denied) =>
+ isEmailBlocked(email, denied)
+ );
// Deny access if the email is denied
if (isDenied) {
@@ -316,22 +324,6 @@ export async function POST(request: NextRequest) {
}
}
- if (link.team?.globalBlockList && link.team.globalBlockList.length > 0) {
- const emailDomain = email.substring(email.lastIndexOf("@"));
- const isGloballyBlocked = link.team.globalBlockList.some((blocked) => {
- return (
- blocked === email ||
- (blocked.startsWith("@") && emailDomain === blocked)
- );
- });
- if (isGloballyBlocked) {
- return NextResponse.json(
- { message: "Access denied" },
- { status: 403 },
- );
- }
- }
-
// Check if group is allowed to visit the link
if (link.audienceType === LinkAudienceType.GROUP && link.groupId) {
const group = await prisma.viewerGroup.findUnique({
@@ -360,11 +352,11 @@ export async function POST(request: NextRequest) {
);
// Extract domain from email
- const emailDomain = email.substring(email.lastIndexOf("@"));
+ const emailDomain = extractEmailDomain(email);
// Check domain access
- const hasDomainAccess = group.domains.some(
+ const hasDomainAccess = emailDomain ? group.domains.some(
(domain) => domain === emailDomain,
- );
+ ) : false;
if (!isMember && !hasDomainAccess) {
return NextResponse.json(
diff --git a/app/api/views/route.ts b/app/api/views/route.ts
index 40d326d0..7cb60f8b 100644
--- a/app/api/views/route.ts
+++ b/app/api/views/route.ts
@@ -21,8 +21,9 @@ import { CustomUser, WatermarkConfigSchema } from "@/lib/types";
import { checkPassword, decryptEncrpytedPassword, log } from "@/lib/utils";
import { generateOTP } from "@/lib/utils/generate-otp";
import { LOCALHOST_IP } from "@/lib/utils/geo";
-import { getIpAddress } from "@/lib/utils/ip";
import { validateEmail } from "@/lib/utils/validate-email";
+import { checkGlobalBlockList } from "@/lib/utils/global-block-list";
+import { extractEmailDomain, isEmailBlocked } from "@/lib/utils/email-domain";
export async function POST(request: NextRequest) {
try {
@@ -208,18 +209,30 @@ export async function POST(request: NextRequest) {
);
}
+ // Check global block list first - this overrides all other access controls
+ const globalBlockCheck = checkGlobalBlockList(email, link.team?.globalBlockList);
+ if (globalBlockCheck.error) {
+ return NextResponse.json(
+ { message: globalBlockCheck.error },
+ { status: 400 },
+ );
+ }
+ if (globalBlockCheck.isBlocked) {
+ return NextResponse.json(
+ { message: "Access denied" },
+ { status: 403 },
+ );
+ }
+
// Check if email is allowed to visit the link
if (link.allowList && link.allowList.length > 0) {
- // Extract the domain from the email address
- const emailDomain = email.substring(email.lastIndexOf("@"));
-
// Determine if the email or its domain is allowed
- const isAllowed = link.allowList.some((allowed) => {
- return (
+ const isAllowed = link.allowList.some((allowed) =>
+ isEmailBlocked(email, allowed) === false && (
allowed === email ||
- (allowed.startsWith("@") && emailDomain === allowed)
- );
- });
+ (allowed.startsWith("@") && extractEmailDomain(email) === allowed)
+ )
+ );
// Deny access if the email is not allowed
if (!isAllowed) {
@@ -232,16 +245,10 @@ export async function POST(request: NextRequest) {
// Check if email is denied to visit the link
if (link.denyList && link.denyList.length > 0) {
- // Extract the domain from the email address
- const emailDomain = email.substring(email.lastIndexOf("@"));
-
// Determine if the email or its domain is denied
- const isDenied = link.denyList.some((denied) => {
- return (
- denied === email ||
- (denied.startsWith("@") && emailDomain === denied)
- );
- });
+ const isDenied = link.denyList.some((denied) =>
+ isEmailBlocked(email, denied)
+ );
// Deny access if the email is denied
if (isDenied) {
@@ -252,22 +259,6 @@ export async function POST(request: NextRequest) {
}
}
- if (link.team?.globalBlockList && link.team.globalBlockList.length > 0) {
- const emailDomain = email.substring(email.lastIndexOf("@"));
- const isGloballyBlocked = link.team.globalBlockList.some((blocked) => {
- return (
- blocked === email ||
- (blocked.startsWith("@") && emailDomain === blocked)
- );
- });
- if (isGloballyBlocked) {
- return NextResponse.json(
- { message: "Access denied" },
- { status: 403 },
- );
- }
- }
-
// Request OTP Code for email verification if
// 1) email verification is required and
// 2) code is not provided or token not provided
diff --git a/lib/utils/email-domain.ts b/lib/utils/email-domain.ts
new file mode 100644
index 00000000..4f3b882a
--- /dev/null
+++ b/lib/utils/email-domain.ts
@@ -0,0 +1,54 @@
+
+export function extractEmailDomain(email: string): string | null {
+ if (!email || typeof email !== 'string') {
+ return null;
+ }
+ const normalizedEmail = email.trim().toLowerCase();
+
+ const atSymbolCount = (normalizedEmail.match(/@/g) || []).length;
+ if (atSymbolCount !== 1) {
+ return null;
+ }
+
+ const atIndex = normalizedEmail.lastIndexOf('@');
+ if (atIndex === -1 || atIndex === normalizedEmail.length - 1) {
+ return null;
+ }
+
+ const domain = normalizedEmail.substring(atIndex);
+
+ if (domain.length <= 1) {
+ return null;
+ }
+
+ return domain;
+}
+
+export function normalizeBlockListEntry(entry: string): string {
+ if (!entry || typeof entry !== 'string') {
+ return '';
+ }
+ return entry.trim().toLowerCase();
+}
+
+export function isEmailBlocked(email: string, blockListEntry: string): boolean {
+ if (!email || !blockListEntry) {
+ return false;
+ }
+
+ const normalizedEmail = email.trim().toLowerCase();
+ const normalizedEntry = normalizeBlockListEntry(blockListEntry);
+
+ // Direct email match
+ if (normalizedEmail === normalizedEntry) {
+ return true;
+ }
+
+ // Domain match (entry starts with @)
+ if (normalizedEntry.startsWith('@')) {
+ const emailDomain = extractEmailDomain(normalizedEmail);
+ return emailDomain === normalizedEntry;
+ }
+
+ return false;
+}
\ No newline at end of file
diff --git a/lib/utils/global-block-list.ts b/lib/utils/global-block-list.ts
new file mode 100644
index 00000000..87dff662
--- /dev/null
+++ b/lib/utils/global-block-list.ts
@@ -0,0 +1,24 @@
+import { validateEmail } from "@/lib/utils/validate-email";
+import { isEmailBlocked } from "@/lib/utils/email-domain";
+
+export function checkGlobalBlockList(
+ email: string | undefined,
+ globalBlockList: string[] | undefined,
+): { isBlocked: boolean; error?: string } {
+ if (!email || !globalBlockList || globalBlockList.length === 0) {
+ return { isBlocked: false };
+ }
+
+ if (!validateEmail(email)) {
+ return {
+ isBlocked: false,
+ error: "Invalid email address"
+ };
+ }
+
+ const isBlocked = globalBlockList.some((blockedEntry) =>
+ isEmailBlocked(email, blockedEntry)
+ );
+
+ return { isBlocked };
+}
\ No newline at end of file
diff --git a/pages/api/links/[id]/documents/[documentId].ts b/pages/api/links/[id]/documents/[documentId].ts
index be5e145a..3ce3daa9 100644
--- a/pages/api/links/[id]/documents/[documentId].ts
+++ b/pages/api/links/[id]/documents/[documentId].ts
@@ -5,6 +5,8 @@ import { DataroomBrand, LinkAudienceType } from "@prisma/client";
import { fetchDataroomDocumentLinkData } from "@/lib/api/links/link-data";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
+import { validateEmail } from "@/lib/utils/validate-email";
+import { checkGlobalBlockList } from "@/lib/utils/global-block-list";
export default async function handle(
req: NextApiRequest,
@@ -90,21 +92,16 @@ export default async function handle(
}
const { email } = req.query as { email?: string };
- if (email && link.team?.globalBlockList && link.team.globalBlockList.length > 0) {
- const emailDomain = email.substring(email.lastIndexOf("@"));
-
- const isBlocked = link.team.globalBlockList.some((blocked) => {
- return (
- blocked === email ||
- (blocked.startsWith("@") && emailDomain === blocked)
- );
- });
-
- if (isBlocked) {
- return res.status(403).json(
- { message: "Access denied" },
- );
- }
+ const globalBlockCheck = checkGlobalBlockList(email, link.team?.globalBlockList);
+ if (globalBlockCheck.error) {
+ return res.status(400).json(
+ { message: globalBlockCheck.error },
+ );
+ }
+ if (globalBlockCheck.isBlocked) {
+ return res.status(403).json(
+ { message: "Access denied" },
+ );
}
let brand: Partial<DataroomBrand> | null = null;
diff --git a/pages/api/links/domains/[...domainSlug].ts b/pages/api/links/domains/[...domainSlug].ts
index 2dbec274..8fd86760 100644
--- a/pages/api/links/domains/[...domainSlug].ts
+++ b/pages/api/links/domains/[...domainSlug].ts
@@ -9,6 +9,7 @@ import {
} from "@/lib/api/links/link-data";
import prisma from "@/lib/prisma";
import { log } from "@/lib/utils";
+import { checkGlobalBlockList } from "@/lib/utils/global-block-list";
export default async function handle(
req: NextApiRequest,
@@ -120,19 +121,16 @@ export default async function handle(
}
const { email } = req.query as { email?: string };
- if (email && link.team?.globalBlockList && link.team.globalBlockList.length > 0) {
- const emailDomain = email.substring(email.lastIndexOf("@"));
- const isBlocked = link.team.globalBlockList.some((blocked) => {
- return (
- blocked === email ||
- (blocked.startsWith("@") && emailDomain === blocked)
- );
- });
- if (isBlocked) {
- return res.status(403).json(
- { message: "Access denied" },
- );
- }
+ const globalBlockCheck = checkGlobalBlockList(email, link.team?.globalBlockList);
+ if (globalBlockCheck.error) {
+ return res.status(400).json(
+ { message: globalBlockCheck.error },
+ );
+ }
+ if (globalBlockCheck.isBlocked) {
+ return res.status(403).json(
+ { message: "Access denied" },
+ );
}
const teamPlan = link.team?.plan || "free";