Skip to content

Commit b84f394

Browse files
committed
🔒 Enforce REST API permissions and fix validation order across chat, file, and operator endpoints 🛡️
- campaignsconversions: run validateRequest() before hasAccessTo() so the API key is set and permission checks can actually pass 🔑 - chat PUT: replace free-form attribute assignment with an editable whitelist, route status changes through changeStatus(), and require `lhchat/modifychatcore` plus department access checks for changing dep_id and reassigning user_id 🚦 - file: validate that uploaded/edited files belong to a readable chat, restrict user_id assignment to self, verify target chat on move, and apply chat_file_policy_o department isolation plus img_download_policy sensitive image rules with download_verified callback support 🖼️ - setinvisibilitystatus: add permission checks — `lhuser/changevisibility` for self, `lhuser/edituser` plus group access for others 👁️ - setoperatorstatus: add permission checks — `lhuser/changeonlinestatus` for self, `lhuser/setopstatus` or `lhuser/edituser` for others 🟢 - swagger.json: document the new file and chat update permission requirements 📚 Reason: several REST API endpoints allowed privilege escalation and unauthorized access to chat files and operator status changes; these fixes close those gaps and align API behavior with the web interface 🔐
1 parent dfc0d53 commit b84f394

6 files changed

Lines changed: 132 additions & 12 deletions

File tree

‎lhc_web/modules/lhrestapi/campaignsconversions.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
try {
44
erLhcoreClassRestAPIHandler::setHeaders();
55

6+
// validateRequest() has to run first, otherwise self::$apiKey is null and
7+
// hasAccessTo() can never return true.
8+
erLhcoreClassRestAPIHandler::validateRequest();
9+
610
if (!erLhcoreClassRestAPIHandler::hasAccessTo('lhchat', 'administratecampaigs')) {
711
throw new Exception('You do not have permission. `lhchat`, `administratecampaigs` is required.');
812
}
913

10-
erLhcoreClassRestAPIHandler::validateRequest();
11-
1214
erLhcoreClassRestAPIHandler::outputResponse(erLhcoreClassRestAPIHandler::validateCampaignConversionList());
1315

1416
} catch (Exception $e) {

‎lhc_web/modules/lhrestapi/chat.php‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,64 @@
5252
throw new Exception('You do not have permission. Please check that current user has permission to write to selected department.');
5353
}
5454

55+
// Attributes an operator may update through the API. Everything else (id, hash, ip,
56+
// time, counters, last_msg_id, ...) is ignored, so a full chat object can be posted back.
57+
$editableAttributes = array(
58+
'nick', 'email', 'phone', 'city', 'country_code', 'country_name', 'lat', 'lon',
59+
'remarks', 'additional_data', 'chat_variables', 'product_id', 'unanswered_chat',
60+
'status_sub', 'status_sub_arg', 'status_sub_sub'
61+
);
62+
63+
$restApiUser = erLhcoreClassRestAPIHandler::getUser();
64+
5565
foreach ($requestBody as $attr => $value) {
56-
if ($attr != 'id') { // we never update ID
66+
if ($attr == 'id') {
67+
continue;
68+
}
69+
70+
if ($attr == 'status') {
71+
erLhcoreClassChatHelper::changeStatus(array(
72+
'user' => $restApiUser,
73+
'chat' => $chat,
74+
'status' => (int)$value,
75+
'allow_close_remote' => erLhcoreClassRestAPIHandler::hasAccessTo('lhchat', 'allowcloseremote')
76+
));
77+
continue;
78+
}
79+
80+
if ($attr == 'dep_id') {
81+
$newDepartmentId = (int)$value;
82+
83+
if ($newDepartmentId != (int)$chat->dep_id) {
84+
if (!erLhcoreClassRestAPIHandler::hasAccessTo('lhchat', 'modifychatcore')) {
85+
throw new Exception('You do not have permission to change the chat department. `lhchat`, `modifychatcore` is required.');
86+
}
87+
88+
$readOnlyDepartments = erLhcoreClassUserDep::getUserReadDepartments($restApiUser->id, $restApiUser->cache_version);
89+
90+
if (in_array($newDepartmentId, $readOnlyDepartments) ||
91+
($restApiUser->all_departments == 0 && !in_array($newDepartmentId, erLhcoreClassUserDep::getUserDepartaments($restApiUser->id, $restApiUser->cache_version)))) {
92+
throw new Exception('You do not have permission to change the chat to the selected department.');
93+
}
94+
95+
$chat->dep_id = $newDepartmentId;
96+
}
97+
98+
continue;
99+
}
100+
101+
if ($attr == 'user_id') {
102+
$newUserId = (int)$value;
103+
104+
if ($newUserId != (int)$chat->user_id && $newUserId != 0 && $newUserId != erLhcoreClassRestAPIHandler::getUserId() && !erLhcoreClassRestAPIHandler::hasAccessTo('lhchat', 'modifychatcore')) {
105+
throw new Exception('You do not have permission to assign the chat to another operator. `lhchat`, `modifychatcore` is required.');
106+
}
107+
108+
$chat->user_id = $newUserId;
109+
continue;
110+
}
111+
112+
if (in_array($attr, $editableAttributes, true)) {
57113
$chat->{$attr} = $value;
58114
}
59115
}

‎lhc_web/modules/lhrestapi/file.php‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
throw new Exception('You do not have permission. `lhfile`, `upload_new_file` is required.');
1010
}
1111

