Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeError: Division by zero #24

Open
bartbutenaers opened this issue Oct 17, 2024 · 3 comments
Open

RangeError: Division by zero #24

bartbutenaers opened this issue Oct 17, 2024 · 3 comments
Labels

Comments

@bartbutenaers
Copy link

Hi @silverwind,
Thanks for sharing this useful library!

I would like to allow only the Google API servers to access my home automation system for Google assistant. There is a python module available to calculate the ip ranges of the Google API servers (in this tutorial), but I need to convert that program to Javascript:

  1. Get the ip ranges from all Google servers
  2. Get the ip ranges from the Google GCP servers, where malicious users could rent a vm for hacking
  3. Ip ranges from the Google API servers = list from step 1 - list from step 2

Here is a simplified version of my code:

(async () => {
    const { excludeCidr } = await import('cidr-tools');

    const allIpRanges = ["8.8.4.0/24","8.8.8.0/24","8.34.208.0/20","8.35.192.0/20","23.236.48.0/20","23.251.128.0/19","34.0.0.0/15","34.2.0.0/16","34.3.0.0/23","34.3.3.0/24","34.3.4.0/24","34.3.8.0/21","34.3.16.0/20","34.3.32.0/19","34.3.64.0/18","34.4.0.0/14","34.8.0.0/13","34.16.0.0/12","34.32.0.0/11","34.64.0.0/10","34.128.0.0/10","35.184.0.0/13","35.192.0.0/14","35.196.0.0/15","35.198.0.0/16","35.199.0.0/17","35.199.128.0/18","35.200.0.0/13","35.208.0.0/12","35.224.0.0/12","35.240.0.0/13","57.140.192.0/18","64.15.112.0/20","64.233.160.0/19","66.22.228.0/23","66.102.0.0/20","66.249.64.0/19","70.32.128.0/19","72.14.192.0/18","74.125.0.0/16","104.154.0.0/15","104.196.0.0/14","104.237.160.0/19","107.167.160.0/19","107.178.192.0/18","108.59.80.0/20","108.170.192.0/18","108.177.0.0/17","130.211.0.0/16","136.22.160.0/20","136.22.176.0/21","136.22.184.0/23","136.22.186.0/24","136.124.0.0/15","142.250.0.0/15","146.148.0.0/17","152.65.208.0/22","152.65.214.0/23","152.65.218.0/23","152.65.222.0/23","152.65.224.0/19","162.120.128.0/17","162.216.148.0/22","162.222.176.0/21","172.110.32.0/21","172.217.0.0/16","172.253.0.0/16","173.194.0.0/16","173.255.112.0/20","192.158.28.0/22","192.178.0.0/15","193.186.4.0/24","199.36.154.0/23","199.36.156.0/24","199.192.112.0/22","199.223.232.0/21","207.223.160.0/20","208.65.152.0/22","208.68.108.0/22","208.81.188.0/22","208.117.224.0/19","209.85.128.0/17","216.58.192.0/19","216.73.80.0/20","216.239.32.0/19","2001:4860::/32","2404:6800::/32","2404:f340::/32","2600:1900::/28","2605:ef80::/32","2606:40::/32","2606:73c0::/32","2607:1c0:241:40::/60","2607:1c0:300::/40","2607:f8b0::/32","2620:11a:a000::/40","2620:120:e000::/40","2800:3f0::/32","2a00:1450::/32","2c0f:fb50::/32"]
    const gcpIpRanges = ["34.1.208.0/20","34.35.0.0/16","34.152.86.0/23","34.177.50.0/23","2600:1900:8000::/44","34.80.0.0/15","34.137.0.0/16","35.185.128.0/19","35.185.160.0/20","35.187.144.0/20","35.189.160.0/19","35.194.128.0/17","35.201.128.0/17","35.206.192.0/18","35.220.32.0/21","35.221.128.0/17","35.229.128.0/17","35.234.0.0/18","35.235.16.0/20","35.236.128.0/18","35.242.32.0/21","104.155.192.0/19","104.155.224.0/20","104.199.128.0/18","104.199.192.0/19","104.199.224.0/20","104.199.242.0/23","104.199.244.0/22","104.199.248.0/21","107.167.176.0/20","130.211.240.0/20","2600:1900:4030::/44","34.92.0.0/16","34.96.128.0/17","34.104.88.0/21","34.124.24.0/21","34.150.0.0/17","35.215.128.0/18","35.220.27.0/24","35.220.128.0/17","35.241.64.0/18","35.242.27.0/24","35.243.8.0/21","2600:1900:41a0::/44","34.84.0.0/16","34.85.0.0/17","34.104.62.0/23","34.104.128.0/17","34.127.190.0/23","34.146.0.0/16","34.157.64.0/20","34.157.164.0/22","34.157.192.0/20","35.187.192.0/19","35.189.128.0/19","35.190.224.0/20","35.194.96.0/19","35.200.0.0/17","35.213.0.0/17","35.220.56.0/22","35.221.64.0/18","35.230.240.0/20","35.242.56.0/22","35.243.64.0/18","104.198.80.0/20","104.198.112.0/20","2600:1900:4050::/44","34.97.0.0/16","34.104.49.0/24","34.127.177.0/24","35.217.128.0/17","35.220.45.0/24","35.242.45.0/24","35.243.56.0/21","2600:1900:41d0::/44","34.0.96.0/19","34.22.64.0/19","34.22.96.0/20","34.47.64.0/18","34.50.0.0/18","34.64.32.0/19","34.64.64.0/22","34.64.68.0/22","34.64.72.0/21","34.64.80.0/20","34.64.96.0/19","34.64.128.0/22","34.64.132.0/22","34.64.136.0/21","34.64.144.0/20","34.64.160.0/19","34.64.192.0/18","35.216.0.0/17","2600:1901:8180::/44","34.0.227.0/24","34.47.128.0/17","34.93.0.0/16","34.100.128.0/17","34.104.108.0/23","34.124.44.0/23","34.152.64.0/22","34.153.58.0/23","34.153.250.0/23","34.157.87.0/24","34.157.215.0/24","34.177.32.0/22","35.200.128.0/17","35.201.41.0/24","35.207.192.0/18","35.220.42.0/24","35.234.208.0/20","35.242.42.0/24","35.244.0.0/18","2600:1900:40a0::/44","34.0.0.0/20","34.104.120.0/23","34.124.56.0/23","34.126.208.0/20","34.131.0.0/16","34.153.32.0/24","34.153.224.0/24","2600:1900:41b0::/44","34.1.128.0/20","34.1.192.0/20","34.2.16.0/20","34.21.128.0/17","34.87.0.0/17","34.87.128.0/18","34.104.58.0/23","34.104.106.0/23","34.124.42.0/23","34.124.128.0/17","34.126.64.0/18","34.126.128.0/18","34.128.44.0/23","34.128.60.0/23","34.142.128.0/17","34.143.128.0/17","34.153.40.0/23","34.153.232.0/23","34.157.82.0/23","34.157.88.0/23","34.157.210.0/23","35.185.176.0/20","35.186.144.0/20","35.187.224.0/19","35.197.128.0/19","35.198.192.0/18","35.213.128.0/18","35.220.24.0/23","35.234.192.0/20","35.240.128.0/17","35.242.24.0/23","35.247.128.0/18","2600:1900:4080::/44","34.34.216.0/21","34.50.64.0/18","34.101.18.0/24","34.101.20.0/22","34.101.24.0/22","34.101.32.0/19","34.101.64.0/18","34.101.128.0/17","34.128.64.0/18","34.152.68.0/24","34.153.44.0/24","34.153.236.0/24","34.157.254.0/24","35.219.0.0/17","2600:1901:8170::/44","34.40.128.0/17","34.87.192.0/18","34.104.104.0/23","34.116.64.0/18","34.124.40.0/23","34.128.36.0/24","34.128.48.0/24","34.151.64.0/18","34.151.128.0/18","35.189.0.0/18","35.197.160.0/19","35.201.0.0/19","35.213.192.0/18","35.220.41.0/24","35.234.224.0/20","35.242.41.0/24","35.244.64.0/18","2600:1900:40b0::/44","34.0.16.0/20","34.1.176.0/20","34.104.122.0/23","34.124.58.0/23","34.126.192.0/20","34.129.0.0/16","2600:1900:41c0::/44","34.0.240.0/20","34.104.116.0/22","34.116.128.0/17","34.118.0.0/17","34.124.52.0/22","2600:1900:4140::/44","34.88.0.0/16","34.104.96.0/21","34.124.32.0/21","35.203.232.0/21","35.217.0.0/18","35.220.26.0/24","35.228.0.0/16","35.242.26.0/24","2600:1900:4150::/44","34.0.192.0/19","34.157.44.0/23","34.157.172.0/23","34.164.0.0/16","34.175.0.0/16","2600:1901:8100::/44","8.34.208.0/23","8.34.211.0/24","8.34.220.0/22","23.251.128.0/20","34.14.0.0/17","34.22.112.0/20","34.22.128.0/17","34.34.128.0/18","34.38.0.0/16","34.52.128.0/17","34.53.128.0/17","34.62.0.0/16","34.76.0.0/14","34.118.254.0/23","34.140.0.0/16","35.187.0.0/17","35.187.160.0/19","35.189.192.0/18","35.190.192.0/19","35.195.0.0/16","35.205.0.0/16","35.206.128.0/18","35.210.0.0/16","35.220.96.0/19","35.233.0.0/17","35.240.0.0/17","35.241.128.0/17","35.242.64.0/19","104.155.0.0/17","104.199.0.0/18","104.199.66.0/23","104.199.68.0/22","104.199.72.0/21","104.199.80.0/20","104.199.96.0/20","130.211.48.0/20","130.211.64.0/19","130.211.96.0/20","146.148.2.0/23","146.148.4.0/22","146.148.8.0/21","146.148.16.0/20","146.148.112.0/20","192.158.28.0/22","2600:1900:4010::/44","34.1.160.0/20","34.32.0.0/17","34.152.80.0/23","34.177.36.0/23","2600:1901:81f0::/44","34.1.144.0/20","34.17.0.0/16","34.157.124.0/23","34.157.250.0/23","2600:1901:81b0::/44","34.13.0.0/18","34.39.0.0/17","34.50.192.0/18","34.89.0.0/17","34.105.128.0/17","34.127.186.0/23","34.128.52.0/22","34.142.0.0/17","34.147.128.0/17","34.153.128.0/18","34.157.36.0/22","34.157.40.0/22","34.157.168.0/22","35.189.64.0/18","35.197.192.0/18","35.203.210.0/23","35.203.212.0/22","35.203.216.0/22","35.214.0.0/17","35.220.20.0/22","35.230.128.0/19","35.234.128.0/19","35.235.48.0/20","35.242.20.0/22","35.242.128.0/18","35.246.0.0/17","2600:1900:40c0::/44","34.0.224.0/24","34.0.226.0/24","34.40.0.0/17","34.89.128.0/17","34.104.112.0/23","34.107.0.0/17","34.118.244.0/22","34.124.48.0/23","34.141.0.0/17","34.157.48.0/20","34.157.176.0/20","34.159.0.0/16","34.179.0.0/16","34.181.0.0/17","35.198.64.0/18","35.198.128.0/18","35.207.64.0/18","35.207.128.0/18","35.220.18.0/23","35.234.64.0/18","35.235.32.0/20","35.242.18.0/23","35.242.192.0/18","35.246.128.0/17","2600:1900:40d0::/44","34.1.224.0/19","34.12.0.0/16","34.13.128.0/17","34.32.128.0/17","34.34.0.0/17","34.90.0.0/15","34.104.126.0/23","34.124.62.0/23","34.141.128.0/17","34.147.0.0/17","34.157.80.0/23","34.157.92.0/22","34.157.208.0/23","34.157.220.0/22","35.204.0.0/16","35.214.128.0/17","35.220.16.0/23","35.234.160.0/20","35.242.16.0/23","2600:1900:4060::/44","34.65.0.0/16","34.104.110.0/23","34.124.46.0/23","35.216.128.0/17","35.220.44.0/24","35.235.216.0/21","35.242.44.0/24","2600:1900:4160::/44","34.0.160.0/19","34.153.38.0/24","34.153.230.0/24","34.154.0.0/16","34.157.8.0/23","34.157.121.0/24","34.157.136.0/23","34.157.249.0/24","35.219.224.0/19","2600:1901:8110::/44","34.1.0.0/20","34.155.0.0/16","34.157.12.0/22","34.157.140.0/22","34.163.0.0/16","2600:1901:8120::/44","34.8.0.0/16","34.36.0.0/16","34.49.0.0/16","34.54.0.0/16","34.95.64.0/18","34.96.64.0/18","34.98.64.0/18","34.102.128.0/17","34.104.27.0/24","34.107.128.0/17","34.110.128.0/17","34.111.0.0/16","34.116.0.0/21","34.117.0.0/16","34.120.0.0/16","34.128.128.0/18","34.144.192.0/18","34.149.0.0/16","34.160.0.0/16","35.186.192.0/18","35.190.0.0/18","35.190.64.0/19","35.190.112.0/20","35.201.64.0/18","35.227.192.0/18","35.241.0.0/18","35.244.128.0/17","107.178.240.0/20","130.211.4.0/22","130.211.8.0/21","130.211.16.0/20","130.211.32.0/20","2600:1901::/48","34.1.32.0/20","34.18.0.0/16","34.157.126.0/23","34.157.252.0/23","2600:1901:81c0::/44","34.1.48.0/20","34.152.84.0/23","34.166.0.0/16","34.177.48.0/23","2600:1900:5400::/44","34.0.64.0/19","34.157.90.0/23","34.157.216.0/23","34.165.0.0/16","2600:1901:8160::/44","34.19.128.0/17","34.20.0.0/17","34.47.0.0/18","34.95.0.0/18","34.104.76.0/22","34.118.128.0/18","34.124.12.0/22","34.128.37.0/24","34.128.42.0/23","34.128.49.0/24","34.128.58.0/23","34.152.0.0/18","35.203.0.0/17","35.215.0.0/18","35.220.43.0/24","35.234.240.0/20","35.242.43.0/24","2600:1900:40e0::/44","34.0.32.0/20","34.104.114.0/23","34.124.50.0/23","34.124.112.0/20","34.130.0.0/16","34.152.69.0/24","34.157.255.0/24","2600:1900:41e0::/44","34.2.0.0/20","34.51.0.0/17","34.153.42.0/23","34.153.234.0/23","2600:1900:4290::/44","34.39.128.0/17","34.95.128.0/17","34.104.80.0/21","34.124.16.0/21","34.151.0.0/18","34.151.192.0/18","35.198.0.0/18","35.199.64.0/18","35.215.192.0/18","35.220.40.0/24","35.235.0.0/20","35.242.40.0/24","35.247.192.0/18","2600:1900:40f0::/44","34.0.48.0/20","34.104.50.0/23","34.127.178.0/23","34.153.33.0/24","34.153.225.0/24","34.176.0.0/16","2600:1901:4010::/44","8.34.210.0/24","8.34.212.0/22","8.34.216.0/22","8.35.192.0/21","23.236.48.0/20","23.251.144.0/20","34.0.225.0/24","34.9.0.0/16","34.10.0.0/16","34.13.68.0/22","34.16.0.0/17","34.27.0.0/16","34.28.0.0/14","34.33.0.0/16","34.41.0.0/16","34.42.0.0/16","34.44.0.0/15","34.46.0.0/16","34.55.0.0/16","34.56.0.0/14","34.60.0.0/15","34.63.0.0/16","34.66.0.0/15","34.68.0.0/14","34.72.0.0/16","34.118.200.0/21","34.121.0.0/16","34.122.0.0/15","34.128.32.0/22","34.132.0.0/14","34.136.0.0/16","34.153.48.0/21","34.153.240.0/21","34.157.84.0/23","34.157.96.0/20","34.157.212.0/23","34.157.224.0/20","34.170.0.0/15","34.172.0.0/15","34.177.52.0/22","35.184.0.0/16","35.188.0.0/17","35.188.128.0/18","35.188.192.0/19","35.192.0.0/15","35.194.0.0/18","35.202.0.0/16","35.206.64.0/18","35.208.0.0/15","35.220.64.0/19","35.222.0.0/15","35.224.0.0/15","35.226.0.0/16","35.232.0.0/16","35.238.0.0/15","35.242.96.0/19","104.154.16.0/20","104.154.32.0/19","104.154.64.0/19","104.154.96.0/20","104.154.113.0/24","104.154.114.0/23","104.154.116.0/22","104.154.120.0/23","104.154.128.0/17","104.155.128.0/18","104.197.0.0/16","104.198.16.0/20","104.198.32.0/19","104.198.64.0/20","104.198.128.0/17","107.178.208.0/20","108.59.80.0/21","130.211.112.0/20","130.211.128.0/18","130.211.192.0/19","130.211.224.0/20","146.148.32.0/19","146.148.64.0/19","146.148.96.0/20","162.222.176.0/21","173.255.112.0/21","199.192.115.0/24","199.223.232.0/22","199.223.236.0/24","2600:1900:4000::/44","34.22.0.0/19","35.186.0.0/17","35.186.128.0/20","35.206.32.0/19","35.220.46.0/24","35.242.46.0/24","107.167.160.0/20","108.59.88.0/21","173.255.120.0/21","2600:1900:4070::/44","34.23.0.0/16","34.24.0.0/15","34.26.0.0/16","34.73.0.0/16","34.74.0.0/15","34.98.128.0/21","34.112.0.0/16","34.118.250.0/23","34.138.0.0/15","34.148.0.0/16","34.152.72.0/21","34.177.40.0/21","35.185.0.0/17","35.190.128.0/18","35.196.0.0/16","35.207.0.0/18","35.211.0.0/16","35.220.0.0/20","35.227.0.0/17","35.229.16.0/20","35.229.32.0/19","35.229.64.0/18","35.231.0.0/16","35.237.0.0/16","35.242.0.0/20","35.243.128.0/17","104.196.0.0/18","104.196.65.0/24","104.196.66.0/23","104.196.68.0/22","104.196.96.0/19","104.196.128.0/18","104.196.192.0/19","162.216.148.0/22","2600:1900:4020::/44","34.11.0.0/17","34.21.0.0/17","34.48.0.0/16","34.85.128.0/17","34.86.0.0/16","34.104.60.0/23","34.104.124.0/23","34.118.252.0/23","34.124.60.0/23","34.127.188.0/23","34.145.128.0/17","34.150.128.0/17","34.157.0.0/21","34.157.16.0/20","34.157.128.0/21","34.157.144.0/20","34.181.128.0/17","34.182.128.0/17","35.186.160.0/19","35.188.224.0/19","35.194.64.0/19","35.199.0.0/18","35.212.0.0/17","35.220.60.0/22","35.221.0.0/18","35.230.160.0/19","35.234.176.0/20","35.236.192.0/18","35.242.60.0/22","35.243.40.0/21","35.245.0.0/16","2600:1900:4090::/44","34.1.16.0/20","34.157.32.0/22","34.157.160.0/22","34.162.0.0/16","2600:1901:8130::/44","34.104.56.0/23","34.127.184.0/23","34.161.0.0/16","35.206.10.0/23","2600:1901:8150::/44","34.0.128.0/19","34.157.46.0/23","34.157.174.0/23","34.174.0.0/16","2600:1901:8140::/44","34.11.128.0/17","34.19.0.0/17","34.53.0.0/17","34.82.0.0/15","34.105.0.0/17","34.118.192.0/21","34.127.0.0/17","34.145.0.0/17","34.157.112.0/21","34.157.240.0/21","34.168.0.0/15","34.182.0.0/17","35.185.192.0/18","35.197.0.0/17","35.199.144.0/20","35.199.160.0/19","35.203.128.0/18","35.212.128.0/17","35.220.48.0/21","35.227.128.0/18","35.230.0.0/17","35.233.128.0/17","35.242.48.0/21","35.243.32.0/21","35.247.0.0/17","104.196.224.0/19","104.198.0.0/20","104.198.96.0/20","104.199.112.0/20","2600:1900:4040::/44","34.20.128.0/17","34.94.0.0/16","34.102.0.0/17","34.104.64.0/21","34.108.0.0/16","34.118.248.0/23","34.124.0.0/21","35.215.64.0/18","35.220.47.0/24","35.235.64.0/18","35.236.0.0/17","35.242.47.0/24","35.243.0.0/21","2600:1900:4120::/44","34.22.32.0/19","34.104.52.0/24","34.106.0.0/16","34.127.180.0/24","35.217.64.0/18","35.220.31.0/24","35.242.31.0/24","2600:1900:4170::/44","34.16.128.0/17","34.50.160.0/19","34.104.72.0/22","34.118.240.0/22","34.124.8.0/22","34.125.0.0/16","35.219.128.0/18","2600:1900:4180::/44","34.37.0.0/16","34.128.46.0/23","34.128.62.0/23","2600:1900:4280::/44"]

    const apiIpRanges = excludeCidr(allIpRanges, gcpIpRanges)

    aipIpRanges.forEach(ipRange => console.log(ipRange))
})();

