Skip to content

Commit a702e0b

Browse files
authored
docs: warn against passing user input to orderBy() and groupBy() (#10771)
1 parent eb64665 commit a702e0b

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎docs/database/query_builder.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,18 @@ $builder->groupBy(['title', 'date']);
729729
// Produces: GROUP BY title, date
730730
```
731731

732+
!!! warning "Warning"
733+
The first parameter is treated as a field name or SQL expression, not as a value. Identifier protection does not prevent SQL injection here, because expressions such as subqueries and function calls are passed through unchanged. Never pass user-supplied data to `groupBy()` without validating it against a list of allowed columns.
734+
735+
```php
736+
<?php
737+
738+
$allowed = ['title', 'date', 'category'];
739+
$group = in_array($group, $allowed, true) ? $group : 'title';
740+
741+
$builder->groupBy($group);
742+
```
743+
732744
#### $builder->distinct()
733745

734746
Adds the **DISTINCT** keyword to a query:
@@ -1020,6 +1032,18 @@ $builder->orderBy('name', 'ASC');
10201032
// Produces: ORDER BY `title` DESC, `name` ASC
10211033
```
10221034

1035+
!!! warning "Warning"
1036+
The first parameter is treated as a field name or SQL expression, not as a value. Identifier protection does not prevent SQL injection here, because expressions such as subqueries and function calls are passed through unchanged. Never pass user-supplied data, such as a sort column from the query string, to `orderBy()` without validating it against a list of allowed columns.
1037+
1038+
```php
1039+
<?php
1040+
1041+
$allowed = ['title', 'name', 'created_at'];
1042+
$sort = in_array($sort, $allowed, true) ? $sort : 'created_at';
1043+
1044+
$builder->orderBy($sort, $direction === 'asc' ? 'ASC' : 'DESC');
1045+
```
1046+
10231047
If you choose the `RANDOM` direction option, then the first parameter will be ignored, unless you specify a numeric seed value.
10241048

10251049
```php

0 commit comments

Comments
 (0)