|
| 1 | +<p>This rule is deprecated, and will eventually be removed.</p> |
| 2 | +<p>Basic authentication is a vulnerable method of user authentication that should be avoided. It functions by transmitting a Base64 encoded username |
| 3 | +and password. As Base64 is easy to recognize and reverse, sensitive data may be leaked this way.</p> |
1 | 4 | <h2>Why is this an issue?</h2> |
2 | | -<p>Basic authentication’s only means of obfuscation is Base64 encoding. Since Base64 encoding is easily recognized and reversed, it offers only the |
3 | | -thinnest veil of protection to your users, and should not be used.</p> |
4 | | -<h3>Noncompliant code example</h3> |
5 | | -<pre> |
6 | | -// in web.xml |
7 | | -<web-app ...> |
8 | | - <!-- ... --> |
| 5 | +<p>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 |
| 6 | +requires authentication, the client includes the username and password (concatenated together and Base64 encoded) in the "Authorization" header of the |
| 7 | +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 |
| 8 | +include these credentials.</p> |
| 9 | +<p>Basic authentication is considered insecure for several reasons:</p> |
| 10 | +<ul> |
| 11 | + <li> It transmits user credentials in plain text, making them susceptible to interception and eavesdropping. </li> |
| 12 | + <li> It relies solely on the server’s ability to verify the provided credentials. There is no mechanism for additional security measures like |
| 13 | + multi-factor authentication or account lockouts after multiple failed login attempts. </li> |
| 14 | + <li> It does not provide a way to manage user sessions securely. The client typically includes the credentials in every request, which creates more |
| 15 | + opportunities for an attacker to steal these credentials. </li> |
| 16 | +</ul> |
| 17 | +<p>These security limitations make basic authentication an insecure choice for authentication or authorization over HTTP.</p> |
| 18 | +<h3>What is the potential impact?</h3> |
| 19 | +<p>Basic authentication transmits passwords in plain text, which makes it vulnerable to interception by attackers.</p> |
| 20 | +<h4>Session hijacking and man-in-the-middle attack</h4> |
| 21 | +<p>If an attacker gains access to the network traffic, they can easily capture the username and password. Basic authentication does not provide any |
| 22 | +mechanism to protect against session hijacking attacks. Once a user is authenticated, the session identifier (the username and password) is sent in |
| 23 | +clear text with each subsequent request. If attackers can intercept one request, they can use it to impersonate the authenticated user, gaining |
| 24 | +unauthorized access to their account and potentially performing malicious actions.</p> |
| 25 | +<h4>Brute-force attacks</h4> |
| 26 | +<p>Basic authentication does not have any built-in protection against brute-force attacks. Attackers can repeatedly guess passwords until they find |
| 27 | +the correct one, especially if weak or commonly used passwords are used. This can lead to unauthorized access to user accounts and potential data |
| 28 | +breaches.</p> |
| 29 | +<h2>How to fix it in Java EE</h2> |
| 30 | +<h3>Code examples</h3> |
| 31 | +<p>The following code uses basic authentication to protect web server endpoints.</p> |
| 32 | +<h4>Noncompliant code example</h4> |
| 33 | +<pre data-diff-id="201" data-diff-type="noncompliant"> |
| 34 | +<!-- web.xml --> |
| 35 | +<web-app> |
9 | 36 | <login-config> |
10 | 37 | <auth-method>BASIC</auth-method> |
11 | 38 | </login-config> |
12 | 39 | </web-app> |
13 | 40 | </pre> |
14 | | -<h3>Exceptions</h3> |
15 | | -<p>The rule will not raise any issue if HTTPS is enabled, on any URL-pattern.</p> |
16 | | -<pre> |
17 | | -<web-app ...> |
18 | | - <!-- ... --> |
19 | | - <security-constraint> |
20 | | - <web-resource-collection> |
21 | | - <web-resource-name>HTTPS enabled</web-resource-name> |
22 | | - <url-pattern>/*</url-pattern> |
23 | | - </web-resource-collection> |
24 | | - <user-data-constraint> |
25 | | - <transport-guarantee>CONFIDENTIAL</transport-guarantee> |
26 | | - </user-data-constraint> |
27 | | - </security-constraint> |
| 41 | +<h4>Compliant solution</h4> |
| 42 | +<pre data-diff-id="201" data-diff-type="compliant"> |
| 43 | +<!-- web.xml --> |
| 44 | +<web-app> |
| 45 | + <login-config> |
| 46 | + <auth-method>FORM</auth-method> |
| 47 | + <form-login-config> |
| 48 | + <form-login-page>/login.jsp</form-login-page> |
| 49 | + <form-error-page>/login-error.jsp</form-error-page> |
| 50 | + </form-login-config> |
| 51 | + </login-config> |
28 | 52 | </web-app> |
29 | 53 | </pre> |
| 54 | +<h3>How does this work?</h3> |
| 55 | +<h4>Token-based authentication and OAuth</h4> |
| 56 | +<p>Token-based authentication is a safer alternative than basic authentication. A unique token is generated upon successful authentication and sent to |
| 57 | +the client, which is then included in subsequent requests. Therefore, it eliminates the need to transmit sensitive credentials with each request. |
| 58 | +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 |
| 59 | +access to a user’s account.</p> |
| 60 | +<p>Additionally, both token-based authentication and OAuth support mechanisms for token expiration, revocation, and refresh. This gives more |
| 61 | +flexibility than basic authentication, as compromised tokens carry much less risk than a compromised password.</p> |
| 62 | +<p>The Jakarta EE Security API offers robust and standardized methods to handle authentication and authorization in Jakarta EE applications. In the |
| 63 | +example, form-based authentication is applied to the <code>web.xml</code> configuration file. After a user successfully logs into the application, a |
| 64 | +session is created for the user. A session token is stored in a cookie and is used for subsequent requests.</p> |
| 65 | +<h4>Integrate with an Identity and Access Management (IAM) System</h4> |
| 66 | +<p>For more advanced authentication and authorization capabilities, consider integrating the backend with an IAM system. Doing so gives access to |
| 67 | +features like single sign-on (SSO), role-based access control, and centralized user management. As of Jakarta EE 10, support for OpenID Connect (OIDC) |
| 68 | +is included. Using this authentication method, several OIDC providers can be integrated easily, such as Auth0, Okta, and Azure Active Directory.</p> |
| 69 | +<h4>SSL encryption for HTTP requests</h4> |
| 70 | +<p>With basic authentication, user credentials are transmitted in plain text, which makes them vulnerable to interception and eavesdropping. However, |
| 71 | +when HTTPS is employed, the data is encrypted before transmission, making it significantly more difficult for attackers to intercept and decipher the |
| 72 | +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 |
| 73 | +safe.</p> |
| 74 | +<p>In Jakarta EE, HTTPS traffic can be enabled by setting the <code>transportGuarantee</code> attribute to <code>CONFIDENTIAL</code> in |
| 75 | +<code>web.xml</code>.</p> |
30 | 76 | <h2>Resources</h2> |
| 77 | +<h3>Documentation</h3> |
| 78 | +<ul> |
| 79 | + <li> MDN web docs - <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication">HTTP authentication</a> </li> |
| 80 | +</ul> |
| 81 | +<h3>Standards</h3> |
31 | 82 | <ul> |
32 | | - <li> <a href="https://owasp.org/Top10/A04_2021-Insecure_Design/">OWASP Top 10 2021 Category A4</a> - Insecure Design </li> |
33 | | - <li> <a href="https://www.owasp.org/www-project-top-ten/2017/A3_2017-Sensitive_Data_Exposure">OWASP Top 10 2017 Category A3</a> - Sensitive Data |
34 | | - Exposure </li> |
| 83 | + <li> OWASP - <a href="https://owasp.org/Top10/A04_2021-Insecure_Design/">Top 10 2021 Category A4 - Insecure Design</a> </li> |
| 84 | + <li> OWASP - <a href="https://owasp.org/www-project-top-ten/2017/A3_2017-Sensitive_Data_Exposure">Top 10 2017 Category A3 - Sensitive Data |
| 85 | + Exposure</a> </li> |
35 | 86 | <li> <a href="https://cheatsheetseries.owasp.org/cheatsheets/Web_Service_Security_Cheat_Sheet.html#user-authentication">OWASP Web Service Security |
36 | 87 | Cheat Sheet</a> </li> |
37 | | - <li> <a href="https://cwe.mitre.org/data/definitions/522">MITRE, CWE-522</a> - Insufficiently Protected Credentials </li> |
38 | | - <li> <a href="https://www.sans.org/top25-software-errors/#cat3">SANS Top 25</a> - Porous Defenses </li> |
| 88 | + <li> CWE - <a href="https://cwe.mitre.org/data/definitions/522">CWE-522 - Insufficiently Protected Credentials</a> </li> |
| 89 | + <li> STIG Viewer - <a href="https://stigviewer.com/stig/application_security_and_development/2023-06-08/finding/V-222533">Application Security and |
| 90 | + Development: V-222533</a> - The application must authenticate all network connected endpoint devices before establishing any connection. </li> |
39 | 91 | </ul> |
40 | 92 |
|
0 commit comments