Skip to content

Commit

Permalink
Add example code for CodeQL to flag
Browse files Browse the repository at this point in the history
  • Loading branch information
samjwu committed May 1, 2024
1 parent 2fa11a8 commit caa88ed
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.conf.urls import url
from django.db import connection


def show_user(request, username):
with connection.cursor() as cursor:
# BAD -- Using string formatting
cursor.execute("SELECT * FROM users WHERE username = '%s'" % username)

Check failure

Code scanning / CodeQL

SQL query built from user-controlled sources High test

This SQL query depends on a
user-provided value
.
user = cursor.fetchone()

# GOOD -- Using parameters
cursor.execute("SELECT * FROM users WHERE username = %s", username)
user = cursor.fetchone()

# BAD -- Manually quoting placeholder (%s)
cursor.execute("SELECT * FROM users WHERE username = '%s'", username)
user = cursor.fetchone()

urlpatterns = [url(r'^users/(?P<username>[^/]+)$', show_user)]

0 comments on commit caa88ed

Please sign in to comment.