|
1 | | -<p>Because it is easy to extract strings from an application source code or binary, passwords should not be hard-coded. This is particularly true for |
2 | | -applications that are distributed or that are open-source.</p> |
| 1 | +<h2>Why is this an issue?</h2> |
| 2 | +<p>Hard-coding credentials in source code or binaries makes it easy for attackers to extract sensitive information, especially in distributed or |
| 3 | +open-source applications. This practice exposes your application to significant security risks.</p> |
| 4 | +<p>This rule flags instances of hard-coded credentials used in database and LDAP connections. It looks for hard-coded credentials in connection |
| 5 | +strings, and for variable names that match any of the patterns from the provided list.</p> |
3 | 6 | <p>In the past, it has led to the following vulnerabilities:</p> |
4 | 7 | <ul> |
5 | 8 | <li> <a href="https://www.cve.org/CVERecord?id=CVE-2019-13466">CVE-2019-13466</a> </li> |
6 | 9 | <li> <a href="https://www.cve.org/CVERecord?id=CVE-2018-15389">CVE-2018-15389</a> </li> |
7 | 10 | </ul> |
8 | | -<p>Passwords should be stored outside of the code in a configuration file, a database, or a password management service.</p> |
9 | | -<p>This rule flags instances of hard-coded passwords used in database and LDAP connections. It looks for hard-coded passwords in connection strings, |
10 | | -and for variable names that match any of the patterns from the provided list.</p> |
11 | | -<h2>Ask Yourself Whether</h2> |
12 | | -<ul> |
13 | | - <li> The password allows access to a sensitive component like a database, a file storage, an API, or a service. </li> |
14 | | - <li> The password is used in production environments. </li> |
15 | | - <li> Application re-distribution is required before updating the password. </li> |
16 | | -</ul> |
17 | | -<p>There would be a risk if you answered yes to any of those questions.</p> |
18 | | -<h2>Recommended Secure Coding Practices</h2> |
19 | | -<ul> |
20 | | - <li> Store the credentials in a configuration file that is not pushed to the code repository. </li> |
21 | | - <li> Store the credentials in a database. </li> |
22 | | - <li> Use your cloud provider’s service for managing secrets. </li> |
23 | | - <li> If a password has been disclosed through the source code: change it. </li> |
24 | | -</ul> |
25 | | -<h2>Sensitive Code Example</h2> |
26 | | -<pre> |
| 11 | +<h2>How to fix it</h2> |
| 12 | +<p>Credentials should be stored in a configuration file that is not committed to the code repository, in a database, or managed by your cloud |
| 13 | +provider’s secrets management service. If a password is exposed in the source code, it must be changed immediately.</p> |
| 14 | +<h3>Code Examples</h3> |
| 15 | +<h4>Noncompliant code example</h4> |
| 16 | +<pre data-diff-id="1" data-diff-type="noncompliant"> |
27 | 17 | String username = "steve"; |
28 | 18 | String password = "blue"; |
29 | 19 | Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?" + |
30 | | - "user=" + username + "&password=" + password); // Sensitive |
| 20 | + "user=" + username + "&password=" + password); // Noncompliant |
31 | 21 | </pre> |
32 | | -<h2>Compliant Solution</h2> |
33 | | -<pre> |
| 22 | +<h4>Compliant solution</h4> |
| 23 | +<pre data-diff-id="1" data-diff-type="compliant"> |
34 | 24 | String username = getEncryptedUser(); |
35 | 25 | String password = getEncryptedPassword(); |
36 | 26 | Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?" + |
37 | 27 | "user=" + username + "&password=" + password); |
38 | 28 | </pre> |
39 | | -<h2>See</h2> |
| 29 | +<h2>Resources</h2> |
40 | 30 | <ul> |
41 | 31 | <li> OWASP - <a href="https://owasp.org/Top10/A07_2021-Identification_and_Authentication_Failures/">Top 10 2021 Category A7 - Identification and |
42 | 32 | Authentication Failures</a> </li> |
43 | 33 | <li> OWASP - <a href="https://owasp.org/www-project-top-ten/2017/A2_2017-Broken_Authentication">Top 10 2017 Category A2 - Broken Authentication</a> |
44 | 34 | </li> |
45 | 35 | <li> CWE - <a href="https://cwe.mitre.org/data/definitions/798">CWE-798 - Use of Hard-coded Credentials</a> </li> |
46 | 36 | <li> CWE - <a href="https://cwe.mitre.org/data/definitions/259">CWE-259 - Use of Hard-coded Password</a> </li> |
47 | | - <li> <a href="https://wiki.sei.cmu.edu/confluence/x/OjdGBQ">CERT, MSC03-J.</a> - Never hard code sensitive information </li> |
48 | 37 | <li> Derived from FindSecBugs rule <a href="https://h3xstream.github.io/find-sec-bugs/bugs.htm#HARD_CODE_PASSWORD">Hard Coded Password</a> </li> |
49 | 38 | </ul> |
50 | 39 |
|
0 commit comments