org.sonar.l10n.py.rules.python.S2115.html Maven / Gradle / Ivy
When accessing a database, an empty password should be avoided as it introduces a weakness.
Why is this an issue?
When a database does not require a password for authentication, it allows anyone to access and manipulate the data stored within it. Exploiting
this vulnerability typically involves identifying the target database and establishing a connection to it without the need for any authentication
credentials.
What is the potential impact?
Once connected, an attacker can perform various malicious actions, such as viewing, modifying, or deleting sensitive information, potentially
leading to data breaches or unauthorized access to critical systems. It is crucial to address this vulnerability promptly to ensure the security and
integrity of the database and the data it contains.
Unauthorized Access to Sensitive Data
When a database lacks a password for authentication, it opens the door for unauthorized individuals to gain access to sensitive data. This can
include personally identifiable information (PII), financial records, intellectual property, or any other confidential information stored in the
database. Without proper access controls in place, malicious actors can exploit this vulnerability to retrieve sensitive data, potentially leading to
identity theft, financial loss, or reputational damage.
Compromise of System Integrity
Without a password requirement, unauthorized individuals can gain unrestricted access to a database, potentially compromising the integrity of the
entire system. Attackers can inject malicious code, alter configurations, or manipulate data within the database, leading to system malfunctions,
unauthorized system access, or even complete system compromise. This can disrupt business operations, cause financial losses, and expose the
organization to further security risks.
Unwanted Modifications or Deletions
The absence of a password for database access allows anyone to make modifications or deletions to the data stored within it. This poses a
significant risk, as unauthorized changes can lead to data corruption, loss of critical information, or the introduction of malicious content. For
example, an attacker could modify financial records, tamper with customer orders, or delete important files, causing severe disruptions to business
processes and potentially leading to financial and legal consequences.
Overall, the lack of a password configured to access a database poses a serious security risk, enabling unauthorized access, data breaches, system
compromise, and unwanted modifications or deletions. It is essential to address this vulnerability promptly to safeguard sensitive data, maintain
system integrity, and protect the organization from potential harm.
How to fix it in MySQL Connector/Python
Code examples
The following code uses an empty password to connect to a MySQL database.
The vulnerability can be fixed by using a strong password retrieved from an environment variable DB_PASSWORD
. This environment
variable is set during deployment. It should be strong and different for each database.
Noncompliant code example
from mysql.connector import connection
connection.MySQLConnection(host='localhost', user='sonarsource', password='') # Noncompliant
Compliant solution
from mysql.connector import connection
import os
db_password = os.getenv('DB_PASSWORD')
connection.MySQLConnection(host='localhost', user='sonarsource', password=db_password)
Pitfalls
Hard-coded passwords
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.
Here are a few reasons why it is not recommended:
- Security Vulnerability: Hard-coded passwords can be easily discovered by anyone who has access to the code, such as other developers or
attackers. This can lead to unauthorized access to the database and potential data breaches.
- Lack of Flexibility: Hard-coded passwords make it difficult to change the password without modifying the code. If the password needs to be
updated, it would require recompiling and redeploying the code, which can be time-consuming and error-prone.
- Version Control Issues: Storing passwords in code can lead to version control issues. If the code is shared or stored in a version control
system, the password will be visible to anyone with access to the repository, which is a security risk.
To mitigate these risks, it is recommended to use secure methods for storing and retrieving passwords, such as using environment variables,
configuration files, or secure key management systems. These methods allow for better security, flexibility, and separation of sensitive information
from the codebase.
How to fix it in SQLAlchemy
Code examples
The following code uses an empty password to connect to a Postgres database.
The vulnerability can be fixed by using a strong password retrieved from an environment variable DB_PASSWORD
. This environment
variable is set during deployment. It should be strong and different for each database.
Noncompliant code example
def configure_app(app):
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://user:@domain.com" # Noncompliant
Compliant solution
def configure_app(app):
db_password = os.getenv('DB_PASSWORD')
app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://user:{db_password}@domain.com"
Pitfalls
Hard-coded passwords
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.
Here are a few reasons why it is not recommended:
- Security Vulnerability: Hard-coded passwords can be easily discovered by anyone who has access to the code, such as other developers or
attackers. This can lead to unauthorized access to the database and potential data breaches.
- Lack of Flexibility: Hard-coded passwords make it difficult to change the password without modifying the code. If the password needs to be
updated, it would require recompiling and redeploying the code, which can be time-consuming and error-prone.
- Version Control Issues: Storing passwords in code can lead to version control issues. If the code is shared or stored in a version control
system, the password will be visible to anyone with access to the repository, which is a security risk.
To mitigate these risks, it is recommended to use secure methods for storing and retrieving passwords, such as using environment variables,
configuration files, or secure key management systems. These methods allow for better security, flexibility, and separation of sensitive information
from the codebase.
How to fix it in Django
Code examples
The following code uses an empty password to connect to a Postgres database.
The vulnerability can be fixed by using a strong password retrieved from an environment variable DB_PASSWORD
. This environment
variable is set during deployment. It should be strong and different for each database.
Noncompliant code example
# settings.py
DATABASES = {
'postgresql_db': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'quickdb',
'USER': 'sonarsource',
'PASSWORD': '', # Noncompliant
'HOST': 'localhost',
'PORT': '5432'
}
}
Compliant solution
# settings.py
import os
DATABASES = {
'postgresql_db': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'quickdb',
'USER': 'sonarsource',
'PASSWORD': os.getenv('DB_PASSWORD'),
'HOST': 'localhost',
'PORT': '5432'
}
}
Pitfalls
Hard-coded passwords
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.
Here are a few reasons why it is not recommended:
- Security Vulnerability: Hard-coded passwords can be easily discovered by anyone who has access to the code, such as other developers or
attackers. This can lead to unauthorized access to the database and potential data breaches.
- Lack of Flexibility: Hard-coded passwords make it difficult to change the password without modifying the code. If the password needs to be
updated, it would require recompiling and redeploying the code, which can be time-consuming and error-prone.
- Version Control Issues: Storing passwords in code can lead to version control issues. If the code is shared or stored in a version control
system, the password will be visible to anyone with access to the repository, which is a security risk.
To mitigate these risks, it is recommended to use secure methods for storing and retrieving passwords, such as using environment variables,
configuration files, or secure key management systems. These methods allow for better security, flexibility, and separation of sensitive information
from the codebase.
Resources
Standards