Skip to content

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

Shift network restrictions to database settings page

Joshen Lima716b1cb72e9
diff --git a/package-lock.json b/package-lock.json
index 9746c433cef..e5700845757 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -20305,6 +20305,23 @@
"integrity": "sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==",
"dev": true
},
+ "node_modules/ip-address": {
+ "version": "8.1.0",
+ "resolved": "https://registry.npmjs.org/ip-address/-/ip-address-8.1.0.tgz",
+ "integrity": "sha512-Wz91gZKpNKoXtqvY8ScarKYwhXoK4r/b5QuT+uywe/azv0/nUCo7Bh0IRRI7F9DHR06kJNWtzMGLIbXavngbKA==",
+ "dependencies": {
+ "jsbn": "1.1.0",
+ "sprintf-js": "1.1.2"
+ },
+ "engines": {
+ "node": ">= 12"
+ }
+ },
+ "node_modules/ip-address/node_modules/sprintf-js": {
+ "version": "1.1.2",
+ "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.2.tgz",
+ "integrity": "sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug=="
+ },
"node_modules/ipaddr.js": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
@@ -22939,6 +22956,11 @@
"node": ">=4.0.0"
}
},
+ "node_modules/jsbn": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-1.1.0.tgz",
+ "integrity": "sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A=="
+ },
"node_modules/jsdom": {
"version": "16.7.0",
"resolved": "https://registry.npmjs.org/jsdom/-/jsdom-16.7.0.tgz",
@@ -38330,6 +38352,7 @@
"generate-password": "^1.7.0",
"html-to-image": "^1.10.8",
"immutability-helper": "^3.1.1",
+ "ip-address": "^8.1.0",
"ipaddr.js": "^2.0.1",
"json-logic-js": "^2.0.2",
"lodash": "^4.17.21",
@@ -54269,6 +54292,22 @@
"integrity": "sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==",
"dev": true
},
+ "ip-address": {
+ "version": "8.1.0",
+ "resolved": "https://registry.npmjs.org/ip-address/-/ip-address-8.1.0.tgz",
+ "integrity": "sha512-Wz91gZKpNKoXtqvY8ScarKYwhXoK4r/b5QuT+uywe/azv0/nUCo7Bh0IRRI7F9DHR06kJNWtzMGLIbXavngbKA==",
+ "requires": {
+ "jsbn": "1.1.0",
+ "sprintf-js": "1.1.2"
+ },
+ "dependencies": {
+ "sprintf-js": {
+ "version": "1.1.2",
+ "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.2.tgz",
+ "integrity": "sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug=="
+ }
+ }
+ },
"ipaddr.js": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
@@ -56360,6 +56399,11 @@
}
}
},
+ "jsbn": {
+ "version": "1.1.0",
+ "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-1.1.0.tgz",
+ "integrity": "sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A=="
+ },
"jsdom": {
"version": "16.7.0",
"resolved": "https://registry.npmjs.org/jsdom/-/jsdom-16.7.0.tgz",
@@ -64881,6 +64925,7 @@
"generate-password": "^1.7.0",
"html-to-image": "^1.10.8",
"immutability-helper": "^3.1.1",
+ "ip-address": "*",
"ipaddr.js": "^2.0.1",
"jest": "^27.4.0",
"jest-canvas-mock": "^2.3.1",
diff --git a/studio/components/interfaces/Settings/Database/NetworkRestrictions/AddRestrictionModal.tsx b/studio/components/interfaces/Settings/Database/NetworkRestrictions/AddRestrictionModal.tsx
index 94ecb1fabf4..0edccfe8841 100644
--- a/studio/components/interfaces/Settings/Database/NetworkRestrictions/AddRestrictionModal.tsx
+++ b/studio/components/interfaces/Settings/Database/NetworkRestrictions/AddRestrictionModal.tsx
@@ -1,6 +1,8 @@
import { FC } from 'react'
+import { Address4 } from 'ip-address'
import ipaddr from 'ipaddr.js'
-import { Alert, Button, Form, Input, Modal } from 'ui'
+import { Button, Form, IconHelpCircle, Input, Modal } from 'ui'
+import * as Tooltip from '@radix-ui/react-tooltip'
import { useStore, useParams } from 'hooks'
import { useNetworkRestrictionsApplyMutation } from 'data/network-restrictions/network-retrictions-apply-mutation'
@@ -24,32 +26,57 @@ const AddRestrictionModal: FC<Props> = ({
const { ref } = useParams()
const { mutateAsync: applyNetworkRestrictions } = useNetworkRestrictionsApplyMutation()
+ const getAddressEndRange = (cidr: string) => {
+ try {
+ const address = new Address4(cidr)
+ return { start: address.startAddress().address, end: address.endAddress().address }
+ } catch (error: any) {
+ return undefined
+ }
+ }
+
const validate = (values: any) => {
const errors: any = {}
+ const { ipAddress, cidrBlockSize } = values
+
+ if (cidrBlockSize < 0 || cidrBlockSize > 32) {
+ errors.cidrBlockSize = 'Size has to be between 0 to 32'
+ }
try {
- // Check if already exists
- const alreadyExists = restrictedIps.includes(values.address)
- if (alreadyExists) {
- errors.address = 'This restriction already exists'
- return errors
- }
- // Check if valid IP address
- const [address, range] = ipaddr.IPv4.parseCIDR(values.address)
- // Check if private range
- const isPrivate = ipaddr.parse(address.octets.join('.')).range() === 'private'
- if (isPrivate) errors.address = 'Private IP addresses are not supported'
+ const address = ipaddr.parse(ipAddress)
+ const isIpv6 = address.kind() === 'ipv6'
+ const isPrivate = address.range() === 'private'
+
+ if (isIpv6) errors.ipAddress = 'Only IPv4 addresses are supported at the moment'
+ else if (isPrivate) errors.ipAddress = 'Private IP addresses are not supported'
} catch (error: any) {
- if (error.message.includes('string is not formatted like an IPv4 CIDR range')) {
- try {
- // Check if IPv6 format
- ipaddr.IPv6.parseCIDR(values.address)
- errors.address = 'Only IPv4 addresses are supported at the moment'
- } catch (error: any) {
- errors.address = 'Please enter a valid address in CIDR notation'
- }
- }
+ errors.ipAddress = 'Please enter a valid IP address'
}
+
+ // try {
+ // // Check if already exists
+ // const alreadyExists = restrictedIps.includes(values.address)
+ // if (alreadyExists) {
+ // errors.address = 'This restriction already exists'
+ // return errors
+ // }
+ // // Check if valid IP address
+ // const [address, range] = ipaddr.IPv4.parseCIDR(values.address)
+ // // Check if private range
+ // const isPrivate = ipaddr.parse(address.octets.join('.')).range() === 'private'
+ // if (isPrivate) errors.address = 'Private IP addresses are not supported'
+ // } catch (error: any) {
+ // if (error.message.includes('string is not formatted like an IPv4 CIDR range')) {
+ // try {
+ // // Check if IPv6 format
+ // ipaddr.IPv6.parseCIDR(values.address)
+ // errors.address = 'Only IPv4 addresses are supported at the moment'
+ // } catch (error: any) {
+ // errors.address = 'Please enter a valid address in CIDR notation'
+ // }
+ // }
+ // }
return errors
}
@@ -57,9 +84,8 @@ const AddRestrictionModal: FC<Props> = ({
if (!ref) return console.error('Project ref is required')
setSubmitting(true)
- const dbAllowedCidrs = hasOverachingRestriction
- ? [values.address]
- : [...restrictedIps, values.address]
+ const cidr = `${values.ipAddress}/${values.cidrBlockSize}`
+ const dbAllowedCidrs = hasOverachingRestriction ? [cidr] : [...restrictedIps, cidr]
try {
await applyNetworkRestrictions({ projectRef: ref, dbAllowedCidrs })
@@ -87,11 +113,16 @@ const AddRestrictionModal: FC<Props> = ({
validateOnBlur
id={formId}
className="!border-t-0"
- initialValues={{ address: '' }}
+ initialValues={{ ipAddress: '', cidrBlockSize: undefined }}
validate={validate}
onSubmit={onSubmit}
>
- {({ isSubmitting }: { isSubmitting: boolean }) => {
+ {({ isSubmitting, values }: { isSubmitting: boolean; values: any }) => {
+ const isValidBlockSize =
+ values.cidrBlockSize !== '' && values.cidrBlockSize >= 0 && values.cidrBlockSize <= 32
+ const availableAddresses = Math.pow(2, 32 - (values?.cidrBlockSize ?? 0))
+ const addressRange = getAddressEndRange(`${values.ipAddress}/${values.cidrBlockSize}`)
+
return (
<>
<Modal.Content>
@@ -106,12 +137,66 @@ const AddRestrictionModal: FC<Props> = ({
title="Note: Restrictions only apply to your database and PgBouncer"
description="They do not currently apply to APIs offered over HTTPS, such as PostgREST, Storage, or Authentication"
/>
- <Input
- label="IPv4 address in CIDR notation"
- id="address"
- name="address"
- placeholder="0.0.0.0/0"
- />
+ <div className="flex space-x-4">
+ <div className="w-[55%]">
+ <Input
+ label="IPv4 address"
+ id="ipAddress"
+ name="ipAddress"
+ placeholder="0.0.0.0"
+ />
+ </div>
+ <div>
+ <Input
+ label={
+ <div className="flex items-center space-x-2">
+ <p>CIDR Block Size</p>
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger>
+ <IconHelpCircle size="tiny" strokeWidth={2} />
+ </Tooltip.Trigger>
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
+ 'border border-scale-200 w-[300px]',
+ ].join(' ')}
+ >
+ <span className="text-xs text-scale-1200">
+ 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 32.
+ </span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Root>
+ </div>
+ }
+ id="cidrBlockSize"
+ name="cidrBlockSize"
+ type="number"
+ placeholder="0"
+ min={0}
+ max={32}
+ />
+ </div>
+ </div>
+ {isValidBlockSize && (
+ <div className="">
+ <p className="text-sm text-scale-1000">
+ Number of addresses: {availableAddresses}
+ </p>
+ {addressRange !== undefined && (
+ <p className="text-sm text-scale-1000">
+ Selected address space:{' '}
+ <code className="text-xs">{addressRange.start}</code> to{' '}
+ <code className="text-xs">{addressRange.end}</code>
+ </p>
+ )}
+ </div>
+ )}
</div>
</Modal.Content>
<div className="flex items-center justify-end px-6 py-4 border-t space-x-2">
diff --git a/studio/package.json b/studio/package.json
index 4bcbb3b9072..fa76b3dcf83 100644
--- a/studio/package.json
+++ b/studio/package.json
@@ -45,6 +45,7 @@
"generate-password": "^1.7.0",
"html-to-image": "^1.10.8",
"immutability-helper": "^3.1.1",
+ "ip-address": "^8.1.0",
"ipaddr.js": "^2.0.1",
"json-logic-js": "^2.0.2",
"lodash": "^4.17.21",