Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Backend/app/db/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from sqlalchemy.exc import SQLAlchemyError
import os
from dotenv import load_dotenv

from urllib.parse import quote_plus
# Load environment variables from .env
load_dotenv()

# Fetch database credentials
USER = os.getenv("user")
PASSWORD = os.getenv("password")
raw_password = os.getenv("password")
PASSWORD = quote_plus(raw_password) if raw_password else None
Comment on lines +12 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

None password interpolates as literal string "None" in the connection URL.

When raw_password is None (env var not set), PASSWORD becomes None. On line 19, this interpolates as the literal string "None", producing a malformed URL like postgresql+asyncpg://user:None@host:port/db. This defeats the purpose of the conditional check.

Consider failing fast when required credentials are missing, or using an empty string as a fallback:

Suggested fix (fail-fast approach)
 raw_password = os.getenv("password")
-PASSWORD = quote_plus(raw_password) if raw_password else None
+if not raw_password:
+    raise ValueError("Database password environment variable 'password' is required")
+PASSWORD = quote_plus(raw_password)
Alternative fix (empty string fallback)
 raw_password = os.getenv("password")
-PASSWORD = quote_plus(raw_password) if raw_password else None
+PASSWORD = quote_plus(raw_password) if raw_password else ""
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
raw_password = os.getenv("password")
PASSWORD = quote_plus(raw_password) if raw_password else None
raw_password = os.getenv("password")
if not raw_password:
raise ValueError("Database password environment variable 'password' is required")
PASSWORD = quote_plus(raw_password)
Suggested change
raw_password = os.getenv("password")
PASSWORD = quote_plus(raw_password) if raw_password else None
raw_password = os.getenv("password")
PASSWORD = quote_plus(raw_password) if raw_password else ""
🤖 Prompt for AI Agents
In @Backend/app/db/db.py around lines 12 - 13, The code sets PASSWORD =
quote_plus(raw_password) if raw_password else None which causes the literal
string "None" to appear in the DB URL when interpolated; change this to either
fail fast or use an empty password fallback: if you want fail-fast, check
raw_password and raise a clear exception (or log and exit) when raw_password is
falsy; if you prefer fallback, set PASSWORD = quote_plus(raw_password) if
raw_password else "" so the connection string builder (where the URL is
composed) will not insert the string "None". Ensure you update any code that
expects PASSWORD to be None accordingly.

HOST = os.getenv("host")
PORT = os.getenv("port")
DBNAME = os.getenv("dbname")
Expand Down
Loading