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.

Add unsupported keys

Steven Tey80148c951da0
diff --git a/apps/web/lib/api/links/utils/process-key.ts b/apps/web/lib/api/links/utils/process-key.ts
index 72e6e3984b..c0dfcb153c 100644
--- a/apps/web/lib/api/links/utils/process-key.ts
+++ b/apps/web/lib/api/links/utils/process-key.ts
@@ -1,4 +1,9 @@
-import { isDubDomain, punyEncode, validKeyRegex } from "@dub/utils";
+import {
+ isDubDomain,
+ isUnsupportedKey,
+ punyEncode,
+ validKeyRegex,
+} from "@dub/utils";
export function processKey({ domain, key }: { domain: string; key: string }) {
// Skip if root domain
@@ -15,7 +20,7 @@ export function processKey({ domain, key }: { domain: string; key: string }) {
}
// if key ends with .php, return null (we don't support .php in links)
- if (key.endsWith(".php")) {
+ if (isUnsupportedKey(key)) {
return null;
}
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 8adf9307ab..7714613db7 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -12,6 +12,7 @@ import {
LEGAL_WORKSPACE_ID,
LOCALHOST_GEO_DATA,
isDubDomain,
+ isUnsupportedKey,
nanoid,
punyEncode,
} from "@dub/utils";
@@ -53,7 +54,7 @@ export default async function LinkMiddleware(
// we don't support .php links (too much bot traffic)
// hence we redirect to the root domain and add `dub-no-track` header to avoid tracking bot traffic
- if (key.endsWith(".php")) {
+ if (isUnsupportedKey(key)) {
return NextResponse.redirect(new URL("/?dub-no-track=1", req.url), {
headers: {
...DUB_HEADERS,
diff --git a/apps/web/scripts/update-not-found.ts b/apps/web/scripts/update-not-found.ts
index 63006b1b4d..7442360aae 100644
--- a/apps/web/scripts/update-not-found.ts
+++ b/apps/web/scripts/update-not-found.ts
@@ -2,70 +2,16 @@ import { prisma } from "@/lib/prisma";
import "dotenv-flow/config";
async function main() {
- const domains = await prisma.domain.findMany({
+ const domains = await prisma.domain.updateMany({
where: {
- notFoundUrl: null,
- project: {
- plan: {
- not: "free",
- },
- },
- links: {
- some: {
- key: "_root",
- url: {
- not: "",
- },
- },
- },
+ placeholder: "https://dub.co/help/article/what-is-dub",
},
- select: {
- id: true,
- slug: true,
- notFoundUrl: true,
- project: {
- select: {
- slug: true,
- plan: true,
- },
- },
- links: {
- select: {
- key: true,
- url: true,
- },
- where: {
- key: "_root",
- },
- },
- },
- take: 10,
- orderBy: {
- createdAt: "asc",
+ data: {
+ placeholder: null,
},
});
- const updatedDomains = await Promise.all(
- domains.map((domain) => {
- return prisma.domain.update({
- where: { id: domain.id },
- data: { notFoundUrl: domain.links[0].url },
- select: {
- id: true,
- slug: true,
- notFoundUrl: true,
- project: {
- select: {
- slug: true,
- plan: true,
- },
- },
- },
- });
- }),
- );
-
- console.table(updatedDomains);
+ console.log(domains);
}
main();
diff --git a/apps/web/tests/redirects/index.test.ts b/apps/web/tests/redirects/index.test.ts
index dd449a2fc5..2ea9558b39 100644
--- a/apps/web/tests/redirects/index.test.ts
+++ b/apps/web/tests/redirects/index.test.ts
@@ -78,4 +78,17 @@ describe.runIf(env.CI)("Link Redirects", async () => {
expect(response.headers.get("x-powered-by")).toBe(poweredBy);
expect(response.status).toBe(302);
});
+
+ test("unsupported key", async () => {
+ const response = await fetch(`${h.baseUrl}/wp-admin.php`, {
+ ...fetchOptions,
+ headers: {},
+ });
+
+ expect(response.headers.get("location")).toBe(
+ "https://dub.co/?dub-no-track=1",
+ );
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
});
diff --git a/packages/utils/src/functions/domains.ts b/packages/utils/src/functions/domains.ts
index ef564b5555..fb6fc9cb48 100644
--- a/packages/utils/src/functions/domains.ts
+++ b/packages/utils/src/functions/domains.ts
@@ -33,11 +33,6 @@ export const validDomainRegex = new RegExp(
/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/,
);
-// allow letters, numbers, '-', '_', '/', '.', and emojis
-export const validKeyRegex = new RegExp(
- /^[0-9A-Za-z_\u0080-\uFFFF\/\-\p{Emoji}.]+$/u,
-);
-
export const validSlugRegex = new RegExp(/^[a-zA-Z0-9\-]+$/);
export const getSubdomain = (name: string, apexName: string) => {
diff --git a/packages/utils/src/functions/index.ts b/packages/utils/src/functions/index.ts
index ea4f77b6a4..9286dd79e0 100644
--- a/packages/utils/src/functions/index.ts
+++ b/packages/utils/src/functions/index.ts
@@ -13,6 +13,7 @@ export * from "./fetcher";
export * from "./hash-string";
export * from "./images";
export * from "./is-iframeable";
+export * from "./keys";
export * from "./link-constructor";
export * from "./log";
export * from "./nanoid";
diff --git a/packages/utils/src/functions/keys.ts b/packages/utils/src/functions/keys.ts
new file mode 100644
index 0000000000..f7035aa033
--- /dev/null
+++ b/packages/utils/src/functions/keys.ts
@@ -0,0 +1,9 @@
+// allow letters, numbers, '-', '_', '/', '.', and emojis
+export const validKeyRegex = new RegExp(
+ /^[0-9A-Za-z_\u0080-\uFFFF\/\-\p{Emoji}.]+$/u,
+);
+
+export const isUnsupportedKey = (key: string) => {
+ const unsupportedExtensions = [".php", ".php7"];
+ return unsupportedExtensions.some((extension) => key.endsWith(extension));
+};