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.

guard against removing machine user with active tokens

Steven Tey0be359b745f3
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
index 1396391208..0ab9334632 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/users/route.ts
@@ -8,6 +8,7 @@ import {
getWorkspaceUsersQuerySchema,
workspaceUserSchema,
} from "@/lib/zod/schemas/workspaces";
+import { pluralize } from "@dub/utils";
import { WorkspaceRole } from "@prisma/client";
import { NextResponse } from "next/server";
import * as z from "zod/v4";
@@ -151,6 +152,24 @@ export const DELETE = withWorkspace(
});
}
+ const activeRestrictedTokens = await prisma.restrictedToken.count({
+ where: {
+ projectId: workspace.id,
+ userId,
+ lastUsed: {
+ not: null,
+ },
+ },
+ });
+
+ if (activeRestrictedTokens) {
+ const tokenTense = pluralize("token", activeRestrictedTokens);
+ throw new DubApiError({
+ code: "bad_request",
+ message: `This user has ${activeRestrictedTokens} active restricted ${tokenTense}. Please remove the ${tokenTense} first before removing the user from the workspace.`,
+ });
+ }
+
const [response] = await Promise.allSettled([
// Remove the user from the workspace
prisma.projectUsers.delete({
diff --git a/apps/web/lib/is-generic-email.ts b/apps/web/lib/is-generic-email.ts
index a0e52199b0..32e9f4ac4e 100644
--- a/apps/web/lib/is-generic-email.ts
+++ b/apps/web/lib/is-generic-email.ts
@@ -69,6 +69,7 @@ const GENERIC_EMAIL_DOMAINS = [
"hi2.in",
"tempmailplus.net",
"linuxmail.org",
+ "jk8h.info",
];
const GENERIC_EMAIL_DOMAIN_PREFIXES = [
diff --git a/apps/web/ui/modals/delete-token-modal.tsx b/apps/web/ui/modals/delete-token-modal.tsx
index 0afa81d22f..88eb95a947 100644
--- a/apps/web/ui/modals/delete-token-modal.tsx
+++ b/apps/web/ui/modals/delete-token-modal.tsx
@@ -1,6 +1,8 @@
import useWorkspace from "@/lib/swr/use-workspace";
import { TokenProps } from "@/lib/types";
import { Button, Key, Modal, Tooltip, useMediaQuery } from "@dub/ui";
+import { TriangleWarning } from "@dub/ui/icons";
+import { timeAgo } from "@dub/utils";
import {
Dispatch,
SetStateAction,
@@ -71,10 +73,12 @@ function DeleteTokenModal({
>
<div className="space-y-2 border-b border-neutral-200 px-4 py-4 sm:px-6">
<h3 className="text-lg font-medium">Delete API Key</h3>
- <p className="text-sm text-neutral-500">
- This will permanently delete the API key for and revoke all access to
- your account. Are you sure you want to continue?
- </p>
+ {!token.lastUsed && (
+ <p className="text-sm text-neutral-500">
+ This will permanently delete the API key and revoke all access to
+ your account. Are you sure you want to continue?
+ </p>
+ )}
</div>
<form
@@ -84,6 +88,23 @@ function DeleteTokenModal({
}}
className="flex flex-col space-y-4 bg-neutral-50 px-4 py-4 sm:px-6"
>
+ {token.lastUsed && (
+ <div className="flex items-start gap-3 rounded-lg border border-amber-200 bg-amber-50 p-4">
+ <TriangleWarning className="mt-0.5 size-4 shrink-0 text-amber-600" />
+ <div className="space-y-1">
+ <h3 className="text-sm font-semibold text-amber-900">
+ This key is actively in use (last used{" "}
+ {timeAgo(token.lastUsed, { withAgo: true })})
+ </h3>
+ <p className="text-sm text-amber-900">
+ Deleting this key will immediately break any integrations,
+ tracking, or API requests that depend on it. Are you sure you
+ want to continue?
+ </p>
+ </div>
+ </div>
+ )}
+
<div className="relative flex items-center gap-2 space-x-3 rounded-md border border-neutral-300 bg-white px-4 py-2">
<Key className="size-5 text-neutral-500" />
diff --git a/apps/web/ui/modals/remove-workspace-user-modal.tsx b/apps/web/ui/modals/remove-workspace-user-modal.tsx
index 1103a8eace..a6960c2527 100644
--- a/apps/web/ui/modals/remove-workspace-user-modal.tsx
+++ b/apps/web/ui/modals/remove-workspace-user-modal.tsx
@@ -32,7 +32,7 @@ function RemoveWorkspaceUserModal({
const { data: session } = useSession();
const [removing, setRemoving] = useState(false);
const [verification, setVerification] = useState("");
- const { id: workspaceId, name: workspaceName } = useWorkspace();
+ const { id: workspaceId, name: workspaceName, slug } = useWorkspace();
const searchParams = useSearchParams();
const isInvite = searchParams.get("status") === "invited";
@@ -110,10 +110,14 @@ function RemoveWorkspaceUserModal({
<ul className="mt-6 space-y-2">
{restrictedTokens.map((token, index) => (
<li key={index} className="flex items-center justify-between">
- <span className="text-sm font-medium leading-4 text-amber-900">
+ <a
+ href={`/${slug}/settings/tokens`}
+ target="_blank"
+ className="line-clamp-1 text-sm font-medium leading-4 text-amber-900 underline underline-offset-2 hover:text-amber-700"
+ >
{token.name}
- </span>
- <span className="text-xs font-normal leading-4 text-amber-700">
+ </a>
+ <span className="whitespace-nowrap text-xs font-normal leading-4 text-amber-700">
used {timeAgo(token.lastUsed, { withAgo: true })}
</span>
</li>