JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 27.0w AdaCore Inc
Java


JAVA.INSEC.HIC : Hostname in Condition (Java)

Summary

A host name is used in a condition.

Properties

Class Name Hostname in Condition (Java)
Significance security
Mnemonic JAVA.INSEC.HIC
Categories
CWE CWE:287 Improper Authentication
OWASP-2017 OWASP-2017:A2 Broken Authentication
OWASP-2021 OWASP-2021:A7 Identification and Authentication Failures
OWASP-2025 OWASP-2025:A07 Authentication Failures
Availability Available for Java only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Hostname in Condition (Java)"

Example

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class HostnameCheck {

    // Deciding whether a peer is trusted by comparing its resolved host name
    // against a literal is insecure: DNS is spoofable, so an attacker can make
    // getCanonicalHostName() return the expected name for a host they control.
    boolean isTrustedPeer(InetAddress peer) {
        if (peer.getCanonicalHostName().equals("admin.example.com"))  // 'Hostname in Condition (Java)' warning issued here
            return true;
        return false;
    }

    // The secure alternative: authenticate by a fixed numeric address obtained
    // through getAddress(), which does not depend on a spoofable name lookup.
    // A cryptographic check (e.g. a verified TLS certificate) would be stronger
    // still.
    boolean isTrustedAddress(InetAddress peer) throws UnknownHostException {
        byte[] trusted = InetAddress.getByName("203.0.113.7").getAddress();
        return Arrays.equals(peer.getAddress(), trusted);  // ok: decision is not based on the resolved host name
    }
}

To reproduce this example, analyze it with the following configuration:

JAVA_ANALYSIS_ENTRY_POINTS_MODE = ALL_METHODS

Resolution

Check if the warning actually corresponds to a real security issue related to authentication and, if that is the case, use a more secure authentication procedure.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.adacore.com/csm.