URL equals/hashCode Triggers DNS Lookup

ID

java.url_equals_hashcode

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

reliability

Description

Reports calls to equals() or hashCode() on a java.net.URL object. Both methods trigger a DNS resolution to compare the host addresses, which is blocking, slow, and unreliable.

Rationale

java.net.URL.equals() and hashCode() resolve the hostname to an IP address to compare URLs. This makes the result dependent on DNS availability and network configuration, and can cause unexpected blocking and performance degradation.

// Bad -- triggers DNS resolution
URL a = new URL("http://example.com");
URL b = new URL("http://example.com");
a.equals(b); // DNS lookup!

Remediation

Use java.net.URI instead, which performs pure string comparison.

URI a = new URI("http://example.com");
URI b = new URI("http://example.com");
a.equals(b); // No DNS lookup