The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Fix URI malformed errors when decoding link keys
Kiran K1fa7f479c4f5
diff --git a/apps/web/lib/planetscale/check-if-key-exists.ts b/apps/web/lib/planetscale/check-if-key-exists.tsindex 9069c7abee..72e02f4c50 100644--- a/apps/web/lib/planetscale/check-if-key-exists.ts+++ b/apps/web/lib/planetscale/check-if-key-exists.ts@@ -1,4 +1,4 @@-import { punyEncode } from "@dub/utils";+import { punyEncode, safeDecodeURIComponent } from "@dub/utils";import {encodeKey,isCaseSensitiveDomain,@@ -18,7 +18,7 @@ export const checkIfKeyExists = async ({encodeKey(key): // for non-case sensitive domains, we need to make sure that the key is always URI-decoded + punycode-encoded// (cause that's how we store it in MySQL)- punyEncode(decodeURIComponent(key));+ punyEncode(safeDecodeURIComponent(key));const { rows } =(await conn.execute(diff --git a/apps/web/lib/planetscale/get-link-via-edge.ts b/apps/web/lib/planetscale/get-link-via-edge.tsindex 5da1e2f319..b934f8047b 100644--- a/apps/web/lib/planetscale/get-link-via-edge.ts+++ b/apps/web/lib/planetscale/get-link-via-edge.ts@@ -1,4 +1,4 @@-import { punyEncode } from "@dub/utils";+import { punyEncode, safeDecodeURIComponent } from "@dub/utils";import {decodeKeyIfCaseSensitive,encodeKey,@@ -20,7 +20,7 @@ const getLinkViaEdgeHelper = async ({encodeKey(key): // for non-case sensitive domains, we need to make sure that the key is always URI-decoded + punycode-encoded// (cause that's how we store it in MySQL)- punyEncode(decodeURIComponent(key));+ punyEncode(safeDecodeURIComponent(key));const { rows } =(await conn.execute(`SELECT * FROM Link WHERE domain = ? AND \`key\` = ?`, [diff --git a/apps/web/lib/planetscale/get-link-with-partner.ts b/apps/web/lib/planetscale/get-link-with-partner.tsindex 531fb0ea35..63117fc028 100644--- a/apps/web/lib/planetscale/get-link-with-partner.ts+++ b/apps/web/lib/planetscale/get-link-with-partner.ts@@ -1,4 +1,4 @@-import { punyEncode } from "@dub/utils";+import { punyEncode, safeDecodeURIComponent } from "@dub/utils";import {decodeKeyIfCaseSensitive,encodeKey,@@ -30,7 +30,7 @@ export const getLinkWithPartner = async ({}): Promise<QueryResult | null> => {const keyToQuery = isCaseSensitiveDomain(domain)? encodeKey(key)- : punyEncode(decodeURIComponent(key));+ : punyEncode(safeDecodeURIComponent(key));console.time("getLinkWithPartner");diff --git a/apps/web/tests/misc/safe-decode-uri-component.test.ts b/apps/web/tests/misc/safe-decode-uri-component.test.tsnew file mode 100644index 0000000000..b122d44de2--- /dev/null+++ b/apps/web/tests/misc/safe-decode-uri-component.test.ts@@ -0,0 +1,19 @@+import { safeDecodeURIComponent } from "@dub/utils";+import { describe, expect, it } from "vitest";++describe("safeDecodeURIComponent", () => {+ it("decodes valid percent-encoding", () => {+ expect(safeDecodeURIComponent("hello%20world")).toBe("hello world");+ });++ it("returns the original string for malformed sequences", () => {+ expect(safeDecodeURIComponent("100%off")).toBe("100%off");+ expect(safeDecodeURIComponent("a%")).toBe("a%");+ expect(safeDecodeURIComponent("a%2")).toBe("a%2");+ });++ it("leaves plain and already-decoded keys unchanged", () => {+ expect(safeDecodeURIComponent("github")).toBe("github");+ expect(safeDecodeURIComponent("안녕")).toBe("안녕");+ });+});diff --git a/packages/utils/src/functions/urls.ts b/packages/utils/src/functions/urls.tsindex 66e170bbda..af7d5a31d9 100644--- a/packages/utils/src/functions/urls.ts+++ b/packages/utils/src/functions/urls.ts@@ -7,6 +7,14 @@ export const isValidUrl = (url: string) => {}};+export function safeDecodeURIComponent(value: string): string {+ try {+ return decodeURIComponent(value);+ } catch {+ return value;+ }+}+const SAFE_LINK_SCHEMES = new Set(["http:", "https:", "mailto:"]);export function isSafeLinkHref(