Skip to content

Commit 89ebf71

Browse files
authored
PropertyTableSortForm: Don't use ipl`s CSRF counter measure (#2937)
It's incompatible with gipfl`s… fixes #2935
2 parents 4032d49 + cdd3fea commit 89ebf71

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

library/Director/Web/Form/PropertyTableSortForm.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
namespace Icinga\Module\Director\Web\Form;
44

5-
use Icinga\Web\Session;
5+
use ipl\Html\Contract\FormElement;
66
use ipl\Html\Form;
7+
use ipl\Html\FormElement\HiddenElement;
78
use ipl\Html\ValidHtml;
8-
use ipl\Web\Common\CsrfCounterMeasure;
99

1010
class PropertyTableSortForm extends Form
1111
{
12-
use CsrfCounterMeasure;
13-
1412
protected $method = 'POST';
1513

1614
/** @var string Name of the form */
@@ -28,7 +26,38 @@ public function __construct(string $name, ValidHtml $table)
2826
protected function assemble()
2927
{
3028
$this->addElement('hidden', '__FORM_NAME', ['value' => $this->name]);
31-
$this->addElement($this->createCsrfCounterMeasure(Session::getSession()->getId()));
29+
$this->addElement($this->createCsrfCounterMeasure());
3230
$this->addHtml($this->table);
3331
}
32+
33+
/**
34+
* Create a form element to countermeasure CSRF attacks
35+
*
36+
* @return FormElement
37+
*/
38+
protected function createCsrfCounterMeasure(): FormElement
39+
{
40+
$token = CsrfToken::generate();
41+
42+
$options = [
43+
'ignore' => true,
44+
'required' => true,
45+
'validators' => ['Callback' => function ($token) {
46+
return CsrfToken::isValid($token);
47+
}]
48+
];
49+
50+
$element = new class (QuickForm::CSRF, $options) extends HiddenElement {
51+
public function hasValue(): bool
52+
{
53+
return true; // The validator must run even if the value is empty
54+
}
55+
};
56+
57+
$element->getAttributes()->registerAttributeCallback('value', function () use ($token) {
58+
return $token;
59+
});
60+
61+
return $element;
62+
}
3463
}

library/Director/Web/Table/PropertymodifierTable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
1313
use gipfl\IcingaWeb2\Url;
1414
use Icinga\Module\Director\Web\Form\PropertyTableSortForm;
15+
use Icinga\Module\Director\Web\Form\QuickForm;
1516
use ipl\Html\Form;
1617
use ipl\Html\HtmlString;
1718

@@ -59,7 +60,7 @@ public function render()
5960
return (new PropertyTableSortForm($this->getUniqueFormName(), new HtmlString(parent::render())))
6061
->setAction($this->request->getUrl()->getAbsoluteUrl())
6162
->on(Form::ON_SENT, function (PropertyTableSortForm $form) {
62-
$csrf = $form->getElement('CSRFToken');
63+
$csrf = $form->getElement(QuickForm::CSRF);
6364
if ($csrf !== null && $csrf->isValid()) {
6465
$this->reallyHandleSortPriorityActions();
6566
}

library/Director/Web/Table/SyncpropertyTable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use gipfl\IcingaWeb2\Table\Extension\ZfSortablePriority;
99
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
1010
use Icinga\Module\Director\Web\Form\PropertyTableSortForm;
11+
use Icinga\Module\Director\Web\Form\QuickForm;
1112
use ipl\Html\Form;
1213
use ipl\Html\HtmlString;
1314

@@ -44,7 +45,7 @@ public function render()
4445
return (new PropertyTableSortForm($this->getUniqueFormName(), new HtmlString(parent::render())))
4546
->setAction($this->request->getUrl()->getAbsoluteUrl())
4647
->on(Form::ON_SENT, function (PropertyTableSortForm $form) {
47-
$csrf = $form->getElement('CSRFToken');
48+
$csrf = $form->getElement(QuickForm::CSRF);
4849
if ($csrf !== null && $csrf->isValid()) {
4950
$this->reallyHandleSortPriorityActions();
5051
}

0 commit comments

Comments
 (0)