Skip to content

Commit

Permalink
validate entity_restrict when available with idor tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
orthagh authored and trasher committed Mar 2, 2021
1 parent b2f6732 commit aade65b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
4 changes: 3 additions & 1 deletion ajax/dropdownTrackingDeviceType.php
Expand Up @@ -76,7 +76,9 @@
'multiple' => $_POST["multiple"],
'myname' => $_POST["myname"],
'rand' => $_POST["rand"],
'_idor_token' => Session::getNewIDORToken($itemtype),
'_idor_token' => Session::getNewIDORToken($itemtype, [
'entity_restrict' => $_POST['entity_restrict'],
]),
];

if (isset($_POST["used"]) && !empty($_POST["used"])) {
Expand Down
4 changes: 3 additions & 1 deletion inc/computer_item.class.php
Expand Up @@ -682,7 +682,9 @@ static function dropdownConnect($itemtype, $fromtype, $myname, $entity_restrict
'itemtype' => $itemtype,
'onlyglobal' => $onlyglobal,
'used' => $used,
'_idor_token' => Session::getNewIDORToken($itemtype),
'_idor_token' => Session::getNewIDORToken($itemtype, [
'entity_restrict' => $entity_restrict,
]),
];

echo Html::jsAjaxDropdown($myname, $field_id,
Expand Down
16 changes: 9 additions & 7 deletions inc/dropdown.class.php
Expand Up @@ -173,11 +173,13 @@ static function show($itemtype, $options = []) {
'condition' => $params['condition'],
'used' => $params['used'],
'toadd' => $params['toadd'],
'entity_restrict' => (is_array($params['entity']) ? json_encode(array_values($params['entity'])) : $params['entity']),
'entity_restrict' => ($entity_restrict = (is_array($params['entity']) ? json_encode(array_values($params['entity'])) : $params['entity'])),
'on_change' => $params['on_change'],
'permit_select_parent' => $params['permit_select_parent'],
'specific_tags' => $params['specific_tags'],
'_idor_token' => Session::getNewIDORToken($itemtype),
'_idor_token' => Session::getNewIDORToken($itemtype, [
'entity_restrict' => $entity_restrict,
]),
];

$output = "<span class='no-wrap'>";
Expand Down Expand Up @@ -2182,6 +2184,11 @@ static function showListLimit($onchange = '', $display = true) {
public static function getDropdownValue($post, $json = true) {
global $DB, $CFG_GLPI;

// check if asked itemtype is the one originaly requested by the form
if (!Session::validateIDOR($post)) {
return;
}

if (isset($post["entity_restrict"])
&& !is_array($post["entity_restrict"])
&& (substr($post["entity_restrict"], 0, 1) === '[')
Expand All @@ -2199,11 +2206,6 @@ public static function getDropdownValue($post, $json = true) {
$post['entity_restrict'] = $_SESSION['glpiactiveentities'];
}

// check if asked itemtype is the one originaly requested by the form
if (!Session::validateIDOR($post)) {
return;
}

// Security
if (!($item = getItemForItemtype($post['itemtype']))) {
return;
Expand Down
7 changes: 5 additions & 2 deletions inc/user.class.php
Expand Up @@ -4026,9 +4026,12 @@ static function dropdown($options = []) {
'on_change' => $p['on_change'],
'used' => $p['used'],
'inactive_deleted' => $p['inactive_deleted'],
'entity_restrict' => (is_array($p['entity']) ? json_encode(array_values($p['entity'])) : $p['entity']),
'entity_restrict' => ($entity_restrict = (is_array($p['entity']) ? json_encode(array_values($p['entity'])) : $p['entity'])),
'specific_tags' => $p['specific_tags'],
'_idor_token' => Session::getNewIDORToken(__CLASS__, ['right' => $p['right']]),
'_idor_token' => Session::getNewIDORToken(__CLASS__, [
'right' => $p['right'],
'entity_restrict' => $entity_restrict,
]),
];

$output = Html::jsAjaxDropdown($p['name'], $field_id,
Expand Down
12 changes: 10 additions & 2 deletions tests/functionnal/Dropdown.php
Expand Up @@ -765,7 +765,7 @@ public function testGetDropdownValue($params, $expected, $session_params = []) {
}
}

$params['_idor_token'] = \Session::getNewIDORToken($params['itemtype'] ?? '');
$params['_idor_token'] = $this->generateIdor($params);

$result = \Dropdown::getDropdownValue($params, false);

Expand Down Expand Up @@ -928,7 +928,7 @@ public function testGetDropdownConnect($params, $expected, $session_params = [])
}
}

$params['_idor_token'] = \Session::getNewIDORToken($params['itemtype'] ?? '');
$params['_idor_token'] = $this->generateIdor($params);

$result = \Dropdown::getDropdownConnect($params, false);

Expand Down Expand Up @@ -1331,4 +1331,12 @@ public function testGetDropdownValuePaginate() {
->hasSize(2);

}

private function generateIdor(array $params = []) {
$idor_add_params = [];
if (isset($params['entity_restrict'])) {
$idor_add_params['entity_restrict'] = $params['entity_restrict'];
}
return \Session::getNewIDORToken(($params['itemtype'] ?? ''), $idor_add_params);
}
}
1 change: 1 addition & 0 deletions tests/functionnal/Session.php
Expand Up @@ -375,6 +375,7 @@ protected function idorProvider() {
['itemtype' => 'Ticket'],
['itemtype' => 'Glpi\\Dashboard\\Item'],
['itemtype' => 'User', 'add_params' => ['right' => 'all']],
['itemtype' => 'User', 'add_params' => ['entity_restrict' => 0]],
];
}

Expand Down

0 comments on commit aade65b

Please sign in to comment.