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.

address CR feedback

Steven Tey75e4f7bbbb8e
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 57824e9573..d20f4cc3de 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -9,6 +9,7 @@ import {
getUrlFromStringIfValid,
isDubDomain,
isGoogleClickTrackerDomain,
+ isSafeLinkHref,
isUnsupportedKey,
nanoid,
punyEncode,
@@ -204,43 +205,6 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {
dubTestUrlValue: testUrl,
};
- // for Google click tracker domains (dub.sh, dub.link):
- // if there is a redirection url set, then use it instead of the target
- if (isGoogleClickTrackerDomain(domain)) {
- const redirectionUrl = getUrlFromStringIfValid(
- searchParamsObj[REDIRECTION_QUERY_PARAM] ?? "",
- );
-
- // if redirectionUrl is present, return it immediately
- if (redirectionUrl) {
- ev.waitUntil(
- recordClick({
- req,
- clickId,
- workspaceId,
- linkId,
- domain,
- key,
- url: redirectionUrl,
- programId: cachedLink.programId,
- partnerId: cachedLink.partnerId,
- shouldCacheClickId,
- }),
- );
-
- return createResponseWithCookies(
- NextResponse.redirect(redirectionUrl, {
- headers: {
- ...DUB_HEADERS,
- ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
- },
- status: key === "_root" ? 301 : 302,
- }),
- cookieData,
- );
- }
- }
-
// only show inspect modal if the link is not password protected
if (inspectMode && !password) {
return NextResponse.rewrite(
@@ -321,6 +285,43 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {
}
}
+ // for Google click tracker domains (dub.sh, dub.link):
+ // if there is a redirection url set, then use it instead of the target
+ if (isGoogleClickTrackerDomain(domain)) {
+ const redirectionUrl = getUrlFromStringIfValid(
+ searchParamsObj[REDIRECTION_QUERY_PARAM] ?? "",
+ );
+
+ // if redirectionUrl is present, return it immediately
+ if (redirectionUrl && isSafeLinkHref(redirectionUrl)) {
+ ev.waitUntil(
+ recordClick({
+ req,
+ clickId,
+ workspaceId,
+ linkId,
+ domain,
+ key,
+ url: redirectionUrl,
+ programId: cachedLink.programId,
+ partnerId: cachedLink.partnerId,
+ shouldCacheClickId,
+ }),
+ );
+
+ return createResponseWithCookies(
+ NextResponse.redirect(redirectionUrl, {
+ headers: {
+ ...DUB_HEADERS,
+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
+ },
+ status: key === "_root" ? 301 : 302,
+ }),
+ cookieData,
+ );
+ }
+ }
+
// for root domain links, if there's no destination URL, rewrite to placeholder page
if (!url) {
ev.waitUntil(