Skip to content

Commit cb15dc9

Browse files
committed
Fix CSRF validation for sorting in property tables
1 parent 428a49f commit cb15dc9

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Icinga\Module\Director\Web\Form;
4+
5+
use ipl\Html\Form;
6+
use ipl\Html\ValidHtml;
7+
8+
class PropertyTableSortForm extends Form
9+
{
10+
/** @var string Name of the table using the form */
11+
private $name;
12+
13+
/** @var string Table to sort */
14+
private $table;
15+
16+
public function __construct(string $name, ValidHtml $table)
17+
{
18+
$this->name = $name;
19+
$this->table = $table;
20+
}
21+
22+
protected function assemble()
23+
{
24+
$this->addElement('hidden', '__FORM_NAME', ['value' => $this->name]);
25+
$this->addElement('hidden', '__FORM_CSRF', ['value' => CsrfToken::generate()]);
26+
$this->addHtml($this->table);
27+
}
28+
}

library/Director/Web/Table/PropertymodifierTable.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
use gipfl\IcingaWeb2\Table\Extension\ZfSortablePriority;
1111
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
1212
use gipfl\IcingaWeb2\Url;
13+
use Icinga\Module\Director\Web\Form\CsrfToken;
14+
use Icinga\Module\Director\Web\Form\PropertyTableSortForm;
15+
use Icinga\Module\Director\Web\Form\QuickForm;
16+
use ipl\Html\HtmlString;
1317

1418
class PropertymodifierTable extends ZfQueryBasedTable
1519
{
@@ -51,7 +55,24 @@ public function render()
5155
if ($this->readOnly) {
5256
return parent::render();
5357
}
54-
return $this->renderWithSortableForm();
58+
59+
if ($this->request === null) {
60+
return parent::render();
61+
}
62+
63+
if ($this->request->isPost() && $this->hasBeenSent($this->request)) {
64+
if (! CsrfToken::isValid($this->request->get(QuickForm::CSRF))) {
65+
die('Invalid CSRF token provided');
66+
}
67+
68+
$this->reallyHandleSortPriorityActions();
69+
}
70+
71+
$url = $this->request->getUrl();
72+
$form = (new PropertyTableSortForm($this->getUniqueFormName(), new HtmlString(parent::render())))
73+
->setAction($url->getAbsoluteUrl());
74+
75+
return $form->render();
5576
}
5677

5778
protected function assemble()

library/Director/Web/Table/SyncpropertyTable.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
use gipfl\IcingaWeb2\Link;
77
use gipfl\IcingaWeb2\Table\Extension\ZfSortablePriority;
88
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
9+
use Icinga\Module\Director\Web\Form\CsrfToken;
10+
use Icinga\Module\Director\Web\Form\PropertyTableSortForm;
11+
use Icinga\Module\Director\Web\Form\QuickForm;
12+
use ipl\Html\Form;
13+
use ipl\Html\HtmlString;
914

1015
class SyncpropertyTable extends ZfQueryBasedTable
1116
{
@@ -33,7 +38,23 @@ public static function create(SyncRule $rule)
3338

3439
public function render()
3540
{
36-
return $this->renderWithSortableForm();
41+
if ($this->request === null) {
42+
return parent::render();
43+
}
44+
45+
if ($this->request->isPost() && $this->hasBeenSent($this->request)) {
46+
if (! CsrfToken::isValid($this->request->get(QuickForm::CSRF))) {
47+
die('Invalid CSRF token provided');
48+
}
49+
50+
$this->reallyHandleSortPriorityActions();
51+
}
52+
53+
$url = $this->request->getUrl();
54+
$form = (new PropertyTableSortForm($this->getUniqueFormName(), new HtmlString(parent::render())))
55+
->setAction($url->getAbsoluteUrl());
56+
57+
return $form->render();
3758
}
3859

3960
public function renderRow($row)

0 commit comments

Comments
 (0)