Skip to content

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

Replace ipaddr.js with ip-address module

Joshen Limf8edb924a1f7
diff --git a/package-lock.json b/package-lock.json
index e5700845757..29773e5561f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -38353,7 +38353,6 @@
"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",
"lottie-react": "^2.2.1",
@@ -38685,14 +38684,6 @@
"resolved": "https://registry.npmjs.org/internmap/-/internmap-1.0.1.tgz",
"integrity": "sha512-lDB5YccMydFBtasVtxnZ3MRBHuaoE8GKsppq+EchKL2U4nK/DmEpPHNH8MZe5HkMtpSiTSOZwfN0tzYjO/lJEw=="
},
- "studio/node_modules/ipaddr.js": {
- "version": "2.0.1",
- "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.0.1.tgz",
- "integrity": "sha512-1qTgH9NG+IIJ4yfKs2e6Pp1bZg8wbDbKHT21HrLIeYBTRLgMYKnMTPAuI3Lcs61nfx5h1xlXnbJtH1kX5/d/ng==",
- "engines": {
- "node": ">= 10"
- }
- },
"studio/node_modules/openapi-types": {
"version": "9.3.1",
"resolved": "https://registry.npmjs.org/openapi-types/-/openapi-types-9.3.1.tgz",
@@ -64925,8 +64916,7 @@
"generate-password": "^1.7.0",
"html-to-image": "^1.10.8",
"immutability-helper": "^3.1.1",
- "ip-address": "*",
- "ipaddr.js": "^2.0.1",
+ "ip-address": "^8.1.0",
"jest": "^27.4.0",
"jest-canvas-mock": "^2.3.1",
"json-logic-js": "^2.0.2",
@@ -65156,11 +65146,6 @@
"resolved": "https://registry.npmjs.org/internmap/-/internmap-1.0.1.tgz",
"integrity": "sha512-lDB5YccMydFBtasVtxnZ3MRBHuaoE8GKsppq+EchKL2U4nK/DmEpPHNH8MZe5HkMtpSiTSOZwfN0tzYjO/lJEw=="
},
- "ipaddr.js": {
- "version": "2.0.1",
- "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.0.1.tgz",
- "integrity": "sha512-1qTgH9NG+IIJ4yfKs2e6Pp1bZg8wbDbKHT21HrLIeYBTRLgMYKnMTPAuI3Lcs61nfx5h1xlXnbJtH1kX5/d/ng=="
- },
"openapi-types": {
"version": "9.3.1",
"resolved": "https://registry.npmjs.org/openapi-types/-/openapi-types-9.3.1.tgz",
diff --git a/studio/components/interfaces/Settings/Database/NetworkRestrictions/AddRestrictionModal.tsx b/studio/components/interfaces/Settings/Database/NetworkRestrictions/AddRestrictionModal.tsx
index 0edccfe8841..16efbdf24be 100644
--- a/studio/components/interfaces/Settings/Database/NetworkRestrictions/AddRestrictionModal.tsx
+++ b/studio/components/interfaces/Settings/Database/NetworkRestrictions/AddRestrictionModal.tsx
@@ -1,12 +1,12 @@
import { FC } from 'react'
import { Address4 } from 'ip-address'
-import ipaddr from 'ipaddr.js'
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'
import InformationBox from 'components/ui/InformationBox'
+import { checkIfPrivate, getAddressEndRange } from './NetworkRestrictions.utils'
+import { useNetworkRestrictionsApplyMutation } from 'data/network-restrictions/network-retrictions-apply-mutation'
interface Props {
restrictedIps: string[]
@@ -26,57 +26,24 @@ 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 {
- const address = ipaddr.parse(ipAddress)
- const isIpv6 = address.kind() === 'ipv6'
- const isPrivate = address.range() === 'private'
+ // Validate CIDR block size
+ const isOutOfCidrSizeRange = cidrBlockSize < 0 || cidrBlockSize > 32
+ if (isOutOfCidrSizeRange) errors.cidrBlockSize = 'Size has to be between 0 to 32'
- 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) {
+ // Validate IP address
+ const isValid = Address4.isValid(ipAddress)
+ if (!isValid) {
errors.ipAddress = 'Please enter a valid IP address'
+ return errors
}
- // 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'
- // }
- // }
- // }
+ const isPrivate = checkIfPrivate(ipAddress)
+ if (isPrivate) errors.ipAddress = 'Private IP addresses are not supported'
+
return errors
}
@@ -129,7 +96,7 @@ const AddRestrictionModal: FC<Props> = ({
<div className="py-6 space-y-4">
<p className="text-sm text-scale-1100">
This will add an IP address range to a list of allowed ranges that can access
- your project's database.
+ your database. Only IPv4 addresses are supported at the moment.
</p>
<InformationBox
defaultVisibility
diff --git a/studio/components/interfaces/Settings/Database/NetworkRestrictions/NetworkRestrictions.utils.ts b/studio/components/interfaces/Settings/Database/NetworkRestrictions/NetworkRestrictions.utils.ts
new file mode 100644
index 00000000000..36999d6bb0b
--- /dev/null
+++ b/studio/components/interfaces/Settings/Database/NetworkRestrictions/NetworkRestrictions.utils.ts
@@ -0,0 +1,22 @@
+import { Address4 } from 'ip-address'
+
+const privateIPRanges = [
+ new Address4('10.0.0.0/8'),
+ new Address4('172.16.0.0/12'),
+ new Address4('192.168.0.0/16'),
+]
+
+export const checkIfPrivate = (cidr: string) => {
+ const address = new Address4(cidr)
+ const res = privateIPRanges.map((range) => address.isInSubnet(range))
+ return res.includes(true)
+}
+
+export const getAddressEndRange = (cidr: string) => {
+ try {
+ const address = new Address4(cidr)
+ return { start: address.startAddress().address, end: address.endAddress().address }
+ } catch (error: any) {
+ return undefined
+ }
+}
diff --git a/studio/package.json b/studio/package.json
index fa76b3dcf83..1e7e8d7ea37 100644
--- a/studio/package.json
+++ b/studio/package.json
@@ -46,7 +46,6 @@
"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",
"lottie-react": "^2.2.1",