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.

Fix whitelisted domain check (#4544)

Steven Teydb2627f56b95
diff --git a/apps/web/lib/api/links/malicious-link-check.ts b/apps/web/lib/api/links/malicious-link-check.ts
index 80bba2a9b7..133c86360e 100644
--- a/apps/web/lib/api/links/malicious-link-check.ts
+++ b/apps/web/lib/api/links/malicious-link-check.ts
@@ -1,4 +1,5 @@
-import { isBlacklistedDomain, updateConfig } from "@/lib/edge-config";
+import { isBlacklistedDomain } from "@/lib/edge-config/is-blacklisted-domain";
+import { updateConfig } from "@/lib/edge-config/update";
import { getDomainWithoutWWW } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { experimental_evaluate as evaluate } from "ai";
@@ -10,11 +11,17 @@ export async function maliciousLinkCheck(url: string) {
return false;
}
- const domainBlacklisted = await isBlacklistedDomain(domain);
- if (domainBlacklisted) {
+ const domainStatus = await isBlacklistedDomain(domain);
+
+ if (domainStatus === "blacklisted") {
return true;
}
+ // skip remaining checks for trusted domains
+ if (domainStatus === "whitelisted") {
+ return false;
+ }
+
// run jev check
try {
const result = await evaluate({
diff --git a/apps/web/lib/edge-config/index.ts b/apps/web/lib/edge-config/index.ts
index 1b77e1d99d..a3206facfa 100644
--- a/apps/web/lib/edge-config/index.ts
+++ b/apps/web/lib/edge-config/index.ts
@@ -1,6 +1,5 @@
export * from "./get-feature-flags";
export * from "./get-partner-feature-flags";
-export * from "./is-blacklisted-domain";
export * from "./is-blacklisted-email";
export * from "./is-blacklisted-key";
export * from "./is-blacklisted-referrer";
diff --git a/apps/web/lib/edge-config/is-blacklisted-domain.ts b/apps/web/lib/edge-config/is-blacklisted-domain.ts
index fed6970524..67368c6d27 100644
--- a/apps/web/lib/edge-config/is-blacklisted-domain.ts
+++ b/apps/web/lib/edge-config/is-blacklisted-domain.ts
@@ -1,12 +1,10 @@
import { getAll } from "@vercel/edge-config";
-export const isBlacklistedDomain = async (domain: string): Promise<boolean> => {
- if (!process.env.EDGE_CONFIG) {
- return false;
- }
-
- if (!domain) {
- return false;
+export const isBlacklistedDomain = async (
+ domain: string,
+): Promise<"whitelisted" | "blacklisted" | null> => {
+ if (!process.env.EDGE_CONFIG || !domain) {
+ return null;
}
try {
@@ -17,8 +15,7 @@ export const isBlacklistedDomain = async (domain: string): Promise<boolean> => {
} = await getAll(["domains", "terms", "whitelistedDomains"]);
if (whitelistedDomains.includes(domain)) {
- console.log("Domain is whitelisted", domain);
- return false;
+ return "whitelisted";
}
const blacklistedTermsRegex = new RegExp(
@@ -27,15 +24,15 @@ export const isBlacklistedDomain = async (domain: string): Promise<boolean> => {
.join("|"),
);
- const isBlacklisted =
- blacklistedDomains.includes(domain) || blacklistedTermsRegex.test(domain);
-
- if (isBlacklisted) {
- return true;
+ if (
+ blacklistedDomains.includes(domain) ||
+ blacklistedTermsRegex.test(domain)
+ ) {
+ return "blacklisted";
}
- return false;
- } catch (e) {
- return false;
+ return null;
+ } catch {
+ return null;
}
};
diff --git a/apps/web/scripts/restore-banned-workspace.ts b/apps/web/scripts/restore-banned-workspace.ts
new file mode 100644
index 0000000000..0d2064c8d7
--- /dev/null
+++ b/apps/web/scripts/restore-banned-workspace.ts
@@ -0,0 +1,135 @@
+import { generateRandomString } from "@/lib/api/utils/generate-random-string";
+import { prisma } from "@/lib/prisma";
+import { LEGAL_USER_ID, nanoid } from "@dub/utils";
+import "dotenv-flow/config";
+import { linkCache } from "../lib/api/links/cache";
+
+const WORKSPACE_ID = "ws_xxx";
+const WORKSPACE_NAME = "xxx";
+const WORKSPACE_SLUG = "xxx";
+const USER_ID = "user_xxx";
+
+const LINK_IDS = [];
+
+async function main() {
+ let workspace = await prisma.project.findUnique({
+ where: {
+ id: WORKSPACE_ID,
+ },
+ });
+
+ if (!workspace) {
+ workspace = await prisma.project.create({
+ data: {
+ id: WORKSPACE_ID,
+ name: WORKSPACE_NAME,
+ slug: WORKSPACE_SLUG,
+ billingCycleStart: new Date().getDate(),
+ invoicePrefix: generateRandomString(8),
+ inviteCode: nanoid(24),
+ defaultDomains: {
+ create: {},
+ },
+ users: {
+ create: {
+ userId: LEGAL_USER_ID,
+ role: "owner",
+ notificationPreference: {
+ create: {},
+ },
+ },
+ },
+ },
+ });
+
+ console.log(`Created workspace ${workspace.slug} (${workspace.id})`);
+ } else {
+ console.log(
+ `Workspace already exists: ${workspace.slug} (${workspace.id})`,
+ );
+
+ await prisma.projectUsers.upsert({
+ where: {
+ userId_projectId: {
+ userId: LEGAL_USER_ID,
+ projectId: WORKSPACE_ID,
+ },
+ },
+ create: {
+ userId: LEGAL_USER_ID,
+ projectId: WORKSPACE_ID,
+ role: "owner",
+ notificationPreference: {
+ create: {},
+ },
+ },
+ update: {
+ role: "owner",
+ },
+ });
+
+ console.log(`Added legal user ${LEGAL_USER_ID} as workspace owner`);
+ }
+
+ const links = await prisma.link.findMany({
+ where: {
+ id: {
+ in: LINK_IDS,
+ },
+ },
+ select: {
+ id: true,
+ domain: true,
+ key: true,
+ projectId: true,
+ userId: true,
+ },
+ });
+
+ const missingLinkIds = LINK_IDS.filter(
+ (id) => !links.some((link) => link.id === id),
+ );
+
+ if (missingLinkIds.length > 0) {
+ console.log(`Missing ${missingLinkIds.length} links:`, missingLinkIds);
+ }
+
+ console.table(links);
+
+ if (links.length === 0) {
+ console.log("No links found to restore.");
+ return;
+ }
+
+ const [redisRes, prismaRes] = await Promise.allSettled([
+ linkCache.expireMany(links),
+ prisma.link.updateMany({
+ where: {
+ id: {
+ in: links.map((link) => link.id),
+ },
+ },
+ data: {
+ projectId: WORKSPACE_ID,
+ userId: USER_ID,
+ },
+ }),
+ ]);
+
+ await prisma.project.update({
+ where: {
+ id: WORKSPACE_ID,
+ },
+ data: {
+ totalLinks: links.length,
+ },
+ });
+
+ console.log(
+ `Restored ${links.length} links to ${workspace.slug}`,
+ redisRes,
+ prismaRes,
+ );
+}
+
+main();