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 bot misdetection for Instagram webview on Pixel devices (#3873)

Pedro Ladeiraa46bca86de18
diff --git a/apps/web/lib/middleware/utils/detect-bot.ts b/apps/web/lib/middleware/utils/detect-bot.ts
index fc814fe062..cb6b2e0f69 100644
--- a/apps/web/lib/middleware/utils/detect-bot.ts
+++ b/apps/web/lib/middleware/utils/detect-bot.ts
@@ -3,6 +3,11 @@ import { userAgent } from "next/server";
import { IP_BOTS, IP_RANGES_BOTS, REFERRER_BOTS, UA_BOTS } from "./bots-list";
import { isIpInRange } from "./is-ip-in-range";
+// UA fragments that look like a bot pattern but aren't – e.g. Instagram's
+// in-app webview on Pixel devices appends "Google/google" in its device
+// descriptor, which would otherwise trip the generic "google" entry in UA_BOTS
+const UA_FALSE_POSITIVES = [/Google\/google\b/];
+
export const detectBot = (req: Request) => {
const searchParams = new URL(req.url).searchParams;
@@ -19,7 +24,17 @@ export const detectBot = (req: Request) => {
const ua = userAgent(req);
if (ua) {
- return ua.isBot || UA_BOTS.some((bot) => new RegExp(bot, "i").test(ua.ua));
+ const isKnownFalsePositive = UA_FALSE_POSITIVES.some((pattern) =>
+ pattern.test(ua.ua),
+ );
+ const sanitizedUa = UA_FALSE_POSITIVES.reduce(
+ (s, pattern) => s.replace(pattern, ""),
+ ua.ua,
+ );
+ return (
+ (!isKnownFalsePositive && ua.isBot) ||
+ UA_BOTS.some((bot) => new RegExp(bot, "i").test(sanitizedUa))
+ );
}
// Check referer