fix: replace f-string SQL with psycopg2.sql for safe identifier handling#32
fix: replace f-string SQL with psycopg2.sql for safe identifier handling#32wicky-zipstack wants to merge 1 commit intomainfrom
Conversation
Replace all raw f-string SQL interpolation in sample_project.py with psycopg2.sql.SQL, sql.Identifier, and sql.Literal for proper escaping: - CREATE/DROP DATABASE, CREATE/DROP USER — sql.Identifier for names - GRANT, ALTER SCHEMA — sql.Identifier for schema and user names - WHERE datname = ... — sql.Literal for value parameters - CREATE USER ... PASSWORD — sql.Literal for password value - Schema DDL (DROP/CREATE SCHEMA) — sql.Identifier for schema names
|
| Filename | Overview |
|---|---|
| backend/backend/application/sample_project/sample_project.py | SQL injection remediation: replaces all f-string SQL interpolation with psycopg2.sql.SQL/Identifier/Literal; removes unused user_check_query; loosens execute_sql_queries type annotation from list[str] to list. |
Sequence Diagram
sequenceDiagram
participant C as Caller
participant SP as SampleProject
participant PG as PostgreSQL (admin DB)
participant NPG as PostgreSQL (new DB)
C->>SP: load_sample_project()
SP->>SP: check_project_limit()
SP->>SP: create_new_database()
SP->>PG: user_exist() — SELECT 1 FROM pg_roles WHERE rolname = %s
PG-->>SP: True / False
SP->>PG: CREATE DATABASE "db_name" TEMPLATE "master_db" (sql.Identifier)
SP->>PG: CREATE USER "user_name" WITH ENCRYPTED PASSWORD 'pwd' (sql.Identifier + sql.Literal)
SP->>PG: GRANT ALL PRIVILEGES ON DATABASE "db_name" TO "user_name"
SP->>SP: _grant_schema_permissions_on_new_db()
SP->>NPG: GRANT USAGE ON SCHEMA "raw"|"dev"|"stg"|"prod" TO "user_name"
SP->>NPG: GRANT SELECT/INSERT/UPDATE/DELETE ON ALL TABLES IN SCHEMA {} TO {}
SP->>NPG: ALTER DEFAULT PRIVILEGES IN SCHEMA {} GRANT ... TO {}
SP->>NPG: ALTER SCHEMA "dev"|"stg"|"prod" OWNER TO "user_name"
SP->>SP: create_project_connection()
SP->>SP: create_schemas() [only if _clone_db=False]
SP->>PG: DROP/CREATE SCHEMA "raw"|"dev"|"stg"|"prod" (sql.Identifier)
SP->>SP: upload_and_run_csv()
SP->>SP: create_and_load_models()
SP-->>C: project details
Prompt To Fix All With AI
This is a comment left during a code review.
Path: backend/backend/application/sample_project/sample_project.py
Line: 157
Comment:
**Type annotation weakened to bare `list`**
The annotation was downgraded from `list[str]` to `list`. Since the method now exclusively receives `sql.Composable` objects, a more accurate annotation would help IDEs and static analysis tools catch callers that still pass raw strings.
```suggestion
def execute_sql_queries(self, statements: list["sql.Composable"]):
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix: replace f-string SQL with psycopg2...." | Re-trigger Greptile
| return self._postgres_connection | ||
|
|
||
| def execute_sql_queries(self, statements: list[str]): | ||
| def execute_sql_queries(self, statements: list): |
There was a problem hiding this comment.
Type annotation weakened to bare
list
The annotation was downgraded from list[str] to list. Since the method now exclusively receives sql.Composable objects, a more accurate annotation would help IDEs and static analysis tools catch callers that still pass raw strings.
| def execute_sql_queries(self, statements: list): | |
| def execute_sql_queries(self, statements: list["sql.Composable"]): |
Prompt To Fix With AI
This is a comment left during a code review.
Path: backend/backend/application/sample_project/sample_project.py
Line: 157
Comment:
**Type annotation weakened to bare `list`**
The annotation was downgraded from `list[str]` to `list`. Since the method now exclusively receives `sql.Composable` objects, a more accurate annotation would help IDEs and static analysis tools catch callers that still pass raw strings.
```suggestion
def execute_sql_queries(self, statements: list["sql.Composable"]):
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Good catch. Updated the type annotation would be more accurate as list[sql.Composable]. Will address in a follow-up — not a functional issue.
What
sample_project.pywithpsycopg2.sql.SQL,sql.Identifier, andsql.LiteralWhy
re.sub, proper parameterization is the industry standardHow
sql.Identifierfor database namessql.Identifierfor user names,sql.Literalfor passwordssql.Identifierfor schema and user namessql.Literalfor value parameterssql.Identifierfor schema names in DROP/CREATESampleProject— covers all 4 sample projects (Jaffle Shop Starter/Final, DVD Rental Starter/Final)Can this PR break any existing features. If yes, please list possible items. If no, please explain why. (PS: Admins do not merge the PR without this section filled)
psycopg2.sqlgenerates identical SQL output but with proper quoting. The only difference is identifiers are now double-quoted (e.g.,"my_database"instead ofmy_database), which is valid PostgreSQL and handles reserved words correctly. Sample project creation should work identically.Database Migrations
Env Config
Relevant Docs
Related Issues or PRs
Dependencies Versions
Notes on Testing
Screenshots
N/A
Checklist