Skip to content

Prioritize JSON parsing for body#356

Draft
kapyteinaikido wants to merge 19 commits into
mainfrom
django_body_parsing_bypass
Draft

Prioritize JSON parsing for body#356
kapyteinaikido wants to merge 19 commits into
mainfrom
django_body_parsing_bypass

Conversation

@kapyteinaikido

@kapyteinaikido kapyteinaikido commented Apr 7, 2025

Copy link
Copy Markdown
Contributor


with connection.cursor() as cursor:
query = 'INSERT INTO sample_app_dogs (dog_name, dog_boss) VALUES ("%s", "N/A")' % dog_name
cursor.execute(query)

Check failure

Code scanning / CodeQL

SQL query built from user-controlled sources

This SQL query depends on a [user-provided value](1).

Copilot Autofix

AI over 1 year ago

To fix the problem, we need to use parameterized queries instead of string formatting to construct the SQL query. Parameterized queries ensure that user input is properly escaped and handled by the database driver, preventing SQL injection attacks.

The best way to fix the problem without changing existing functionality is to modify the cursor.execute call to use query parameters. This involves replacing the string formatting with a parameterized query and passing the user-provided value as a parameter to the execute method.

Suggested changeset 1
sample-apps/django-mysql/sample_app/views.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/sample-apps/django-mysql/sample_app/views.py b/sample-apps/django-mysql/sample_app/views.py
--- a/sample-apps/django-mysql/sample_app/views.py
+++ b/sample-apps/django-mysql/sample_app/views.py
@@ -36,5 +36,5 @@
         with connection.cursor() as cursor:
-            query = 'INSERT INTO sample_app_dogs (dog_name, dog_boss) VALUES ("%s", "N/A")' % dog_name
-            print("QUERY : ", query)
-            cursor.execute(query)
+            query = 'INSERT INTO sample_app_dogs (dog_name, dog_boss) VALUES (%s, "N/A")'
+            print("QUERY : ", query % dog_name)
+            cursor.execute(query, [dog_name])
         return HttpResponse("Dog page created")
@@ -49,4 +49,4 @@
         with connection.cursor() as cursor:
-            query = 'INSERT INTO sample_app_dogs (dog_name, dog_boss) VALUES ("%s", "N/A")' % dog_name
-            cursor.execute(query)
+            query = 'INSERT INTO sample_app_dogs (dog_name, dog_boss) VALUES (%s, "N/A")'
+            cursor.execute(query, [dog_name])
         return JsonResponse({"status": "Dog page created"})
\ No newline at end of file
EOF
@@ -36,5 +36,5 @@
with connection.cursor() as cursor:
query = 'INSERT INTO sample_app_dogs (dog_name, dog_boss) VALUES ("%s", "N/A")' % dog_name
print("QUERY : ", query)
cursor.execute(query)
query = 'INSERT INTO sample_app_dogs (dog_name, dog_boss) VALUES (%s, "N/A")'
print("QUERY : ", query % dog_name)
cursor.execute(query, [dog_name])
return HttpResponse("Dog page created")
@@ -49,4 +49,4 @@
with connection.cursor() as cursor:
query = 'INSERT INTO sample_app_dogs (dog_name, dog_boss) VALUES ("%s", "N/A")' % dog_name
cursor.execute(query)
query = 'INSERT INTO sample_app_dogs (dog_name, dog_boss) VALUES (%s, "N/A")'
cursor.execute(query, [dog_name])
return JsonResponse({"status": "Dog page created"})
Copilot is powered by AI and may make mistakes. Always verify output.
@kapyteinaikido
kapyteinaikido marked this pull request as draft April 8, 2025 12:26
@kapyteinaikido
kapyteinaikido marked this pull request as ready for review April 9, 2025 09:39
@bitterpanda63 bitterpanda63 added WIP Work In Progress Blocked This means the PR is blocked because of another PR and removed Blocked This means the PR is blocked because of another PR labels Apr 11, 2025
@bitterpanda63
bitterpanda63 marked this pull request as draft April 11, 2025 12:28
@bitterpanda63
bitterpanda63 self-requested a review April 11, 2025 12:28
@kapyteinaikido kapyteinaikido removed the WIP Work In Progress label Apr 14, 2025
@kapyteinaikido
kapyteinaikido marked this pull request as ready for review April 14, 2025 14:31
@bitterpanda63
bitterpanda63 marked this pull request as draft August 18, 2025 09:43
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.

4 participants