Skip to content

Commit 45e4991

Browse files
committed
Merge branch 'release/v5.9.0'
2 parents af56838 + d8dff5a commit 45e4991

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

change-log.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
##Change Log
1010

11+
###v5.9.0
12+
- Added escapeColumns feature to escape the values.
13+
- Addresses XSS filtering issue #128.
14+
1115
###v5.8.6
1216
- Fix DT_Row options when returning a flatten array response.
1317
- Fix PR #126.

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This package is created to handle [server-side](https://www.datatables.net/manua
2828
- Decorate your data output using [`league\fractal`](https://github.com/thephpleague/fractal) Transformer.
2929
- Works with Laravel Dependency Injection and IoC Container.
3030
- Provides a [DataTable Html Builder](http://datatables.yajrabox.com/html) to help you use the package with less code.
31+
- Provides XSS filtering function to optionally escape all or specified column values using `escapeColumns('*'\['column'])` method.
3132

3233
## Buy me a beer
3334
<a href='https://pledgie.com/campaigns/29515'><img alt='Click here to lend your support to: Laravel Datatables and make a donation at pledgie.com !' src='https://pledgie.com/campaigns/29515.png?skin_name=chrome' border='0' ></a>

src/yajra/Datatables/Engines/BaseEngine.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ abstract class BaseEngine implements DataTableEngine
5959
protected $columns = [];
6060

6161
/**
62-
* DT columns definitions container (add/edit/remove/filter/order).
62+
* DT columns definitions container (add/edit/remove/filter/order/escape).
6363
*
6464
* @var array
6565
*/
@@ -69,6 +69,7 @@ abstract class BaseEngine implements DataTableEngine
6969
'excess' => ['rn', 'row_num'],
7070
'filter' => [],
7171
'order' => [],
72+
'escape' => [],
7273
];
7374

7475
/**
@@ -136,7 +137,7 @@ abstract class BaseEngine implements DataTableEngine
136137
/**
137138
* Output transformer.
138139
*
139-
* @var TransformerAbstract
140+
* @var \League\Fractal\TransformerAbstract
140141
*/
141142
protected $transformer = null;
142143

@@ -381,6 +382,19 @@ public function removeColumn()
381382
return $this;
382383
}
383384

385+
/**
386+
* Declare columns to escape values.
387+
*
388+
* @param string|array $columns
389+
* @return $this
390+
*/
391+
public function escapeColumns($columns = '*')
392+
{
393+
$this->columnDef['escape'] = $columns;
394+
395+
return $this;
396+
}
397+
384398
/**
385399
* Allows previous API calls where the methods were snake_case.
386400
* Will convert a camelCase API call to a snake_case call.

src/yajra/Datatables/Processors/DataProcessor.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
class DataProcessor
1313
{
1414

15+
/**
16+
* Columns to escape value.
17+
*
18+
* @var array
19+
*/
20+
private $escapeColumns = [];
21+
1522
/**
1623
* Processed data output
1724
*
@@ -55,6 +62,7 @@ public function __construct($results, array $columnDef, array $templates)
5562
$this->appendColumns = $columnDef['append'];
5663
$this->editColumns = $columnDef['edit'];
5764
$this->excessColumns = $columnDef['excess'];
65+
$this->escapeColumns = $columnDef['escape'];
5866
$this->templates = $templates;
5967
}
6068

@@ -77,7 +85,7 @@ public function process($object = false)
7785
$this->output[] = $object ? $value : $this->flatten($value);
7886
}
7987

80-
return $this->output;
88+
return $this->escapeColumns($this->output);
8189
}
8290

8391
/**
@@ -169,4 +177,23 @@ public function flatten(array $array)
169177

170178
return $return;
171179
}
180+
181+
/**
182+
* Escape column values as declared.
183+
*
184+
* @param array $output
185+
* @return array
186+
*/
187+
protected function escapeColumns(array $output)
188+
{
189+
return array_map(function ($row) {
190+
foreach ($row as $key => $value) {
191+
if ($this->escapeColumns == '*' || in_array($key, $this->escapeColumns, true)) {
192+
$row[$key] = e($value);
193+
}
194+
}
195+
196+
return $row;
197+
}, $output);
198+
}
172199
}

0 commit comments

Comments
 (0)