Skip to content

Commit 334308e

Browse files
Fixed #36778 -- Extended advice to sanitize input before using in query expressions.
Thanks Clifford Gama and Simon Charette for reviews.
1 parent af60ae4 commit 334308e

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

docs/internals/security.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ validated::
111111
return JsonResponse(status=200)
112112
return JsonResponse(form.errors, status=400)
113113

114-
Similarly, as Django's raw SQL constructs (such as :meth:`~.QuerySet.extra` and
115-
:class:`.RawSQL` expression) provide developers with full control over the
116-
query, they are insecure if user input is not properly handled. As explained in
114+
Similarly, as Django's raw SQL constructs (such as :meth:`~.QuerySet.extra`,
115+
:class:`.RawSQL`, and :ref:`keyword arguments to database functions
116+
<avoiding-sql-injection-in-query-expressions>`) provide developers with full
117+
control over the query, they are insecure if user input is not properly
118+
handled. As explained in
117119
our :ref:`security documentation <sql-injection-protection>`, it is the
118120
developer's responsibility to safely process user input for these functions.
119121

docs/ref/models/database-functions.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ The classes documented below provide a way for users to use functions provided
99
by the underlying database as annotations, aggregations, or filters in Django.
1010
Functions are also :doc:`expressions </ref/models/expressions>`, so they can be
1111
used and combined with other expressions like :ref:`aggregate functions
12-
<aggregation-functions>`.
12+
<aggregation-functions>`. See the :class:`~django.db.models.Func` documentation
13+
for security considerations.
1314

1415
We'll be using the following model in examples of each function::
1516

docs/ref/models/expressions.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,16 @@ replace the attributes of the same name without having to define your own
434434
class. :ref:`output_field<output-field>` can be used to define the expected
435435
return type.
436436

437+
.. admonition:: Sanitize input used to configure a query expression
438+
439+
Built-in database functions (such as
440+
:class:`~django.db.models.functions.Cast`) vary in whether arguments such
441+
as ``output_field`` can be supplied positionally or only by keyword. For
442+
``output_field`` and several other cases, the input ultimately reaches
443+
``Func()`` as a keyword argument, so the advice to avoid constructing
444+
keyword arguments from untrusted user input applies as equally to these
445+
arguments as it does to ``**extra``.
446+
437447
``Aggregate()`` expressions
438448
---------------------------
439449

0 commit comments

Comments
 (0)