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 partner link URL parsing

Tim Wilson8d8514c25982
diff --git a/apps/web/ui/modals/partner-link-modal.tsx b/apps/web/ui/modals/partner-link-modal.tsx
index 23d16b6daa..2592f32b89 100644
--- a/apps/web/ui/modals/partner-link-modal.tsx
+++ b/apps/web/ui/modals/partner-link-modal.tsx
@@ -23,6 +23,7 @@ import {
getDomainWithoutWWW,
getPrettyUrl,
linkConstructor,
+ regexEscape,
} from "@dub/utils";
import { AnimatePresence, motion } from "framer-motion";
import {
@@ -179,7 +180,10 @@ function PartnerLinkModalContent({
const form = useForm<PartnerLinkFormData>({
defaultValues: link
? {
- url: link.url.replace(`https://${destinationDomain}/`, ""),
+ url: link.url.replace(
+ new RegExp(`^https?:\/\/${regexEscape(destinationDomain)}\/?`),
+ "",
+ ),
key: link.key,
comments: link.comments ?? "",
}
diff --git a/packages/utils/src/functions/index.ts b/packages/utils/src/functions/index.ts
index ecc893d39e..7de915919a 100644
--- a/packages/utils/src/functions/index.ts
+++ b/packages/utils/src/functions/index.ts
@@ -24,6 +24,7 @@ export * from "./normalize-string";
export * from "./pluralize";
export * from "./punycode";
export * from "./random-value";
+export * from "./regex-escape";
export * from "./resize-image";
export * from "./smart-truncate";
export * from "./stable-sort";
diff --git a/packages/utils/src/functions/regex-escape.ts b/packages/utils/src/functions/regex-escape.ts
new file mode 100644
index 0000000000..1469f89d87
--- /dev/null
+++ b/packages/utils/src/functions/regex-escape.ts
@@ -0,0 +1,7 @@
+/**
+ * Escapes a string for use in a regular expression.
+ * https://stackoverflow.com/a/6969486
+ */
+export function regexEscape(str: string) {
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+}