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.

Enhance bot detection with improved UA and IP checks

Kiran K733d8290badc
diff --git a/apps/web/lib/middleware/utils/detect-bot.ts b/apps/web/lib/middleware/utils/detect-bot.ts
index 72db59f704..25900f0517 100644
--- a/apps/web/lib/middleware/utils/detect-bot.ts
+++ b/apps/web/lib/middleware/utils/detect-bot.ts
@@ -1,22 +1,35 @@
+import { IP_BOTS, UA_BOTS } from "@dub/utils/src/constants";
+import { ipAddress } from "@vercel/functions";
+import { userAgent } from "next/server";
+
export const detectBot = (req: Request) => {
const searchParams = new URL(req.url).searchParams;
- if (searchParams.get("bot")) return true;
- const ua = req.headers.get("User-Agent");
- if (ua) {
- /* Note:
- * - bot is for most bots & crawlers
- * - metatags is for Dub.co Metatags API (https://api.dub.co/metatags)
- * - ChatGPT is for ChatGPT
- * - bluesky is for Bluesky crawler
- * - facebookexternalhit is for Facebook crawler
- * - WhatsApp is for WhatsApp crawler
- * - MetaInspector is for https://metatags.io/
- * - Go-http-client/1.1 is a bot: https://user-agents.net/string/go-http-client-1-1
- * - iframely is for https://iframely.com/docs/about (used by Notion, Linear)
- */
- return /bot|metatags|chatgpt|bluesky|facebookexternalhit|WhatsApp|google|baidu|bing|msn|duckduckbot|teoma|slurp|yandex|MetaInspector|Go-http-client|iframely/i.test(
- ua,
- );
+
+ if (searchParams.get("bot")) {
+ return true;
}
- return false;
+
+ // Check ua
+ const ua = userAgent(req);
+
+ if (!ua) {
+ return false;
+ }
+
+ if (ua.isBot || UA_BOTS.some((bot) => new RegExp(bot, "i").test(ua.ua))) {
+ return true;
+ }
+
+ // Check ip
+ let ip = ipAddress(req);
+
+ if (!ip) {
+ return false;
+ }
+
+ if (ip.includes("/")) {
+ ip = ip.split("/")[0];
+ }
+
+ return IP_BOTS.includes(ip);
};
diff --git a/packages/utils/src/constants/bots.ts b/packages/utils/src/constants/bots.ts
new file mode 100644
index 0000000000..793cdbd460
--- /dev/null
+++ b/packages/utils/src/constants/bots.ts
@@ -0,0 +1,72 @@
+export const UA_BOTS = [
+ "bot",
+ "metatags", // Dub.co Metatags API (https://api.dub.co/metatags)
+ "chatgpt",
+ "bluesky",
+ "facebookexternalhit",
+ "WhatsApp",
+ "google",
+ "baidu",
+ "bing",
+ "msn",
+ "duckduckbot",
+ "teoma",
+ "slurp",
+ "yandex",
+ "MetaInspector",
+ "Go-http-client",
+ "iframely",
+
+ // new
+ "ia_archiver",
+ "Sogou",
+ "SkypeUriPreview",
+ "vkShare",
+ "Slackbot",
+ "Tumblr",
+ "python",
+ "FeedBurner",
+ "upptime",
+ "Hyperping",
+ "cron-job",
+ "InternetMeasurement",
+ "HostTracker",
+];
+
+export const IP_BOTS = [
+ "127.0.0.1",
+ "52.112.74.60",
+ "52.112.125.8",
+ "52.112.49.104",
+ "52.112.49.112",
+ "52.112.39.132",
+ "52.123.190.36",
+ "52.123.190.60",
+ "52.112.95.132",
+ "52.123.190.88",
+ "52.112.95.133",
+ "52.123.190.124",
+ "52.112.39.133",
+ "52.112.49.156",
+ "195.211.23.206",
+ "172.69.34.226",
+ "52.112.49.196",
+ "172.69.34.225",
+ "52.112.125.9",
+ "172.69.34.17",
+ "172.69.34.18",
+ "195.211.23.207",
+ "52.112.49.157",
+ "172.69.22.217",
+ "162.158.167.225",
+ "162.158.167.226",
+ "52.112.74.61",
+ "172.69.34.194",
+ "27.124.32.71",
+ "172.69.22.216",
+ "172.69.34.193",
+ "195.211.23.208",
+ "195.211.23.210",
+ "110.40.20.105",
+ "134.122.196.16",
+];
diff --git a/packages/utils/src/constants/index.ts b/packages/utils/src/constants/index.ts
index 51999a5abe..223cda4573 100644
--- a/packages/utils/src/constants/index.ts
+++ b/packages/utils/src/constants/index.ts
@@ -1,3 +1,4 @@
+export * from "./bots";
export * from "./cctlds";
export * from "./connect-supported-countries";
export * from "./continents";