File tree 1 file changed +10
-6
lines changed
1 file changed +10
-6
lines changed Original file line number Diff line number Diff line change @@ -1929,12 +1929,16 @@ How to use placeholders to bind values in SQL queries
1929
1929
1930
1930
SQL operations usually need to use values from Python variables. However,
1931
1931
beware of using Python's string operations to assemble queries, as they
1932
- are vulnerable to `SQL injection attacks `_ (see the `xkcd webcomic
1933
- <https://xkcd.com/327/> `_ for a humorous example of what can go wrong)::
1934
-
1935
- # Never do this -- insecure!
1936
- symbol = 'RHAT'
1937
- cur.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
1932
+ are vulnerable to `SQL injection attacks `_. For example, an attacker can simply
1933
+ close the single quote and inject ``OR TRUE `` to select all rows::
1934
+
1935
+ >>> # Never do this -- insecure!
1936
+ >>> symbol = input()
1937
+ ' OR TRUE; --
1938
+ >>> sql = "SELECT * FROM stocks WHERE symbol = '%s'" % symbol
1939
+ >>> print(sql)
1940
+ SELECT * FROM stocks WHERE symbol = '' OR TRUE; --'
1941
+ >>> cur.execute(sql)
1938
1942
1939
1943
Instead, use the DB-API's parameter substitution. To insert a variable into a
1940
1944
query string, use a placeholder in the string, and substitute the actual values
You can’t perform that action at this time.
0 commit comments