Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

obfuscateCustomerEmail

Steven Tey6ebba7954fb8
diff --git a/apps/web/app/(ee)/api/cron/export/events/partner/route.ts b/apps/web/app/(ee)/api/cron/export/events/partner/route.ts
index f4adf745ac..6f38dcdbb4 100644
--- a/apps/web/app/(ee)/api/cron/export/events/partner/route.ts
+++ b/apps/web/app/(ee)/api/cron/export/events/partner/route.ts
@@ -6,6 +6,7 @@ import { getFirstFilterValue } from "@/lib/analytics/filter-helpers";
import { convertToCSV } from "@/lib/analytics/utils/convert-to-csv";
import { createDownloadableExport } from "@/lib/api/create-downloadable-export";
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { obfuscateCustomerEmail } from "@/lib/api/partner-profile/obfuscate-customer-email";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { generateExportFilename } from "@/lib/api/utils/generate-export-filename";
import { generateRandomString } from "@/lib/api/utils/generate-random-string";
@@ -144,7 +145,7 @@ export async function POST(req: Request) {
email: customer.email
? customerDataSharingEnabledAt
? customer.email
- : customer.email.replace(/(?<=^.).+(?=.@)/, "****")
+ : obfuscateCustomerEmail(customer.email)
: customer.name || generateRandomName(),
...(customerDataSharingEnabledAt && {
name: customer.name || generateRandomName(),
diff --git a/apps/web/app/(ee)/api/embed/referrals/earnings/route.ts b/apps/web/app/(ee)/api/embed/referrals/earnings/route.ts
index 123d3496a6..60de3b3797 100644
--- a/apps/web/app/(ee)/api/embed/referrals/earnings/route.ts
+++ b/apps/web/app/(ee)/api/embed/referrals/earnings/route.ts
@@ -1,5 +1,6 @@
import { REFERRALS_EMBED_EARNINGS_LIMIT } from "@/lib/constants/misc";
import { withReferralsEmbedToken } from "@/lib/embed/referrals/auth";
+import { obfuscateCustomerEmail } from "@/lib/api/partner-profile/obfuscate-customer-email";
import { generateRandomName } from "@/lib/names";
import { PartnerEarningsSchema } from "@/lib/zod/schemas/partner-profile";
import { prisma } from "@dub/prisma";
@@ -54,7 +55,7 @@ export const GET = withReferralsEmbedToken(
email: e.customer.email
? programEnrollment.customerDataSharingEnabledAt
? e.customer.email
- : e.customer.email.replace(/(?<=^.).+(?=.@)/, "****")
+ : obfuscateCustomerEmail(e.customer.email)
: e.customer.name || generateRandomName(),
}
: null,
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
index b1ef667b65..0cf36d81cf 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
@@ -1,6 +1,7 @@
import { getCustomerEvents } from "@/lib/analytics/get-customer-events";
import { transformCustomer } from "@/lib/api/customers/transform-customer";
import { DubApiError } from "@/lib/api/errors";
+import { obfuscateCustomerEmail } from "@/lib/api/partner-profile/obfuscate-customer-email";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { withPartnerProfile } from "@/lib/auth/partner";
import {
@@ -92,7 +93,7 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {
email: customer.email
? customerDataSharingEnabledAt
? customer.email
- : customer.email.replace(/(?<=^.).+(?=.@)/, "****")
+ : obfuscateCustomerEmail(customer.email)
: customer.name || generateRandomName(),
}),
activity: {
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/route.ts
index 6b73219f82..b5971b8e02 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/route.ts
@@ -1,5 +1,6 @@
import { transformCustomer } from "@/lib/api/customers/transform-customer";
import { DubApiError } from "@/lib/api/errors";
+import { obfuscateCustomerEmail } from "@/lib/api/partner-profile/obfuscate-customer-email";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { withPartnerProfile } from "@/lib/auth/partner";
import {
@@ -94,7 +95,7 @@ export const GET = withPartnerProfile(
email: customer.email
? customerDataSharingEnabledAt
? customer.email
- : customer.email.replace(/(?<=^.).+(?=.@)/, "****")
+ : obfuscateCustomerEmail(customer.email)
: customer.name || generateRandomName(),
}),
activity: {
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/count/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/count/route.ts
index 15289464ce..05b0a467ce 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/count/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/count/route.ts
@@ -1,4 +1,5 @@
import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
+import { obfuscateCustomerEmail } from "@/lib/api/partner-profile/obfuscate-customer-email";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { withPartnerProfile } from "@/lib/auth/partner";
import { generateRandomName } from "@/lib/names";
@@ -105,7 +106,7 @@ export const GET = withPartnerProfile(
email: customer?.email
? customerDataSharingEnabledAt
? customer.email
- : customer.email.replace(/(?<=^.).+(?=.@)/, "****")
+ : obfuscateCustomerEmail(customer.email)
: customer?.name || generateRandomName(),
_count,
};
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/route.ts
index 0d243d3cbd..be08c0dae2 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/earnings/route.ts
@@ -1,4 +1,5 @@
import { getStartEndDates } from "@/lib/analytics/utils/get-start-end-dates";
+import { obfuscateCustomerEmail } from "@/lib/api/partner-profile/obfuscate-customer-email";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { withPartnerProfile } from "@/lib/auth/partner";
import { generateRandomName } from "@/lib/names";
@@ -89,7 +90,7 @@ export const GET = withPartnerProfile(
...e.customer,
email: customerDataSharingEnabledAt
? customerEmail
- : customerEmail.replace(/(?<=^.).+(?=.@)/, "****"),
+ : obfuscateCustomerEmail(customerEmail),
country: e.customer?.country,
}
: null,
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/events/export/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/events/export/route.ts
index 33e1a26bb6..7a6950d6f1 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/events/export/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/events/export/route.ts
@@ -7,6 +7,7 @@ import { getAnalytics } from "@/lib/analytics/get-analytics";
import { getEvents } from "@/lib/analytics/get-events";
import { convertToCSV } from "@/lib/analytics/utils";
import { DubApiError } from "@/lib/api/errors";
+import { obfuscateCustomerEmail } from "@/lib/api/partner-profile/obfuscate-customer-email";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { withPartnerProfile } from "@/lib/auth/partner";
import {
@@ -220,7 +221,7 @@ export const GET = withPartnerProfile(
email: customer.email
? customerDataSharingEnabledAt
? customer.email
- : customer.email.replace(/(?<=^.).+(?=.@)/, "****")
+ : obfuscateCustomerEmail(customer.email)
: customer.name || generateRandomName(),
...(customerDataSharingEnabledAt && {
name: customer.name || generateRandomName(),
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/events/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/events/route.ts
index d3d2e7ec07..4c567285de 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/events/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/events/route.ts
@@ -1,6 +1,7 @@
import { getFirstFilterValue } from "@/lib/analytics/filter-helpers";
import { getEvents } from "@/lib/analytics/get-events";
import { DubApiError } from "@/lib/api/errors";
+import { obfuscateCustomerEmail } from "@/lib/api/partner-profile/obfuscate-customer-email";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { withPartnerProfile } from "@/lib/auth/partner";
import {
@@ -128,7 +129,7 @@ export const GET = withPartnerProfile(
email: customer.email
? customerDataSharingEnabledAt
? customer.email
- : customer.email.replace(/(?<=^.).+(?=.@)/, "****")
+ : obfuscateCustomerEmail(customer.email)
: customer.name || generateRandomName(),
...(customerDataSharingEnabledAt && {
name: customer.name || generateRandomName(),
diff --git a/apps/web/lib/api/partner-profile/obfuscate-customer-email.ts b/apps/web/lib/api/partner-profile/obfuscate-customer-email.ts
new file mode 100644
index 0000000000..f9e0456a21
--- /dev/null
+++ b/apps/web/lib/api/partner-profile/obfuscate-customer-email.ts
@@ -0,0 +1,30 @@
+/**
+ * Obfuscates an email to the form a*****@g****.com:
+ * - Local part: first character visible, rest replaced with * (4–7 chars, approximate)
+ * - Domain: first character of domain name visible, rest replaced with *, TLD unchanged (4–7 chars, approximate)
+ * Uses consistent approximate lengths so the same email always obfuscates to the same string.
+ */
+const MIN_ASTERISKS = 4;
+const MAX_ASTERISKS = 7;
+
+function approximateAsteriskCount(seed: number): number {
+ return MIN_ASTERISKS + (Math.abs(seed) % (MAX_ASTERISKS - MIN_ASTERISKS + 1));
+}
+
+export function obfuscateCustomerEmail(email: string): string {
+ const [local, domain] = email.split("@");
+ if (!local || !domain) return email;
+ const dotIndex = domain.indexOf(".");
+ const domainName = dotIndex >= 0 ? domain.slice(0, dotIndex) : domain;
+ const tld = dotIndex >= 0 ? domain.slice(dotIndex) : "";
+ const localAsterisks = approximateAsteriskCount(local.charCodeAt(0) + local.length);
+ const domainAsterisks = approximateAsteriskCount(domainName.charCodeAt(0) + domainName.length);
+ return (
+ local[0] +
+ "*".repeat(localAsterisks) +
+ "@" +
+ domainName[0] +
+ "*".repeat(domainAsterisks) +
+ tld
+ );
+}