Skip to content

Commit dbb3426

Browse files
authored
Merge pull request #6599 from kenjis/fix-docs-query-builder-sql-injection
docs: add SQL Injection Protection in QueryBuilder
2 parents 979baa1 + 9ea9e1b commit dbb3426

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

user_guide_src/source/database/queries.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ prefixing set ``true`` (boolean) via the second parameter:
9191

9292
.. literalinclude:: queries/008.php
9393

94-
****************
95-
Escaping Queries
96-
****************
94+
***************
95+
Escaping Values
96+
***************
9797

9898
It's a very good security practice to escape your data before submitting
9999
it into your database. CodeIgniter has three methods that help you do

user_guide_src/source/database/query_builder.rst

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@ system.
1919
:local:
2020
:depth: 2
2121

22+
************************
23+
SQL Injection Protection
24+
************************
25+
26+
You can generate SQL statements quite safely with the Query Builder. However,
27+
it is not designed to prevent SQL injection no matter what data you pass.
28+
29+
Arguments passed to the Query Builder can be:
30+
1. **identifiers** such as field (or table) names
31+
2. their **values**
32+
3. a part of **SQL strings**
33+
34+
The Query Builder will escape all **values** by default.
35+
36+
It will also try to properly protect **identifiers** and identifiers in
37+
**SQL strings** by default.
38+
However, it is implemented to work well in many use cases and
39+
is not designed to prevent all attacks.
40+
Therefore, you should never feed in user input to them without proper validation.
41+
42+
Also, many methods have the ``$escape`` parameter that can be set to disable escaping.
43+
If ``$escape`` is set to false, no protection is provided by the Query Builder,
44+
so you must ensure by yourself that
45+
they are properly escaped or protected before passing it to the Query Builder.
46+
The same is true when using ``RawSql``, which specifies a raw SQL statement.
47+
2248
*************************
2349
Loading the Query Builder
2450
*************************
@@ -124,7 +150,7 @@ Since v4.2.0, ``$builder->select()`` accepts a ``CodeIgniter\Database\RawSql`` i
124150

125151
.. literalinclude:: query_builder/099.php
126152

127-
.. warning:: When you use ``RawSql``, you MUST escape the data manually. Failure to do so could result in SQL injections.
153+
.. warning:: When you use ``RawSql``, you MUST escape the values and protect the identifiers manually. Failure to do so could result in SQL injections.
128154

129155
$builder->selectMax()
130156
---------------------
@@ -241,7 +267,7 @@ Since v4.2.0, ``$builder->join()`` accepts a ``CodeIgniter\Database\RawSql`` ins
241267

242268
.. literalinclude:: query_builder/102.php
243269

244-
.. warning:: When you use ``RawSql``, you MUST escape the data manually. Failure to do so could result in SQL injections.
270+
.. warning:: When you use ``RawSql``, you MUST escape the values and protect the identifiers manually. Failure to do so could result in SQL injections.
245271

246272
*************************
247273
Looking for Specific Data
@@ -298,8 +324,7 @@ methods:
298324

299325
.. literalinclude:: query_builder/026.php
300326

301-
.. warning:: If you are using user-supplied data within the string, you MUST escape the
302-
data manually. Failure to do so could result in SQL injections.
327+
.. warning:: If you are using user-supplied data within the string, you MUST escape the values and protect the identifiers manually. Failure to do so could result in SQL injections.
303328

304329
.. literalinclude:: query_builder/027.php
305330

@@ -312,7 +337,7 @@ methods:
312337

313338
.. literalinclude:: query_builder/100.php
314339

315-
.. warning:: When you use ``RawSql``, you MUST escape the data manually. Failure to do so could result in SQL injections.
340+
.. warning:: When you use ``RawSql``, you MUST escape the values and protect the identifiers manually. Failure to do so could result in SQL injections.
316341

317342
.. _query-builder-where-subquery:
318343

@@ -427,7 +452,7 @@ searches.
427452

428453
.. literalinclude:: query_builder/101.php
429454

430-
.. warning:: When you use ``RawSql``, you MUST escape the data manually. Failure to do so could result in SQL injections.
455+
.. warning:: When you use ``RawSql``, you MUST escape the values and protect the identifiers manually. Failure to do so could result in SQL injections.
431456

432457
$builder->orLike()
433458
------------------
@@ -483,7 +508,7 @@ You can also pass an array of multiple values as well:
483508

484509
.. literalinclude:: query_builder/049.php
485510

486-
If you are using a database that CodeIgniter escapes queries for, you
511+
If you are using a database that CodeIgniter escapes values for, you
487512
can prevent escaping content by passing an optional third argument, and
488513
setting it to ``false``.
489514

@@ -872,7 +897,7 @@ In the above example, if we assume that the ``title`` field is our primary
872897
key, then if a row containing ``My title`` as the ``title`` value, that row
873898
will be deleted with our new row data replacing it.
874899

875-
Usage of the ``set()`` method is also allowed and all fields are
900+
Usage of the ``set()`` method is also allowed and all values are
876901
automatically escaped, just like with ``insert()``.
877902

878903
$builder->set()
@@ -891,7 +916,7 @@ based on whether you are doing an insert or an update:
891916
.. literalinclude:: query_builder/084.php
892917

893918
``set()`` will also accept an optional third parameter (``$escape``), that
894-
will prevent data from being escaped if set to ``false``. To illustrate the
919+
will prevent the values from being escaped if set to ``false``. To illustrate the
895920
difference, here is ``set()`` used both with and without the escape
896921
parameter.
897922

0 commit comments

Comments
 (0)