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 values become literal string "None" in the connection URL.

When raw_password is None, PASSWORD is set to Python's None object. When interpolated into the f-string on line 19, this becomes the literal string "None", producing an invalid URL like:

postgresql+asyncpg://user:None@host:5432/dbname

The same issue applies to USER, HOST, PORT, and DBNAME if any environment variable is missing.

Suggested fix: Validate required env vars or handle None gracefully
 # Fetch database credentials
 USER = os.getenv("user")
 raw_password = os.getenv("password")
-PASSWORD = quote_plus(raw_password) if raw_password else None
+PASSWORD = quote_plus(raw_password) if raw_password else ""
 HOST = os.getenv("host")
 PORT = os.getenv("port")
 DBNAME = os.getenv("dbname")
+
+# Validate required environment variables
+required_vars = {"user": USER, "host": HOST, "port": PORT, "dbname": DBNAME}
+missing = [name for name, val in required_vars.items() if not val]
+if missing:
+    raise ValueError(f"Missing required environment variables: {', '.join(missing)}")

Also applies to: 19-19

πŸ€– Prompt for AI Agents
In @Backend/app/db/db.py around lines 12 - 13, The connection URL is getting
literal "None" when env vars are missing because raw_password (and similarly
raw_user/raw_host/raw_port/raw_dbname) can be None and are inserted into the
f-string; update the code around raw_password/PASSWORD and the corresponding
USER, HOST, PORT, DBNAME variables to either (a) validate required env vars
early and raise a clear exception if any are missing, or (b) normalize values
before URL building by setting PASSWORD = quote_plus(raw_password) if
raw_password else "" (and do the same for USER/HOST/PORT/DBNAME) so the f-string
does not contain the string "None"; ensure the code that constructs the
connection URL handles empty values correctly or fails fast with a descriptive
error.

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