rspec/rules/S2115/python/how-to-fix-it/flask-sqlalchemy.adoc
sebastien-andrivet-sonarsource 89de4d7476
Modify rule S2115: Update to LaYC format (APPSEC-799) (#2927)
## Review

A dedicated reviewer checked the rule description successfully for:

- [x] logical errors and incorrect information
- [x] information gaps and missing content
- [x] text style and tone
- [x] PR summary and labels follow [the
guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)
2023-08-25 14:34:42 +02:00

29 lines
861 B
Plaintext

== 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
[source,python,diff-id=103,diff-type=noncompliant]
----
def configure_app(app):
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://user:@domain.com" # Noncompliant
----
==== Compliant solution
[source,python,diff-id=103,diff-type=compliant]
----
def configure_app(app):
db_password = os.getenv('DB_PASSWORD')
app.config['SQLALCHEMY_DATABASE_URI'] = f"postgresql://user:{db_password}@domain.com"
----
=== Pitfalls
include::../../common/pitfalls/hard-coded.adoc[]