Skip to content

Commit 1ddff46

Browse files
committed
Chat: Use security token when sending messages
1 parent 19af444 commit 1ddff46

File tree

3 files changed

+75
-23
lines changed

3 files changed

+75
-23
lines changed

main/chat/chat.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
$view->assign('emoji_smile', \Emojione\Emojione::toImage(':smile:'));
6060
$view->assign('restrict_to_coach', api_get_configuration_value('course_chat_restrict_to_coach'));
6161
$view->assign('send_message_only_on_button', api_get_configuration_value('course_chat_send_message_only_on_button') === true ? 1 : 0);
62+
$view->assign('course_chat_sec_token', Security::get_token('course_chat'));
6263

6364
$template = $view->get_template('chat/chat.tpl');
6465
$content = $view->fetch($template);

main/inc/ajax/course_chat.ajax.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
/**
44
* Responses to AJAX calls for course chat.
55
*/
6+
7+
use Symfony\Component\HttpFoundation\JsonResponse as HttpResponse;
8+
use Symfony\Component\HttpFoundation\Request as HttpRequest;
9+
610
require_once __DIR__.'/../global.inc.php';
711

812
if (!api_protect_course_script(false)) {
@@ -15,8 +19,17 @@
1519
$groupId = api_get_group_id();
1620
$json = ['status' => false];
1721

22+
$httpRequest = HttpRequest::createFromGlobals();
23+
$httpResponse = HttpResponse::create();
24+
1825
$courseChatUtils = new CourseChatUtils($courseId, $userId, $sessionId, $groupId);
1926

27+
$token = Security::getTokenFromSession('course_chat');
28+
29+
if ($httpRequest->headers->get('x-token') !== $token) {
30+
$_REQUEST['action'] = 'error';
31+
}
32+
2033
switch ($_REQUEST['action']) {
2134
case 'chat_logout':
2235
$logInfo = [
@@ -78,5 +91,8 @@
7891
break;
7992
}
8093

81-
header('Content-Type: application/json');
82-
echo json_encode($json);
94+
$token = Security::get_token('course_chat');
95+
96+
$httpResponse->headers->set('x-token', $token);
97+
$httpResponse->setData($json);
98+
$httpResponse->send();

main/template/default/chat/chat.tpl

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,28 @@ $(function () {
7171
_historySize: -1,
7272
usersOnline: 0,
7373
currentFriend: 0,
74+
xToken: '{{ course_chat_sec_token }}',
7475
call: false,
7576
track: function () {
7677
return $
77-
.get(ChChat._ajaxUrl, {
78-
action: 'track',
79-
size: ChChat._historySize,
80-
users_online: ChChat.usersOnline,
81-
friend: ChChat.currentFriend
78+
.ajax({
79+
url: ChChat._ajaxUrl,
80+
method: 'GET',
81+
headers: { 'x-token': ChChat.xToken },
82+
data: {
83+
action: 'track',
84+
size: ChChat._historySize,
85+
users_online: ChChat.usersOnline,
86+
friend: ChChat.currentFriend
87+
}
8288
})
83-
.done(function (response) {
89+
.done(function (response, textStatus, jqXhr) {
90+
ChChat.xToken = jqXhr.getResponseHeader('x-token');
91+
92+
if (!response.status) {
93+
return;
94+
}
95+
8496
try {
8597
if (response.data.history) {
8698
ChChat._historySize = response.data.oldFileSize;
@@ -140,11 +152,18 @@ $(function () {
140152
$('#chat-users').html(html);
141153
},
142154
onPreviewListener: function () {
143-
$.post(ChChat._ajaxUrl, {
144-
action: 'preview',
145-
'message': $('textarea#chat-writer').val()
155+
$.ajax({
156+
url: ChChat._ajaxUrl,
157+
method: 'POST',
158+
headers: { 'x-token': ChChat.xToken },
159+
data: {
160+
action: 'preview',
161+
'message': $('textarea#chat-writer').val()
162+
}
146163
})
147-
.done(function (response) {
164+
.done(function (response, textStatus, jqXhr) {
165+
ChChat.xToken = jqXhr.getResponseHeader('x-token');
166+
148167
if (!response.status) {
149168
return;
150169
}
@@ -164,20 +183,29 @@ $(function () {
164183
var self = this;
165184
self.disabled = true;
166185
167-
$.post(ChChat._ajaxUrl, {
168-
action: 'write',
169-
message: textarea.val(),
170-
friend: ChChat.currentFriend
186+
$.ajax({
187+
method: 'POST',
188+
url: ChChat._ajaxUrl,
189+
headers: { 'x-token': ChChat.xToken },
190+
data: {
191+
action: 'write',
192+
message: textarea.val(),
193+
friend: ChChat.currentFriend
194+
}
171195
})
172-
.done(function (response) {
196+
.done(function (response, textStatus, jqXhr) {
173197
self.disabled = false;
174198
199+
ChChat.xToken = jqXhr.getResponseHeader('x-token');
200+
201+
textarea.prop('disabled', false);
202+
$(".emoji-wysiwyg-editor").prop('contenteditable', 'true');
203+
175204
if (!response.status) {
176205
return;
177206
}
178-
textarea.prop('disabled', false);
207+
179208
textarea.val('');
180-
$(".emoji-wysiwyg-editor").prop('contenteditable', 'true');
181209
$(".emoji-wysiwyg-editor").html('');
182210
});
183211
},
@@ -186,11 +214,18 @@ $(function () {
186214
e.preventDefault();
187215
return;
188216
}
189-
$.get(ChChat._ajaxUrl, {
190-
action: 'reset',
191-
friend: ChChat.currentFriend
217+
$.ajax({
218+
url: ChChat._ajaxUrl,
219+
method: 'GET',
220+
headers: { 'x-token': ChChat.xToken },
221+
data: {
222+
action: 'reset',
223+
friend: ChChat.currentFriend
224+
}
192225
})
193-
.done(function (response) {
226+
.done(function (response, textStatus, jqXhr) {
227+
ChChat.xToken = jqXhr.getResponseHeader('x-token');
228+
194229
if (!response.status) {
195230
return;
196231
}

0 commit comments

Comments
 (0)