The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Limit unverified OAuth app installs to the developer or owning workspace
Kiran K4d831ffd8efd
diff --git a/apps/web/app/api/oauth/authorize/route.ts b/apps/web/app/api/oauth/authorize/route.tsindex c03fd02560..2f01f1125b 100644--- a/apps/web/app/api/oauth/authorize/route.ts+++ b/apps/web/app/api/oauth/authorize/route.ts@@ -1,4 +1,8 @@import { DubApiError } from "@/lib/api/errors";+import {+ canInstallOAuthApp,+ UNVERIFIED_APP_INSTALL_MESSAGE,+} from "@/lib/api/oauth/can-install-oauth-app";import { OAUTH_CONFIG } from "@/lib/api/oauth/constants";import { createToken } from "@/lib/api/oauth/utils";import { consolidateScopes, getScopesForRole } from "@/lib/api/tokens/scopes";@@ -42,13 +46,27 @@ export const POST = withWorkspace(select: {redirectUris: true,pkce: true,- integrationId: true,+ integration: {+ select: {+ id: true,+ verified: true,+ projectId: true,+ userId: true,+ },+ },},});+ if (!app.integration) {+ throw new DubApiError({+ code: "not_found",+ message: "Could not find OAuth application.",+ });+ }+if ([STRIPE_INTEGRATION_ID, SHOPIFY_INTEGRATION_ID].includes(- app.integrationId,+ app.integration.id,) &&(workspace.plan === "free" || workspace.plan === "pro")) {@@ -76,6 +94,19 @@ export const POST = withWorkspace(});}+ if (+ !canInstallOAuthApp({+ integration: app.integration,+ workspace,+ userId: session.user.id,+ })+ ) {+ throw new DubApiError({+ code: "forbidden",+ message: UNVERIFIED_APP_INSTALL_MESSAGE,+ });+ }+const { code } = await prisma.oAuthCode.create({data: {clientId,diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsxindex 788dc74e7b..d4a2658c5b 100644--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx@@ -1,16 +1,26 @@"use client";+import {+ canInstallOAuthApp,+ UNVERIFIED_APP_INSTALL_MESSAGE,+} from "@/lib/api/oauth/can-install-oauth-app";import { consolidateScopes, getScopesForRole } from "@/lib/api/tokens/scopes";import { clientAccessCheck } from "@/lib/client-access-check";import useWorkspaces from "@/lib/swr/use-workspaces";+import { WorkspaceProps } from "@/lib/types";import { authorizeRequestSchema } from "@/lib/zod/schemas/oauth";import { WorkspaceSelector } from "@/ui/workspaces/workspace-selector";import { Button } from "@dub/ui";+import { Integration } from "@prisma/client";import { useSession } from "next-auth/react";-import { useEffect, useMemo, useState } from "react";+import { useCallback, useEffect, useMemo, useState } from "react";import { toast } from "sonner";import * as z from "zod/v4";+interface AuthorizeFormProps extends z.infer<typeof authorizeRequestSchema> {+ integration: Pick<Integration, "verified" | "projectId" | "userId">;+}+export const AuthorizeForm = ({client_id,redirect_uri,@@ -19,7 +29,8 @@ export const AuthorizeForm = ({scope,code_challenge,code_challenge_method,-}: z.infer<typeof authorizeRequestSchema>) => {+ integration,+}: AuthorizeFormProps) => {const { data: session } = useSession();const { workspaces, loading: workspacesLoading } = useWorkspaces();const [submitting, setSubmitting] = useState(false);@@ -27,59 +38,99 @@ export const AuthorizeForm = ({null,);- useEffect(() => {- setSelectedWorkspace(session?.user?.["defaultWorkspace"] || null);- }, [session]);-- const { permissionsError, missingScopes, workspaceUnresolvedMessage } =- useMemo(() => {- if (!selectedWorkspace) {- return {- permissionsError: undefined,- missingScopes: [] as string[],- workspaceUnresolvedMessage: undefined,- };- }+ const userId = session?.user?.id;- if (workspacesLoading || workspaces === undefined) {- return {- permissionsError: undefined,- missingScopes: [] as string[],- workspaceUnresolvedMessage: "Loading workspaces...",- };+ const isWorkspaceAllowed = useCallback(+ (workspace: WorkspaceProps) => {+ if (!userId) {+ return false;}- const workspace = workspaces.find(- (workspace) => workspace.slug === selectedWorkspace,- );+ return canInstallOAuthApp({+ integration,+ workspace,+ userId,+ });+ },+ [integration, userId],+ );- if (!workspace) {- return {- permissionsError: undefined,- missingScopes: [] as string[],- workspaceUnresolvedMessage: "Please select a valid workspace",- };- }+ useEffect(() => {+ if (!workspaces || workspacesLoading || !userId || selectedWorkspace) {+ return;+ }- const userRole = workspace.users[0].role;+ const defaultSlug = session?.user?.["defaultWorkspace"] || null;+ const defaultWorkspace = defaultSlug+ ? workspaces.find((workspace) => workspace.slug === defaultSlug)+ : undefined;- const permissionsError = clientAccessCheck({- action: "integrations.write",- role: userRole,- customPermissionDescription: "install this integration",- }).error;+ if (defaultWorkspace && isWorkspaceAllowed(defaultWorkspace)) {+ setSelectedWorkspace(defaultWorkspace.slug);+ return;+ }++ const firstAllowed = workspaces.find(isWorkspaceAllowed);+ setSelectedWorkspace(firstAllowed?.slug ?? defaultSlug);+ }, [+ workspaces,+ workspacesLoading,+ session,+ userId,+ selectedWorkspace,+ isWorkspaceAllowed,+ ]);++ const authorizeDisabledTooltip = useMemo((): string | undefined => {+ if (!selectedWorkspace) {+ return "Please select a workspace to continue";+ }++ if (workspacesLoading || workspaces === undefined) {+ return "Loading workspaces...";+ }- const scopesForRole = getScopesForRole(userRole);- const missingScopes = consolidateScopes(scope).filter(- (scope) => !scopesForRole.includes(scope) && scope !== "user.read",- );+ const workspace = workspaces.find(+ (workspace) => workspace.slug === selectedWorkspace,+ );++ if (!workspace) {+ return "Please select a valid workspace";+ }++ if (!isWorkspaceAllowed(workspace)) {+ return UNVERIFIED_APP_INSTALL_MESSAGE;+ }++ const userRole = workspace.users[0].role;++ const permissionsError = clientAccessCheck({+ action: "integrations.write",+ role: userRole,+ customPermissionDescription: "install this integration",+ }).error;++ if (typeof permissionsError === "string") {+ return permissionsError;+ }++ const missingScopes = consolidateScopes(scope).filter(+ (scope) =>+ !getScopesForRole(userRole).includes(scope) && scope !== "user.read",+ );++ if (missingScopes.length > 0) {+ return "You don't have the permission to install this integration";+ }- return {- permissionsError,- missingScopes,- workspaceUnresolvedMessage: undefined,- };- }, [workspaces, workspacesLoading, selectedWorkspace, scope]);+ return undefined;+ }, [+ workspaces,+ workspacesLoading,+ selectedWorkspace,+ scope,+ isWorkspaceAllowed,+ ]);// Decline the requestconst onDecline = () => {@@ -168,21 +219,7 @@ export const AuthorizeForm = ({text="Authorize"type="submit"loading={submitting}- disabled={- !selectedWorkspace ||- !!workspaceUnresolvedMessage ||- !!permissionsError ||- missingScopes.length > 0- }- disabledTooltip={- !selectedWorkspace- ? "Please select a workspace to continue"- : workspaceUnresolvedMessage ||- permissionsError ||- (missingScopes.length > 0- ? "You don't have the permission to install this integration"- : undefined)- }+ disabledTooltip={authorizeDisabledTooltip}/></div></form>diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/loading.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/loading.tsxindex 6b75cb9fff..effda57cfa 100644--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/loading.tsx+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/loading.tsx@@ -3,8 +3,8 @@ import { ArrowLeftRight } from "lucide-react";export default function AuthorizeLoading() {return (- <div className="relative z-10 m-auto w-full max-w-md border-y border-neutral-200 sm:rounded-2xl sm:border sm:shadow-xl">- <div className="flex flex-col items-center justify-center gap-3 border-b border-neutral-200 bg-white px-4 py-6 pt-8 text-center sm:rounded-t-2xl sm:px-16">+ <div className="relative z-10 mx-4 my-auto w-full max-w-md rounded-2xl border border-neutral-200 bg-white shadow-xl sm:mx-auto">+ <div className="flex flex-col items-center justify-center gap-3 border-b border-neutral-200 px-4 py-6 pt-8 text-center sm:rounded-t-2xl sm:px-16"><div className="flex items-center gap-3"><div className="size-12 rounded-full border border-neutral-200 bg-gradient-to-t from-neutral-100" /><ArrowLeftRight className="size-5 text-neutral-300" />@@ -16,7 +16,7 @@ export default function AuthorizeLoading() {</div><div className="h-4 w-1/3 animate-pulse rounded bg-neutral-200" /></div>- <div className="flex flex-col space-y-3 bg-white px-2 py-6 sm:px-10">+ <div className="flex flex-col space-y-3 px-4 py-6 sm:px-10"><div className="h-4 w-1/3 animate-pulse rounded bg-neutral-200" /><div className="h-4 w-3/4 animate-pulse rounded bg-neutral-200" /><div className="h-4 w-3/4 animate-pulse rounded bg-neutral-200" />@@ -24,7 +24,7 @@ export default function AuthorizeLoading() {<div className="h-4 w-3/4 animate-pulse rounded bg-neutral-200" /><div className="h-4 w-full animate-pulse rounded bg-neutral-200" /></div>- <div className="flex flex-col space-y-2 border-t border-neutral-200 bg-white px-2 py-6 sm:rounded-b-2xl sm:px-10">+ <div className="flex flex-col space-y-2 border-t border-neutral-200 px-4 py-6 sm:rounded-b-2xl sm:px-10"><div className="h-4 w-1/2 animate-pulse rounded bg-neutral-200" /><div className="h-10 w-full animate-pulse rounded bg-neutral-200" /><div className="flex gap-4">diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/page.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/page.tsxindex 29e4913ce9..636bb8c1a3 100644--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/page.tsx+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/page.tsx@@ -42,8 +42,8 @@ export default async function Authorize(props: {}return (- <div className="relative z-10 m-auto w-full max-w-md border-y border-neutral-200 sm:rounded-2xl sm:border sm:shadow-xl">- <div className="flex flex-col items-center justify-center gap-3 border-b border-neutral-200 bg-white px-4 py-6 pt-8 text-center sm:rounded-t-2xl sm:px-16">+ <div className="relative z-10 mx-4 my-auto w-full max-w-md rounded-2xl border border-neutral-200 bg-white shadow-xl sm:mx-auto">+ <div className="flex flex-col items-center justify-center gap-3 border-b border-neutral-200 px-4 py-6 pt-8 text-center sm:rounded-t-2xl sm:px-16"><div className="flex items-center gap-3"><a href={integration.website} target="_blank" rel="noreferrer">{integration.logo ? (@@ -81,18 +81,24 @@ export default async function Authorize(props: {</span>{!integration.verified && (- <div className="flex items-center gap-2 rounded-md bg-yellow-50 p-2 text-sm text-yellow-700">- <CircleWarning className="size-4" />- <span>Dub hasn't verified this app</span>+ <div className="flex w-full items-start gap-2.5 rounded-lg border border-amber-200 bg-amber-50 p-3 text-left sm:-mx-6 sm:w-auto sm:self-stretch">+ <CircleWarning className="mt-0.5 size-4 shrink-0 text-amber-600" />+ <div className="flex flex-col gap-0.5 text-sm text-amber-900">+ <p className="font-medium">Dub hasn't verified this app</p>+ <p className="text-amber-800">+ It can only be installed by its developer or on the developer's+ workspace.+ </p>+ </div></div>)}</div>- <div className="flex flex-col space-y-3 bg-white px-2 py-6 sm:px-10">+ <div className="flex flex-col space-y-3 px-4 py-6 sm:px-10"><ScopesRequested scopes={requestParams.scope} /></div>- <div className="flex flex-col space-y-2 border-t border-neutral-200 bg-white px-2 py-6 sm:rounded-b-2xl sm:px-10">+ <div className="flex flex-col space-y-2 border-t border-neutral-200 px-4 py-6 sm:rounded-b-2xl sm:px-10"><Suspense>- <AuthorizeForm {...requestParams} />+ <AuthorizeForm {...requestParams} integration={integration} /></Suspense></div></div>diff --git a/apps/web/lib/api/oauth/actions.ts b/apps/web/lib/api/oauth/actions.tsindex 377270472f..acb68a974e 100644--- a/apps/web/lib/api/oauth/actions.ts+++ b/apps/web/lib/api/oauth/actions.ts@@ -30,6 +30,8 @@ export const validateAuthorizeRequest = async (params: any) => {website: true,logo: true,verified: true,+ projectId: true,+ userId: true,},},},diff --git a/apps/web/lib/api/oauth/can-install-oauth-app.ts b/apps/web/lib/api/oauth/can-install-oauth-app.tsnew file mode 100644index 0000000000..4f7ad44489--- /dev/null+++ b/apps/web/lib/api/oauth/can-install-oauth-app.ts@@ -0,0 +1,29 @@+import { normalizeWorkspaceId } from "@/lib/api/workspaces/workspace-id";+import { WorkspaceProps } from "@/lib/types";+import { Integration } from "@prisma/client";++export const UNVERIFIED_APP_INSTALL_MESSAGE =+ "Dub hasn't verified this app. It can only be installed by its developer or on the developer's workspace.";++export const canInstallOAuthApp = ({+ integration,+ workspace,+ userId,+}: {+ integration: Pick<Integration, "verified" | "projectId" | "userId">;+ workspace: Pick<WorkspaceProps, "id">;+ userId: string;+}): boolean => {+ if (integration.verified) {+ return true;+ }++ if (integration.userId && integration.userId === userId) {+ return true;+ }++ return (+ normalizeWorkspaceId(workspace.id) ===+ normalizeWorkspaceId(integration.projectId)+ );+};