Skip to content

Commit d64e7d5

Browse files
committed
Soft fail on clientside data corruption
Refs #388
1 parent b5e9ee4 commit d64e7d5

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

.php-cs-fixer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@
2424
'declare_strict_types' => true,
2525
'strict_param' => true,
2626
'strict_comparison' => true,
27-
'array_syntax' => ['syntax' => 'short'],
2827
'concat_space' => ['spacing' => 'one'],
2928
'header_comment' => ['header' => $header, 'location' => 'after_open'],
3029

3130
'mb_str_functions' => true,
32-
//'ordered_class_elements' => true,
3331
'ordered_imports' => true,
3432
'phpdoc_align' => false,
3533
'phpdoc_separation' => false,

src/DataTableState.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,12 @@ private function handleOrderBy(ParameterBag $parameters): void
9292
if ($parameters->has('order')) {
9393
$this->orderBy = [];
9494
foreach ($parameters->all()['order'] ?? [] as $order) {
95-
$column = $this->getDataTable()->getColumn((int) $order['column']);
96-
$this->addOrderBy($column, $order['dir'] ?? DataTable::SORT_ASCENDING);
95+
try {
96+
$column = $this->getDataTable()->getColumn((int) $order['column']);
97+
$this->addOrderBy($column, $order['dir'] ?? DataTable::SORT_ASCENDING);
98+
} catch (\Throwable $t) {
99+
// Column index and direction can be corrupted by malicious clients, ignore any exceptions thus caused
100+
}
97101
}
98102
}
99103
}

tests/Unit/DataTableTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ public function testSortDirectionValidation(): void
144144
$this->expectException(\InvalidArgumentException::class);
145145
$this->expectExceptionMessage('direction must be one of');
146146

147+
$datatable = $this
148+
->createMockDataTable()
149+
->add('foo', TextColumn::class, ['searchable' => true])
150+
;
151+
$datatable->handleRequest(Request::create('/foo', Request::METHOD_POST, ['_dt' => $datatable->getName(), 'draw' => 684]));
152+
$datatable->getState()->addOrderBy($datatable->getColumn(0), 'foo');
153+
}
154+
155+
public function testInvalidSortParametersAreIgnored(): void
156+
{
147157
$datatable = $this
148158
->createMockDataTable()
149159
->add('foo', TextColumn::class, ['searchable' => true])
@@ -156,6 +166,7 @@ public function testSortDirectionValidation(): void
156166
'dir' => 'foo',
157167
]],
158168
]));
169+
$this->assertEmpty($datatable->getState()->getOrderBy());
159170
}
160171

161172
public function testPostMethod(): void

0 commit comments

Comments
 (0)