Skip to content
Merged
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed

- fix escape SQL values

## [2.15.6] - 2026-05-05

### Fixed
Expand Down
84 changes: 40 additions & 44 deletions inc/commoninjectionlib.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* @link https://github.com/pluginsGLPI/datainjection
* -------------------------------------------------------------------------
*/
use Glpi\DBAL\QueryExpression;
use Glpi\DBAL\QuerySubQuery;
use Glpi\Exception\Http\HttpException;
use Glpi\Features\AssignableItem;

Expand Down Expand Up @@ -1865,7 +1867,6 @@ private function dataAlreadyInDB($injectionClass, $itemtype)
/** @var DBmysql $DB */
global $DB;

$where = "";
$continue = true;

$injectionClass->getOptions($this->primary_type);
Expand Down Expand Up @@ -1893,17 +1894,15 @@ private function dataAlreadyInDB($injectionClass, $itemtype)
if (!$continue) {
$this->values[$itemtype]['id'] = self::ITEM_NOT_FOUND;
} else {
$sql = "SELECT *
FROM `" . $injectionClass->getTable() . "`";

if (!is_a($itemtype, CommonDBTM::class, true)) {
throw new HttpException(500, 'Class ' . $itemtype . ' is not a valid class');
}
$item = new $itemtype();
$item = new $itemtype();
$where = [];

//If it's a computer device
if ($item instanceof CommonDevice) {
$sql .= " WHERE `designation` = '" .
$this->getValueByItemtypeAndName($itemtype, 'designation') . "'";
$where['designation'] = $this->getValueByItemtypeAndName($itemtype, 'designation');
} elseif ($item instanceof CommonDBRelation) {
//Type is a relation : check it this relation still exists
//Define the side of the relation to use
Expand All @@ -1919,94 +1918,91 @@ private function dataAlreadyInDB($injectionClass, $itemtype)
$source_itemtype = $item::$itemtype_1;
$destination_itemtype = $item::$itemtype_2;
}
$where .= " AND `$source_id`='" .
$this->getValueByItemtypeAndName($itemtype, $source_id) . "'";
$where[$source_id] = $this->getValueByItemtypeAndName($itemtype, $source_id);
$where[$destination_id] = $this->getValueByItemtypeAndName($itemtype, $destination_id);
if ($item->isField('itemtype')) {
$where .= " AND `$source_itemtype`='" .
$this->getValueByItemtypeAndName($itemtype, $source_itemtype) . "'";
$where[$source_itemtype] = $this->getValueByItemtypeAndName($itemtype, $source_itemtype);
}
$where .= " AND `" . $destination_id . "`='" .
$this->getValueByItemtypeAndName($itemtype, $destination_id) . "'";
$sql .= " WHERE 1 " . $where;
} else {
//Type is not a relation

//Type can be deleted
if ($injectionClass->maybeDeleted()) {
$where .= " AND `is_deleted` = '0' ";
$where['is_deleted'] = 0;
}

//Type can be a template
if ($injectionClass->maybeTemplate()) {
$where .= " AND `is_template` = '0' ";
$where['is_template'] = 0;
}

//Type can be assigned to an entity
if ($injectionClass->isEntityAssign()) {
//Type can be recursive
if ($injectionClass->maybeRecursive()) {
$where_entity = getEntitiesRestrictRequest(
" AND",
$injectionClass->getTable(),
"entities_id",
$this->getValueByItemtypeAndName(
$itemtype,
$where = array_merge(
$where,
getEntitiesRestrictCriteria(
$injectionClass->getTable(),
'entities_id',
$this->getValueByItemtypeAndName($itemtype, 'entities_id'),
true,
),
true,
);
} else {
//Type cannot be recursive
$where_entity = " AND `entities_id` = '" .
$this->getValueByItemtypeAndName($itemtype, 'entities_id') . "'";
$where['entities_id'] = $this->getValueByItemtypeAndName($itemtype, 'entities_id');
}
} else { //If no entity assignment for this itemtype
$where_entity = "";
}

//Add mandatory fields to the query only if it's the primary_type to be injected
if ($itemtype == $this->primary_type) {
foreach ($this->mandatory_fields[$itemtype] as $field => $is_mandatory) {
if ($is_mandatory) {
if ($item instanceof User && $field == "useremails_id") {
$email = $DB->escape($this->getValueByItemtypeAndName($itemtype, $field));
$where .= " AND `id` IN (SELECT `users_id` FROM glpi_useremails WHERE `email` = '$email') ";
$where['id'] = new QuerySubQuery([
'SELECT' => 'users_id',
'FROM' => 'glpi_useremails',
'WHERE' => ['email' => $this->getValueByItemtypeAndName($itemtype, $field)],
]);
} else {
$where .= " AND `" . $field . "`='" . (string) $this->getValueByItemtypeAndName($itemtype, $field) . "'";
$where[$field] = $this->getValueByItemtypeAndName($itemtype, $field);
}
}
}
} else {
//Table contains an itemtype field
if ($injectionClass->isField('itemtype')) {
$where .= " AND `itemtype` = '" . $this->getValueByItemtypeAndName(
$itemtype,
'itemtype',
) . "'";
$where['itemtype'] = $this->getValueByItemtypeAndName($itemtype, 'itemtype');
}

//Table contains an items_id field
if ($injectionClass->isField('items_id')) {
$where .= " AND `items_id` = '" . $this->getValueByItemtypeAndName(
$itemtype,
'items_id',
) . "'";
$where['items_id'] = $this->getValueByItemtypeAndName($itemtype, 'items_id');
}
}

//Add additional parameters specific to this itemtype (or function checkPresent exists)
if (method_exists($injectionClass, 'checkPresent')) {
$where .= $injectionClass->checkPresent($this->values, $options);
$extra = trim((string) $injectionClass->checkPresent($this->values, $options));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The three implementations of checkPresent() (softwareversion, softwarelicense, networkport) still return raw SQL with unescaped name values.
QueryExpression continues to receive potentially dangerous values.
These methods should either be updated to return an array of criteria, or at the very least, $DB->escape() should be added to the string fields.

$extra = trim(preg_replace('/^\s*AND\s+/i', '', $extra));
if ($extra !== '') {
$where[] = new QueryExpression($extra);
}
Comment on lines +1987 to +2001
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An exception should be thrown or a warning logged if $extra is not an array and is not empty

}
$sql .= " WHERE 1 " . $where_entity . " " . $where;
}
$result = $DB->doQuery($sql);
if ($DB->numrows($result) > 0) {
$db_fields = $DB->fetchAssoc($result);

$result = $DB->request([
'FROM' => $injectionClass->getTable(),
'WHERE' => $where,
]);

if (count($result) > 0) {
$db_fields = $result->current();
foreach ($db_fields as $key => $value) {
$this->setValueForItemtype($itemtype, $key, $value, true);
}
$this->setValueForItemtype($itemtype, 'id', $DB->result($result, 0, 'id'));
$this->setValueForItemtype($itemtype, 'id', $db_fields['id']);
} else {
$this->setValueForItemtype($itemtype, 'id', self::ITEM_NOT_FOUND);
}
Expand Down
1 change: 1 addition & 0 deletions inc/model.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ public function readUploadedFile($options = [])
),
];
}
unset($_FILES['filename']);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ?
I'm not sure if this is directly related to the topic of this PR?!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After move_uploaded_file(), the temporary file is deleted, but $_FILES['filename'] still contains its path.

Later, when rendering the page, Html::footer() calls createFromGlobals(). An UploadedFile instance is created for each entry, and it checks whether the temporary file still exists. Since the file has already been moved and no longer exists, an exception is thrown.

unset() clears the corresponding entry from $_FILES after the file has been moved.

}

//If file has not the right extension, reject it and delete if
Expand Down