CodeQL documentation

Insecure basic authentication

ID: java/insecure-basic-auth
Kind: path-problem
Severity: warning
Precision: medium
   - security
   - external/cwe/cwe-522
   - external/cwe/cwe-319
Query suites:
   - java-security-extended.qls
   - java-security-and-quality.qls

Click to see the query in the CodeQL repository

Basic authentication only obfuscates usernames and passwords in Base64 encoding, which can be easily recognized and reversed, thus it must not be transmitted over the cleartext HTTP channel. Transmitting sensitive information without using HTTPS makes the data vulnerable to packet sniffing.


Either use a more secure authentication mechanism like digest authentication or federated authentication, or use the HTTPS communication protocol.


The following example shows two ways of using basic authentication. In the ‘BAD’ case, the credentials are transmitted over HTTP. In the ‘GOOD’ case, the credentials are transmitted over HTTPS.

public class InsecureBasicAuth {
   * Test basic authentication with Apache HTTP request.
  public void testApacheHttpRequest(String username, String password) {

    // BAD: basic authentication over HTTP
    String url = "";

    // GOOD: basic authentication over HTTPS
    url = "";

    HttpPost post = new HttpPost(url);
    post.setHeader("Accept", "application/json");
    post.setHeader("Content-type", "application/json");

    String authString = username + ":" + password;
    byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
    String authStringEnc = new String(authEncBytes);

    post.addHeader("Authorization", "Basic " + authStringEnc);

   * Test basic authentication with Java HTTP URL connection.
  public void testHttpUrlConnection(String username, String password) {

    // BAD: basic authentication over HTTP
    String urlStr = "";

    // GOOD: basic authentication over HTTPS
    urlStr = "";

    String authString = username + ":" + password;
    String encoding = Base64.getEncoder().encodeToString(authString.getBytes("UTF-8"));
    URL url = new URL(urlStr);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty("Authorization", "Basic " + encoding);


  • © GitHub, Inc.
  • Terms
  • Privacy