Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Feat: Add UI for displaying and unbanning IP addresses (#17782)

Monica Khoury65afbccdeff6
diff --git a/studio/components/interfaces/Settings/Database/BannedIPs.tsx b/studio/components/interfaces/Settings/Database/BannedIPs.tsx
new file mode 100644
index 00000000000..a1a3ecfe9af
--- /dev/null
+++ b/studio/components/interfaces/Settings/Database/BannedIPs.tsx
@@ -0,0 +1,129 @@
+import Link from 'next/link'
+import { useState } from 'react'
+import { useParams } from 'common/hooks'
+import { useStore } from 'hooks'
+import {
+ FormHeader,
+ FormPanel,
+} from 'components/ui/Forms'
+import {
+ AlertDescription_Shadcn_,
+ AlertTitle_Shadcn_,
+ Alert_Shadcn_,
+ Button,
+ IconAlertTriangle,
+ IconExternalLink,
+ IconGlobe,
+ Modal,
+} from 'ui'
+import ConfirmationModal from 'components/ui/ConfirmationModal'
+
+import { useBannedIPsQuery } from 'data/banned-ips/banned-ips-query'
+import { useBannedIPsDeleteMutation } from 'data/banned-ips/banned-ips-delete-mutations'
+
+const BannedIPs = () => {
+ const { ref } = useParams()
+ const [selectedIPToUnban, setSelectedIPToUnban] = useState<string | null>(null) // Track the selected IP for unban
+ const {
+ data: ipList
+ } = useBannedIPsQuery({
+ projectRef: ref,
+ })
+
+ const { ui } = useStore();
+ const [showUnban, setShowUnban] = useState(false)
+ const [confirmingIP, setConfirmingIP] = useState<string | null>(null) // Track the IP being confirmed for unban
+
+ const { mutate: unbanIPs, isLoading: isUnbanning } = useBannedIPsDeleteMutation({
+ onSuccess: () => {
+ ui.setNotification({
+ category: 'success',
+ message: 'IP address successfully unbanned'
+ });
+ setSelectedIPToUnban(null); // Reset the selected IP for unban
+ setShowUnban(false)
+ },
+ onError: (error) => {
+ ui.setNotification({
+ category: 'error',
+ message: `Failed to unban IP: ${error?.message}`,
+ });
+ },
+ });
+
+ const onConfirmUnbanIP = () => {
+ if (confirmingIP == null || !ref) return;
+ unbanIPs({
+ projectRef: ref,
+ ips: [confirmingIP], // Pass the IP as an array
+ });
+ }
+
+ const openConfirmationModal = (ip: string) => {
+ setSelectedIPToUnban(ip); // Set the selected IP for unban
+ setConfirmingIP(ip); // Set the IP being confirmed for unban
+ setShowUnban(true);
+ }
+
+ return (
+ <div>
+ <div className="flex items-center justify-between">
+ <FormHeader title="Banned IPs" description="We monitor unsuccessful logins and block IPs for security" />
+ <div className="flex items-center space-x-2 mb-6">
+ <Link href="https://supabase.com/docs/reference/cli/supabase-network-bans">
+ <a target="_blank">
+ <Button type="default" icon={<IconExternalLink />}>
+ Documentation
+ </Button>
+ </a>
+ </Link>
+ </div>
+ </div>
+ <FormPanel>
+ {ipList && ipList.banned_ipv4_addresses.length > 0 ? (
+ ipList.banned_ipv4_addresses.map((ip) => (
+ <div key={ip} className="px-8 py-4 flex items-center justify-between">
+ <div className="flex items-center space-x-5">
+ <IconGlobe size={16} className="text-scale-1000" />
+ <p className="text-sm font-mono">{ip}</p>
+ </div>
+ <div>
+ <Button type="default" onClick={() => openConfirmationModal(ip)}>
+ Unban IP
+ </Button>
+ </div>
+ </div>
+ ))
+ ) : (
+ <p className="text-scale-1100 text-sm px-8 py-4">There are no banned IP addresses for your project.</p>
+ )}
+ </FormPanel>
+
+ <ConfirmationModal
+ danger
+ size="medium"
+ loading={isUnbanning}
+ visible={showUnban}
+ header="Confirm Unban IP"
+ buttonLabel="Confirm Unban"
+ buttonLoadingLabel="Unbanning..."
+ onSelectConfirm={onConfirmUnbanIP}
+ onSelectCancel={() => setShowUnban(false)}
+ >
+ <Modal.Content>
+ <div className="py-6">
+ <Alert_Shadcn_ variant="warning">
+ <IconAlertTriangle strokeWidth={2} />
+ <AlertTitle_Shadcn_>This action cannot be undone</AlertTitle_Shadcn_>
+ <AlertDescription_Shadcn_>
+ Are you sure you want to unban this IP address {selectedIPToUnban}?
+ </AlertDescription_Shadcn_>
+ </Alert_Shadcn_>
+ </div>
+ </Modal.Content>
+ </ConfirmationModal>
+ </div>
+ );
+}
+
+export default BannedIPs
diff --git a/studio/data/banned-ips/banned-ips-delete-mutations.ts b/studio/data/banned-ips/banned-ips-delete-mutations.ts
new file mode 100644
index 00000000000..2ec68c10392
--- /dev/null
+++ b/studio/data/banned-ips/banned-ips-delete-mutations.ts
@@ -0,0 +1,60 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { del } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { BannedIPKeys } from './keys'
+
+import toast from 'react-hot-toast'
+
+export type IPDeleteVariables = {
+ projectRef: string
+ /** can only be one for now */
+ ips: string[]
+}
+
+export async function deleteBannedIPs({ projectRef, ips }: IPDeleteVariables) {
+ const { data, error } = await del(`/v1/projects/{ref}/network-bans`, {
+ params: {
+ path: { ref: projectRef }
+ },
+ body: { ipv4_addresses: ips }
+ })
+
+ if (error) throw error
+ return data
+}
+
+type IPDeleteData = Awaited<ReturnType<typeof deleteBannedIPs>>
+
+export const useBannedIPsDeleteMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<IPDeleteData, ResponseError, IPDeleteVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+ return useMutation<IPDeleteData, ResponseError, IPDeleteVariables>(
+ (vars) => deleteBannedIPs(vars),
+ {
+ async onSuccess(data, variables, context) {
+ const { projectRef, ips } = variables
+
+ await Promise.all([
+ queryClient.invalidateQueries(BannedIPKeys.list(projectRef)),
+ queryClient.invalidateQueries(BannedIPKeys.detail(ips)),
+ ])
+
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to unban ips: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}
diff --git a/studio/data/banned-ips/banned-ips-query.ts b/studio/data/banned-ips/banned-ips-query.ts
new file mode 100644
index 00000000000..d9be83f92df
--- /dev/null
+++ b/studio/data/banned-ips/banned-ips-query.ts
@@ -0,0 +1,46 @@
+import { useQuery, UseQueryOptions } from '@tanstack/react-query'
+import { post } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { BannedIPKeys } from './keys'
+
+export type BannedIPVariables = {
+ projectRef?: string
+};
+
+export type BannedIPsData = BannedIPVariables[];
+export type BannedIPsError = ResponseError;
+
+export async function getBannedIPs({ projectRef }: BannedIPVariables, signal?: AbortSignal) {
+ if (!projectRef) throw new Error('projectRef is required')
+
+ const { data, error } = await post(`/v1/projects/{ref}/network-bans/retrieve`, {
+ params: {
+ path: { ref: projectRef }
+ },
+ signal,
+ })
+
+ if (error) {
+ throw error;
+ }
+
+ return data
+}
+
+export type IPData = Awaited<ReturnType<typeof getBannedIPs>>
+export type IPError = ResponseError
+
+export const useBannedIPsQuery = <TData = IPData>(
+ { projectRef }: BannedIPVariables,
+ { enabled = true, ...options }: UseQueryOptions<IPData, IPError, TData> = {}
+) =>
+ useQuery<IPData, IPError, TData>(
+ BannedIPKeys.list(projectRef),
+ ({ signal }) => getBannedIPs({ projectRef }, signal),
+ {
+ enabled: enabled && typeof projectRef !== 'undefined',
+ ...options,
+ }
+ )
+
+
diff --git a/studio/data/banned-ips/keys.ts b/studio/data/banned-ips/keys.ts
new file mode 100644
index 00000000000..c92262078d6
--- /dev/null
+++ b/studio/data/banned-ips/keys.ts
@@ -0,0 +1,4 @@
+export const BannedIPKeys = {
+ list: (projectRef: string | undefined) => ['projects', projectRef, 'banned-ips'] as const,
+ detail: (ips: string[] | undefined) => ['banned-ips', ips] as const,
+ }
\ No newline at end of file
diff --git a/studio/pages/project/[ref]/settings/database.tsx b/studio/pages/project/[ref]/settings/database.tsx
index 84e17d414df..9b9a4d3446c 100644
--- a/studio/pages/project/[ref]/settings/database.tsx
+++ b/studio/pages/project/[ref]/settings/database.tsx
@@ -9,6 +9,7 @@ import {
import SSLConfiguration from 'components/interfaces/Settings/Database/SSLConfiguration'
import DiskSizeConfiguration from 'components/interfaces/Settings/Database/DiskSizeConfiguration'
+import BannedIPs from 'components/interfaces/Settings/Database/BannedIPs'
const ProjectSettings: NextPageWithLayout = () => {
return (
@@ -17,6 +18,7 @@ const ProjectSettings: NextPageWithLayout = () => {
<DatabaseSettings />
<ConnectionPooling />
<SSLConfiguration />
+ <BannedIPs />
<DiskSizeConfiguration />
<NetworkRestrictions />
</div>