All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opsbears.webcomponents.net.InvalidIPAddressFormatException Maven / Gradle / Ivy

The newest version!
package com.opsbears.webcomponents.net;

import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import java.security.InvalidParameterException;

/**
 * This exception indicates that the IP address passed has an invalid format. This usually happens when the passed
 * string is not actually a valid IP address (contains extra characters, is missing some characters or is simply
 * invalid). It is also possible that you are encountering this object because you are creating an `IPv4Address`
 * instance for an IPv6 address and vice versa.
 *
 * Generally speaking, you should not encounter this error if you are instantiating the correct IP address object from a
 * trusted source. When creating IP address representations from strings that come from untrusted sources, you may
 * encounter this error and should display a proper error message to the user.
 *
 * This exception can also be a good indicator of a bug. For example, when you are parsing the X-Forwarded-For header,
 * this exception can indicate that the user manipulated the header to include something that is not an IP address. You
 * should not trust the X-Forwarded-For header, unless your webserver filters it OR your program checks the REMOTE_ADDR
 * to be one of your trusted proxies.
 *
 * The offending original address object is available from `getAddress()`.
 *
 * ## Solution:
 *
 * - Use `IPAddressHelper::getFromString()` to create IPAddress objects where you don't know the address type.
 * - If the IP address came from a user source (such as an input field), catch this exception and display a useful error
 *   error message.
 * - If the IP address came from a programmatic source and you are using this code to parse untrusted data, catch this
 *   exception in order to handle errors properly.
 * - If the IP address came from a programmatic source and you are using this code to parse trusted data, this error
 *   usually indicates a bug. Follow the path of the IP address and find the source and fix the bug there.
 */
@ParametersAreNonnullByDefault
@Immutable
public class InvalidIPAddressFormatException extends InvalidParameterException {
    private Object address;

    public InvalidIPAddressFormatException(Object address) {
        super("Invalid IP address format: " + (String)address);
        this.address = address;
    }

    public Object getAddress() {
        return address;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy