My django project makes use of lots and lots of database queries, some are complex and some are basic SELECT queries with no conditions or logic involved.
So far I have been using the sqlite3
module to manage my database, instead of the django ORM which has worked very well. One problem or drawback I am aware of using raw SQL queries is their security flaws when compared to django's ORM, such as being viable to SQL injection attacks when passing in user input into my raw SQL queries.
My question is - Is it absolutely necessary to use django's ORM for queries involving user input or can I use a general function to remove any potentially malicious characters eg (,' -, *, ;)
def remove_characters(string:str):
characters = ["'", ";", "-", "*"]
for char in characters:
if char in string:
string = string.replace(char, "")
return string
example of vunrable query in my project
username = "logan9997"
password = "x' or 'x' = 'x"
def check_login(self, username, password):
sql = f"""
SELECT *
FROM App_user
WHERE username = '{remove_character(username)}'
AND password = '{{remove_character(password)}'
"""
Without the remove_characters
function a hacker could gain access to someone else's account if the inputs were not sanitized
would this remove ALL threats of an SQL injection attack?
And would it just make more sense to use the ORM for queries involving user input?