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.

check twice in getRandomKey

Steven Teycc12790e2103
diff --git a/apps/web/lib/api/links/utils/key-checks.ts b/apps/web/lib/api/links/utils/key-checks.ts
index f48823bc8e..313bae5bf2 100644
--- a/apps/web/lib/api/links/utils/key-checks.ts
+++ b/apps/web/lib/api/links/utils/key-checks.ts
@@ -11,7 +11,6 @@ import {
isDubDomain,
isReservedKeyGlobal,
} from "@dub/utils";
-import { encodeKeyIfCaseSensitive } from "../case-sensitivity";
export async function keyChecks({
domain,
@@ -37,12 +36,7 @@ export async function keyChecks({
};
}
- key = encodeKeyIfCaseSensitive({
- domain,
- key,
- });
-
- const link = await checkIfKeyExists(domain, key);
+ const link = await checkIfKeyExists({ domain, key });
if (link) {
return {
error: "Duplicate key: This short link already exists.",
diff --git a/apps/web/lib/planetscale/check-if-key-exists.ts b/apps/web/lib/planetscale/check-if-key-exists.ts
index 66330397ab..5d72cf7060 100644
--- a/apps/web/lib/planetscale/check-if-key-exists.ts
+++ b/apps/web/lib/planetscale/check-if-key-exists.ts
@@ -5,14 +5,23 @@ import {
} from "../api/links/case-sensitivity";
import { conn } from "./connection";
-export const checkIfKeyExists = async (domain: string, key: string) => {
+export const checkIfKeyExists = async ({
+ domain,
+ key,
+ ignoreCaseSensitivity = false,
+}: {
+ domain: string;
+ key: string;
+ ignoreCaseSensitivity?: boolean;
+}) => {
const isCaseSensitive = isCaseSensitiveDomain(domain);
- const keyToQuery = isCaseSensitive
- ? // for case sensitive domains, we need to encode the key
- 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));
+ const keyToQuery =
+ isCaseSensitive && !ignoreCaseSensitivity
+ ? // for case sensitive domains, we need to encode the key
+ 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));
const { rows } =
(await conn.execute(
diff --git a/apps/web/lib/planetscale/get-random-key.ts b/apps/web/lib/planetscale/get-random-key.ts
index 31b84d8ab9..a7e6434443 100644
--- a/apps/web/lib/planetscale/get-random-key.ts
+++ b/apps/web/lib/planetscale/get-random-key.ts
@@ -1,4 +1,5 @@
import { nanoid } from "@dub/utils";
+import { isCaseSensitiveDomain } from "../api/links/case-sensitivity";
import { checkIfKeyExists } from "./check-if-key-exists";
export async function getRandomKey({
@@ -15,11 +16,21 @@ export async function getRandomKey({
if (prefix) {
key = `${prefix.replace(/^\/|\/$/g, "")}/${key}`;
}
- const exists = await checkIfKeyExists(domain, key);
+ const exists = await checkIfKeyExists({ domain, key });
if (exists) {
// by the off chance that key already exists
return getRandomKey({ domain, prefix, long });
} else {
+ if (isCaseSensitiveDomain(domain)) {
+ const unencodedExists = await checkIfKeyExists({
+ domain,
+ key,
+ ignoreCaseSensitivity: true,
+ });
+ if (unencodedExists) {
+ return getRandomKey({ domain, prefix, long });
+ }
+ }
return key;
}
}