But when I run this (via node xxx.js) I get the following error:

file:///home/xxxxxxx/node_modules/cidr-tools/dist/index.js:136
        start = (part.end / biggest - 1n) * biggest;
                          ^
RangeError: Division by zero
    at subparts (file:///home/xxxxxxx/node_modules/cidr-tools/dist/index.js:136:27)
    at subparts (file:///home/xxxxxxx/node_modules/cidr-tools/dist/index.js:149:26)
    at doMerge (file:///home/xxxxxxx/node_modules/cidr-tools/dist/index.js:187:25)
    at mergeCidr (file:///home/xxxxxxx/node_modules/cidr-tools/dist/index.js:205:17)
    at excludeCidr (file:///home/xxxxxxx/node_modules/cidr-tools/dist/index.js:211:20)
    at /home/xxxxxxx/simplified_ip_ranges.js:7:25

Node.js v18.19.0

I would really appreciate if you could help me with this, because the calculations are quite a bit away from my comfort zone...

Thanks!!
Bart

@silverwind silverwind added the bug label Oct 21, 2024
@silverwind
Copy link
Owner

silverwind commented Oct 21, 2024

Definitely a bug. These input lists likely can likely to be divided down until a minimum remains that reproduces the error. Maybe you can help with that, otherwise I will try that myself later.

@bartbutenaers
Copy link
Author

Hi @silverwind,
Well it was my initial plan to reduce both lists to the minimum, so that you wouldn't have to do that. But I failed doing that. So I tried again tonight, and I see that it fails for index 3:

image

But unfortunately I don't see the relation between these elements and the ones in my original array. So I am afraid my knowledge about the topic is a bit to limited, in order to be able to simplify the example further ;-(

@jmarcgit
Copy link

jmarcgit commented Nov 27, 2024

@bartbutenaers , I think your problem happens because you mix up ipv4 and ipv6 addresses in your arrays. I process ipv4 and ipv6 ranges in two distinct steps and I have no problem at all. Excluding ipv6 ranges from ipv4 ranges (or the opposite) doesn't make any sense. You have to build a list of ipv4 ranges and a list of ipv6 ranges for each json file and process them separately. My code here :

https://github.com/jmarcgit/google-nftables/blob/main/google-nftables.mjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants