Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
static boolean hostMatchesCidr(String host, String cidrPattern) {
    if (cidrPattern == null || !cidrPattern.contains("/")) {
        return false;
    }

    try  try {{
		InetAddress address = CidrUtils.parseCidrAddress(cidrPattern);
        if (cidrPatternCidrUtils.containsisIpv6(":"address)) {
			return new SubnetUtils6(cidrPattern).getInfo().isInRange(host);
        } else SubnetUtils6{
 subnet = new SubnetUtils6(cidrPattern);
            return subnet.getInfo().isInRange(host);
        } else {
            	SubnetUtils subnet = new SubnetUtils(cidrPattern);
            subnet.setInclusiveHostCount(true);
            return subnet.getInfo().isInRange(host);
        }
	}    } catch (IllegalArgumentException e) {
    	return false;
	}
}


// parseCidrAddress will strip prefix length i.e., from "192.168.0.0/24" we do "192.168.0.0" before returnits false;
passed    }
}into InetAddress.getByName(...). 

2. Validation Rules

When creating ACLs, we validate CIDR patterns in AclControlManager.java:

...

Code Block
languagejava
/**
 * Validates the host pattern of an ACL entry.
 *
 * Accepts:
 * - Wildcard "*" (matches any host)
 * - Valid IPv4 address (e.g., "192.168.1.1")
 * - Valid IPv6 address (e.g., "2001:db8::1")
 * - Valid IPv4 CIDR notation (e.g., "192.168.0.0/24"), which requires cidrSupported=true
 * - Valid IPv6 CIDR notation (e.g., "2001:db8::/32"), which requires cidrSupported=true
 *
 * @param host The host pattern to validate
 * @param cidrSupported Whether CIDR notation is supported by the current metadata version
 * @throws InvalidRequestException if the host pattern is invalid
 * @throws UnsupportedVersionException if CIDR notation is used but not supported
 */
static void validateHostPattern(String host, boolean cidrSupported) {
    if (host == null || host.isEmpty()) {
        throw new InvalidRequestException("Host pattern cannot be null or empty");
    }

    if ("*".equals(host)) {
        return;
    }

    if (host.contains("/")) {
        if (!cidrSupported) {
            throw new UnsupportedVersionException(
                "CIDR-based ACL host patterns require metadata version " +
                MetadataVersion.IBP_4_X_IVZ + " or higher.");
        }
        validateCidrNotation(host);
    }
}

/**
 * Validates a CIDR notation pattern.
 * Supports both IPv4 (e.g., "192.168.0.0/24") and IPv6 (e.g., "2001:db8::/32") CIDR patterns.
 *
 * @param cidrPattern The CIDR pattern to validate
 * @throws InvalidRequestException if the CIDR pattern is invalid
 */
static void validateCidrNotation(String cidrPattern) {
      try {
    	InetAddress address   if = CidrUtils.parseCidrAddress(cidrPattern.contains(":")) {);
        if    (CidrUtils.isIpv6(address)) {
			new SubnetUtils6(cidrPattern);
        		} else {
            			new SubnetUtils(cidrPattern);
        		}
    	} catch (IllegalArgumentException e) {
        
		throw new InvalidRequestException("Invalid CIDR notation '" + cidrPattern + "': " + e.getMessage());
    }
}

This ensures that (i.) IPv4 prefix length is 0-32 (enforced by SubnetUtils); (ii.) IPv6 prefix length is 0-128 (enforced by SubnetUtils6) and (iii.) invalid patterns are rejected with clear error message.

...