Skip to content

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

fix: isEmailMatched

Marc Seitze2862a81b88b
diff --git a/app/api/views-dataroom/route.ts b/app/api/views-dataroom/route.ts
index 8564a715..633af43a 100644
--- a/app/api/views-dataroom/route.ts
+++ b/app/api/views-dataroom/route.ts
@@ -22,11 +22,11 @@ import { parseSheet } from "@/lib/sheet";
import { recordLinkView } from "@/lib/tracking/record-link-view";
import { CustomUser, WatermarkConfigSchema } from "@/lib/types";
import { checkPassword, decryptEncrpytedPassword, log } from "@/lib/utils";
+import { extractEmailDomain, isEmailMatched } from "@/lib/utils/email-domain";
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";
+import { validateEmail } from "@/lib/utils/validate-email";
export async function POST(request: NextRequest) {
try {
@@ -275,7 +275,10 @@ export async function POST(request: NextRequest) {
}
// Check global block list first - this overrides all other access controls
- const globalBlockCheck = checkGlobalBlockList(email, link.team?.globalBlockList);
+ const globalBlockCheck = checkGlobalBlockList(
+ email,
+ link.team?.globalBlockList,
+ );
if (globalBlockCheck.error) {
return NextResponse.json(
{ message: globalBlockCheck.error },
@@ -283,20 +286,14 @@ export async function POST(request: NextRequest) {
);
}
if (globalBlockCheck.isBlocked) {
- return NextResponse.json(
- { message: "Access denied" },
- { status: 403 },
- );
+ return NextResponse.json({ message: "Access denied" }, { status: 403 });
}
// Check if email is allowed to visit the link
if (link.allowList && link.allowList.length > 0) {
// Determine if the email or its domain is allowed
const isAllowed = link.allowList.some((allowed) =>
- isEmailBlocked(email, allowed) === false && (
- allowed === email ||
- (allowed.startsWith("@") && extractEmailDomain(email) === allowed)
- )
+ isEmailMatched(email, allowed),
);
// Deny access if the email is not allowed
@@ -312,7 +309,7 @@ export async function POST(request: NextRequest) {
if (link.denyList && link.denyList.length > 0) {
// Determine if the email or its domain is denied
const isDenied = link.denyList.some((denied) =>
- isEmailBlocked(email, denied)
+ isEmailMatched(email, denied),
);
// Deny access if the email is denied
@@ -354,9 +351,9 @@ export async function POST(request: NextRequest) {
// Extract domain from email
const emailDomain = extractEmailDomain(email);
// Check domain access
- const hasDomainAccess = emailDomain ? group.domains.some(
- (domain) => domain === emailDomain,
- ) : false;
+ const hasDomainAccess = emailDomain
+ ? group.domains.some((domain) => domain === emailDomain)
+ : false;
if (!isMember && !hasDomainAccess) {
return NextResponse.json(
@@ -596,28 +593,28 @@ export async function POST(request: NextRequest) {
...(link.enableAgreement &&
link.agreementId &&
hasConfirmedAgreement && {
- agreementResponse: {
- create: {
- agreementId: link.agreementId,
+ agreementResponse: {
+ create: {
+ agreementId: link.agreementId,
+ },
},
- },
- }),
+ }),
...(link.audienceType === LinkAudienceType.GROUP &&
link.groupId && {
- groupId: link.groupId,
- }),
+ groupId: link.groupId,
+ }),
...(customFields &&
link.customFields.length > 0 && {
- customFieldResponse: {
- create: {
- data: link.customFields.map((field) => ({
- identifier: field.identifier,
- label: field.label,
- response: customFields[field.identifier] || "",
- })),
+ customFieldResponse: {
+ create: {
+ data: link.customFields.map((field) => ({
+ identifier: field.identifier,
+ label: field.label,
+ response: customFields[field.identifier] || "",
+ })),
+ },
},
- },
- }),
+ }),
};
// ** DATAROOM_VIEW **
@@ -921,15 +918,15 @@ export async function POST(request: NextRequest) {
documentVersion.type === "image" ||
documentVersion.type === "zip" ||
documentVersion.type === "video")) ||
- (documentVersion && useAdvancedExcelViewer)
+ (documentVersion && useAdvancedExcelViewer)
? documentVersion.file
: undefined,
pages: documentPages ? documentPages : undefined,
notionData: undefined,
sheetData:
documentVersion &&
- documentVersion.type === "sheet" &&
- !useAdvancedExcelViewer
+ documentVersion.type === "sheet" &&
+ !useAdvancedExcelViewer
? sheetData
: undefined,
fileType: documentVersion
@@ -943,16 +940,16 @@ export async function POST(request: NextRequest) {
viewerEmail: viewer?.email ?? email ?? verifiedEmail ?? null,
ipAddress:
link.enableWatermark &&
- link.watermarkConfig &&
- WatermarkConfigSchema.parse(link.watermarkConfig).text.includes(
- "{{ipAddress}}",
- )
+ link.watermarkConfig &&
+ WatermarkConfigSchema.parse(link.watermarkConfig).text.includes(
+ "{{ipAddress}}",
+ )
? (ipAddress(request) ?? LOCALHOST_IP)
: undefined,
useAdvancedExcelViewer:
documentVersion &&
- documentVersion.type === "sheet" &&
- useAdvancedExcelViewer
+ documentVersion.type === "sheet" &&
+ useAdvancedExcelViewer
? useAdvancedExcelViewer
: undefined,
canDownload: canDownload,
diff --git a/app/api/views/route.ts b/app/api/views/route.ts
index 7cb60f8b..2ae77def 100644
--- a/app/api/views/route.ts
+++ b/app/api/views/route.ts
@@ -19,11 +19,11 @@ import { parseSheet } from "@/lib/sheet";
import { recordLinkView } from "@/lib/tracking/record-link-view";
import { CustomUser, WatermarkConfigSchema } from "@/lib/types";
import { checkPassword, decryptEncrpytedPassword, log } from "@/lib/utils";
+import { extractEmailDomain, isEmailMatched } from "@/lib/utils/email-domain";
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";
+import { validateEmail } from "@/lib/utils/validate-email";
export async function POST(request: NextRequest) {
try {
@@ -210,7 +210,10 @@ export async function POST(request: NextRequest) {
}
// Check global block list first - this overrides all other access controls
- const globalBlockCheck = checkGlobalBlockList(email, link.team?.globalBlockList);
+ const globalBlockCheck = checkGlobalBlockList(
+ email,
+ link.team?.globalBlockList,
+ );
if (globalBlockCheck.error) {
return NextResponse.json(
{ message: globalBlockCheck.error },
@@ -218,20 +221,14 @@ export async function POST(request: NextRequest) {
);
}
if (globalBlockCheck.isBlocked) {
- return NextResponse.json(
- { message: "Access denied" },
- { status: 403 },
- );
+ return NextResponse.json({ message: "Access denied" }, { status: 403 });
}
// Check if email is allowed to visit the link
if (link.allowList && link.allowList.length > 0) {
// Determine if the email or its domain is allowed
const isAllowed = link.allowList.some((allowed) =>
- isEmailBlocked(email, allowed) === false && (
- allowed === email ||
- (allowed.startsWith("@") && extractEmailDomain(email) === allowed)
- )
+ isEmailMatched(email, allowed),
);
// Deny access if the email is not allowed
@@ -247,7 +244,7 @@ export async function POST(request: NextRequest) {
if (link.denyList && link.denyList.length > 0) {
// Determine if the email or its domain is denied
const isDenied = link.denyList.some((denied) =>
- isEmailBlocked(email, denied)
+ isEmailMatched(email, denied),
);
// Deny access if the email is denied
diff --git a/lib/utils/email-domain.ts b/lib/utils/email-domain.ts
index 4f3b882a..01a39f7e 100644
--- a/lib/utils/email-domain.ts
+++ b/lib/utils/email-domain.ts
@@ -1,54 +1,53 @@
-
export function extractEmailDomain(email: string): string | null {
- if (!email || typeof email !== 'string') {
- return null;
- }
- const normalizedEmail = email.trim().toLowerCase();
+ if (!email || typeof email !== "string") {
+ return null;
+ }
+ const normalizedEmail = email.trim().toLowerCase();
- const atSymbolCount = (normalizedEmail.match(/@/g) || []).length;
- if (atSymbolCount !== 1) {
- return null;
- }
+ 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 atIndex = normalizedEmail.lastIndexOf("@");
+ if (atIndex === -1 || atIndex === normalizedEmail.length - 1) {
+ return null;
+ }
- const domain = normalizedEmail.substring(atIndex);
+ const domain = normalizedEmail.substring(atIndex);
- if (domain.length <= 1) {
- return null;
- }
+ if (domain.length <= 1) {
+ return null;
+ }
- return domain;
+ return domain;
}
-export function normalizeBlockListEntry(entry: string): string {
- if (!entry || typeof entry !== 'string') {
- return '';
- }
- return entry.trim().toLowerCase();
+export function normalizeListEntry(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;
- }
+export function isEmailMatched(email: string, entry: string): boolean {
+ if (!email || !entry) {
+ return false;
+ }
- const normalizedEmail = email.trim().toLowerCase();
- const normalizedEntry = normalizeBlockListEntry(blockListEntry);
+ const normalizedEmail = email.trim().toLowerCase();
+ const normalizedEntry = normalizeListEntry(entry);
- // Direct email match
- if (normalizedEmail === normalizedEntry) {
- return true;
- }
+ // Direct email match
+ if (normalizedEmail === normalizedEntry) {
+ return true;
+ }
- // Domain match (entry starts with @)
- if (normalizedEntry.startsWith('@')) {
- const emailDomain = extractEmailDomain(normalizedEmail);
- return emailDomain === normalizedEntry;
- }
+ // Domain match (entry starts with @)
+ if (normalizedEntry.startsWith("@")) {
+ const emailDomain = extractEmailDomain(normalizedEmail);
+ return emailDomain === normalizedEntry;
+ }
- return false;
-}
\ No newline at end of file
+ return false;
+}
diff --git a/lib/utils/global-block-list.ts b/lib/utils/global-block-list.ts
index 87dff662..0a948444 100644
--- a/lib/utils/global-block-list.ts
+++ b/lib/utils/global-block-list.ts
@@ -1,24 +1,24 @@
+import { isEmailMatched } from "@/lib/utils/email-domain";
import { validateEmail } from "@/lib/utils/validate-email";
-import { isEmailBlocked } from "@/lib/utils/email-domain";
export function checkGlobalBlockList(
- email: string | undefined,
- globalBlockList: string[] | undefined,
+ email: string | undefined,
+ globalBlockList: string[] | undefined,
): { isBlocked: boolean; error?: string } {
- if (!email || !globalBlockList || globalBlockList.length === 0) {
- return { isBlocked: false };
- }
+ if (!email || !globalBlockList || globalBlockList.length === 0) {
+ return { isBlocked: false };
+ }
- if (!validateEmail(email)) {
- return {
- isBlocked: false,
- error: "Invalid email address"
- };
- }
+ if (!validateEmail(email)) {
+ return {
+ isBlocked: false,
+ error: "Invalid email address",
+ };
+ }
- const isBlocked = globalBlockList.some((blockedEntry) =>
- isEmailBlocked(email, blockedEntry)
- );
+ const isBlocked = globalBlockList.some((blockedEntry) =>
+ isEmailMatched(email, blockedEntry),
+ );
- return { isBlocked };
-}
\ No newline at end of file
+ return { isBlocked };
+}