|
| 1 | +// Validate subnet format and values |
| 2 | +export function isValidSubnet(subnet: string): boolean { |
| 3 | + try { |
| 4 | + // Handle single IP addresses (treat as /32) |
| 5 | + if (!subnet.includes("/")) { |
| 6 | + return isValidIPAddress(subnet); |
| 7 | + } |
| 8 | + |
| 9 | + // Parse the subnet (e.g., "10.0.5.0/24") |
| 10 | + const [subnetIp, prefixLength] = subnet.split("/"); |
| 11 | + |
| 12 | + if (!subnetIp || !prefixLength) { |
| 13 | + return false; |
| 14 | + } |
| 15 | + |
| 16 | + // Validate prefix length |
| 17 | + const prefix = parseInt(prefixLength, 10); |
| 18 | + if (isNaN(prefix) || prefix < 0 || prefix > 32) { |
| 19 | + return false; |
| 20 | + } |
| 21 | + |
| 22 | + // Validate IP address format |
| 23 | + if (!isValidIPAddress(subnetIp)) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + // Check if the IP is a valid network address |
| 28 | + const ipToInt = (ipAddr: string): number => { |
| 29 | + const parts = ipAddr.split("."); |
| 30 | + return ( |
| 31 | + parts.reduce((acc, part) => { |
| 32 | + const num = parseInt(part, 10); |
| 33 | + return (acc << 8) + num; |
| 34 | + }, 0) >>> 0 |
| 35 | + ); |
| 36 | + }; |
| 37 | + |
| 38 | + const subnetIpInt = ipToInt(subnetIp); |
| 39 | + const mask = (0xffffffff << (32 - prefix)) >>> 0; |
| 40 | + const networkAddress = (subnetIpInt & mask) >>> 0; |
| 41 | + |
| 42 | + // Check if the provided IP is actually the network address |
| 43 | + // Comment out these lines if you want to allow any IP in subnet notation |
| 44 | + if (subnetIpInt !== networkAddress) { |
| 45 | + console.warn( |
| 46 | + `IP ${subnetIp} is not a network address for /${prefix}. Expected: ${intToIp(networkAddress)}`, |
| 47 | + ); |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + return true; |
| 52 | + } catch (error) { |
| 53 | + console.error( |
| 54 | + `Error checking subnet: ${error instanceof Error ? error.message : "Unknown error"}`, |
| 55 | + ); |
| 56 | + return false; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Utility export function to validate IP address format |
| 61 | +// Taken from https://stackoverflow.com/a/36760050 |
| 62 | +export function isValidIPAddress(ip: string): boolean { |
| 63 | + const ipRegex = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/; |
| 64 | + return ipRegex.test(ip); |
| 65 | +} |
| 66 | + |
| 67 | +// Helper export function to convert integer back to IP string |
| 68 | +export function intToIp(int: number): string { |
| 69 | + return [ |
| 70 | + (int >>> 24) & 255, |
| 71 | + (int >>> 16) & 255, |
| 72 | + (int >>> 8) & 255, |
| 73 | + int & 255, |
| 74 | + ].join("."); |
| 75 | +} |
| 76 | + |
| 77 | +// Robust subnet check with proper CIDR notation support |
| 78 | +export function isInBlockedSubnet(ip: string, subnet: string): boolean { |
| 79 | + if (!ip || !subnet) return false; |
| 80 | + |
| 81 | + // Validate inputs first |
| 82 | + if (!isValidIPAddress(ip)) { |
| 83 | + console.warn(`Invalid IP address format: ${ip}`); |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + if (!isValidSubnet(subnet)) { |
| 88 | + console.warn(`Invalid subnet format: ${subnet}`); |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + try { |
| 93 | + // Normalize subnet (add /32 for single IPs) |
| 94 | + const normalizedSubnet = subnet.includes("/") ? subnet : `${subnet}/32`; |
| 95 | + const [subnetIp, prefixLength] = normalizedSubnet.split("/"); |
| 96 | + const prefix = parseInt(prefixLength, 10); |
| 97 | + |
| 98 | + // Convert IP addresses to 32-bit integers |
| 99 | + const ipToInt = (ipAddr: string): number => { |
| 100 | + const parts = ipAddr.split("."); |
| 101 | + return ( |
| 102 | + parts.reduce((acc, part) => { |
| 103 | + const num = parseInt(part, 10); |
| 104 | + return (acc << 8) + num; |
| 105 | + }, 0) >>> 0 |
| 106 | + ); |
| 107 | + }; |
| 108 | + |
| 109 | + const targetIpInt = ipToInt(ip); |
| 110 | + const subnetIpInt = ipToInt(subnetIp); |
| 111 | + |
| 112 | + // Create subnet mask |
| 113 | + const mask = (0xffffffff << (32 - prefix)) >>> 0; |
| 114 | + |
| 115 | + // Check if the IP is in the subnet |
| 116 | + return (targetIpInt & mask) === (subnetIpInt & mask); |
| 117 | + } catch (error) { |
| 118 | + console.error( |
| 119 | + `Error checking subnet: ${error instanceof Error ? error.message : "Unknown error"}`, |
| 120 | + ); |
| 121 | + return false; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// Enhanced version with support for multiple subnets |
| 126 | +export function isInAnyBlockedSubnet(ip: string, subnets: string[]): boolean { |
| 127 | + if (subnets.length === 0) return false; |
| 128 | + |
| 129 | + // Validate IP once |
| 130 | + if (!isValidIPAddress(ip)) { |
| 131 | + console.warn(`Invalid IP address format: ${ip}`); |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + // Filter valid subnets and check |
| 136 | + const validSubnets = subnets.filter((subnet) => { |
| 137 | + const isValid = isValidSubnet(subnet); |
| 138 | + if (!isValid) { |
| 139 | + console.warn(`Skipping invalid subnet: ${subnet}`); |
| 140 | + } |
| 141 | + return isValid; |
| 142 | + }); |
| 143 | + |
| 144 | + return validSubnets.some((subnet) => isInBlockedSubnet(ip, subnet)); |
| 145 | +} |
0 commit comments