Skip to content

Conversation

@andregri
Copy link
Owner

No description provided.

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 5 months ago

To fix the issue, the SQL query on line 16 should be rewritten to use parameterized queries instead of string concatenation. Parameterized queries ensure that user input is properly escaped and prevent SQL injection attacks. Specifically:

  1. Replace the concatenation of name into the query string with a placeholder (%s).
  2. Pass the user-provided value (name) as a parameter to the cursor.execute method, ensuring it is safely escaped.

This change will align the code with the secure practices demonstrated in the example provided in the background section.


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
@@ -15,3 +15,4 @@
         cursor.execute(
-            "SELECT * FROM books WHERE name LIKE '%" + name + "%'"
+            "SELECT * FROM books WHERE name LIKE %s",
+            ("%" + name + "%",)
         )
EOF
@@ -15,3 +15,4 @@
cursor.execute(
"SELECT * FROM books WHERE name LIKE '%" + name + "%'"
"SELECT * FROM books WHERE name LIKE %s",
("%" + name + "%",)
)
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.

2 participants