The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
sanitize social media handles
Kiran K6638a33f3897
diff --git a/apps/web/lib/actions/partners/update-online-presence.ts b/apps/web/lib/actions/partners/update-online-presence.tsindex 6ba78669bf..ec1235a66c 100644--- a/apps/web/lib/actions/partners/update-online-presence.ts+++ b/apps/web/lib/actions/partners/update-online-presence.ts@@ -4,6 +4,7 @@ import {generateCodeChallengeHash,generateCodeVerifier,} from "@/lib/api/oauth/utils";+import { sanitizeHandle } from "@/lib/social-utils";import { parseUrlSchemaAllowEmpty } from "@/lib/zod/schemas/utils";import { prisma } from "@dub/prisma";import { isValidUrl, PARTNERS_DOMAIN_WITH_NGROK } from "@dub/utils";@@ -15,11 +16,26 @@ import { ONLINE_PRESENCE_PROVIDERS } from "./online-presence-providers";const updateOnlinePresenceSchema = z.object({website: parseUrlSchemaAllowEmpty().nullish(),- youtube: z.string().nullish(),- twitter: z.string().nullish(),- linkedin: z.string().nullish(),- instagram: z.string().nullish(),- tiktok: z.string().nullish(),+ youtube: z+ .string()+ .nullish()+ .transform((input) => sanitizeHandle(input, "youtube")),+ twitter: z+ .string()+ .nullish()+ .transform((input) => sanitizeHandle(input, "twitter")),+ linkedin: z+ .string()+ .nullish()+ .transform((input) => sanitizeHandle(input, "linkedin")),+ instagram: z+ .string()+ .nullish()+ .transform((input) => sanitizeHandle(input, "instagram")),+ tiktok: z+ .string()+ .nullish()+ .transform((input) => sanitizeHandle(input, "tiktok")),source: z.enum(["onboarding", "settings"]).default("onboarding"),});diff --git a/apps/web/lib/social-utils.ts b/apps/web/lib/social-utils.tsnew file mode 100644index 0000000000..098ca7520c--- /dev/null+++ b/apps/web/lib/social-utils.ts@@ -0,0 +1,84 @@+export type SocialPlatform =+ | "youtube"+ | "twitter"+ | "linkedin"+ | "instagram"+ | "tiktok";++interface SocialPlatformConfig {+ patterns: RegExp[];+ allowedChars: RegExp;+ maxLength?: number;+}++const PLATFORM_CONFIGS: Record<SocialPlatform, SocialPlatformConfig> = {+ youtube: {+ patterns: [+ /^(?:.*\.)?(?:youtube\.com|youtu\.be)\/(?:channel\/|c\/|user\/|@)?([^\/\?]+)/i,+ /^@([^\/\?]+)/i,+ ],+ allowedChars: /[^\w.-]/g,+ maxLength: 30,+ },+ twitter: {+ patterns: [+ /^(?:.*\.)?(?:twitter\.com|x\.com)\/([^\/\?]+)/i,+ /^@([^\/\?]+)/i,+ ],+ allowedChars: /[^\w]/g,+ maxLength: 15,+ },+ linkedin: {+ patterns: [/^(?:.*\.)?linkedin\.com\/(?:in\/)?([^\/\?]+)/i],+ allowedChars: /[^\w-]/g,+ maxLength: 30,+ },+ instagram: {+ patterns: [/^(?:.*\.)?instagram\.com\/([^\/\?]+)/i, /^@([^\/\?]+)/i],+ allowedChars: /[^\w.]/g,+ maxLength: 30,+ },+ tiktok: {+ patterns: [/^(?:.*\.)?tiktok\.com\/(?:@)?([^\/\?]+)/i, /^@([^\/\?]+)/i],+ allowedChars: /[^\w.]/g,+ maxLength: 24,+ },+};++export const sanitizeHandle = (+ input: string | null | undefined,+ platform: SocialPlatform,+) => {+ if (!input || typeof input !== "string") {+ return null;+ }++ let handle = input.trim();+ if (!handle) {+ return null;+ }++ handle = handle.replace(/^https?:\/\//i, "").replace(/^www\./i, "");++ const { patterns, allowedChars, maxLength } = PLATFORM_CONFIGS[platform];++ for (const pattern of patterns) {+ const match = handle.match(pattern);++ if (match) {+ handle = match[1];+ break;+ }+ }++ handle = handle+ .replace(/\/.*$/, "")+ .replace(/\?.*$/, "")+ .replace(allowedChars, "");++ if (maxLength) {+ handle = handle.substring(0, maxLength);+ }++ return handle || null;+};diff --git a/apps/web/ui/partners/online-presence-form.tsx b/apps/web/ui/partners/online-presence-form.tsxindex da6a31f611..18f3c073ce 100644--- a/apps/web/ui/partners/online-presence-form.tsx+++ b/apps/web/ui/partners/online-presence-form.tsx@@ -2,6 +2,7 @@import { parseActionError } from "@/lib/actions/parse-action-errors";import { updateOnlinePresenceAction } from "@/lib/actions/partners/update-online-presence";+import { sanitizeHandle, SocialPlatform } from "@/lib/social-utils";import usePartnerProfile from "@/lib/swr/use-partner-profile";import { parseUrlSchemaAllowEmpty } from "@/lib/zod/schemas/utils";import { DomainVerificationModal } from "@/ui/modals/domain-verification-modal";@@ -73,6 +74,7 @@ export function OnlinePresenceForm({getValues,handleSubmit,reset,+ setValue,formState: { errors, isSubmitting, isSubmitSuccessful },} = form;@@ -98,6 +100,19 @@ export function OnlinePresenceForm({const startVerification = useOAuthVerification(variant);+ const onPasteSocial = useCallback(+ (e: React.ClipboardEvent<HTMLInputElement>, platform: SocialPlatform) => {+ const text = e.clipboardData.getData("text/plain");+ const sanitized = sanitizeHandle(text, platform);++ if (sanitized) {+ setValue(platform, sanitized);+ e.preventDefault();+ }+ },+ [setValue],+ );+return (<><DomainVerificationModal@@ -192,7 +207,7 @@ export function OnlinePresenceForm({: "border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:ring-neutral-500",)}placeholder="handle"- onPaste={onPasteSocial}+ onPaste={(e) => onPasteSocial(e, "youtube")}{...register("youtube")}/></div>@@ -226,7 +241,7 @@ export function OnlinePresenceForm({: "border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:ring-neutral-500",)}placeholder="handle"- onPaste={onPasteSocial}+ onPaste={(e) => onPasteSocial(e, "twitter")}{...register("twitter")}/></div>@@ -260,7 +275,7 @@ export function OnlinePresenceForm({: "border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:ring-neutral-500",)}placeholder="handle"- onPaste={onPasteSocial}+ onPaste={(e) => onPasteSocial(e, "linkedin")}{...register("linkedin")}/></div>@@ -295,7 +310,7 @@ export function OnlinePresenceForm({: "border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:ring-neutral-500",)}placeholder="handle"- onPaste={onPasteSocial}+ onPaste={(e) => onPasteSocial(e, "instagram")}{...register("instagram")}/></div>@@ -333,7 +348,7 @@ export function OnlinePresenceForm({: "border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:ring-neutral-500",)}placeholder="handle"- onPaste={onPasteSocial}+ onPaste={(e) => onPasteSocial(e, "tiktok")}{...register("tiktok")}/></div>@@ -502,11 +517,3 @@ function FormRow({</div>);}--const onPasteSocial = (e: React.ClipboardEvent<HTMLInputElement>) => {- e.preventDefault();-- // Extract the final portion of any URL- const text = e.clipboardData.getData("text/plain");- e.currentTarget.value = (text.split("/").at(-1) ?? text).replace(/^@/, "");-};