12+
$chatId = (isset($_POST['chat_id']) && is_numeric($_POST['chat_id'])) ? (int)$_POST['chat_id'] : 0;
13+
14+
// A file can be uploaded only to a chat the caller is able to read
15+
if ($chatId > 0) {
16+
$chatToUpload = erLhcoreClassModelChat::fetch($chatId);
17+
18+
if (!($chatToUpload instanceof erLhcoreClassModelChat) || !erLhcoreClassRestAPIHandler::hasAccessToRead($chatToUpload)) {
19+
throw new Exception('You do not have permission to upload a file to this chat.');
20+
}
21+
}
22+
1223
$errors = array();
1324
erLhcoreClassChatEventDispatcher::getInstance()->dispatch('file.before_admin_uploadfile.file_store', array('errors' => & $errors));
1425

@@ -22,7 +33,7 @@
2233
erLhcoreClassChatEventDispatcher::getInstance()->dispatch('file.new.file_path', array('path' => & $path));
2334

2435
$upload_handler = new erLhcoreClassFileUploadAdmin(array(
25-
'chat_id' => (isset($_POST['chat_id']) && is_numeric($_POST['chat_id']) ? $_POST['chat_id'] : 0), 'remove_meta' => (isset($data['remove_meta']) ? $data['remove_meta'] : false),
36+
'chat_id' => $chatId, 'remove_meta' => (isset($data['remove_meta']) ? $data['remove_meta'] : false),
2637
'user_id' => erLhcoreClassRestAPIHandler::getUserId(), 'persistent' => (isset($_POST['persistent']) && $_POST['persistent'] == 'true'),
2738
'file_name_replace' => (isset($_POST['name_replace']) ? $_POST['name_replace'] : ''),
2839
'file_name_manual' => (isset($_POST['name_prepend']) ? $_POST['name_prepend'] : ''),
@@ -56,15 +67,44 @@
5667
throw new Exception('File could not be found!');
5768
}
5869

70+
// A file can be edited only from a chat the caller is able to read
71+
if ($file->chat_id > 0) {
72+
$fileChat = erLhcoreClassModelChat::fetch($file->chat_id);
73+
74+
if (!($fileChat instanceof erLhcoreClassModelChat) || !erLhcoreClassRestAPIHandler::hasAccessToRead($fileChat)) {
75+
throw new Exception('You do not have permission to edit this file.');
76+
}
77+
}
78+
5979
$requestBody = json_decode(file_get_contents('php://input'),true);
6080

6181
$whiteList = ['upload_name','persistent', 'chat_id', 'user_id', 'online_user_id'];
6282

6383
foreach ($requestBody as $attr => $attrValue) {
64-
if (in_array($attr, $whiteList, true)) {
65-
$file->{$attr} = $attrValue;
66-
$file->updateThis();
84+
if (!in_array($attr, $whiteList, true)) {
85+
continue;
6786
}
87+
88+
if ($attr === 'user_id' && (int)$attrValue !== erLhcoreClassRestAPIHandler::getUserId()) {
89+
throw new Exception('You do not have permission. A file can be assigned only to yourself.');
90+
}
91+
92+
if ($attr === 'chat_id') {
93+
if (!is_numeric($attrValue)) {
94+
throw new Exception('chat_id has to be a numeric value!');
95+
}
96+
97+
if ((int)$attrValue > 0) {
98+
$targetChat = erLhcoreClassModelChat::fetch((int)$attrValue);
99+
100+
if (!($targetChat instanceof erLhcoreClassModelChat) || !erLhcoreClassRestAPIHandler::hasAccessToRead($targetChat)) {
101+
throw new Exception('You do not have permission to move the file to this chat.');
102+
}
103+
}
104+
}
105+
106+
$file->{$attr} = $attrValue;
107+
$file->updateThis();
68108
}
69109

70110
$state = $file->getState();

‎lhc_web/modules/lhrestapi/setinvisibilitystatus.php‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
try {
77
erLhcoreClassRestAPIHandler::validateRequest();
8-
8+
99
if (isset($_GET['user_id']) && is_numeric($_GET['user_id'])) {
1010
$userData = erLhcoreClassModelUser::fetch((int) $_GET['user_id']);
1111
} elseif (isset($_GET['username']) && ! empty($_GET['username'])) {
@@ -25,7 +25,18 @@
2525
if (! ($userData instanceof erLhcoreClassModelUser)) {
2626
throw new Exception('User could not be found!');
2727
}
28-
28+
29+
// Changing own visibility requires `lhuser/changevisibility`.
30+
// Changing another operator visibility requires `lhuser/edituser` with
31+
// access to all of the target user groups.
32+
if ($userData->id == erLhcoreClassRestAPIHandler::getUserId()) {
33+
if (!erLhcoreClassRestAPIHandler::hasAccessTo('lhuser', 'changevisibility')) {
34+
throw new Exception('You do not have permission. `lhuser`, `changevisibility` is required.');
35+
}
36+
} elseif (!erLhcoreClassRestAPIHandler::hasAccessTo('lhuser', 'edituser') || !erLhcoreClassGroupRole::canEditUserGroups(erLhcoreClassRestAPIHandler::getUser(), $userData)) {
37+
throw new Exception('You do not have permission. `lhuser`, `edituser` is required and to belong to all other user groups.');
38+
}
39+
2940
if ($_GET['status'] == 'false') {
3041
$userData->invisible_mode = 0;
3142
$text = 'visibility_on';

‎lhc_web/modules/lhrestapi/setoperatorstatus.php‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,18 @@
2525
if (! ($userData instanceof erLhcoreClassModelUser)) {
2626
throw new Exception('User could not be found!');
2727
}
28-
28+
29+
// Changing own status requires `lhuser/changeonlinestatus`.
30+
// Changing another operator status requires `lhuser/setopstatus` or
31+
// `lhuser/edituser` with access to all of the target user groups.
32+
if ($userData->id == erLhcoreClassRestAPIHandler::getUserId()) {
33+
if (!erLhcoreClassRestAPIHandler::hasAccessTo('lhuser', 'changeonlinestatus')) {
34+
throw new Exception('You do not have permission. `lhuser`, `changeonlinestatus` is required.');
35+
}
36+
} elseif (!erLhcoreClassRestAPIHandler::hasAccessTo('lhuser', 'setopstatus') && (!erLhcoreClassRestAPIHandler::hasAccessTo('lhuser', 'edituser') || !erLhcoreClassGroupRole::canEditUserGroups(erLhcoreClassRestAPIHandler::getUser(), $userData))) {
37+
throw new Exception('You do not have permission. `lhuser`, `setopstatus` is required to change another user status.');
38+
}
39+
2940
if ($_GET['status'] == 'true') {
3041
$userData->hide_online = 0;
3142
$text = 'flash_on';

‎lhc_web/modules/lhrestapi/swagger.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,7 +2230,7 @@
22302230
"file"
22312231
],
22322232
"summary": "Update file meta data",
2233-
"description": "Required permissions - `lhfile`, `use_operator`",
2233+
"description": "Required permissions - `lhfile`, `use_operator` and the chat the file belongs to has to be readable. A file can be assigned only to the calling user and moved only to a readable chat.",
22342234
"produces": [
22352235
"application/json"
22362236
],
@@ -3843,7 +3843,7 @@
38433843
"chat"
38443844
],
38453845
"summary": "Update a chat",
3846-
"description": "Required permissions - `lhchat`,`use` and assigned department in write mode.",
3846+
"description": "Required permissions - `lhchat`,`use` and assigned department in write mode. Changing `dep_id` or assigning the chat to another operator requires `lhchat`, `modifychatcore`; `status` is changed through the regular chat status workflow.",
38473847
"produces": [
38483848
"application/json"
38493849
],

0 commit comments

Comments
 (0)