본문 바로가기
  •                        自分に負けずやれば出来る
  • 自分を信じる
Computer/Database

SQL injection protection in python

by Divertome 2021. 6. 6.

SQL injection protection in python


기본적으로 장고의 queryset(ORM)을 사용할 경우에는 injection에 대한 보호가 됨으로 따로 처리해줄 필요가 없다.
extra(),RawSQL()등을 사용하거나 connection을 통해 직접 쿼리를 통해 데이터베이스에 접근하는 경우에는 SQL injection에 대한 처리를 따로 해줄 필요가 있다.

https://bobby-tables.com/python.html 에서 python sql injection에 대해 설명하고 있는데 이를 참고하였다.

다음과 같다.

Bad Case:

# Do NOT do it this way.
cmd = "update people set name='%s' where id='%s'" % (name, id) 
curs.execute(cmd)

Good Case:

cmd = "update people set name=%s where id=%s"
curs.execute(cmd, (name, id))

For cases involving a single variable do this:

cmd = "SELECT \* FROM PEOPLE WHERE name = %s"
curs.execute(cmd, (name,))