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 validatePartnerLinkUrl

Steven Teyafefae5a782c
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-group-additional-link-modal.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-group-additional-link-modal.tsx
index c8818c6b23..4d6a1faaa3 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-group-additional-link-modal.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/add-edit-group-additional-link-modal.tsx
@@ -5,7 +5,7 @@ import { useApiMutation } from "@/lib/swr/use-api-mutation";
import useGroup from "@/lib/swr/use-group";
import { PartnerGroupAdditionalLink } from "@/lib/types";
import { MAX_ADDITIONAL_PARTNER_LINKS } from "@/lib/zod/schemas/groups";
-import { Button, Input, Modal } from "@dub/ui";
+import { Badge, Button, Input, Modal } from "@dub/ui";
import { CircleCheckFill } from "@dub/ui/icons";
import { cn } from "@dub/utils";
import { Dispatch, SetStateAction, useState } from "react";
@@ -16,12 +16,13 @@ const URL_VALIDATION_MODES = [
{
value: "domain",
label: "Any page",
- description: "Allows links to any page under this domain",
+ description: "Allows links to any page on this domain",
+ recommended: true,
},
{
value: "exact",
label: "Single page",
- description: "Restricts links to this single destination URL",
+ description: "Restricts links to the homepage only",
},
];
@@ -157,8 +158,7 @@ function AddDestinationUrlModalContent({
key={type.value}
className={cn(
"relative flex w-full cursor-pointer items-start gap-0.5 rounded-md border border-neutral-200 bg-white p-3 text-neutral-600",
- "hover:bg-neutral-50",
- "transition-all duration-150",
+ "transition-all duration-150 hover:bg-neutral-50",
isSelected &&
"border-black bg-neutral-50 text-neutral-900 ring-1 ring-black",
)}
@@ -170,19 +170,24 @@ function AddDestinationUrlModalContent({
{...register("validationMode")}
/>
- <div className="flex grow flex-col text-sm">
+ <div className="flex grow flex-col whitespace-nowrap text-sm">
<span className="font-medium">{type.label}</span>
<span className="text-neutral-600">
{type.description}
</span>
</div>
- <CircleCheckFill
- className={cn(
- "-mr-px -mt-px flex size-4 scale-75 items-center justify-center rounded-full opacity-0 transition-[transform,opacity] duration-150",
- isSelected && "scale-100 opacity-100",
+ <div className="flex items-center justify-end gap-1">
+ {type.recommended && (
+ <Badge variant="blueGradient">Recommended</Badge>
)}
- />
+ <CircleCheckFill
+ className={cn(
+ "-mr-px -mt-px flex size-4 scale-75 items-center justify-center rounded-full opacity-0 transition-[transform,opacity] duration-150",
+ isSelected && "scale-100 opacity-100",
+ )}
+ />
+ </div>
</label>
);
})}
diff --git a/apps/web/lib/api/links/validate-partner-link-url.ts b/apps/web/lib/api/links/validate-partner-link-url.ts
index 6e9abfa545..375dbde52d 100644
--- a/apps/web/lib/api/links/validate-partner-link-url.ts
+++ b/apps/web/lib/api/links/validate-partner-link-url.ts
@@ -1,5 +1,5 @@
import { PartnerGroupAdditionalLink } from "@/lib/types";
-import { getApexDomain } from "@dub/utils";
+import { getUrlObjFromString } from "@dub/utils";
import { PartnerGroup } from "@prisma/client";
import { DubApiError } from "../errors";
@@ -23,37 +23,38 @@ export const validatePartnerLinkUrl = ({
const additionalLinks = group.additionalLinks as PartnerGroupAdditionalLink[];
- const domain = getApexDomain(url);
+ if (!additionalLinks) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "You cannot create additional links for this program.",
+ });
+ }
+
+ const { hostname: urlHostname, pathname: urlPathname } =
+ getUrlObjFromString(url) ?? {};
// Find matching additional link based on its domain
const additionalLink = additionalLinks.find((additionalLink) => {
- return additionalLink.domain === domain;
+ return additionalLink.domain === urlHostname;
});
if (!additionalLink) {
throw new DubApiError({
code: "bad_request",
- message: `The provided URL (${url}) does not match any of the program's additional link domains.`,
+ message: `The provided URL's domain (${urlHostname}) does not match the program's link domains.`,
});
}
// Check the validation mode
- if (additionalLink.validationMode === "exact") {
- if (getApexDomain(url) !== additionalLink.domain) {
- throw new DubApiError({
- code: "bad_request",
- message: `The provided URL (${url}) does not match the program's URL (${additionalLink.domain}).`,
- });
- }
- }
-
- if (additionalLink.validationMode === "domain") {
- if (domain !== additionalLink.domain) {
- throw new DubApiError({
- code: "bad_request",
- message: `The provided URL domain (${domain}) does not match the program's domain (${additionalLink.domain}).`,
- });
- }
+ if (
+ additionalLink.validationMode === "exact" &&
+ urlPathname &&
+ urlPathname.slice(1).length > 0
+ ) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: `The provided URL is not an exact match for the program's link domain (${additionalLink.domain}).`,
+ });
}
return true;
diff --git a/apps/web/ui/modals/partner-link-modal.tsx b/apps/web/ui/modals/partner-link-modal.tsx
index e47b9e32c3..315f21855c 100644
--- a/apps/web/ui/modals/partner-link-modal.tsx
+++ b/apps/web/ui/modals/partner-link-modal.tsx
@@ -25,7 +25,7 @@ import {
} from "@dub/ui/icons";
import {
cn,
- getApexDomain,
+ getDomainWithoutWWW,
getPathnameFromUrl,
// getPathnameFromUrl,
linkConstructor,
@@ -192,7 +192,9 @@ function PartnerLinkModalContent({
);
const [destinationDomain, setDestinationDomain] = useState(
- link ? getApexDomain(link.url) : destinationDomains?.[0] ?? null,
+ link
+ ? (getDomainWithoutWWW(link.url) as string)
+ : destinationDomains?.[0] ?? null,
);
useEffect(() => {