An IP address is written in the code as a string

tx0/java:hardcoded-ip-addressstrictwarningsmell

What it finds

An IP address written in the code, as "10.0.12.7", ties the program to one network: moving a service means a new release, and the address is visible to everyone who reads the code. Configuration keeps it apart.

Reported are string literals that are an IPv4 address, optionally with a port. Addresses that are the same everywhere are left alone: the loopback range 127.x.x.x, 0.0.0.0, the broadcast address and the documentation ranges 192.0.2.x, 198.51.100.x and 203.0.113.x.

From the rule's source, hardcoded_ip.rs.

Example from the rule's tests
class Servers {  String primary = "10.0.12.7";tx0 reports: the address 10.0.12.7 is written in the code  String withPort = "172.16.0.4:9200";tx0 reports: the address 172.16.0.4:9200  String local = "127.0.0.1";  String any = "0.0.0.0";  String version = "1.2.3";  String docs = "192.0.2.10";}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:hardcoded-ip-address" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:hardcoded-ip-address] reason="..."

tx0 explain tx0/java:hardcoded-ip-address shows the same in the terminal. Found a false positive? Report it.