Skip to content

Commit 4b4fb79

Browse files
committed
fix: Add table name whitelist to prevent SQL injection in project deletion
The DELETE query loop in Project.delete() interpolated table names directly into an f-string. Added an explicit whitelist with a validation check before each query to prevent SQL injection if table names ever become user-controlled. Flagged by Bandit (B608) and Semgrep.
1 parent 468b85b commit 4b4fb79

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

backend/models/postgis/project.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,10 +926,15 @@ async def delete(self, db: Database):
926926
"project_partnerships",
927927
]
928928

929+
# Whitelist of allowed table names to prevent SQL injection via
930+
# dynamic table name interpolation, since table names cannot be parameterized.
931+
allowed_tables = set(related_tables)
932+
929933
# Start a transaction to ensure atomic deletion
930934
async with db.transaction():
931-
# Loop through each table and execute the delete query
932935
for table in related_tables:
936+
if table not in allowed_tables:
937+
raise ValueError(f"Invalid table name: {table}")
933938
await db.execute(
934939
f"DELETE FROM {table} WHERE project_id = :project_id",
935940
{"project_id": self.id},

0 commit comments

Comments
 (0)