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.

Extend the `customerEmailMatch` fraud to domain match, historical domain match (#3572)

Kiran K5677af1d28ea
diff --git a/apps/web/lib/api/fraud/constants.ts b/apps/web/lib/api/fraud/constants.ts
index 8ff623662f..fc388a9d6f 100644
--- a/apps/web/lib/api/fraud/constants.ts
+++ b/apps/web/lib/api/fraud/constants.ts
@@ -6,7 +6,7 @@ export const FRAUD_RULES: FraudRuleInfo[] = [
type: "customerEmailMatch",
name: "Matching customer email",
description:
- "Partner's email matches a customer's email and could be a self referral.",
+ "Customer's email matches the partner's email or a previously referred customer by the same partner.",
scope: "conversionEvent",
configurable: true,
},
diff --git a/apps/web/lib/api/fraud/rules/check-customer-email-match.ts b/apps/web/lib/api/fraud/rules/check-customer-email-match.ts
index f7b74095f7..e6fc335b6a 100644
--- a/apps/web/lib/api/fraud/rules/check-customer-email-match.ts
+++ b/apps/web/lib/api/fraud/rules/check-customer-email-match.ts
@@ -1,11 +1,15 @@
+import { isGenericEmail } from "@/lib/is-generic-email";
import { FraudEventContext } from "@/lib/types";
+import { CustomerEmailMatchType } from "@/lib/zod/schemas/fraud";
+import { prisma } from "@dub/prisma";
import { defineFraudRule } from "../define-fraud-rule";
-import { normalizeEmail } from "../utils";
+import { extractEmailDomain, normalizeEmail } from "../utils";
-// Partner's email matches a customer's email and could be a self referral.
+// Partner's email matches a customer's email, shares the same email domain,
+// or the customer's domain matches a previously referred customer.
export const checkCustomerEmailMatch = defineFraudRule({
type: "customerEmailMatch",
- evaluate: async ({ partner, customer }: FraudEventContext) => {
+ evaluate: async ({ program, partner, customer }: FraudEventContext) => {
// Return false if either email is missing
if (!partner.email || !customer.email) {
return {
@@ -17,10 +21,67 @@ export const checkCustomerEmailMatch = defineFraudRule({
const normalizedPartnerEmail = normalizeEmail(partner.email);
const normalizedCustomerEmail = normalizeEmail(customer.email);
- // Check for exact match
+ // 1. Exact email match (strongest signal)
if (normalizedPartnerEmail === normalizedCustomerEmail) {
return {
triggered: true,
+ metadata: {
+ matchType: CustomerEmailMatchType.EXACT,
+ },
+ };
+ }
+
+ // Extract domains for domain-level checks
+ const partnerEmailDomain = extractEmailDomain(partner.email);
+ const customerEmailDomain = extractEmailDomain(customer.email);
+
+ if (!partnerEmailDomain || !customerEmailDomain) {
+ return {
+ triggered: false,
+ };
+ }
+
+ // Skip domain matching for free email providers
+ if (isGenericEmail(customer.email)) {
+ return {
+ triggered: false,
+ };
+ }
+
+ // 2. Partner-customer domain match
+ if (partnerEmailDomain === customerEmailDomain) {
+ return {
+ triggered: true,
+ metadata: {
+ matchType: CustomerEmailMatchType.DOMAIN_MATCH,
+ },
+ };
+ }
+
+ // 3. Historical domain match — customer's email domain matches
+ // a previously referred customer from the same partner
+ const historicalMatch = await prisma.customer.findFirst({
+ where: {
+ programId: program.id,
+ partnerId: partner.id,
+ id: {
+ not: customer.id,
+ },
+ email: {
+ endsWith: `@${customerEmailDomain}`,
+ },
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ if (historicalMatch) {
+ return {
+ triggered: true,
+ metadata: {
+ matchType: CustomerEmailMatchType.HISTORICAL_DOMAIN_MATCH,
+ },
};
}
diff --git a/apps/web/lib/api/fraud/rules/check-customer-email-suspicious.ts b/apps/web/lib/api/fraud/rules/check-customer-email-suspicious.ts
index 38c330ad30..3090acb913 100644
--- a/apps/web/lib/api/fraud/rules/check-customer-email-suspicious.ts
+++ b/apps/web/lib/api/fraud/rules/check-customer-email-suspicious.ts
@@ -1,6 +1,7 @@
import { FraudEventContext } from "@/lib/types";
import { redis } from "@/lib/upstash/redis";
import { defineFraudRule } from "../define-fraud-rule";
+import { extractEmailDomain } from "../utils";
export const checkCustomerEmailSuspicious = defineFraudRule({
type: "customerEmailSuspiciousDomain",
@@ -12,16 +13,13 @@ export const checkCustomerEmailSuspicious = defineFraudRule({
};
}
- // Extract domain from email
- const emailParts = customer.email.split("@");
- if (emailParts.length !== 2) {
+ const domain = extractEmailDomain(customer.email);
+ if (!domain) {
return {
triggered: false,
};
}
- const domain = emailParts[1].toLowerCase().trim();
-
try {
const isDisposable = await redis.sismember(
"disposableEmailDomains",
diff --git a/apps/web/lib/api/fraud/rules/check-partner-email-masked.ts b/apps/web/lib/api/fraud/rules/check-partner-email-masked.ts
index 17156e5831..4d7f180963 100644
--- a/apps/web/lib/api/fraud/rules/check-partner-email-masked.ts
+++ b/apps/web/lib/api/fraud/rules/check-partner-email-masked.ts
@@ -1,4 +1,5 @@
import { PartnerProps } from "@/lib/types";
+import { extractEmailDomain } from "../utils";
// Checks if the partner is using an Apple private relay email address
export function checkPartnerEmailMasked(partner: Pick<PartnerProps, "email">) {
@@ -6,12 +7,10 @@ export function checkPartnerEmailMasked(partner: Pick<PartnerProps, "email">) {
return false;
}
- const emailParts = partner.email.split("@");
- if (emailParts.length !== 2) {
+ const domain = extractEmailDomain(partner.email);
+ if (!domain) {
return false;
}
- const domain = emailParts[1].toLowerCase().trim();
-
return domain === "privaterelay.appleid.com";
}
diff --git a/apps/web/lib/api/fraud/utils.ts b/apps/web/lib/api/fraud/utils.ts
index ec977849d1..52abbb1564 100644
--- a/apps/web/lib/api/fraud/utils.ts
+++ b/apps/web/lib/api/fraud/utils.ts
@@ -16,8 +16,19 @@ type GetIdentityFieldsForFraudEventInput = Pick<
"type" | "partnerId" | "customerId" | "sourceProgramId" | "metadata"
>;
+// Extract domain from email
+export function extractEmailDomain(email: string) {
+ const parts = email.toLowerCase().trim().split("@");
+
+ if (parts.length !== 2 || !parts[0] || !parts[1]) {
+ return null;
+ }
+
+ return parts[1];
+}
+
// Normalize email for comparison
-export function normalizeEmail(email: string): string {
+export function normalizeEmail(email: string) {
const trimmed = email.toLowerCase().trim();
const parts = trimmed.split("@");
diff --git a/apps/web/lib/is-generic-email.ts b/apps/web/lib/is-generic-email.ts
index 2e6d2e9041..8b0971487c 100644
--- a/apps/web/lib/is-generic-email.ts
+++ b/apps/web/lib/is-generic-email.ts
@@ -1,7 +1,13 @@
export const GENERIC_EMAIL_DOMAINS = [
"gmail.com",
+ "googlemail.com",
"yahoo.com",
+ "yahoo.co.uk",
+ "yahoo.co.jp",
+ "yahoo.fr",
+ "yahoo.de",
"hotmail.com",
+ "hotmail.co.uk",
"outlook.com",
"icloud.com",
"aol.com",
@@ -9,9 +15,22 @@ export const GENERIC_EMAIL_DOMAINS = [
"verizon.net",
"att.net",
"me.com",
+ "mac.com",
"msn.com",
"live.com",
"protonmail.com",
+ "proton.me",
+ "zoho.com",
+ "yandex.com",
+ "yandex.ru",
+ "mail.com",
+ "mail.ru",
+ "gmx.com",
+ "gmx.net",
+ "fastmail.com",
+ "tutanota.com",
+ "tuta.com",
+ "privaterelay.appleid.com",
];
export const isGenericEmail = (email: string) => {
diff --git a/apps/web/lib/zod/schemas/fraud.ts b/apps/web/lib/zod/schemas/fraud.ts
index e0400fb1a7..861d4268a4 100644
--- a/apps/web/lib/zod/schemas/fraud.ts
+++ b/apps/web/lib/zod/schemas/fraud.ts
@@ -8,6 +8,12 @@ import { UserSchema } from "./users";
export const MAX_RESOLUTION_REASON_LENGTH = 200;
+export enum CustomerEmailMatchType {
+ EXACT = "exact",
+ DOMAIN_MATCH = "domainMatch",
+ HISTORICAL_DOMAIN_MATCH = "historicalDomainMatch",
+}
+
export const fraudGroupSchema = z.object({
id: z.string(),
type: z.enum(FraudRuleType),
@@ -286,6 +292,12 @@ export const fraudEventSchemas = {
email: true,
avatar: true,
}),
+ metadata: z
+ .object({
+ matchType: z.enum(CustomerEmailMatchType),
+ })
+ .nullable()
+ .optional(),
}),
customerEmailSuspiciousDomain: z.object({
diff --git a/apps/web/tests/fraud/index.test.ts b/apps/web/tests/fraud/index.test.ts
index 570f95fb4b..e179e557cf 100644
--- a/apps/web/tests/fraud/index.test.ts
+++ b/apps/web/tests/fraud/index.test.ts
@@ -1,5 +1,9 @@
+import { extractEmailDomain } from "@/lib/api/fraud/utils";
import { Customer, TrackLeadResponse } from "@/lib/types";
-import { fraudEventSchemas } from "@/lib/zod/schemas/fraud";
+import {
+ CustomerEmailMatchType,
+ fraudEventSchemas,
+} from "@/lib/zod/schemas/fraud";
import { FraudRuleType } from "@dub/prisma/client";
import { randomCustomer, retry } from "tests/utils/helpers";
import { HttpClient } from "tests/utils/http";
@@ -16,7 +20,7 @@ describe.concurrent("/fraud/**", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();
- test("FraudRuleType = customerEmailMatch", async () => {
+ test("FraudRuleType = customerEmailMatch (exact match)", async () => {
const clickLink = E2E_FRAUD_PARTNER.links.customerEmailMatch;
// Track a click
@@ -55,6 +59,46 @@ describe.concurrent("/fraud/**", async () => {
http,
customer,
ruleType: "customerEmailMatch",
+ metadata: {
+ matchType: CustomerEmailMatchType.EXACT,
+ },
+ });
+ });
+
+ test("FraudRuleType = customerEmailMatch (domain match)", async () => {
+ const clickLink = E2E_FRAUD_PARTNER.links.customerEmailMatch;
+
+ const clickResponse = await http.post<{ clickId: string }>({
+ path: "/track/click",
+ headers: { ...E2E_TRACK_CLICK_HEADERS },
+ body: { domain: clickLink.domain, key: clickLink.key },
+ });
+
+ const partnerEmailDomain = extractEmailDomain(E2E_FRAUD_PARTNER.email)!;
+
+ const customer = randomCustomer({
+ emailDomain: partnerEmailDomain,
+ });
+
+ await http.post<TrackLeadResponse>({
+ path: "/track/lead",
+ body: {
+ eventName: "Signup",
+ clickId: clickResponse.data.clickId,
+ customerId: customer.externalId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ },
+ });
+
+ await verifyFraudEvent({
+ http,
+ customer,
+ ruleType: "customerEmailMatch",
+ metadata: {
+ matchType: CustomerEmailMatchType.DOMAIN_MATCH,
+ },
});
});
@@ -134,6 +178,9 @@ describe.concurrent("/fraud/**", async () => {
http,
customer,
ruleType: "referralSourceBanned",
+ metadata: {
+ source: E2E_FRAUD_REFERRAL_SOURCE_BANNED_DOMAIN,
+ },
});
});
@@ -174,6 +221,10 @@ describe.concurrent("/fraud/**", async () => {
http,
customer,
ruleType: "paidTrafficDetected",
+ metadata: {
+ source: "google",
+ url: "https://dub.co/paid-traffic?gclid=1234567890&gad_source=1",
+ },
});
});
});
@@ -182,10 +233,12 @@ const verifyFraudEvent = async ({
http,
customer,
ruleType,
+ metadata,
}: {
http: HttpClient;
customer: Pick<Customer, "externalId">;
ruleType: FraudRuleType;
+ metadata?: Record<string, unknown>;
}) => {
// Resolve customerId from customerExternalID
const { data: customers } = await http.get<Customer[]>({
@@ -204,6 +257,7 @@ const verifyFraudEvent = async ({
// Assert fraud event shape
expect(fraudEvent).toStrictEqual({
+ ...(metadata && { metadata }),
createdAt: expect.any(String),
customer: expect.objectContaining({
id: customers[0].id,
@@ -211,17 +265,6 @@ const verifyFraudEvent = async ({
email: customers[0].email,
avatar: customers[0].avatar,
}),
- ...(ruleType === "paidTrafficDetected" && {
- metadata: {
- source: "google",
- url: "https://dub.co/paid-traffic?gclid=1234567890&gad_source=1",
- },
- }),
- ...(ruleType === "referralSourceBanned" && {
- metadata: {
- source: E2E_FRAUD_REFERRAL_SOURCE_BANNED_DOMAIN,
- },
- }),
});
};
diff --git a/apps/web/tests/utils/helpers.ts b/apps/web/tests/utils/helpers.ts
index 7b71e73066..9310cde732 100644
--- a/apps/web/tests/utils/helpers.ts
+++ b/apps/web/tests/utils/helpers.ts
@@ -19,8 +19,8 @@ export const randomCustomer = ({
};
};
-export const randomTagName = () => {
- return `e2e-${randomId()}`;
+export const randomTagName = (length?: number) => {
+ return `e2e-${randomId(length)}`;
};
export const randomEmail = ({
diff --git a/apps/web/ui/partners/fraud-risks/fraud-events-tables/fraud-matching-customer-email-table.tsx b/apps/web/ui/partners/fraud-risks/fraud-events-tables/fraud-matching-customer-email-table.tsx
index 544e9a24d3..829d8c0dbf 100644
--- a/apps/web/ui/partners/fraud-risks/fraud-events-tables/fraud-matching-customer-email-table.tsx
+++ b/apps/web/ui/partners/fraud-risks/fraud-events-tables/fraud-matching-customer-email-table.tsx
@@ -2,7 +2,10 @@
import { useFraudEvents } from "@/lib/swr/use-fraud-events";
import useWorkspace from "@/lib/swr/use-workspace";
-import { fraudEventSchemas } from "@/lib/zod/schemas/fraud";
+import {
+ CustomerEmailMatchType,
+ fraudEventSchemas,
+} from "@/lib/zod/schemas/fraud";
import { CustomerRowItem } from "@/ui/customers/customer-row-item";
import { Button, Table, TimestampTooltip, useTable } from "@dub/ui";
import { formatDateTimeSmart } from "@dub/utils";
@@ -11,6 +14,12 @@ import * as z from "zod/v4";
type EventDataProps = z.infer<(typeof fraudEventSchemas)["customerEmailMatch"]>;
+const MATCH_TYPE_LABELS: Record<CustomerEmailMatchType, string> = {
+ [CustomerEmailMatchType.EXACT]: "Exact email match",
+ [CustomerEmailMatchType.DOMAIN_MATCH]: "Domain match",
+ [CustomerEmailMatchType.HISTORICAL_DOMAIN_MATCH]: "Historical domain match",
+};
+
export function FraudMatchingCustomerEmailTable() {
const { slug: workspaceSlug } = useWorkspace();
@@ -60,6 +69,21 @@ export function FraudMatchingCustomerEmailTable() {
);
},
},
+ {
+ id: "matchType",
+ header: "Match type",
+ minSize: 120,
+ size: 180,
+ cell: ({ row }) => {
+ const matchType = row.original.metadata?.matchType ?? "exact";
+
+ return (
+ <span className="text-sm text-neutral-600">
+ {MATCH_TYPE_LABELS[matchType]}
+ </span>
+ );
+ },
+ },
{
id: "view",
header: "",