summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-12-08 20:45:32 (GMT)
committerGitHub <noreply@github.com>2022-12-08 20:45:32 (GMT)
commit8ef604518745989a3991a37f58368d2ce3c27d84 (patch)
treede6dcd03b167cc5b6d07c2567e4d479c996c542b /Doc/library
parent0e2c7839bd297ad284fd07bf3736f722b87175df (diff)
downloadcpython-8ef604518745989a3991a37f58368d2ce3c27d84.zip
cpython-8ef604518745989a3991a37f58368d2ce3c27d84.tar.gz
cpython-8ef604518745989a3991a37f58368d2ce3c27d84.tar.bz2
gh-96250: Improve sqlite3 injection attack example (GH-99270)
(cherry picked from commit 41d4ac9da348ca33056e271d71588b2dc3a6d48d) 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>
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/sqlite3.rst16
1 files changed, 10 insertions, 6 deletions
diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst
index 9775f80..65fa1b6 100644
--- a/Doc/library/sqlite3.rst
+++ b/Doc/library/sqlite3.rst
@@ -1427,12 +1427,16 @@ How to use placeholders to bind values in SQL queries
SQL operations usually need to use values from Python variables. However,
beware of using Python's string operations to assemble queries, as they
-are vulnerable to `SQL injection attacks`_ (see the `xkcd webcomic
-<https://xkcd.com/327/>`_ for a humorous example of what can go wrong)::
-
- # Never do this -- insecure!
- symbol = 'RHAT'
- cur.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
+are vulnerable to `SQL injection attacks`_. For example, an attacker can simply
+close the single quote and inject ``OR TRUE`` to select all rows::
+
+ >>> # Never do this -- insecure!
+ >>> symbol = input()
+ ' OR TRUE; --
+ >>> sql = "SELECT * FROM stocks WHERE symbol = '%s'" % symbol
+ >>> print(sql)
+ SELECT * FROM stocks WHERE symbol = '' OR TRUE; --'
+ >>> cur.execute(sql)
Instead, use the DB-API's parameter substitution. To insert a variable into a
query string, use a placeholder in the string, and substitute the actual values