Language: Java
Label: safe
CWE: CWE-918 \u2014 Server-Side Request Forgery (SSRF)
The web server receives a URL or similar request from an upstream component and retrieves the contents of this URL, but it does not sufficiently ensure that the request is being sent to the expected destination.

Code:
    public static InetAddressRange parse(String addressStr, String prefixSizeStr) {
        int prefixSize;
        try {
            prefixSize = Integer.parseInt(prefixSizeStr);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException(String.format("Malformed address range entry '%s': Cannot extract size of CIDR mask from '%s'.",
                    addressStr + '/' + prefixSizeStr, prefixSizeStr));
        }

        InetAddress address;
        try {
            address = InetAddresses.forString(addressStr);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("Malformed address range entry '%s': Cannot extract IP address from '%s'.",
                    addressStr + '/' + prefixSizeStr, addressStr));
        }

        // Mask the bytes of the IP address.
        byte[] minBytes = address.getAddress(), maxBytes = address.getAddress();
        int size = prefixSize;
        for (int i = 0; i < minBytes.length; i++) {
            if (size <= 0) {
                minBytes[i] = (byte) 0;
                maxBytes[i] = (byte) 0xFF;
            } else if (size < 8) {
                minBytes[i] = (byte) (minBytes[i] & 0xFF << (8 - size));
                maxBytes[i] = (byte) (maxBytes[i] | ~(0xFF << (8 - size)));
            }

            size -= 8;
        }

        return new InetAddressRange(minBytes, maxBytes);
    }