1
+ < p > When accessing a database, an empty password should be avoided as it introduces a weakness.</ p >
1
2
< h2 > Why is this an issue?</ h2 >
2
- < p > When relying on the password authentication mode for the database connection, a secure password should be chosen.</ p >
3
- < p > This rule raises an issue when an empty password is used.</ p >
4
- < h3 > Noncompliant code example</ h3 >
5
- < p > Flask-SQLAlchemy</ p >
6
- < pre >
3
+ < p > When a database does not require a password for authentication, it allows anyone to access and manipulate the data stored within it. Exploiting
4
+ this vulnerability typically involves identifying the target database and establishing a connection to it without the need for any authentication
5
+ credentials.</ p >
6
+ < h3 > What is the potential impact?</ h3 >
7
+ < p > Once connected, an attacker can perform various malicious actions, such as viewing, modifying, or deleting sensitive information, potentially
8
+ leading to data breaches or unauthorized access to critical systems. It is crucial to address this vulnerability promptly to ensure the security and
9
+ integrity of the database and the data it contains.</ p >
10
+ < h4 > Unauthorized Access to Sensitive Data</ h4 >
11
+ < p > When a database lacks a password for authentication, it opens the door for unauthorized individuals to gain access to sensitive data. This can
12
+ include personally identifiable information (PII), financial records, intellectual property, or any other confidential information stored in the
13
+ database. Without proper access controls in place, malicious actors can exploit this vulnerability to retrieve sensitive data, potentially leading to
14
+ identity theft, financial loss, or reputational damage.</ p >
15
+ < h4 > Compromise of System Integrity</ h4 >
16
+ < p > Without a password requirement, unauthorized individuals can gain unrestricted access to a database, potentially compromising the integrity of the
17
+ entire system. Attackers can inject malicious code, alter configurations, or manipulate data within the database, leading to system malfunctions,
18
+ unauthorized system access, or even complete system compromise. This can disrupt business operations, cause financial losses, and expose the
19
+ organization to further security risks.</ p >
20
+ < h4 > Unwanted Modifications or Deletions</ h4 >
21
+ < p > The absence of a password for database access allows anyone to make modifications or deletions to the data stored within it. This poses a
22
+ significant risk, as unauthorized changes can lead to data corruption, loss of critical information, or the introduction of malicious content. For
23
+ example, an attacker could modify financial records, tamper with customer orders, or delete important files, causing severe disruptions to business
24
+ processes and potentially leading to financial and legal consequences.</ p >
25
+ < p > Overall, the lack of a password configured to access a database poses a serious security risk, enabling unauthorized access, data breaches, system
26
+ compromise, and unwanted modifications or deletions. It is essential to address this vulnerability promptly to safeguard sensitive data, maintain
27
+ system integrity, and protect the organization from potential harm.</ p >
28
+ < h2 > How to fix it in MySQL Connector/Python</ h2 >
29
+ < h3 > Code examples</ h3 >
30
+ < p > The following code uses an empty password to connect to a MySQL database.</ p >
31
+ < p > The vulnerability can be fixed by using a strong password retrieved from an environment variable < code > DB_PASSWORD</ code > . This environment
32
+ variable is set during deployment. It should be strong and different for each database.</ p >
33
+ < h4 > Noncompliant code example</ h4 >
34
+ < pre data-diff-id ="101 " data-diff-type ="noncompliant ">
35
+ from mysql.connector import connection
36
+
37
+ connection.MySQLConnection(host='localhost', user='sonarsource', password='') # Noncompliant
38
+ </ pre >
39
+ < h4 > Compliant solution</ h4 >
40
+ < pre data-diff-id ="101 " data-diff-type ="compliant ">
41
+ from mysql.connector import connection
42
+ import os
43
+
44
+ db_password = os.getenv('DB_PASSWORD')
45
+ connection.MySQLConnection(host='localhost', user='sonarsource', password=db_password)
46
+ </ pre >
47
+ < h3 > Pitfalls</ h3 >
48
+ < h4 > Hard-coded passwords</ h4 >
49
+ < p > It could be tempting to replace the empty password with a hard-coded one. Hard-coding passwords in the code can pose significant security risks.
50
+ Here are a few reasons why it is not recommended:</ p >
51
+ < ol >
52
+ < li > Security Vulnerability: Hard-coded passwords can be easily discovered by anyone who has access to the code, such as other developers or
53
+ attackers. This can lead to unauthorized access to the database and potential data breaches. </ li >
54
+ < li > Lack of Flexibility: Hard-coded passwords make it difficult to change the password without modifying the code. If the password needs to be
55
+ updated, it would require recompiling and redeploying the code, which can be time-consuming and error-prone. </ li >
56
+ < li > Version Control Issues: Storing passwords in code can lead to version control issues. If the code is shared or stored in a version control
57
+ system, the password will be visible to anyone with access to the repository, which is a security risk. </ li >
58
+ </ ol >
59
+ < p > To mitigate these risks, it is recommended to use secure methods for storing and retrieving passwords, such as using environment variables,
60
+ configuration files, or secure key management systems. These methods allow for better security, flexibility, and separation of sensitive information
61
+ from the codebase.</ p >
62
+ < h2 > How to fix it in SQLAlchemy</ h2 >
63
+ < h3 > Code examples</ h3 >
64
+ < p > The following code uses an empty password to connect to a Postgres database.</ p >
65
+ < p > The vulnerability can be fixed by using a strong password retrieved from an environment variable < code > DB_PASSWORD</ code > . This environment
66
+ variable is set during deployment. It should be strong and different for each database.</ p >
67
+ < h4 > Noncompliant code example</ h4 >
68
+ < pre data-diff-id ="103 " data-diff-type ="noncompliant ">
7
69
def configure_app(app):
8
70
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://user:@domain.com" # Noncompliant
9
71
</ pre >
10
- < p > Django</ p >
11
- < pre >
72
+ < h4 > Compliant solution</ h4 >
73
+ < pre data-diff-id ="103 " data-diff-type ="compliant ">
74
+ def configure_app(app):
75
+ db_password = os.getenv('DB_PASSWORD')
76
+ app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://user:{db_password}@domain.com"
77
+ </ pre >
78
+ < h3 > Pitfalls</ h3 >
79
+ < h4 > Hard-coded passwords</ h4 >
80
+ < p > It could be tempting to replace the empty password with a hard-coded one. Hard-coding passwords in the code can pose significant security risks.
81
+ Here are a few reasons why it is not recommended:</ p >
82
+ < ol >
83
+ < li > Security Vulnerability: Hard-coded passwords can be easily discovered by anyone who has access to the code, such as other developers or
84
+ attackers. This can lead to unauthorized access to the database and potential data breaches. </ li >
85
+ < li > Lack of Flexibility: Hard-coded passwords make it difficult to change the password without modifying the code. If the password needs to be
86
+ updated, it would require recompiling and redeploying the code, which can be time-consuming and error-prone. </ li >
87
+ < li > Version Control Issues: Storing passwords in code can lead to version control issues. If the code is shared or stored in a version control
88
+ system, the password will be visible to anyone with access to the repository, which is a security risk. </ li >
89
+ </ ol >
90
+ < p > To mitigate these risks, it is recommended to use secure methods for storing and retrieving passwords, such as using environment variables,
91
+ configuration files, or secure key management systems. These methods allow for better security, flexibility, and separation of sensitive information
92
+ from the codebase.</ p >
93
+ < h2 > How to fix it in Django</ h2 >
94
+ < h3 > Code examples</ h3 >
95
+ < p > The following code uses an empty password to connect to a Postgres database.</ p >
96
+ < p > The vulnerability can be fixed by using a strong password retrieved from an environment variable < code > DB_PASSWORD</ code > . This environment
97
+ variable is set during deployment. It should be strong and different for each database.</ p >
98
+ < h4 > Noncompliant code example</ h4 >
99
+ < pre data-diff-id ="102 " data-diff-type ="noncompliant ">
12
100
# settings.py
13
101
14
102
DATABASES = {
@@ -22,20 +110,8 @@ <h3>Noncompliant code example</h3>
22
110
}
23
111
}
24
112
</ pre >
25
- < p > mysql/mysql-connector-python</ p >
26
- < pre >
27
- from mysql.connector import connection
28
-
29
- connection.MySQLConnection(host='localhost', user='sonarsource', password='') # Noncompliant
30
- </ pre >
31
- < h3 > Compliant solution</ h3 >
32
- < p > Flask-SQLAlchemy</ p >
33
- < pre >
34
- def configure_app(app, pwd):
35
- app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://user:{pwd}@domain.com" # Compliant
36
- </ pre >
37
- < p > Django</ p >
38
- < pre >
113
+ < h4 > Compliant solution</ h4 >
114
+ < pre data-diff-id ="102 " data-diff-type ="compliant ">
39
115
# settings.py
40
116
import os
41
117
@@ -44,21 +120,29 @@ <h3>Compliant solution</h3>
44
120
'ENGINE': 'django.db.backends.postgresql',
45
121
'NAME': 'quickdb',
46
122
'USER': 'sonarsource',
47
- 'PASSWORD': os.getenv('DB_PASSWORD'), # Compliant
123
+ 'PASSWORD': os.getenv('DB_PASSWORD'),
48
124
'HOST': 'localhost',
49
125
'PORT': '5432'
50
126
}
51
127
}
52
128
</ pre >
53
- < p > mysql/mysql-connector-python</ p >
54
- < pre >
55
- from mysql.connector import connection
56
- import os
57
-
58
- db_password = os.getenv('DB_PASSWORD')
59
- connection.MySQLConnection(host='localhost', user='sonarsource', password=db_password) # Compliant
60
- </ pre >
129
+ < h3 > Pitfalls</ h3 >
130
+ < h4 > Hard-coded passwords</ h4 >
131
+ < p > It could be tempting to replace the empty password with a hard-coded one. Hard-coding passwords in the code can pose significant security risks.
132
+ Here are a few reasons why it is not recommended:</ p >
133
+ < ol >
134
+ < li > Security Vulnerability: Hard-coded passwords can be easily discovered by anyone who has access to the code, such as other developers or
135
+ attackers. This can lead to unauthorized access to the database and potential data breaches. </ li >
136
+ < li > Lack of Flexibility: Hard-coded passwords make it difficult to change the password without modifying the code. If the password needs to be
137
+ updated, it would require recompiling and redeploying the code, which can be time-consuming and error-prone. </ li >
138
+ < li > Version Control Issues: Storing passwords in code can lead to version control issues. If the code is shared or stored in a version control
139
+ system, the password will be visible to anyone with access to the repository, which is a security risk. </ li >
140
+ </ ol >
141
+ < p > To mitigate these risks, it is recommended to use secure methods for storing and retrieving passwords, such as using environment variables,
142
+ configuration files, or secure key management systems. These methods allow for better security, flexibility, and separation of sensitive information
143
+ from the codebase.</ p >
61
144
< h2 > Resources</ h2 >
145
+ < h3 > Standards</ h3 >
62
146
< ul >
63
147
< li > < a href ="https://owasp.org/Top10/A07_2021-Identification_and_Authentication_Failures/ "> OWASP Top 10 2021 Category A7</ a > - Identification and
64
148
Authentication Failures </ li >
0 commit comments