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

org.sonar.l10n.xml.rules.xml.S2647.html Maven / Gradle / Ivy

There is a newer version: 2.13.0.5938
Show newest version

This rule is deprecated, and will eventually be removed.

Basic authentication is a vulnerable method of user authentication that should be avoided. It functions by transmitting a Base64 encoded username and password. As Base64 is easy to recognize and reverse, sensitive data may be leaked this way.

Why is this an issue?

Basic authentication is a simple and widely used method of user authentication for HTTP requests. When a client sends a request to a server that requires authentication, the client includes the username and password (concatenated together and Base64 encoded) in the "Authorization" header of the HTTP request. The server verifies the credentials and grants access if they are valid. Every request sent to the server to a protected endpoint must include these credentials.

Basic authentication is considered insecure for several reasons:

  • It transmits user credentials in plain text, making them susceptible to interception and eavesdropping.
  • It relies solely on the server’s ability to verify the provided credentials. There is no mechanism for additional security measures like multi-factor authentication or account lockouts after multiple failed login attempts.
  • It does not provide a way to manage user sessions securely. The client typically includes the credentials in every request, which creates more opportunities for an attacker to steal these credentials.

These security limitations make basic authentication an insecure choice for authentication or authorization over HTTP.

What is the potential impact?

Basic authentication transmits passwords in plain text, which makes it vulnerable to interception by attackers.

Session hijacking and man-in-the-middle attack

If an attacker gains access to the network traffic, they can easily capture the username and password. Basic authentication does not provide any mechanism to protect against session hijacking attacks. Once a user is authenticated, the session identifier (the username and password) is sent in clear text with each subsequent request. If attackers can intercept one request, they can use it to impersonate the authenticated user, gaining unauthorized access to their account and potentially performing malicious actions.

Brute-force attacks

Basic authentication does not have any built-in protection against brute-force attacks. Attackers can repeatedly guess passwords until they find the correct one, especially if weak or commonly used passwords are used. This can lead to unauthorized access to user accounts and potential data breaches.

How to fix it in Java EE

Code examples

The following code uses basic authentication to protect web server endpoints.

Noncompliant code example

<!-- web.xml -->
<web-app>
  <login-config>
    <auth-method>BASIC</auth-method>
  </login-config>
</web-app>

Compliant solution

<!-- web.xml -->
<web-app>
  <login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
      <form-login-page>/login.jsp</form-login-page>
      <form-error-page>/login-error.jsp</form-error-page>
    </form-login-config>
  </login-config>
</web-app>

How does this work?

Token-based authentication and OAuth

Token-based authentication is a safer alternative than basic authentication. A unique token is generated upon successful authentication and sent to the client, which is then included in subsequent requests. Therefore, it eliminates the need to transmit sensitive credentials with each request. OAuth also works by authenticating users via tokens. It gives even more flexibility on top of this by offering scopes, which limit an application’s access to a user’s account.

Additionally, both token-based authentication and OAuth support mechanisms for token expiration, revocation, and refresh. This gives more flexibility than basic authentication, as compromised tokens carry much less risk than a compromised password.

The Jakarta EE Security API offers robust and standardized methods to handle authentication and authorization in Jakarta EE applications. In the example, form-based authentication is applied to the web.xml configuration file. After a user successfully logs into the application, a session is created for the user. A session token is stored in a cookie and is used for subsequent requests.

Integrate with an Identity and Access Management (IAM) System

For more advanced authentication and authorization capabilities, consider integrating the backend with an IAM system. Doing so gives access to features like single sign-on (SSO), role-based access control, and centralized user management. As of Jakarta EE 10, support for OpenID Connect (OIDC) is included. Using this authentication method, several OIDC providers can be integrated easily, such as Auth0, Okta, and Azure Active Directory.

SSL encryption for HTTP requests

With basic authentication, user credentials are transmitted in plain text, which makes them vulnerable to interception and eavesdropping. However, when HTTPS is employed, the data is encrypted before transmission, making it significantly more difficult for attackers to intercept and decipher the credentials. If no other form of authentication is possible for this code, then every request must be sent over HTTPS to ensure credentials are kept safe.

In Jakarta EE, HTTPS traffic can be enabled by setting the transportGuarantee attribute to CONFIDENTIAL in web.xml.

Resources

Documentation

Standards





© 2015 - 2025 Weber Informatics LLC | Privacy Policy