8000 gh-96250: Improve sqlite3 injection attack example (GH-99270) · python/cpython@ecb16d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit ecb16d5

Browse files
miss-islingtonjiajunjieCAM-Gerlacherlend-aasland
authored
gh-96250: Improve sqlite3 injection attack example (GH-99270)
(cherry picked from commit 41d4ac9) Co-authored-by: Jia Junjie <62194633+jiajunjie@users.noreply.github.com> Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM> Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
1 parent 0274a3b commit ecb16d5

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Doc/library/sqlite3.rst

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,12 +1813,16 @@ How to use placeholders to bind values in SQL queries
18131813

18141814
SQL operations usually need to use values from Python variables. However,
18151815
beware of using Python's string operations to assemble queries, as they
1816-
are vulnerable to `SQL injection attacks`_ (see the `xkcd webcomic
1817-
<https://xkcd.com/327/>`_ for a humorous example of what can go wrong)::
1818-
1819-
# Never do this -- insecure!
1820-
symbol = 'RHAT'
1821-
cur.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
1816+
are vulnerable to `SQL injection attacks`_. For example, an attacker can simply
1817+
close the single quote and inject ``OR TRUE`` to select all rows::
1818+
1819+
>>> # Never do this -- insecure!
1820+
>>> symbol = input()
1821+
' OR TRUE; --
1822+
>>> sql = "SELECT * FROM stocks WHERE symbol = '%s'" % symbol
1823+
>>> print(sql)
1824+
SELECT * FROM stocks WHERE symbol = '' OR TRUE; --'
1825+
>>> cur.execute(sql)
18221826

18231827
Instead, use the DB-API's parameter substitution. To insert a variable into a
18241828
query string, use a placeholder in the string, and substitute the actual values

0 commit comments

Comments
 (0)
0