Skip to content

Update routes.py#2

Open
ELI-student001 wants to merge 1 commit intomainfrom
learning-codeql
Open

Update routes.py#2
ELI-student001 wants to merge 1 commit intomainfrom
learning-codeql

Conversation

@ELI-student001
Copy link
Owner

No description provided.

if name:
cursor.execute(
"SELECT * FROM books WHERE name LIKE %s", name
"SELECT * FROM books WHERE name LIKE '%" + name + "%'"

Check failure

Code scanning / CodeQL

SQL query built from user-controlled sources High

This SQL query depends on a
user-provided value
.

Copilot Autofix

AI 6 months ago

To fix this vulnerability in server/routes.py, replace the unsafe string concatenation in the SQL query on line 16 with a parameterized query using the DB API provided by the cursor object. Most Python DB connectors (including those underlying Flask and SQLAlchemy) support using placeholders (often %s for positional parameters) in SQL, with actual parameter values passed as separate arguments. For SQL LIKE statements, you usually need to format the % wildcards as part of the parameter—not inside the query string—so use "SELECT * FROM books WHERE name LIKE %s" with the parameter being f"%{name}%". No new imports are needed. Only line 16 (and the lines around it for context) need to be updated.


Suggested changeset 1
server/routes.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/server/routes.py b/server/routes.py
--- a/server/routes.py
+++ b/server/routes.py
@@ -13,7 +13,7 @@
 
     if name:
         cursor.execute(
-            "SELECT * FROM books WHERE name LIKE '%" + name + "%'"
+            "SELECT * FROM books WHERE name LIKE %s", (f"%{name}%",)
         )
         books = [Book(*row) for row in cursor]
 
EOF
@@ -13,7 +13,7 @@

if name:
cursor.execute(
"SELECT * FROM books WHERE name LIKE '%" + name + "%'"
"SELECT * FROM books WHERE name LIKE %s", (f"%{name}%",)
)
books = [Book(*row) for row in cursor]

Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant