The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Use dub_pk_test_ prefix for publishable keys in staging and sandbox
Kiran K7ff580923381
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/route.tsindex c39fb068da..7dd8e228f1 100644--- a/apps/web/app/api/workspaces/[idOrSlug]/route.ts+++ b/apps/web/app/api/workspaces/[idOrSlug]/route.ts@@ -11,6 +11,10 @@ import { getFeatureFlags } from "@/lib/edge-config";import { jackson } from "@/lib/jackson";import { syncStagingWorkspaceJob } from "@/lib/jobs/handlers/sync-staging-workspace-job";import { prisma } from "@/lib/prisma";+import {+ getPublishableKeyPrefix,+ isValidPublishableKeyForEnvironment,+} from "@/lib/sandbox/environment";import { assertNotStagingWorkspace } from "@/lib/sandbox/workspace-guards";import { mergeSiteVisitTrackingSettings } from "@/lib/sitemaps/site-visit-tracking";import { storage } from "@/lib/storage";@@ -105,6 +109,23 @@ export const PATCH = withWorkspace(when: !!(name || slug || logo),});+ const isNewPublishableKey =+ !!publishableKey && publishableKey !== workspace.publishableKey;++ const matchesEnvironment =+ !!publishableKey &&+ isValidPublishableKeyForEnvironment(+ publishableKey,+ workspace.environment,+ );++ if (isNewPublishableKey && !matchesEnvironment) {+ throw new DubApiError({+ code: "bad_request",+ message: `Publishable keys for ${workspace.environment} workspaces must start with "${getPublishableKeyPrefix(workspace.environment)}".`,+ });+ }+if (["free", "pro"].includes(workspace.plan) && conversionEnabled) {throw new DubApiError({code: "forbidden",diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tracking/publishable-key-field.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tracking/publishable-key-field.tsxindex e10e811f89..8cff06f1b5 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tracking/publishable-key-field.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tracking/publishable-key-field.tsx@@ -1,5 +1,7 @@"use client";+import { getPublishableKeyPrefix } from "@/lib/sandbox/environment";+import useWorkspace from "@/lib/swr/use-workspace";import { useConfirmModal } from "@/ui/modals/confirm-modal";import { Button, Copy, Key, Tick, Tooltip, useCopyToClipboard } from "@dub/ui";import { nanoid } from "@dub/utils";@@ -20,9 +22,11 @@ export function PublishableKeyField({disabled?: boolean;disabledTooltip?: string;}) {+ const { environment } = useWorkspace();const [copied, copyToClipboard] = useCopyToClipboard();- const generateKey = () => `dub_pk_${nanoid(24)}`;+ const generateKey = () =>+ `${getPublishableKeyPrefix(environment)}${nanoid(24)}`;const {setShowConfirmModal: setShowGenerateModal,diff --git a/apps/web/lib/sandbox/environment.ts b/apps/web/lib/sandbox/environment.tsindex 00d55878f4..df407d64bf 100644--- a/apps/web/lib/sandbox/environment.ts+++ b/apps/web/lib/sandbox/environment.ts@@ -16,3 +16,18 @@ export function isProductionEnvironment(environment && environment === WorkspaceEnvironment.production,);}++export function getPublishableKeyPrefix(+ environment: WorkspaceEnvironment | null | undefined,+) {+ return isProductionEnvironment(environment) ? "dub_pk_" : "dub_pk_test_";+}++export function isValidPublishableKeyForEnvironment(+ publishableKey: string,+ environment: WorkspaceEnvironment | null | undefined,+) {+ const isTestKey = publishableKey.startsWith("dub_pk_test_");++ return isProductionEnvironment(environment) ? !isTestKey : isTestKey;+}