Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PropertyTableSortForm: Don't use ipl`s CSRF counter measure #2937

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions library/Director/Web/Form/PropertyTableSortForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace Icinga\Module\Director\Web\Form;

use Icinga\Web\Session;
use ipl\Html\Contract\FormElement;
use ipl\Html\Form;
use ipl\Html\FormElement\HiddenElement;
use ipl\Html\ValidHtml;
use ipl\Web\Common\CsrfCounterMeasure;

class PropertyTableSortForm extends Form
{
use CsrfCounterMeasure;

protected $method = 'POST';

/** @var string Name of the form */
Expand All @@ -28,7 +26,38 @@ public function __construct(string $name, ValidHtml $table)
protected function assemble()
{
$this->addElement('hidden', '__FORM_NAME', ['value' => $this->name]);
$this->addElement($this->createCsrfCounterMeasure(Session::getSession()->getId()));
$this->addElement($this->createCsrfCounterMeasure());
$this->addHtml($this->table);
}

/**
* Create a form element to countermeasure CSRF attacks
*
* @return FormElement
*/
protected function createCsrfCounterMeasure(): FormElement
{
$token = CsrfToken::generate();

$options = [
'ignore' => true,
'required' => true,
'validators' => ['Callback' => function ($token) {
return CsrfToken::isValid($token);
}]
];

$element = new class (QuickForm::CSRF, $options) extends HiddenElement {
public function hasValue(): bool
{
return true; // The validator must run even if the value is empty
}
};

$element->getAttributes()->registerAttributeCallback('value', function () use ($token) {
return $token;
});

return $element;
}
}
3 changes: 2 additions & 1 deletion library/Director/Web/Table/PropertymodifierTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use gipfl\IcingaWeb2\Url;
use Icinga\Module\Director\Web\Form\PropertyTableSortForm;
use Icinga\Module\Director\Web\Form\QuickForm;
use ipl\Html\Form;
use ipl\Html\HtmlString;

Expand Down Expand Up @@ -59,7 +60,7 @@ public function render()
return (new PropertyTableSortForm($this->getUniqueFormName(), new HtmlString(parent::render())))
->setAction($this->request->getUrl()->getAbsoluteUrl())
->on(Form::ON_SENT, function (PropertyTableSortForm $form) {
$csrf = $form->getElement('CSRFToken');
$csrf = $form->getElement(QuickForm::CSRF);
if ($csrf !== null && $csrf->isValid()) {
$this->reallyHandleSortPriorityActions();
}
Expand Down
3 changes: 2 additions & 1 deletion library/Director/Web/Table/SyncpropertyTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use gipfl\IcingaWeb2\Table\Extension\ZfSortablePriority;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use Icinga\Module\Director\Web\Form\PropertyTableSortForm;
use Icinga\Module\Director\Web\Form\QuickForm;
use ipl\Html\Form;
use ipl\Html\HtmlString;

Expand Down Expand Up @@ -44,7 +45,7 @@ public function render()
return (new PropertyTableSortForm($this->getUniqueFormName(), new HtmlString(parent::render())))
->setAction($this->request->getUrl()->getAbsoluteUrl())
->on(Form::ON_SENT, function (PropertyTableSortForm $form) {
$csrf = $form->getElement('CSRFToken');
$csrf = $form->getElement(QuickForm::CSRF);
if ($csrf !== null && $csrf->isValid()) {
$this->reallyHandleSortPriorityActions();
}
Expand Down
Loading