You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user_guide_src/source/database/query_builder.rst
+34-9Lines changed: 34 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,32 @@ system.
19
19
:local:
20
20
:depth: 2
21
21
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
+
22
48
*************************
23
49
Loading the Query Builder
24
50
*************************
@@ -124,7 +150,7 @@ Since v4.2.0, ``$builder->select()`` accepts a ``CodeIgniter\Database\RawSql`` i
124
150
125
151
.. literalinclude:: query_builder/099.php
126
152
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.
128
154
129
155
$builder->selectMax()
130
156
---------------------
@@ -241,7 +267,7 @@ Since v4.2.0, ``$builder->join()`` accepts a ``CodeIgniter\Database\RawSql`` ins
241
267
242
268
.. literalinclude:: query_builder/102.php
243
269
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.
245
271
246
272
*************************
247
273
Looking for Specific Data
@@ -298,8 +324,7 @@ methods:
298
324
299
325
.. literalinclude:: query_builder/026.php
300
326
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.
303
328
304
329
.. literalinclude:: query_builder/027.php
305
330
@@ -312,7 +337,7 @@ methods:
312
337
313
338
.. literalinclude:: query_builder/100.php
314
339
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.
316
341
317
342
.. _query-builder-where-subquery:
318
343
@@ -427,7 +452,7 @@ searches.
427
452
428
453
.. literalinclude:: query_builder/101.php
429
454
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.
431
456
432
457
$builder->orLike()
433
458
------------------
@@ -483,7 +508,7 @@ You can also pass an array of multiple values as well:
483
508
484
509
.. literalinclude:: query_builder/049.php
485
510
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
487
512
can prevent escaping content by passing an optional third argument, and
488
513
setting it to ``false``.
489
514
@@ -872,7 +897,7 @@ In the above example, if we assume that the ``title`` field is our primary
872
897
key, then if a row containing ``My title`` as the ``title`` value, that row
873
898
will be deleted with our new row data replacing it.
874
899
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
876
901
automatically escaped, just like with ``insert()``.
877
902
878
903
$builder->set()
@@ -891,7 +916,7 @@ based on whether you are doing an insert or an update:
891
916
.. literalinclude:: query_builder/084.php
892
917
893
918
``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
895
920
difference, here is ``set()`` used both with and without the escape
0 commit comments