Skip to content
Open
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
2 changes: 1 addition & 1 deletion server/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

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

To fix the issue, the SQL query on line 16 should be rewritten to use parameterized queries, which safely embed user input into the query. This approach prevents SQL injection by ensuring that user input is treated as data rather than executable code.

Specifically:

  1. Replace the string concatenation in the query with a parameterized query using placeholders (%s).
  2. Pass the name parameter as an argument to the execute method, ensuring it is properly escaped by the database driver.

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,3 @@
         cursor.execute(
-            "SELECT * FROM books WHERE name LIKE '%" + name + "%'"
+            "SELECT * FROM books WHERE name LIKE %s", ("%" + name + "%",)
         )
EOF
@@ -15,3 +15,3 @@
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.
)
books = [Book(*row) for row in cursor]

Expand Down