mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem A network restrictions change that was never applied can now wait for the customer's review ([API Docs](https://api.supabase.com/api/v1#tag/projects/POST/v1/projects/{ref}/network-restrictions/review), [CTRL-1934](https://linear.app/supabase/issue/CTRL-1934)). Studio doesn't show it. Lists with an entry that isn't a valid IP range also fail to save without saying which entry is wrong. ## Solution - A change in review shows one callout with **Apply change** and **Discard change**, and both lists together: added entries in green, removed ones in red. Editing is disabled until it's resolved, and discarding asks for confirmation. Only the change on screen is resolved (`pending_requested_at`). - A list the worker is still applying shows as applying and is polled, instead of showing the "not applied correctly" error. - Entries that aren't valid ranges are marked Invalid with a suggested correction, and can be saved corrected. The add dialog rejects IPv4-mapped IPv6 addresses and finds duplicates spelled differently. There's no way to create a review yet: a change only ends up in review when the backend fails to apply a list. I tested the review state locally by editing the database directly. Stacked on #53046, which syncs the API types with production, including the review endpoint. This PR will be retargeted to `master` once that merges. ## Screenshots <img width="712" height="473" alt="Screenshot 2026-11-04 at 5 10 19 PM" src="https://github.com/user-attachments/assets/c72cc38d-f7b4-4269-a787-f27dfe382086" /> ### If pressing `apply` <img width="704" height="150" alt="Screenshot 2026-11-04 at 5 19 27 PM" src="https://github.com/user-attachments/assets/92dce8e5-8d58-48b8-8899-ed01c9f242c2" /> ### If pressing `discard` <img width="712" height="473" alt="Screenshot 2026-11-04 at 5 10 38 PM" src="https://github.com/user-attachments/assets/85656004-d510-4cef-89c1-80d9f95ebb83" /> ### Invalid <img width="754" height="596" alt="Image" src="https://github.com/user-attachments/assets/4ba480f6-af4c-4253-9693-2c545fb77ab1" /> ### Invalid repair <img width="704" height="680" alt="Screenshot 2026-11-04 at 6 03 20 PM" src="https://github.com/user-attachments/assets/ed4e3dcf-8d9b-4588-afc0-938248c78712" /> ### Invalid and Pending <img width="704" height="593" alt="Image" src="https://github.com/user-attachments/assets/7ad76b50-0d08-4505-89ad-b6afff6865af" /> ### Invalid and Pending repair <img width="704" height="585" alt="Screenshot 2026-11-04 at 6 05 04 PM" src="https://github.com/user-attachments/assets/b123453f-0f30-452d-b84c-a556c52458a0" /> ## Review instructions 1. Locally, put a project's network restrictions into review by editing the database directly. There's no API that creates one. 2. Open Database Settings > Network restrictions and check the callout and the added and removed entries. 3. Apply or discard the change. ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
381 lines
14 KiB
TypeScript
381 lines
14 KiB
TypeScript
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import { useParams } from 'common'
|
|
import { HelpCircleIcon, PlusIcon, TrashIcon } from 'lucide-react'
|
|
import { useMemo, useState } from 'react'
|
|
import { useForm, useWatch } from 'react-hook-form'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
Button,
|
|
Dialog,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogSection,
|
|
DialogSectionSeparator,
|
|
DialogTitle,
|
|
Form,
|
|
FormControl,
|
|
FormField,
|
|
Input,
|
|
Tooltip,
|
|
TooltipContent,
|
|
TooltipTrigger,
|
|
} from 'ui'
|
|
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
|
|
import * as z from 'zod'
|
|
|
|
import {
|
|
checkIfPrivate,
|
|
getAddressEndRange,
|
|
getNetworkKey,
|
|
isIPv4MappedIPv6,
|
|
normalize,
|
|
} from './NetworkRestrictions.utils'
|
|
import { DiscardChangesConfirmationDialog } from '@/components/ui-patterns/Dialogs/DiscardChangesConfirmationDialog'
|
|
import InformationBox from '@/components/ui/InformationBox'
|
|
import { useNetworkRestrictionsQuery } from '@/data/network-restrictions/network-restrictions-query'
|
|
import { useNetworkRestrictionsApplyMutation } from '@/data/network-restrictions/network-retrictions-apply-mutation'
|
|
import { useConfirmOnClose } from '@/hooks/ui/useConfirmOnClose'
|
|
import { DOCS_URL } from '@/lib/constants'
|
|
|
|
const IPV4_MAX_CIDR_BLOCK_SIZE = 32
|
|
const IPV6_MAX_CIDR_BLOCK_SIZE = 128
|
|
|
|
type RestrictionType = 'IPv4' | 'IPv6'
|
|
|
|
const getMaxCidrBlockSize = (type: RestrictionType | undefined) =>
|
|
type === 'IPv6' ? IPV6_MAX_CIDR_BLOCK_SIZE : IPV4_MAX_CIDR_BLOCK_SIZE
|
|
|
|
const getFormSchema = (type: RestrictionType | undefined) => {
|
|
const maxCidrBlockSize = getMaxCidrBlockSize(type)
|
|
const cidrBlockSizeValidationMessage = `Size has to be between 0 to ${maxCidrBlockSize}`
|
|
|
|
return z.object({
|
|
cidrBlockSize: z
|
|
.union([
|
|
z.literal(''),
|
|
z.coerce
|
|
.number()
|
|
.min(0, cidrBlockSizeValidationMessage)
|
|
.max(maxCidrBlockSize, cidrBlockSizeValidationMessage),
|
|
])
|
|
.refine((value) => value !== '', 'Size is required'),
|
|
ipAddress: z
|
|
.string()
|
|
.min(1, `Please enter a valid IP address`)
|
|
.ip({
|
|
version: type === 'IPv4' ? 'v4' : 'v6',
|
|
message: `Please enter a valid ${type} address`,
|
|
})
|
|
.refine((val) => !checkIfPrivate(type, val), 'Private IP addresses are not supported')
|
|
.refine(
|
|
(val) => type !== 'IPv6' || !isIPv4MappedIPv6(val),
|
|
"IPv4-mapped IPv6 addresses aren't supported. Add the address as an IPv4 restriction instead."
|
|
),
|
|
})
|
|
}
|
|
|
|
type FormValues = z.infer<ReturnType<typeof getFormSchema>>
|
|
|
|
interface AddRestrictionModalProps {
|
|
type?: RestrictionType
|
|
hasOverachingRestriction: boolean
|
|
onClose: () => void
|
|
}
|
|
|
|
export const AddRestrictionModal = ({
|
|
type,
|
|
hasOverachingRestriction,
|
|
onClose,
|
|
}: AddRestrictionModalProps) => {
|
|
const { ref } = useParams()
|
|
|
|
// Normalized addresses that have been added to the list, pending save
|
|
const [stagedAddresses, setStagedAddresses] = useState<string[]>([])
|
|
|
|
const { data } = useNetworkRestrictionsQuery({ projectRef: ref }, { enabled: type !== undefined })
|
|
const ipv4Restrictions = data?.config?.dbAllowedCidrs ?? []
|
|
const ipv6Restrictions = data?.config?.dbAllowedCidrsV6 ?? []
|
|
const restrictedIps = ipv4Restrictions.concat(ipv6Restrictions)
|
|
|
|
const maxCidrBlockSize = getMaxCidrBlockSize(type)
|
|
const formSchema = useMemo(() => getFormSchema(type), [type])
|
|
const defaultValues: FormValues = { ipAddress: '', cidrBlockSize: maxCidrBlockSize }
|
|
|
|
const form = useForm<FormValues>({
|
|
resolver: zodResolver(formSchema),
|
|
defaultValues,
|
|
// Reset the draft fields when the dialog opens for a different type
|
|
values: defaultValues,
|
|
})
|
|
const { errors } = form.formState
|
|
|
|
const [cidrBlockSize, ipAddress] = useWatch({
|
|
name: ['cidrBlockSize', 'ipAddress'],
|
|
control: form.control,
|
|
})
|
|
const hasDraft = ipAddress.trim() !== ''
|
|
const pendingCount = stagedAddresses.length + (hasDraft ? 1 : 0)
|
|
|
|
const handleClose = () => {
|
|
setStagedAddresses([])
|
|
form.reset()
|
|
onClose()
|
|
}
|
|
|
|
const {
|
|
confirmOnClose,
|
|
handleOpenChange,
|
|
modalProps: discardChangesModalProps,
|
|
} = useConfirmOnClose({
|
|
checkIsDirty: () => pendingCount > 0,
|
|
onClose: handleClose,
|
|
})
|
|
|
|
const { mutate: applyNetworkRestrictions, isPending: isApplying } =
|
|
useNetworkRestrictionsApplyMutation({
|
|
onSuccess: () => handleClose(),
|
|
})
|
|
|
|
// Validates the draft fields and moves a valid entry to the list. Returns the
|
|
// updated list, or undefined if the draft was rejected.
|
|
const addDraftToList = async () => {
|
|
const isValid = await form.trigger()
|
|
if (!isValid) return
|
|
|
|
const { ipAddress, cidrBlockSize } = form.getValues()
|
|
const address = `${ipAddress}/${cidrBlockSize}`
|
|
const normalizedAddress = normalize(address)
|
|
|
|
// Compared as networks, as the API does, so a range already stored in another spelling counts too
|
|
if (restrictedIps.some((ip) => getNetworkKey(ip) === normalizedAddress)) {
|
|
form.setError('ipAddress', { message: `The address ${address} is already restricted` })
|
|
return
|
|
}
|
|
if (stagedAddresses.includes(normalizedAddress)) {
|
|
form.setError('ipAddress', { message: `The address ${address} is already in the list` })
|
|
return
|
|
}
|
|
|
|
const nextAddresses = [...stagedAddresses, normalizedAddress]
|
|
setStagedAddresses(nextAddresses)
|
|
form.reset()
|
|
return nextAddresses
|
|
}
|
|
|
|
const handleSave = async () => {
|
|
if (!ref) return console.error('Project ref is required')
|
|
|
|
// A valid entry that was typed but not yet added to the list is saved too
|
|
const addresses = hasDraft ? await addDraftToList() : stagedAddresses
|
|
if (addresses === undefined || addresses.length === 0) return
|
|
|
|
// Based on the submitted addresses, as the staged list may not have updated yet
|
|
const applyOptions = {
|
|
onSuccess: () =>
|
|
toast.success(addresses.length === 1 ? 'Restriction added' : 'Restrictions added'),
|
|
}
|
|
|
|
// Need to replace over arching restriction (allow all / disallow all)
|
|
if (hasOverachingRestriction) {
|
|
const dbAllowedCidrs = type === 'IPv4' ? addresses : []
|
|
const dbAllowedCidrsV6 = type === 'IPv6' ? addresses : []
|
|
applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs, dbAllowedCidrsV6 }, applyOptions)
|
|
} else {
|
|
const dbAllowedCidrs =
|
|
type === 'IPv4' ? [...ipv4Restrictions, ...addresses] : ipv4Restrictions
|
|
const dbAllowedCidrsV6 =
|
|
type === 'IPv6' ? [...ipv6Restrictions, ...addresses] : ipv6Restrictions
|
|
applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs, dbAllowedCidrsV6 }, applyOptions)
|
|
}
|
|
}
|
|
|
|
const availableAddresses = Math.pow(2, maxCidrBlockSize - (cidrBlockSize ?? 0))
|
|
|
|
const addressRange =
|
|
type !== undefined ? getAddressEndRange(type, `${ipAddress}/${cidrBlockSize}`) : undefined
|
|
|
|
const isValidCIDR =
|
|
errors.cidrBlockSize == null && errors.ipAddress == null && addressRange != null
|
|
|
|
const normalizedAddress = isValidCIDR
|
|
? normalize(`${ipAddress}/${cidrBlockSize}`)
|
|
: `${ipAddress}/${cidrBlockSize}`
|
|
|
|
return (
|
|
<>
|
|
<Dialog open={type !== undefined} onOpenChange={handleOpenChange}>
|
|
<DialogContent size="medium">
|
|
<DialogHeader>
|
|
<DialogTitle>{`Add ${type} restrictions`}</DialogTitle>
|
|
</DialogHeader>
|
|
<DialogSectionSeparator />
|
|
<Form {...form}>
|
|
<DialogSection className="space-y-4">
|
|
<p className="text-sm text-foreground-light">
|
|
This adds IP address ranges to the list of allowed ranges that can access your
|
|
database. Add as many as you need, then save them together.
|
|
</p>
|
|
<InformationBox
|
|
title="Note: Restrictions only apply to direct connections to your database and connection pooler"
|
|
description="They do not currently apply to APIs offered over HTTPS, such as PostgREST, Storage, or Authentication."
|
|
urlLabel="Learn more"
|
|
url={`${DOCS_URL}/guides/platform/network-restrictions#limitations`}
|
|
/>
|
|
<form
|
|
noValidate
|
|
className="space-y-4"
|
|
onSubmit={(event) => {
|
|
event.preventDefault()
|
|
addDraftToList()
|
|
}}
|
|
>
|
|
<div className="flex space-x-4">
|
|
<div className="w-[55%]">
|
|
<FormField
|
|
control={form.control}
|
|
name="ipAddress"
|
|
render={({ field }) => (
|
|
<FormItemLayout layout="vertical" label={`${type} address`}>
|
|
<FormControl>
|
|
<Input {...field} placeholder={type === 'IPv4' ? '0.0.0.0' : '::0'} />
|
|
</FormControl>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
</div>
|
|
<div className="grow">
|
|
<FormField
|
|
control={form.control}
|
|
name="cidrBlockSize"
|
|
render={({ field }) => (
|
|
<FormItemLayout
|
|
layout="vertical"
|
|
label={
|
|
<div className="flex items-center space-x-2">
|
|
<p>CIDR Block Size</p>
|
|
<Tooltip>
|
|
<TooltipTrigger type="button">
|
|
<HelpCircleIcon size="14" strokeWidth={2} />
|
|
</TooltipTrigger>
|
|
<TooltipContent side="bottom" className="w-80">
|
|
Classless inter-domain routing (CIDR) notation is the notation
|
|
used to identify networks and hosts in the networks. The block
|
|
size tells us how many bits we need to take for the network
|
|
prefix, and is a value between 0 to {maxCidrBlockSize}.
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
</div>
|
|
}
|
|
>
|
|
<FormControl>
|
|
<Input
|
|
{...field}
|
|
type="number"
|
|
min={0}
|
|
max={maxCidrBlockSize}
|
|
placeholder={maxCidrBlockSize.toString()}
|
|
/>
|
|
</FormControl>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<Button
|
|
variant="default"
|
|
size="tiny"
|
|
type="submit"
|
|
icon={<PlusIcon />}
|
|
disabled={isApplying || !hasDraft}
|
|
>
|
|
Add to list
|
|
</Button>
|
|
</form>
|
|
</DialogSection>
|
|
{stagedAddresses.length > 0 && (
|
|
<>
|
|
<DialogSectionSeparator />
|
|
<DialogSection className="space-y-2">
|
|
<p className="text-sm">
|
|
{stagedAddresses.length === 1
|
|
? '1 range to add'
|
|
: `${stagedAddresses.length} ranges to add`}
|
|
</p>
|
|
<ul className="space-y-2">
|
|
{stagedAddresses.map((address) => (
|
|
<li key={address} className="flex items-center space-x-2">
|
|
<Input
|
|
readOnly
|
|
size="small"
|
|
value={address}
|
|
aria-label={`Restriction ${address}`}
|
|
className="flex-1"
|
|
/>
|
|
<Button
|
|
size="tiny"
|
|
icon={<TrashIcon size={12} />}
|
|
aria-label={`Remove ${address}`}
|
|
className="h-[34px] w-[34px] shrink-0"
|
|
disabled={isApplying}
|
|
onClick={() =>
|
|
setStagedAddresses((addresses) =>
|
|
addresses.filter((item) => item !== address)
|
|
)
|
|
}
|
|
/>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</DialogSection>
|
|
</>
|
|
)}
|
|
<DialogSectionSeparator />
|
|
{isValidCIDR ? (
|
|
<DialogSection className="space-y-1">
|
|
<p className="text-sm">
|
|
The address range <code>{normalizedAddress}</code> will be restricted
|
|
</p>
|
|
<p className="text-sm text-foreground-light">
|
|
Selected address space: <code>{addressRange.start}</code> to{' '}
|
|
<code>{addressRange.end}</code>{' '}
|
|
</p>
|
|
<p className="text-sm text-foreground-light">
|
|
Number of addresses: {availableAddresses}
|
|
</p>
|
|
</DialogSection>
|
|
) : (
|
|
<DialogSection>
|
|
<div className="h-[68px] flex items-center">
|
|
<p className="text-sm text-foreground-light">
|
|
A summary of your restriction will be shown here after entering a valid IP
|
|
address and CIDR block size. IP addresses will also be normalized.
|
|
</p>
|
|
</div>
|
|
</DialogSection>
|
|
)}
|
|
<DialogFooter>
|
|
<Button disabled={isApplying} onClick={confirmOnClose}>
|
|
Cancel
|
|
</Button>
|
|
<Button
|
|
variant="primary"
|
|
type="button"
|
|
loading={isApplying}
|
|
disabled={isApplying || pendingCount === 0}
|
|
onClick={handleSave}
|
|
>
|
|
{pendingCount === 1 ? 'Save restriction' : 'Save restrictions'}
|
|
</Button>
|
|
</DialogFooter>
|
|
</Form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
<DiscardChangesConfirmationDialog
|
|
{...discardChangesModalProps}
|
|
description="Are you sure you want to discard your changes? Your restrictions will not be saved."
|
|
/>
|
|
</>
|
|
)
|
|
}
|