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.

`encodeURI when `createResponseWithCookies` (#3624)

Steven Tey80a553dd0017
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 178ee9c48d..b9c277c2f6 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -249,7 +249,7 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {
}
const cookieData = {
- path: `/${originalKey}`,
+ path: `/${encodeURI(originalKey)}`,
dubIdCookieName,
dubIdCookieValue: clickId,
dubTestUrlValue: testUrl,
diff --git a/apps/web/lib/middleware/utils/parse.ts b/apps/web/lib/middleware/utils/parse.ts
index 54305c3e24..1156f2c711 100644
--- a/apps/web/lib/middleware/utils/parse.ts
+++ b/apps/web/lib/middleware/utils/parse.ts
@@ -24,7 +24,7 @@ export const parse = (req: NextRequest) => {
const searchParamsString = searchParams.length > 0 ? `?${searchParams}` : "";
const fullPath = `${path}${searchParamsString}`;
- // Here, we are using decodeURIComponent to handle foreign languages like Hebrew
+ // Here, we are using decodeURIComponent to handle foreign languages like Hebrew and Korean
const key = decodeURIComponent(path.split("/")[1]); // key is the first part of the path (e.g. dub.sh/stats/github -> stats)
const fullKey = decodeURIComponent(path.slice(1)); // fullKey is the full path without the first slash (to account for multi-level subpaths, e.g. d.to/github/repo -> github/repo)
diff --git a/apps/web/tests/redirects/index.test.ts b/apps/web/tests/redirects/index.test.ts
index 0f4849471a..0fe6a5906c 100644
--- a/apps/web/tests/redirects/index.test.ts
+++ b/apps/web/tests/redirects/index.test.ts
@@ -12,6 +12,53 @@ const fetchOptions: RequestInit = {
},
};
+// Redirect includes ?dub_id=… and sets a matching dub_id_*_<linkKey> cookie whose value equals the query dub_id
+async function assertRedirectWithDubIdCookie(
+ baseUrl: string,
+ linkKey: string,
+ options?: { via?: string },
+) {
+ const response = await fetch(`${baseUrl}/${linkKey}`, fetchOptions);
+
+ const location = response.headers.get("location");
+ expect(location).toBeTruthy();
+ const redirectUrl = new URL(location!, baseUrl);
+ const dubIdFromQuery = redirectUrl.searchParams.get("dub_id");
+ expect(dubIdFromQuery).toBeTruthy();
+ expect(dubIdFromQuery).toMatch(/^[a-zA-Z0-9_-]+$/);
+
+ if (options?.via != null) {
+ expect(redirectUrl.searchParams.get("via")).toBe(options.via);
+ }
+
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+
+ const setCookie = response.headers.getSetCookie?.() ?? [];
+ const dubIdCookie = setCookie.find((line) => {
+ const name = line.split(";")[0]!.split("=")[0]!.trim();
+ return name.startsWith("dub_id_") && name.endsWith(`_${linkKey}`);
+ });
+ expect(
+ dubIdCookie,
+ "expected Set-Cookie from createResponseWithCookies (dub_id_<domain>_<key>)",
+ ).toBeDefined();
+
+ const pair = dubIdCookie!.split(";")[0]!;
+ const eq = pair.indexOf("=");
+ const cookieValue = pair.slice(eq + 1);
+ expect(cookieValue).toBe(dubIdFromQuery);
+
+ const pathAttr = `/${encodeURI(linkKey)}`;
+ expect(dubIdCookie).toMatch(
+ new RegExp(
+ `Path=${pathAttr.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?:;|$)`,
+ "i",
+ ),
+ );
+ expect(dubIdCookie).toMatch(/Max-Age=3600\b/i);
+}
+
describe.runIf(env.CI)("Link Redirects", async () => {
const h = new IntegrationHarness();
@@ -48,29 +95,48 @@ describe.runIf(env.CI)("Link Redirects", async () => {
expect(response.status).toBe(302);
});
- test("with dub_id", async () => {
- const response = await fetch(
- `${h.baseUrl}/conversion-tracking`,
- fetchOptions,
- );
+ test("korean unicode key", async () => {
+ const hangulKey = "한글링크테스트";
+ const url = new URL(h.baseUrl);
+ url.pathname = `/${hangulKey}`;
- // the location should contain `?dub_id=` query param
- expect(response.headers.get("location")).toMatch(/dub_id=[a-zA-Z0-9]+/);
+ const response = await fetch(url.href, fetchOptions);
+
+ expect(response.headers.get("location")).toBe(
+ "https://youtu.be/9bZkp7q19f0",
+ );
expect(response.headers.get("x-powered-by")).toBe(poweredBy);
expect(response.status).toBe(302);
});
- test("with dub_id and via", async () => {
- const response = await fetch(`${h.baseUrl}/track-test`, fetchOptions);
+ test("hebrew unicode key", async () => {
+ const hebrewKey = "שלום";
+ const url = new URL(h.baseUrl);
+ url.pathname = `/${hebrewKey}`;
+
+ const response = await fetch(url.href, fetchOptions);
- // the location should contain `?dub_id=` query param
- expect(response.headers.get("location")).toMatch(/dub_id=[a-zA-Z0-9]+/);
- // the location should contain `?via=track-test` query param
- expect(response.headers.get("location")).toMatch(/via=track-test/);
+ expect(response.headers.get("location")).toBe(
+ "https://youtube.com/shorts/IdP2WdnJK1o",
+ );
expect(response.headers.get("x-powered-by")).toBe(poweredBy);
expect(response.status).toBe(302);
});
+ test("with dub_id", async () => {
+ await assertRedirectWithDubIdCookie(h.baseUrl, "conversion-tracking");
+ });
+
+ test("with dub_id (and slash in key)", async () => {
+ await assertRedirectWithDubIdCookie(h.baseUrl, "conversion/tracking");
+ });
+
+ test("with dub_id and via", async () => {
+ await assertRedirectWithDubIdCookie(h.baseUrl, "track-test", {
+ via: "track-test",
+ });
+ });
+
test("with dub_client_reference_id", async () => {
const response = await fetch(
`${h.baseUrl}/client_reference_id`,
@@ -220,6 +286,10 @@ describe.runIf(env.CI)("Link Redirects", async () => {
expect(response.status).toBe(302);
});
+ test("with case-sensitive key (and dub_id)", async () => {
+ await assertRedirectWithDubIdCookie(h.baseUrl, "cAsE-sensitive-TeSt");
+ });
+
test("with password", async () => {
const response = await fetch(
`${h.baseUrl}/password/check?pw=dub`,